blob: bd1d5fefb90a0ea23da77c79c63cc67b09c799ff [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;
24import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
25import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020026import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020028import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070029import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
30import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Wale Ogunwale68278562017-09-23 17:13:55 -070031import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070032import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070033import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070034import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
35import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070036import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070037import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
39import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070040import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070041import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070042import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020043import static android.content.pm.PackageManager.PERMISSION_GRANTED;
chaviw59b98852017-06-13 12:05:44 -070044import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010045import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020046import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
47import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070048import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070049import static android.view.Display.TYPE_VIRTUAL;
Winson Chung47900652017-04-06 18:44:25 -070050import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020051import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020052import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020054import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020061import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020063import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020070import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
72import static com.android.server.am.ActivityManagerService.ANIMATE;
73import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
75import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
76import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
77import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
78import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
79import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
80import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
81import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
82import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070083import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020084import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
85import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Winson Chung74666102017-02-22 17:49:24 -080086import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
87import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
88import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Steven Timotius4346f0a2017-09-12 11:07:21 -070089import static com.android.server.am.proto.ActivityStackSupervisorProto.DISPLAYS;
90import static com.android.server.am.proto.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
91import static com.android.server.am.proto.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
92import static com.android.server.am.proto.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
93import static com.android.server.am.proto.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
Jorim Jaggife762342016-10-13 14:33:27 +020094import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080095import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020096
Svetoslav7008b512015-06-24 18:47:07 -070097import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -080098import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -070099import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700100import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100101import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700102import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800104import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700105import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700107import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700110import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700112import android.app.WaitResult;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700113import android.app.WindowConfiguration;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700115import android.content.Context;
116import android.content.Intent;
117import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700119import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.pm.PackageManager;
121import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100122import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800124import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800125import android.hardware.display.DisplayManager;
126import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800127import android.hardware.display.DisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800128import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700129import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100130import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700131import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700132import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700133import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700134import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700135import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700136import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700137import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700138import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700140import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700141import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100142import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700143import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700144import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700145import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700146import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700147import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700148import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800149import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700150import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700151import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800152import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800153import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700154import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700155import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800156import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800157
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700158import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800159import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800160import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700161import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700162import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800163import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700164import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700165import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700166import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700167import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700168
Craig Mautner8d341ef2013-03-26 09:03:27 -0700169import java.io.FileDescriptor;
170import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700171import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800172import java.lang.annotation.Retention;
173import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700174import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700175import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700176import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700177import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700178
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800179public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800180 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700181 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700183 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700184 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800189 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800190
Craig Mautnerf3333272013-04-22 10:55:53 -0700191 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700193
Craig Mautner0eea92c2013-05-16 13:35:39 -0700194 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700196
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700197 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700199
Craig Mautner05d29032013-05-03 13:40:13 -0700200 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
201 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
202 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700203 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700204 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800205 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
206 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
207 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700208 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800209 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
210 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800211
Wale Ogunwale040b4702015-08-06 18:10:50 -0700212 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700213
Wale Ogunwale040b4702015-08-06 18:10:50 -0700214 // Used to indicate if an object (e.g. stack) that we are trying to get
215 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800216 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700217
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700218 // Used to indicate that windows of activities should be preserved during the resize.
219 static final boolean PRESERVE_WINDOWS = true;
220
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221 // Used to indicate if an object (e.g. task) should be moved/created
222 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700223 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224
225 // Used to indicate that an objects (e.g. task) removal from its container
226 // (e.g. stack) is due to it moving to another container.
227 static final boolean MOVING = true;
228
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700229 // Force the focus to change to the stack we are moving a task to..
230 static final boolean FORCE_FOCUS = true;
231
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700232 // Don't execute any calls to resume.
233 static final boolean DEFER_RESUME = true;
234
Winson Chung010927a2016-12-15 16:12:35 -0800235 // Used to indicate that a task is removed it should also be removed from recents.
236 static final boolean REMOVE_FROM_RECENTS = true;
237
Winson Chung6954fc92017-03-24 16:22:12 -0700238 // Used to indicate that pausing an activity should occur immediately without waiting for
239 // the activity callback indicating that it has completed pausing
240 static final boolean PAUSE_IMMEDIATELY = true;
241
Winson Chung7900bee2017-03-10 08:59:25 -0800242 /**
243 * The modes which affect which tasks are returned when calling
244 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
245 */
246 @Retention(RetentionPolicy.SOURCE)
247 @IntDef({
248 MATCH_TASK_IN_STACKS_ONLY,
249 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
250 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
251 })
252 public @interface AnyTaskForIdMatchTaskMode {}
253 // Match only tasks in the current stacks
254 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
255 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
256 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
257 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
258 // provided stack id
259 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
260
Svetoslav7008b512015-06-24 18:47:07 -0700261 // Activity actions an app cannot start if it uses a permission which is not granted.
262 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
263 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700264
Svetoslav7008b512015-06-24 18:47:07 -0700265 static {
266 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
267 Manifest.permission.CAMERA);
268 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
269 Manifest.permission.CAMERA);
270 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
271 Manifest.permission.CALL_PHONE);
272 }
273
Svet Ganov99b60432015-06-27 13:15:22 -0700274 /** Action restriction: launching the activity is not restricted. */
275 private static final int ACTIVITY_RESTRICTION_NONE = 0;
276 /** Action restriction: launching the activity is restricted by a permission. */
277 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
278 /** Action restriction: launching the activity is restricted by an app op. */
279 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700280
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700281 // For debugging to make sure the caller when acquiring/releasing our
282 // wake lock is the system process.
283 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800284 /** The number of distinct task ids that can be assigned to the tasks of a single user */
285 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700286
Craig Mautner27084302013-03-25 08:05:25 -0700287 final ActivityManagerService mService;
288
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800289 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800290
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700291 final ActivityStackSupervisorHandler mHandler;
292
293 /** Short cut */
294 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800295 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700296
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700297 /** Counter for next free stack ID to use for dynamic activity stacks. */
298 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700299
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800300 /**
301 * Maps the task identifier that activities are currently being started in to the userId of the
302 * task. Each time a new task is created, the entry for the userId of the task is incremented
303 */
304 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700305
Craig Mautner2420ead2013-04-01 17:13:20 -0700306 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800307 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700308
Craig Mautnere0a38842013-12-16 16:14:02 -0800309 /** The stack containing the launcher app. Assumed to always be attached to
310 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700312
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800314 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700315
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
317 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800318 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700320
321 /** List of activities that are waiting for a new activity to become visible before completing
322 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700323 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
324 // mStoppingActivities when something else comes up.
325 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700326
Bryce Lee4a194382017-04-04 14:32:48 -0700327 /** List of processes waiting to find out when a specific activity becomes visible. */
328 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329
330 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700331 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700332
Craig Mautnerde4ef022013-04-07 19:01:33 -0700333 /** List of activities that are ready to be stopped, but waiting for the next activity to
334 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800335 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700336
Craig Mautnerf3333272013-04-22 10:55:53 -0700337 /** List of activities that are ready to be finished, but waiting for the previous activity to
338 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800339 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700340
Craig Mautner0eea92c2013-05-16 13:35:39 -0700341 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800342 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700343
Wale Ogunwale22e25262016-02-01 10:32:02 -0800344 /** List of activities whose multi-window mode changed that we need to report to the
345 * application */
346 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
347
348 /** List of activities whose picture-in-picture mode changed that we need to report to the
349 * application */
350 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
351
Winson Chung5af42fc2017-03-24 17:11:33 -0700352 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
353 * the application */
354 Rect mPipModeChangedTargetStackBounds;
355
Craig Mautnerf3333272013-04-22 10:55:53 -0700356 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700357 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700358
Craig Mautnerde4ef022013-04-07 19:01:33 -0700359 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
360 * is being brought in front of us. */
361 boolean mUserLeaving = false;
362
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700364 * We don't want to allow the device to go to sleep while in the process
365 * of launching an activity. This is primarily to allow alarm intent
366 * receivers to launch an activity and get that to run before the device
367 * goes back to sleep.
368 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700369 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700370
371 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700372 * Set when the system is going to sleep, until we have
373 * successfully paused the current activity and released our wake lock.
374 * At that point the system is allowed to actually sleep.
375 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700376 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700377
David Stevens9440dc82017-03-16 19:00:20 -0700378 /**
379 * A list of tokens that cause the top activity to be put to sleep.
380 * They are used by components that may hide and block interaction with underlying
381 * activities.
382 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700383 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700384
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700385 /** Stack id of the front stack when user switched, indexed by userId. */
386 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700387
Bryce Leeaf3a4002017-03-20 10:37:12 -0700388 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800389 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700390 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800391
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800392 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
393
394 private DisplayManagerInternal mDisplayManagerInternal;
395 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800396
Wale Ogunwaled046a012015-12-24 13:05:59 -0800397 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800398 boolean inResumeTopActivity;
399
Andrii Kulian1e32e022016-09-16 15:29:34 -0700400 /**
401 * Temporary rect used during docked stack resize calculation so we don't need to create a new
402 * object each time.
403 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700404 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700405
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700406 // The default minimal size that will be used if the activity doesn't specify its minimal size.
407 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700408 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700409
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700410 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
411 private boolean mTaskLayersChanged = true;
412
Jorim Jaggi275561a2016-02-23 10:11:02 -0500413 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800414
Jorim Jaggiea039a82017-08-02 14:37:49 +0200415 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
416
Andrii Kulian1779e612016-10-12 21:58:25 -0700417 @Override
418 protected int getChildCount() {
419 return mActivityDisplays.size();
420 }
421
422 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700423 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700424 return mActivityDisplays.valueAt(index);
425 }
426
427 @Override
428 protected ConfigurationContainer getParent() {
429 return null;
430 }
431
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700432 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700433 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700434 if (activityDisplay == null) {
435 throw new IllegalArgumentException("No display found with id: " + displayId);
436 }
437
438 return activityDisplay.getOverrideConfiguration();
439 }
440
441 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700442 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700443 if (activityDisplay == null) {
444 throw new IllegalArgumentException("No display found with id: " + displayId);
445 }
446
447 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
448 }
449
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700450 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700451 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
452 int callingUid, ActivityInfo activityInfo) {
453 if (displayId == DEFAULT_DISPLAY) {
454 // No restrictions for the default display.
455 return true;
456 }
457 if (!mService.mSupportsMultiDisplay) {
458 // Can't launch on secondary displays if feature is not supported.
459 return false;
460 }
461 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
462 // Can't apply wrong configuration to non-resizeable activities.
463 return false;
464 }
465 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
466 // Can't place activities to a display that has restricted launch rules.
467 // In this case the request should be made by explicitly adding target display id and
468 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
469 return false;
470 }
471 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700472 }
473
474 /**
475 * Check if configuration of specified display matches current global config.
476 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
477 * the same config as on the default display.
478 * @param displayId Id of the display to check.
479 * @return {@code true} if configuration matches.
480 */
481 private boolean displayConfigMatchesGlobal(int displayId) {
482 if (displayId == DEFAULT_DISPLAY) {
483 return true;
484 }
485 if (displayId == INVALID_DISPLAY) {
486 return false;
487 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700488 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700489 if (targetDisplay == null) {
490 throw new IllegalArgumentException("No display found with id: " + displayId);
491 }
492 return getConfiguration().equals(targetDisplay.getConfiguration());
493 }
494
Wale Ogunwale39381972015-12-17 17:15:29 -0800495 static class FindTaskResult {
496 ActivityRecord r;
497 boolean matchedByRootAffinity;
498 }
499 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
500
Craig Mautneree36c772014-07-16 14:56:05 -0700501 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800502 * Temp storage for display ids sorted in focus order.
503 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
504 * it's more efficient, as the number of displays is usually small.
505 */
506 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
507
508 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100509 * Used to keep track whether app visibilities got changed since the last pause. Useful to
510 * determine whether to invoke the task stack change listener after pausing.
511 */
512 boolean mAppVisibilitiesChangedSinceLastPause;
513
514 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100515 * Set of tasks that are in resizing mode during an app transition to fill the "void".
516 */
517 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
518
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700519
520 /**
521 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
522 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
523 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
524 * like the docked stack going empty.
525 */
526 private boolean mAllowDockedStackResize = true;
527
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100528 /**
Tony Mak853304c2016-04-18 15:17:41 +0100529 * Is dock currently minimized.
530 */
531 boolean mIsDockMinimized;
532
Jorim Jaggife762342016-10-13 14:33:27 +0200533 final KeyguardController mKeyguardController;
534
chaviw59b98852017-06-13 12:05:44 -0700535 private PowerManager mPowerManager;
536 private int mDeferResumeCount;
537
Tony Mak853304c2016-04-18 15:17:41 +0100538 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700539 * Description of a request to start a new activity, which has been held
540 * due to app switches being disabled.
541 */
542 static class PendingActivityLaunch {
543 final ActivityRecord r;
544 final ActivityRecord sourceRecord;
545 final int startFlags;
546 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700547 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700548
549 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700550 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700551 r = _r;
552 sourceRecord = _sourceRecord;
553 startFlags = _startFlags;
554 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700555 callerApp = _callerApp;
556 }
557
558 void sendErrorResult(String message) {
559 try {
560 if (callerApp.thread != null) {
561 callerApp.thread.scheduleCrash(message);
562 }
563 } catch (RemoteException e) {
564 Slog.e(TAG, "Exception scheduling crash of failed "
565 + "activity launcher sourceRecord=" + sourceRecord, e);
566 }
Craig Mautneree36c772014-07-16 14:56:05 -0700567 }
568 }
569
Bryce Leeaf691c02017-03-20 14:20:22 -0700570 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700571 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700572 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800573 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200574 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700575 }
576
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800577 void setRecentTasks(RecentTasks recentTasks) {
578 mRecentTasks = recentTasks;
579 }
580
Jeff Brown2c43c332014-06-12 22:38:59 -0700581 /**
582 * At the time when the constructor runs, the power manager has not yet been
583 * initialized. So we initialize our wakelocks afterwards.
584 */
585 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700586 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
587 mGoingToSleep = mPowerManager
588 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
589 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700590 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700591 }
592
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700593 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 synchronized (mService) {
595 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200596 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597
598 mDisplayManager =
599 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
600 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800601 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602
603 Display[] displays = mDisplayManager.getDisplays();
604 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
605 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700606 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800607 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700608 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 }
610
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700611 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
612 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800613
614 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615 }
Craig Mautner27084302013-03-25 08:05:25 -0700616 }
617
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700618 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800619 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700620 }
621
Craig Mautnerde4ef022013-04-07 19:01:33 -0700622 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800623 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700624 }
625
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700626 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700627 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700628 }
629
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800630 /** The top most stack on its display. */
631 boolean isFrontStackOnDisplay(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700632 return isFrontOfStackList(stack, stack.getDisplay().mStacks);
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800633 }
634
635 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800636 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700637 }
638
Wale Ogunwaled046a012015-12-24 13:05:59 -0800639 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800640 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
641 if (!focusCandidate.isFocusable()) {
642 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800643 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800644 }
645
646 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800647 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800648 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800649
Wale Ogunwaled046a012015-12-24 13:05:59 -0800650 EventLogTags.writeAmFocusedStack(
651 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
652 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
653 }
654
655 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800656 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800657 if (r != null && r.idle) {
658 checkFinishBootingLocked();
659 }
660 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700661 }
662
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700663 void moveHomeStackToFront(String reason) {
664 mHomeStack.moveToFront(reason);
665 }
666
Matthew Ng330757d2017-02-28 14:19:17 -0800667 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700668 final ActivityStack recentsStack = getDefaultDisplay().getStack(
669 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800670 if (recentsStack != null) {
671 recentsStack.moveToFront(reason);
672 }
673 }
674
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700675 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800676 boolean moveHomeStackTaskToTop(String reason) {
677 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700678
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700679 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700680 if (top == null) {
681 return false;
682 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700683 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700684 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700685 }
686
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800687 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700688 if (!mService.mBooting && !mService.mBooted) {
689 // Not ready yet!
690 return false;
691 }
692
Craig Mautner84984fa2014-06-19 11:19:20 -0700693 if (prev != null) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700694 prev.getTask().setTaskToReturnTo(ACTIVITY_TYPE_STANDARD);
Craig Mautner84984fa2014-06-19 11:19:20 -0700695 }
696
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800697 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700698 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800699 final String myReason = reason + " resumeHomeStackTask";
700
Mark Lua56ea122015-10-08 13:31:01 +0800701 // Only resume home activity if isn't finishing.
702 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700703 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800704 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700705 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800706 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700707 }
708
Craig Mautner8d341ef2013-03-26 09:03:27 -0700709 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700710 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
711 }
712
713 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
714 return anyTaskForIdLocked(id, matchMode, null);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700715 }
716
717 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700718 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700719 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800720 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700721 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700722 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700723 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
724 @Nullable ActivityOptions aOptions) {
Winson Chung7900bee2017-03-10 08:59:25 -0800725 // If there is a stack id set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700726 // TODO: Don't really know if this is needed...
727 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
728 throw new IllegalArgumentException("Should not specify activity options for non-restore"
729 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800730 }
731
Craig Mautnere0a38842013-12-16 16:14:02 -0800732 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800733 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800734 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800735 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
736 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700737 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 if (task != null) {
739 return task;
740 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700741 }
742 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800743
Winson Chung7900bee2017-03-10 08:59:25 -0800744 // If we are matching stack tasks only, return now
745 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800746 return null;
747 }
748
Winson Chung7900bee2017-03-10 08:59:25 -0800749 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
750 // the task from recents
751 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700752 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
753
754 if (task == null) {
755 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800756 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
757 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700758
759 return null;
760 }
761
762 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700763 return task;
764 }
765
Winson Chung7900bee2017-03-10 08:59:25 -0800766 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700767 if (!restoreRecentTaskLocked(task, aOptions)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700768 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
769 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800770 return null;
771 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700772 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800773 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700774 }
775
Craig Mautner6170f732013-04-02 13:05:23 -0700776 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800777 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800778 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800779 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800780 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
781 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
782 if (r != null) {
783 return r;
784 }
Craig Mautner6170f732013-04-02 13:05:23 -0700785 }
786 }
787 return null;
788 }
789
Tony Mak853304c2016-04-18 15:17:41 +0100790 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000791 * Detects whether we should show a lock screen in front of this task for a locked user.
792 * <p>
793 * We'll do this if either of the following holds:
794 * <ul>
795 * <li>The top activity explicitly belongs to {@param userId}.</li>
796 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
797 * </ul>
798 *
799 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100800 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000801 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
802 // To handle the case that work app is in the task but just is not the top one.
803 final ActivityRecord activityRecord = task.getTopActivity();
804 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
805
806 return (activityRecord != null && activityRecord.userId == userId)
807 || (resultTo != null && resultTo.userId == userId);
808 }
809
810 /**
811 * Find all visible task stacks containing {@param userId} and intercept them with an activity
812 * to block out the contents and possibly start a credential-confirming intent.
813 *
814 * @param userId user handle for the locked managed profile.
815 */
816 void lockAllProfileTasks(@UserIdInt int userId) {
817 mWindowManager.deferSurfaceLayout();
818 try {
819 final List<ActivityStack> stacks = getStacks();
820 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
821 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
822 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
823 final TaskRecord task = tasks.get(taskNdx);
824
825 // Check the task for a top activity belonging to userId, or returning a result
826 // to an activity belonging to userId. Example case: a document picker for
827 // personal files, opened by a work app, should still get locked.
828 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000829 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
830 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000831 }
Tony Mak853304c2016-04-18 15:17:41 +0100832 }
833 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000834 } finally {
835 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100836 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000837 }
838
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800839 void setNextTaskIdForUserLocked(int taskId, int userId) {
840 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
841 if (taskId > currentTaskId) {
842 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700843 }
844 }
845
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700846 static int nextTaskIdForUser(int taskId, int userId) {
847 int nextTaskId = taskId + 1;
848 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
849 // Wrap around as there will be smaller task ids that are available now.
850 nextTaskId -= MAX_TASK_IDS_PER_USER;
851 }
852 return nextTaskId;
853 }
854
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800855 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800856 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
857 // for a userId u, a taskId can only be in the range
858 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
859 // 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 -0700860 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800861 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700862 || anyTaskForIdLocked(
863 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700864 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800865 if (candidateTaskId == currentTaskId) {
866 // Something wrong!
867 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
868 throw new IllegalStateException("Cannot get an available task id."
869 + " Reached limit of " + MAX_TASK_IDS_PER_USER
870 + " running tasks per user.");
871 }
872 }
873 mCurTaskIdForUser.put(userId, candidateTaskId);
874 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700875 }
876
Chong Zhang6cda19c2016-06-14 19:07:56 -0700877 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800878 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700879 if (stack == null) {
880 return null;
881 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700882 ActivityRecord resumedActivity = stack.mResumedActivity;
883 if (resumedActivity == null || resumedActivity.app == null) {
884 resumedActivity = stack.mPausingActivity;
885 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700886 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700887 }
888 }
889 return resumedActivity;
890 }
891
Dianne Hackbornff072722014-09-24 10:56:28 -0700892 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700893 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800894 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800895 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
896 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800897 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
898 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700899 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800900 continue;
901 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200902 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
903 final ActivityRecord top = stack.topRunningActivityLocked();
904 final int size = mTmpActivityList.size();
905 for (int i = 0; i < size; i++) {
906 final ActivityRecord activity = mTmpActivityList.get(i);
907 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
908 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200910 if (realStartActivityLocked(activity, app,
911 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800912 didSomething = true;
913 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700914 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800915 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200916 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800917 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700918 }
Craig Mautner20e72272013-04-01 13:45:53 -0700919 }
Craig Mautner20e72272013-04-01 13:45:53 -0700920 }
921 }
922 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700923 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700924 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700925 }
Craig Mautner20e72272013-04-01 13:45:53 -0700926 return didSomething;
927 }
928
929 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800930 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
931 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
933 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700934 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800935 continue;
936 }
937 final ActivityRecord resumedActivity = stack.mResumedActivity;
938 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700939 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800940 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800941 return false;
942 }
Craig Mautner20e72272013-04-01 13:45:53 -0700943 }
944 }
Wei Wang65c7a152016-06-02 18:51:22 -0700945 // Send launch end powerhint when idle
946 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700947 return true;
948 }
949
Craig Mautnerde4ef022013-04-07 19:01:33 -0700950 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
952 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
954 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700955 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700957 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800958 return false;
959 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700960 }
961 }
962 }
963 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700964 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800965 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
966 mLastFocusedStack + " to=" + mFocusedStack);
967 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700968 return true;
969 }
970
971 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800972 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800973 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
974 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
976 final ActivityStack stack = stacks.get(stackNdx);
977 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800978 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700979 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800980 return false;
981 }
982 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800983 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700984 }
985 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800986 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 }
988
Craig Mautner2acc3892013-09-23 10:28:14 -0700989 /**
990 * Pause all activities in either all of the stacks or just the back stacks.
991 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700992 * @param resuming The resuming activity.
993 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
994 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700995 * @return true if any activity was paused as a result of this call.
996 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700997 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700998 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800999 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1000 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001001 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1002 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001003 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001004 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001005 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001006 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1007 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001008 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001009 }
1010 }
1011 return someActivityPaused;
1012 }
1013
Craig Mautnerde4ef022013-04-07 19:01:33 -07001014 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001015 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1017 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001018 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1019 final ActivityStack stack = stacks.get(stackNdx);
1020 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001021 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001022 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001023 Slog.d(TAG_STATES,
1024 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001025 pausing = false;
1026 } else {
1027 return false;
1028 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001029 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001030 }
1031 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001032 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001033 }
1034
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001035 void cancelInitializingActivities() {
1036 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1037 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1038 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1039 stacks.get(stackNdx).cancelInitializingActivities();
1040 }
1041 }
1042 }
1043
Bryce Lee4a194382017-04-04 14:32:48 -07001044 void waitActivityVisible(ComponentName name, WaitResult result) {
1045 final WaitInfo waitInfo = new WaitInfo(name, result);
1046 mWaitingForActivityVisible.add(waitInfo);
1047 }
1048
1049 void cleanupActivity(ActivityRecord r) {
1050 // Make sure this record is no longer in the pending finishes list.
1051 // This could happen, for example, if we are trimming activities
1052 // down to the max limit while they are still waiting to finish.
1053 mFinishingActivities.remove(r);
1054 mActivitiesWaitingForVisibleActivity.remove(r);
1055
1056 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001057 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001058 mWaitingForActivityVisible.remove(i);
1059 }
1060 }
1061 }
1062
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001063 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001064 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001065 }
1066
1067 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1068 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001069 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1070 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001071 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001072 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001073 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001074 result.timeout = false;
1075 result.who = w.getComponent();
1076 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1077 result.thisTime = result.totalTime;
1078 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001079 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001080 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001081 if (changed) {
1082 mService.notifyAll();
1083 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001084 }
1085
Chong Zhang5022da32016-06-21 16:31:37 -07001086 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1087 boolean changed = false;
1088 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1089 WaitResult w = mWaitingActivityLaunched.remove(i);
1090 if (w.who == null) {
1091 changed = true;
1092 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1093 // the starting activity ends up moving another activity to front, and it should
1094 // wait for this new activity to become visible instead.
1095 // Do not modify other fields.
1096 w.result = START_TASK_TO_FRONT;
1097 }
1098 }
1099 if (changed) {
1100 mService.notifyAll();
1101 }
1102 }
1103
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001104 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1105 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001106 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001107 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001108 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001109 if (w.who == null) {
1110 changed = true;
1111 w.timeout = timeout;
1112 if (r != null) {
1113 w.who = new ComponentName(r.info.packageName, r.info.name);
1114 }
1115 w.thisTime = thisTime;
1116 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001117 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001118 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001120 if (changed) {
1121 mService.notifyAll();
1122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 }
1124
Craig Mautner29219d92013-04-16 20:19:12 -07001125 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001126 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001127 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001128 if (r != null) {
1129 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001130 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001131
Andrii Kulian7318d632016-07-20 18:59:28 -07001132 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001133 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1134
1135 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1136 final int displayId = mTmpOrderedDisplayIds.get(i);
1137 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1138 if (stacks == null) {
1139 continue;
1140 }
1141 for (int j = stacks.size() - 1; j >= 0; --j) {
1142 final ActivityStack stack = stacks.get(j);
1143 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1144 r = stack.topRunningActivityLocked();
1145 if (r != null) {
1146 return r;
1147 }
Craig Mautner29219d92013-04-16 20:19:12 -07001148 }
1149 }
1150 }
1151 return null;
1152 }
1153
Dianne Hackborn09233282014-04-30 11:33:59 -07001154 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001155 // Gather all of the running tasks for each stack into runningTaskLists.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001156 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001157 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001158 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001159 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001160 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1161 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001162 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001163 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001164 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001165 }
1166 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001167
1168 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1169 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1170 while (maxNum > 0) {
1171 long mostRecentActiveTime = Long.MIN_VALUE;
1172 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001173 final int numTaskLists = runningTaskLists.size();
1174 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1175 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001176 if (!stackTaskList.isEmpty()) {
1177 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1178 if (lastActiveTime > mostRecentActiveTime) {
1179 mostRecentActiveTime = lastActiveTime;
1180 selectedStackList = stackTaskList;
1181 }
1182 }
1183 }
1184 if (selectedStackList != null) {
1185 list.add(selectedStackList.remove(0));
1186 --maxNum;
1187 } else {
1188 break;
1189 }
1190 }
Craig Mautner20e72272013-04-01 13:45:53 -07001191 }
1192
Todd Kennedy7440f172015-12-09 14:31:22 -08001193 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1194 ProfilerInfo profilerInfo) {
1195 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001196 if (aInfo != null) {
1197 // Store the found target back into the intent, because now that
1198 // we have it we never want to do this again. For example, if the
1199 // user navigates back to this point in the history, we should
1200 // always restart the exact same activity.
1201 intent.setComponent(new ComponentName(
1202 aInfo.applicationInfo.packageName, aInfo.name));
1203
1204 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001205 if (!aInfo.processName.equals("system")) {
1206 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001207 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001208 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001209
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001210 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1211 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1212 }
1213
Man Caocfa78b22015-06-11 20:14:34 -07001214 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1215 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1216 }
1217
1218 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001219 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001220 }
1221 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001222 final String intentLaunchToken = intent.getLaunchToken();
1223 if (aInfo.launchToken == null && intentLaunchToken != null) {
1224 aInfo.launchToken = intentLaunchToken;
1225 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001226 }
1227 return aInfo;
1228 }
1229
Todd Kennedy7440f172015-12-09 14:31:22 -08001230 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001231 return resolveIntent(intent, resolvedType, userId, 0);
1232 }
1233
1234 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001235 synchronized (mService) {
1236 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001237 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Todd Kennedy82b08422017-09-28 13:32:05 -07001238 | ActivityManagerService.STOCK_PM_FLAGS, userId, true);
Todd Kennedy7440f172015-12-09 14:31:22 -08001239 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001240 }
1241
1242 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1243 ProfilerInfo profilerInfo, int userId) {
1244 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1245 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1246 }
1247
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001248 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1249 boolean andResume, boolean checkConfig) throws RemoteException {
1250
1251 if (!allPausedActivitiesComplete()) {
1252 // While there are activities pausing we skipping starting any new activities until
1253 // pauses are complete. NOTE: that we also do this for activities that are starting in
1254 // the paused state because they will first be resumed then paused on the client side.
1255 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1256 "realStartActivityLocked: Skipping start of r=" + r
1257 + " some activities pausing...");
1258 return false;
1259 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001260
Bryce Leeaf691c02017-03-20 14:20:22 -07001261 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001262 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001263
1264 beginDeferResume();
1265
Craig Mautner2420ead2013-04-01 17:13:20 -07001266 try {
chaviw59b98852017-06-13 12:05:44 -07001267 r.startFreezingScreenLocked(app, 0);
1268
1269 // schedule launch ticks to collect information about slow apps.
1270 r.startLaunchTickingLocked();
1271
1272 r.app = app;
1273
Jorim Jaggi838c2452017-08-28 15:44:43 +02001274 if (mKeyguardController.isKeyguardLocked()) {
1275 r.notifyUnknownVisibilityLaunched();
1276 }
1277
chaviw59b98852017-06-13 12:05:44 -07001278 // Have the window manager re-evaluate the orientation of the screen based on the new
1279 // activity order. Note that as a result of this, it can call back into the activity
1280 // manager with a new orientation. We don't care about that, because the activity is
1281 // not currently running so we are just restarting it anyway.
1282 if (checkConfig) {
1283 final int displayId = r.getDisplayId();
1284 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1285 getDisplayOverrideConfiguration(displayId),
1286 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1287 // Deferring resume here because we're going to launch new activity shortly.
1288 // We don't want to perform a redundant launch of the same record while ensuring
1289 // configurations and trying to resume top activity of focused stack.
1290 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1291 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 }
chaviw59b98852017-06-13 12:05:44 -07001293
1294 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1295 true /* isTop */)) {
1296 // We only set the visibility to true if the activity is allowed to be visible
1297 // based on
1298 // keyguard state. This avoids setting this into motion in window manager that is
1299 // later cancelled due to later calls to ensure visible activities that set
1300 // visibility back to false.
1301 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 }
chaviw59b98852017-06-13 12:05:44 -07001303
chaviw59b98852017-06-13 12:05:44 -07001304 final int applicationInfoUid =
1305 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1306 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1307 Slog.wtf(TAG,
1308 "User ID for activity changing for " + r
1309 + " appInfo.uid=" + r.appInfo.uid
1310 + " info.ai.uid=" + applicationInfoUid
1311 + " old=" + r.app + " new=" + app);
1312 }
1313
1314 app.waitingToKill = null;
1315 r.launchCount++;
1316 r.lastLaunchTime = SystemClock.uptimeMillis();
1317
1318 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1319
1320 int idx = app.activities.indexOf(r);
1321 if (idx < 0) {
1322 app.activities.add(r);
1323 }
1324 mService.updateLruProcessLocked(app, true, null);
1325 mService.updateOomAdjLocked();
1326
1327 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1328 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001329 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001330 }
1331
1332 try {
1333 if (app.thread == null) {
1334 throw new RemoteException();
1335 }
1336 List<ResultInfo> results = null;
1337 List<ReferrerIntent> newIntents = null;
1338 if (andResume) {
1339 // We don't need to deliver new intents and/or set results if activity is going
1340 // to pause immediately after launch.
1341 results = r.results;
1342 newIntents = r.newIntents;
1343 }
1344 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1345 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1346 + " newIntents=" + newIntents + " andResume=" + andResume);
1347 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1348 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001349 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001350 // Home process is the root process of the task.
1351 mService.mHomeProcess = task.mActivities.get(0).app;
1352 }
1353 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1354 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1355 r.sleeping = false;
1356 r.forceNewConfig = false;
1357 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1358 mService.showAskCompatModeDialogLocked(r);
1359 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1360 ProfilerInfo profilerInfo = null;
1361 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1362 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1363 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001364 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1365 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1366 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001367 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001368 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001369 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001370 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001371 }
1372 }
chaviw59b98852017-06-13 12:05:44 -07001373
Andreas Gampe2b073a02017-06-22 17:28:57 -07001374 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001375 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 }
1377 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001378
chaviw59b98852017-06-13 12:05:44 -07001379 app.hasShownUi = true;
1380 app.pendingUiClean = true;
1381 app.forceProcessStateUpTo(mService.mTopProcessState);
1382 // Because we could be starting an Activity in the system process this may not go
1383 // across a Binder interface which would create a new Configuration. Consequently
1384 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001385
chaviw59b98852017-06-13 12:05:44 -07001386 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1387 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1388 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001389
chaviw59b98852017-06-13 12:05:44 -07001390 logIfTransactionTooLarge(r.intent, r.icicle);
1391 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1392 System.identityHashCode(r), r.info,
1393 // TODO: Have this take the merged configuration instead of separate global
1394 // and override configs.
1395 mergedConfiguration.getGlobalConfiguration(),
1396 mergedConfiguration.getOverrideConfiguration(), r.compat,
1397 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1398 r.persistentState, results, newIntents, !andResume,
1399 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001400
chaviw59b98852017-06-13 12:05:44 -07001401 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1402 // This may be a heavy-weight process! Note that the package
1403 // manager will ensure that only activity can run in the main
1404 // process of the .apk, which is the only thing that will be
1405 // considered heavy-weight.
1406 if (app.processName.equals(app.info.packageName)) {
1407 if (mService.mHeavyWeightProcess != null
1408 && mService.mHeavyWeightProcess != app) {
1409 Slog.w(TAG, "Starting new heavy weight process " + app
1410 + " when already running "
1411 + mService.mHeavyWeightProcess);
1412 }
1413 mService.mHeavyWeightProcess = app;
1414 Message msg = mService.mHandler.obtainMessage(
1415 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1416 msg.obj = r;
1417 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001418 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001419 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001420
chaviw59b98852017-06-13 12:05:44 -07001421 } catch (RemoteException e) {
1422 if (r.launchFailed) {
1423 // This is the second time we failed -- finish activity
1424 // and give up.
1425 Slog.e(TAG, "Second failure launching "
1426 + r.intent.getComponent().flattenToShortString()
1427 + ", giving up", e);
1428 mService.appDiedLocked(app);
1429 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1430 "2nd-crash", false);
1431 return false;
1432 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001433
chaviw59b98852017-06-13 12:05:44 -07001434 // This is the first time we failed -- restart process and
1435 // retry.
1436 r.launchFailed = true;
1437 app.activities.remove(r);
1438 throw e;
1439 }
1440 } finally {
1441 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001442 }
1443
1444 r.launchFailed = false;
1445 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001446 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001447 }
1448
chaviw59b98852017-06-13 12:05:44 -07001449 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001450 // As part of the process of launching, ActivityThread also performs
1451 // a resume.
1452 stack.minimalResumeActivityLocked(r);
1453 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001454 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001455 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001456 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001457 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001458 "Moving to PAUSED: " + r + " (starting in paused state)");
1459 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001460 }
1461
1462 // Launch the new version setup screen if needed. We do this -after-
1463 // launching the initial activity (that is, home), so that it can have
1464 // a chance to initialize itself while in the background, making the
1465 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001466 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001467 mService.startSetupActivityLocked();
1468 }
1469
Dianne Hackborn465fa392014-09-14 14:21:18 -07001470 // Update any services we are bound to that might care about whether
1471 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001472 if (r.app != null) {
1473 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1474 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001475
Craig Mautner2420ead2013-04-01 17:13:20 -07001476 return true;
1477 }
1478
Sudheer Shankafab200f2017-05-17 20:41:53 -07001479 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1480 int extrasSize = 0;
1481 if (intent != null) {
1482 final Bundle extras = intent.getExtras();
1483 if (extras != null) {
1484 extrasSize = extras.getSize();
1485 }
1486 }
1487 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1488 if (extrasSize + icicleSize > 200000) {
1489 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1490 + ", icicle size: " + icicleSize);
1491 }
1492 }
1493
Craig Mautnere79d42682013-04-01 19:01:53 -07001494 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001495 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001496 // Is this activity's application already running?
1497 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001498 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001499
Andrii Kulian02b7a832016-10-06 23:11:56 -07001500 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001501
1502 if (app != null && app.thread != null) {
1503 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001504 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1505 || !"android".equals(r.info.packageName)) {
1506 // Don't add this if it is a platform component that is marked
1507 // to run in multiple processes, because this is actually
1508 // part of the framework so doesn't make sense to track as a
1509 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001510 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1511 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001512 }
George Mount2c92c972014-03-20 09:38:23 -07001513 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001514 return;
1515 } catch (RemoteException e) {
1516 Slog.w(TAG, "Exception when starting activity "
1517 + r.intent.getComponent().flattenToShortString(), e);
1518 }
1519
1520 // If a dead object exception was thrown -- fall through to
1521 // restart the application.
1522 }
1523
1524 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001525 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001526 }
1527
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001528 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001529 String resultWho, int requestCode, int callingPid, int callingUid,
1530 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001531 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001532 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1533 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001534 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001535 return true;
1536 }
1537 final int componentRestriction = getComponentRestrictionForCallingPackage(
1538 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1539 final int actionRestriction = getActionRestrictionForCallingPackage(
1540 intent.getAction(), callingPackage, callingPid, callingUid);
1541 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1542 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1543 if (resultRecord != null) {
1544 resultStack.sendActivityResultLocked(-1,
1545 resultRecord, resultWho, requestCode,
1546 Activity.RESULT_CANCELED, null);
1547 }
1548 final String msg;
1549 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1550 msg = "Permission Denial: starting " + intent.toString()
1551 + " from " + callerApp + " (pid=" + callingPid
1552 + ", uid=" + callingUid + ")" + " with revoked permission "
1553 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1554 } else if (!aInfo.exported) {
1555 msg = "Permission Denial: starting " + intent.toString()
1556 + " from " + callerApp + " (pid=" + callingPid
1557 + ", uid=" + callingUid + ")"
1558 + " not exported from uid " + aInfo.applicationInfo.uid;
1559 } else {
1560 msg = "Permission Denial: starting " + intent.toString()
1561 + " from " + callerApp + " (pid=" + callingPid
1562 + ", uid=" + callingUid + ")"
1563 + " requires " + aInfo.permission;
1564 }
1565 Slog.w(TAG, msg);
1566 throw new SecurityException(msg);
1567 }
1568
1569 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1570 final String message = "Appop Denial: starting " + intent.toString()
1571 + " from " + callerApp + " (pid=" + callingPid
1572 + ", uid=" + callingUid + ")"
1573 + " requires " + AppOpsManager.permissionToOp(
1574 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1575 Slog.w(TAG, message);
1576 return false;
1577 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1578 final String message = "Appop Denial: starting " + intent.toString()
1579 + " from " + callerApp + " (pid=" + callingPid
1580 + ", uid=" + callingUid + ")"
1581 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1582 Slog.w(TAG, message);
1583 return false;
1584 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001585 if (options != null) {
1586 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1587 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1588 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001589 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001590 final String msg = "Permission Denial: starting " + intent.toString()
1591 + " from " + callerApp + " (pid=" + callingPid
1592 + ", uid=" + callingUid + ") with launchTaskId="
1593 + options.getLaunchTaskId();
1594 Slog.w(TAG, msg);
1595 throw new SecurityException(msg);
1596 }
1597 }
1598 // Check if someone tries to launch an activity on a private display with a different
1599 // owner.
1600 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001601 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1602 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001603 final String msg = "Permission Denial: starting " + intent.toString()
1604 + " from " + callerApp + " (pid=" + callingPid
1605 + ", uid=" + callingUid + ") with launchDisplayId="
1606 + launchDisplayId;
1607 Slog.w(TAG, msg);
1608 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001609 }
1610 }
1611
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001612 return true;
1613 }
1614
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001615 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001616 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1617 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001618 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1619 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1620
Andrii Kulian02689a72017-07-06 14:28:59 -07001621 if (callingPid == -1 && callingUid == -1) {
1622 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1623 return true;
1624 }
1625
Andrii Kulian62e6f252017-05-30 22:46:53 -07001626 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001627 if (activityDisplay == null) {
1628 Slog.w(TAG, "Launch on display check: display not found");
1629 return false;
1630 }
1631
Andrii Kulian02689a72017-07-06 14:28:59 -07001632 // Check if the caller has enough privileges to embed activities and launch to private
1633 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001634 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001635 callingUid);
1636 if (startAnyPerm == PERMISSION_GRANTED) {
1637 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1638 + " allow launch any on display");
1639 return true;
1640 }
1641
Andrii Kulian8f070292017-09-12 22:56:49 -07001642 // Check if caller is already present on display
1643 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1644
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001645 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1646 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1647 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001648 // Limit launching on virtual displays, because their contents can be read from Surface
1649 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001650 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1651 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1652 + " disallow launch on virtual display for not-embedded activity.");
1653 return false;
1654 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001655 // Check if the caller is allowed to embed activities from other apps.
1656 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001657 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001658 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1659 + " disallow activity embedding without permission.");
1660 return false;
1661 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001662 }
1663
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001664 if (!activityDisplay.isPrivate()) {
1665 // Anyone can launch on a public display.
1666 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1667 + " allow launch on public display");
1668 return true;
1669 }
1670
1671 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001672 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001673 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1674 + " allow launch for owner of the display");
1675 return true;
1676 }
1677
Andrii Kulian8f070292017-09-12 22:56:49 -07001678 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001679 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1680 + " allow launch for caller present on the display");
1681 return true;
1682 }
1683
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001684 Slog.w(TAG, "Launch on display check: denied");
1685 return false;
1686 }
1687
1688 /** Update lists of UIDs that are present on displays and have access to them. */
1689 void updateUIDsPresentOnDisplay() {
1690 mDisplayAccessUIDs.clear();
1691 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1692 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001693 // Only bother calculating the whitelist for private displays
1694 if (activityDisplay.isPrivate()) {
1695 mDisplayAccessUIDs.append(
1696 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1697 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001698 }
1699 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1700 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1701 }
1702
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001703 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001704 final long identity = Binder.clearCallingIdentity();
1705 try {
1706 return UserManager.get(mService.mContext).getUserInfo(userId);
1707 } finally {
1708 Binder.restoreCallingIdentity(identity);
1709 }
1710 }
1711
Svet Ganov99b60432015-06-27 13:15:22 -07001712 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001713 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001714 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1715 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001716 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001717 return ACTIVITY_RESTRICTION_PERMISSION;
1718 }
1719
Christopher Tateff7add02015-08-17 10:23:22 -07001720 if (activityInfo.permission == null) {
1721 return ACTIVITY_RESTRICTION_NONE;
1722 }
1723
Svet Ganov99b60432015-06-27 13:15:22 -07001724 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1725 if (opCode == AppOpsManager.OP_NONE) {
1726 return ACTIVITY_RESTRICTION_NONE;
1727 }
1728
1729 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1730 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001731 if (!ignoreTargetSecurity) {
1732 return ACTIVITY_RESTRICTION_APPOP;
1733 }
Svet Ganov99b60432015-06-27 13:15:22 -07001734 }
1735
1736 return ACTIVITY_RESTRICTION_NONE;
1737 }
1738
Svetoslav7008b512015-06-24 18:47:07 -07001739 private int getActionRestrictionForCallingPackage(String action,
1740 String callingPackage, int callingPid, int callingUid) {
1741 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001742 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001743 }
1744
1745 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1746 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001747 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001748 }
1749
1750 final PackageInfo packageInfo;
1751 try {
1752 packageInfo = mService.mContext.getPackageManager()
1753 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1754 } catch (PackageManager.NameNotFoundException e) {
1755 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001756 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001757 }
1758
1759 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001760 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001761 }
1762
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001763 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001764 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001765 }
1766
1767 final int opCode = AppOpsManager.permissionToOpCode(permission);
1768 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
1772 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1773 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001774 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001775 }
1776
Svet Ganov99b60432015-06-27 13:15:22 -07001777 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001778 }
1779
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001780 void setLaunchSource(int uid) {
1781 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1782 }
1783
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001784 void acquireLaunchWakelock() {
1785 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1786 throw new IllegalStateException("Calling must be system uid");
1787 }
1788 mLaunchingActivity.acquire();
1789 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1790 // To be safe, don't allow the wake lock to be held for too long.
1791 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1792 }
1793 }
1794
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001795 /**
1796 * Called when the frontmost task is idle.
1797 * @return the state of mService.mBooting before this was called.
1798 */
1799 private boolean checkFinishBootingLocked() {
1800 final boolean booting = mService.mBooting;
1801 boolean enableScreen = false;
1802 mService.mBooting = false;
1803 if (!mService.mBooted) {
1804 mService.mBooted = true;
1805 enableScreen = true;
1806 }
1807 if (booting || enableScreen) {
1808 mService.postFinishBooting(booting, enableScreen);
1809 }
1810 return booting;
1811 }
1812
Craig Mautnerf3333272013-04-22 10:55:53 -07001813 // Checked.
1814 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001815 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001816 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001817
Craig Mautnerf3333272013-04-22 10:55:53 -07001818 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001819 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001820 int NS = 0;
1821 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001822 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001823 boolean activityRemoved = false;
1824
Wale Ogunwale7d701172015-03-11 15:36:30 -07001825 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001826 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001827 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1828 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001829 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1830 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001831 if (fromTimeout) {
1832 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001833 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001834
1835 // This is a hack to semi-deal with a race condition
1836 // in the client where it can be constructed with a
1837 // newer configuration from when we asked it to launch.
1838 // We'll update with whatever configuration it now says
1839 // it used to launch.
1840 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001841 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001842 }
1843
1844 // We are now idle. If someone is waiting for a thumbnail from
1845 // us, we can now deliver.
1846 r.idle = true;
1847
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001848 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001849 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001850 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001851 }
1852 }
1853
1854 if (allResumedActivitiesIdle()) {
1855 if (r != null) {
1856 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001857 }
1858
1859 if (mLaunchingActivity.isHeld()) {
1860 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1861 if (VALIDATE_WAKE_LOCK_CALLER &&
1862 Binder.getCallingUid() != Process.myUid()) {
1863 throw new IllegalStateException("Calling must be system uid");
1864 }
1865 mLaunchingActivity.release();
1866 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001867 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001868 }
1869
1870 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001871 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1872 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001873 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001874 if ((NF = mFinishingActivities.size()) > 0) {
1875 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001876 mFinishingActivities.clear();
1877 }
1878
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001880 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001881 mStartingUsers.clear();
1882 }
1883
Craig Mautnerf3333272013-04-22 10:55:53 -07001884 // Stop any activities that are scheduled to do so but have been
1885 // waiting for the next one to start.
1886 for (int i = 0; i < NS; i++) {
1887 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001888 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001889 if (stack != null) {
1890 if (r.finishing) {
1891 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1892 } else {
1893 stack.stopActivityLocked(r);
1894 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 }
1896 }
1897
1898 // Finish any activities that are scheduled to do so but have been
1899 // waiting for the next one to start.
1900 for (int i = 0; i < NF; i++) {
1901 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001902 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001903 if (stack != null) {
1904 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1905 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001906 }
1907
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001908 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001909 // Complete user switch
1910 if (startingUsers != null) {
1911 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001912 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001913 }
1914 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001915 }
1916
1917 mService.trimApplications();
1918 //dump();
1919 //mWindowManager.dump();
1920
Craig Mautnerf3333272013-04-22 10:55:53 -07001921 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001922 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001923 }
1924
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001925 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001926 }
1927
Craig Mautner8e569572013-10-11 17:36:59 -07001928 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001929 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001930 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1931 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001932 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1933 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1934 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001935 }
Craig Mautner19091252013-10-05 00:03:53 -07001936 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001937 }
1938
1939 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001940 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1941 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001942 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1943 stacks.get(stackNdx).closeSystemDialogsLocked();
1944 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001945 }
1946 }
1947
Craig Mautner93529a42013-10-04 15:03:13 -07001948 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001949 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001950 }
1951
Craig Mautner8d341ef2013-03-26 09:03:27 -07001952 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001953 * Update the last used stack id for non-current user (current user's last
1954 * used stack is the focused stack)
1955 */
1956 void updateUserStackLocked(int userId, ActivityStack stack) {
1957 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07001958 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07001959 }
1960 }
1961
1962 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001963 * @return true if some activity was finished (or would have finished if doit were true).
1964 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001965 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1966 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001967 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001968 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1969 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001970 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001971 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001972 if (stack.finishDisabledPackageActivitiesLocked(
1973 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001974 didSomething = true;
1975 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001976 }
1977 }
1978 return didSomething;
1979 }
1980
Dianne Hackborna413dc02013-07-12 12:02:55 -07001981 void updatePreviousProcessLocked(ActivityRecord r) {
1982 // Now that this process has stopped, we may want to consider
1983 // it to be the previous app to try to keep around in case
1984 // the user wants to return to it.
1985
1986 // First, found out what is currently the foreground app, so that
1987 // we don't blow away the previous app if this activity is being
1988 // hosted by the process that is actually still the foreground.
1989 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1991 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1993 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001994 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001995 if (stack.mResumedActivity != null) {
1996 fgApp = stack.mResumedActivity.app;
1997 } else if (stack.mPausingActivity != null) {
1998 fgApp = stack.mPausingActivity.app;
1999 }
2000 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002001 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002002 }
2003 }
2004
2005 // Now set this one as the previous process, only if that really
2006 // makes sense to.
2007 if (r.app != null && fgApp != null && r.app != fgApp
2008 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002009 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002010 mService.mPreviousProcess = r.app;
2011 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2012 }
2013 }
2014
Wale Ogunwaled046a012015-12-24 13:05:59 -08002015 boolean resumeFocusedStackTopActivityLocked() {
2016 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002017 }
2018
Wale Ogunwaled046a012015-12-24 13:05:59 -08002019 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002020 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002021
2022 if (!readyToResume()) {
2023 return false;
2024 }
2025
Wale Ogunwaled046a012015-12-24 13:05:59 -08002026 if (targetStack != null && isFocusedStack(targetStack)) {
2027 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002028 }
chaviw59b98852017-06-13 12:05:44 -07002029
Wale Ogunwale06579d62016-04-30 15:29:06 -07002030 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2031 if (r == null || r.state != RESUMED) {
2032 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002033 } else if (r.state == RESUMED) {
2034 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2035 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002036 }
chaviw59b98852017-06-13 12:05:44 -07002037
Wale Ogunwaled046a012015-12-24 13:05:59 -08002038 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002039 }
2040
Todd Kennedy39bfee52016-02-24 10:28:21 -08002041 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2042 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2043 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2044 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2045 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2046 }
2047 }
2048 }
2049
Adrian Roos20d7df32016-01-12 18:59:43 +01002050 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2051 TaskRecord finishedTask = null;
2052 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002053 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2054 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002055 final int numStacks = stacks.size();
2056 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2057 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002058 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2059 if (stack == focusedStack || finishedTask == null) {
2060 finishedTask = t;
2061 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002062 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002063 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002064 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002065 }
2066
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002067 void finishVoiceTask(IVoiceInteractionSession session) {
2068 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2069 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2070 final int numStacks = stacks.size();
2071 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2072 final ActivityStack stack = stacks.get(stackNdx);
2073 stack.finishVoiceTask(session);
2074 }
2075 }
2076 }
2077
Andrii Kulianc27916642016-04-12 17:59:27 -07002078 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2079 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002080 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2081 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002082 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002083 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2084 // Caller wants the home activity moved with it. To accomplish this,
2085 // we'll just indicate that this task returns to the home task.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002086 task.setTaskToReturnTo(ACTIVITY_TYPE_HOME);
Craig Mautneraea74a52014-03-08 14:23:10 -08002087 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002088 final ActivityStack currentStack = task.getStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002089 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002090 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2091 + task + " to front. Stack is null");
2092 return;
2093 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002094
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002095 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2096 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2097 task.updateOverrideConfiguration(bounds);
2098
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002099 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002100
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002101 if (stack != currentStack) {
2102 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
2103 "findTaskToMoveToFrontLocked");
2104 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002105 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2106 // still need moveTaskToFrontLocked() below for any transition settings.
2107 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002108 if (StackId.resizeStackWithLaunchBounds(stack.mStackId)) {
2109 resizeStackLocked(stack.mStackId, bounds, null /* tempTaskBounds */,
2110 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2111 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002112 } else {
2113 // WM resizeTask must be done after the task is moved to the correct stack,
2114 // because Task's setBounds() also updates dim layer's bounds, but that has
2115 // dependency on the stack.
2116 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002117 }
2118 }
2119
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002120 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002121 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002122 r == null ? null : r.appTimeTracker, reason);
2123
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002124 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002125 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002126
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002127 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002128 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002129 }
2130
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002131 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002132 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002133 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002134 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002135 return false;
2136 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002137 return (mService.mSupportsPictureInPicture
2138 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002139 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002140 }
2141
Winson Chung55893332017-02-17 17:13:10 -08002142 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002143 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2144 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2145 if (stack != null) {
2146 return stack;
2147 }
2148 }
2149 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002150 }
2151
Wale Ogunwale68278562017-09-23 17:13:55 -07002152 /** @see ActivityDisplay#getStack(int, int) */
2153 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2154 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2155 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2156 if (stack != null) {
2157 return stack;
2158 }
2159 }
2160 return null;
2161 }
2162
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002163 /**
2164 * Returns true if the {@param windowingMode} is supported based on other parameters passed in.
2165 * @param windowingMode The windowing mode we are checking support for.
2166 * @param supportsMultiWindow If we should consider support for multi-window mode in general.
2167 * @param supportsSplitScreen If we should consider support for split-screen multi-window.
2168 * @param supportsFreeform If we should consider support for freeform multi-window.
2169 * @param supportsPip If we should consider support for picture-in-picture mutli-window.
2170 * @param activityType The activity type under consideration.
2171 * @return true if the windowing mode is supported.
2172 */
2173 boolean isWindowingModeSupported(int windowingMode, boolean supportsMultiWindow,
2174 boolean supportsSplitScreen, boolean supportsFreeform, boolean supportsPip,
2175 int activityType) {
2176
2177 if (windowingMode == WINDOWING_MODE_UNDEFINED
2178 || windowingMode == WINDOWING_MODE_FULLSCREEN) {
2179 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002180 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002181 if (!supportsMultiWindow) {
2182 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002183 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002184
2185 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2186 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
2187 return supportsSplitScreen && WindowConfiguration.supportSplitScreenWindowingMode(
2188 windowingMode, activityType);
Matthew Ng330757d2017-02-28 14:19:17 -08002189 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002190
2191 if (!supportsFreeform && windowingMode == WINDOWING_MODE_FREEFORM) {
2192 return false;
2193 }
2194
2195 if (!supportsPip && windowingMode == WINDOWING_MODE_PINNED) {
2196 return false;
2197 }
2198 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002199 }
2200
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002201 private int resolveWindowingMode(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002202 @Nullable TaskRecord task, int activityType) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002203
2204 // First preference if the windowing mode in the activity options if set.
2205 int windowingMode = (options != null)
2206 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
2207
2208 // If windowing mode is unset, then next preference is the candidate task, then the
2209 // activity record.
2210 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2211 if (task != null) {
2212 windowingMode = task.getWindowingMode();
2213 }
2214 if (windowingMode == WINDOWING_MODE_UNDEFINED && r != null) {
2215 windowingMode = r.getWindowingMode();
2216 }
2217 }
2218
2219 // Make sure the windowing mode we are trying to use makes sense for what is supported.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002220 boolean supportsMultiWindow = mService.mSupportsMultiWindow;
2221 boolean supportsSplitScreen = mService.mSupportsSplitScreenMultiWindow;
2222 boolean supportsFreeform = mService.mSupportsFreeformWindowManagement;
2223 boolean supportsPip = mService.mSupportsPictureInPicture;
2224 if (supportsMultiWindow) {
2225 if (task != null) {
2226 supportsMultiWindow = task.isResizeable();
2227 supportsSplitScreen = task.supportsSplitScreenWindowingMode();
2228 // TODO: Do we need to check for freeform and Pip support here?
2229 } else if (r != null) {
2230 supportsMultiWindow = r.isResizeable();
2231 supportsSplitScreen = r.supportsSplitScreenWindowingMode();
2232 supportsFreeform = r.supportsFreeform();
2233 supportsPip = r.supportsPictureInPicture();
2234 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002235 }
2236
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002237 if (isWindowingModeSupported(windowingMode, supportsMultiWindow, supportsSplitScreen,
2238 supportsFreeform, supportsPip, activityType)) {
2239 return windowingMode;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002240 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002241 return WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002242 }
2243
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002244 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002245 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002246 // Preference is given to the activity type for the activity then the task since the type
2247 // once set shouldn't change.
2248 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2249 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2250 activityType = task.getActivityType();
2251 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002252 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2253 return activityType;
2254 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002255 return options != null ? options.getLaunchActivityType() : ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002256 }
2257
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002258 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2259 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2260 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002261 }
2262
2263 /**
2264 * Returns the right stack to use for launching factoring in all the input parameters.
2265 *
2266 * @param r The activity we are trying to launch. Can be null.
2267 * @param options The activity options used to the launch. Can be null.
2268 * @param candidateTask The possible task the activity might be launched in. Can be null.
2269 *
2270 * @return The stack to use for the launch or INVALID_STACK_ID.
2271 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002272 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2273 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2274 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002275 int taskId = INVALID_TASK_ID;
2276 int displayId = INVALID_DISPLAY;
2277 //Rect bounds = null;
2278
2279 // We give preference to the launch preference in activity options.
2280 if (options != null) {
2281 taskId = options.getLaunchTaskId();
2282 displayId = options.getLaunchDisplayId();
2283 // TODO: Need to work this into the equation...
2284 //bounds = options.getLaunchBounds();
2285 }
2286
2287 // First preference for stack goes to the task Id set in the activity options. Use the stack
2288 // associated with that if possible.
2289 if (taskId != INVALID_TASK_ID) {
2290 // Temporarily set the task id to invalid in case in re-entry.
2291 options.setLaunchTaskId(INVALID_TASK_ID);
2292 final TaskRecord task = anyTaskForIdLocked(taskId,
2293 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options);
2294 options.setLaunchTaskId(taskId);
2295 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002296 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002297 }
2298 }
2299
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002300 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002301 int windowingMode = resolveWindowingMode(r, options, candidateTask, activityType);
2302 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002303
2304 // Next preference for stack goes to the display Id set in the activity options or the
2305 // candidate display.
2306 if (displayId == INVALID_DISPLAY) {
2307 displayId = candidateDisplayId;
2308 }
2309 if (displayId != INVALID_DISPLAY) {
2310 if (r != null) {
2311 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002312 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002313 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002314 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002315 }
2316 }
2317 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2318 if (display != null) {
2319 for (int i = display.mStacks.size() - 1; i >= 0; --i) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002320 stack = (T) display.mStacks.get(i);
2321 if (stack.isCompatible(windowingMode, activityType)) {
2322 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002323 }
2324 }
2325 // TODO: We should create the stack we want on the display at this point.
2326 }
2327 }
2328
2329 // Give preference to the stack and display of the input task and activity if they match the
2330 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002331 stack = null;
2332 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002333 if (candidateTask != null) {
2334 stack = candidateTask.getStack();
2335 }
2336 if (stack == null && r != null) {
2337 stack = r.getStack();
2338 }
2339 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002340 if (stack.isCompatible(windowingMode, activityType)) {
2341 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002342 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002343 display = stack.getDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002344 }
2345
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002346 if (display == null
2347 // TODO: Can be removed once we figure-out how non-standard types should launch
2348 // outside the default display.
2349 || (activityType != ACTIVITY_TYPE_STANDARD
2350 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2351 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002352 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002353
2354 stack = display.getOrCreateStack(windowingMode, activityType, onTop);
2355 if (stack != null) {
2356 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002357 }
2358
2359 // Whatever...return some default for now.
2360 if (candidateTask != null && candidateTask.mBounds != null
2361 && mService.mSupportsFreeformWindowManagement) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002362 windowingMode = WINDOWING_MODE_FREEFORM;
2363 } else {
2364 windowingMode = WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002365 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002366 return display.getOrCreateStack(windowingMode, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002367 }
2368
Andrii Kulian16802aa2016-11-02 12:21:33 -07002369 /**
2370 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2371 * If there is no such stack, new dynamic stack can be created.
2372 * @param displayId Target display.
2373 * @param r Activity that should be launched there.
2374 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2375 */
2376 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002377 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002378 if (activityDisplay == null) {
2379 throw new IllegalArgumentException(
2380 "Display with displayId=" + displayId + " not found.");
2381 }
2382
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002383 if (!r.canBeLaunchedOnDisplay(displayId)) {
2384 return null;
2385 }
2386
Andrii Kulian16802aa2016-11-02 12:21:33 -07002387 // Return the topmost valid stack on the display.
2388 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2389 final ActivityStack stack = activityDisplay.mStacks.get(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002390 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002391 return stack;
2392 }
2393 }
2394
2395 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002396 if (displayId != DEFAULT_DISPLAY) {
2397 return activityDisplay.createStack(
2398 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002399 }
2400
2401 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2402 return null;
2403 }
2404
Wale Ogunwale68278562017-09-23 17:13:55 -07002405 // TODO: Can probably be consolidated into getLaunchStack()...
2406 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2407 switch (stack.getActivityType()) {
2408 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2409 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2410 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002411 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002412 switch (stack.getWindowingMode()) {
2413 case WINDOWING_MODE_FULLSCREEN: return true;
2414 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2415 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2416 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2417 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2418 }
2419
2420 if (StackId.isDynamicStack(stack.mStackId)) {
2421 return r.canBeLaunchedOnDisplay(displayId);
2422 }
2423 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2424 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002425 }
2426
Craig Mautner967212c2013-04-13 21:10:58 -07002427 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002428 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002429 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2430 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002431 }
2432 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002433 }
2434
Jorim Jaggife762342016-10-13 14:33:27 +02002435 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2436 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2437 }
2438
Andrii Kulian7fc22812016-12-28 13:04:11 -08002439 /**
2440 * Get next focusable stack in the system. This will search across displays and stacks
2441 * in last-focused order for a focusable and visible stack, different from the target stack.
2442 *
2443 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2444 * searching for next candidate.
2445 * @return Next focusable {@link ActivityStack}, null if not found.
2446 */
2447 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2448 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2449
2450 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2451 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002452 // If a display is registered in WM, it must also be available in AM.
2453 @SuppressWarnings("ConstantConditions")
2454 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002455 for (int j = stacks.size() - 1; j >= 0; --j) {
2456 final ActivityStack stack = stacks.get(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002457 if (stack != currentFocus && stack.isFocusable()
2458 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002459 return stack;
2460 }
2461 }
2462 }
2463
2464 return null;
2465 }
2466
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002467 /**
2468 * Get next valid stack for launching provided activity in the system. This will search across
2469 * displays and stacks in last-focused order for a focusable and visible stack, except those
2470 * that are on a currently focused display.
2471 *
2472 * @param r The activity that is being launched.
2473 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2474 * searching for the next candidate.
2475 * @return Next valid {@link ActivityStack}, null if not found.
2476 */
2477 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2478 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2479 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2480 final int displayId = mTmpOrderedDisplayIds.get(i);
2481 if (displayId == currentFocus) {
2482 continue;
2483 }
2484 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2485 if (stack != null) {
2486 return stack;
2487 }
2488 }
2489 return null;
2490 }
2491
Craig Mautneree2e45a2014-06-27 12:10:03 -07002492 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002493 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002494 }
2495
2496 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002497 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2498 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2499 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002500 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002501 final ArrayList<ActivityRecord> activities = task.mActivities;
2502 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2503 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002504 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002505 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002506 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002507 }
2508 }
2509 }
2510 }
2511 return null;
2512 }
2513
Jorim Jaggidc249c42015-12-15 14:57:31 -08002514 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002515 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002516 if (stackId == DOCKED_STACK_ID) {
2517 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002518 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002519 return;
2520 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002521 final ActivityStack stack = getStack(stackId);
2522 if (stack == null) {
2523 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2524 return;
2525 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002526
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002527 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002528 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002529 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002530 // If the docked stack exists, don't resize non-floating stacks independently of the
2531 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002532 return;
2533 }
2534
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002535 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002536 mWindowManager.deferSurfaceLayout();
2537 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002538 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002539 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2540 // null bounds = fullscreen windowing mode...at least for now.
2541 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2542 } else if (splitScreenActive) {
2543 // If we are in split-screen mode and this stack support split-screen, then
2544 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2545 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2546 }
2547 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002548 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002549 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002550 stack.ensureVisibleActivitiesConfigurationLocked(
2551 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002552 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002553 } finally {
2554 mWindowManager.continueSurfaceLayout();
2555 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002556 }
2557 }
2558
Wale Ogunwale68278562017-09-23 17:13:55 -07002559 private void deferUpdateBounds(int activityType) {
2560 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002561 if (stack != null) {
2562 stack.deferUpdateBounds();
2563 }
2564 }
2565
Wale Ogunwale68278562017-09-23 17:13:55 -07002566 private void continueUpdateBounds(int activityType) {
2567 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002568 if (stack != null) {
2569 stack.continueUpdateBounds();
2570 }
2571 }
2572
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002573 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002574 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002575 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2576 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002577 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002578 if (task != null) {
2579 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002580 }
2581 }
2582 mResizingTasksDuringAnimation.clear();
2583 }
2584
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002585 /**
2586 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
2587 * Can do that once we are no longer using static stack ids. Specially when
2588 * {@link ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} is removed.
2589 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002590 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002591 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002592
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002593 mWindowManager.deferSurfaceLayout();
2594 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002595 final int windowingMode = fromStack.getWindowingMode();
2596 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2597 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2598
2599 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002600 // We are moving all tasks from the docked stack to the fullscreen stack,
2601 // which is dismissing the docked stack, so resize all other stacks to
2602 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002603 final ArrayList<ActivityStack> displayStacks = toDisplay.mStacks;
2604 for (int i = displayStacks.size() - 1; i >= 0; --i) {
2605 final ActivityStack otherStack = displayStacks.get(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002606 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2607 continue;
2608 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002609 resizeStackLocked(otherStack.mStackId, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002610 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002611 }
2612
2613 // Also disable docked stack resizing since we have manually adjusted the
2614 // size of other stacks above and we don't want to trigger a docked stack
2615 // resize when we remove task from it below and it is detached from the
2616 // display because it no longer contains any tasks.
2617 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002618 } else if (inPinnedWindowingMode && onTop) {
2619 // Log if we are expanding the PiP to fullscreen
2620 MetricsLogger.action(mService.mContext,
2621 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002622 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002623
Winson Chung5af42fc2017-03-24 17:11:33 -07002624 // If we are moving from the pinned stack, then the animation takes care of updating
2625 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002626 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002627 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002628
Wale Ogunwale388945c2017-10-04 12:13:46 -07002629 if (!tasks.isEmpty()) {
2630 final int size = tasks.size();
2631 final ActivityStack fullscreenStack = toDisplay.getOrCreateStack(
2632 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, onTop);
2633
2634 if (onTop) {
2635 final int returnToType =
2636 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
2637 for (int i = 0; i < size; i++) {
2638 final TaskRecord task = tasks.get(i);
2639 final boolean isTopTask = i == (size - 1);
2640 if (inPinnedWindowingMode) {
2641 // Update the return-to to reflect where the pinned stack task was moved
2642 // from so that we retain the stack that was previously visible if the
2643 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
2644 task.setTaskToReturnTo(returnToType);
2645 }
2646 // Defer resume until all the tasks have been moved to the fullscreen stack
2647 task.reparent(fullscreenStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
2648 isTopTask /* animate */, DEFER_RESUME,
2649 schedulePictureInPictureModeChange,
2650 "moveTasksToFullscreenStack - onTop");
Winson Chung0c1ca092016-11-10 17:40:34 -08002651 }
Wale Ogunwale388945c2017-10-04 12:13:46 -07002652 } else {
2653 for (int i = 0; i < size; i++) {
2654 final TaskRecord task = tasks.get(i);
2655 // Position the tasks in the fullscreen stack in order at the bottom of the
2656 // stack. Also defer resume until all the tasks have been moved to the
2657 // fullscreen stack.
2658 task.reparent(fullscreenStack, i /* position */,
2659 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2660 schedulePictureInPictureModeChange,
2661 "moveTasksToFullscreenStack - NOT_onTop");
2662 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002663 }
2664 }
Winson Chung74666102017-02-22 17:49:24 -08002665
2666 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2667 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002668 } finally {
2669 mAllowDockedStackResize = true;
2670 mWindowManager.continueSurfaceLayout();
2671 }
2672 }
2673
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002674 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002675 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2676 }
2677
2678 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2679 mWindowManager.inSurfaceTransaction(() ->
2680 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002681 }
2682
Jorim Jaggidc249c42015-12-15 14:57:31 -08002683 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002684 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2685 boolean preserveWindows) {
2686 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2687 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2688 false /* deferResume */);
2689 }
2690
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002691 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002692 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2693 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002694
2695 if (!mAllowDockedStackResize) {
2696 // Docked stack resize currently disabled.
2697 return;
2698 }
2699
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002700 final ActivityStack stack = getDefaultDisplay().getStack(
2701 WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_UNDEFINED);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002702 if (stack == null) {
2703 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2704 return;
2705 }
2706
2707 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2708 mWindowManager.deferSurfaceLayout();
2709 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002710 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2711 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002712 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002713 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002714
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002715 // TODO: Checking for isAttached might not be needed as if the user passes in null
2716 // dockedBounds then they want the docked stack to be dismissed.
2717 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2718 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002719 // In this case we make all other static stacks fullscreen and move all
2720 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002721 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002722
2723 // stack shouldn't contain anymore activities, so nothing to resume.
2724 r = null;
2725 } else {
2726 // Docked stacks occupy a dedicated region on screen so the size of all other
2727 // static stacks need to be adjusted so they don't overlap with the docked stack.
2728 // We get the bounds to use from window manager which has been adjusted for any
2729 // screen controls and is also the same for all stacks.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002730 final ArrayList<ActivityStack> stacks = getStacksOnDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002731 final Rect otherTaskRect = new Rect();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002732 for (int i = stacks.size() - 1; i >= 0; --i) {
2733 final ActivityStack current = stacks.get(i);
2734 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002735 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002736 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002737 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002738 continue;
2739 }
2740 // Need to set windowing mode here before we try to get the dock bounds.
2741 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2742 current.getStackDockedModeBounds(
2743 tempOtherTaskBounds /* currentTempTaskBounds */,
2744 tempRect /* outStackBounds */,
2745 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2746
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002747 resizeStackLocked(current.mStackId, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002748 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2749 tempOtherTaskInsetBounds, preserveWindows,
2750 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002751 }
2752 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002753 if (!deferResume) {
2754 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2755 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002756 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002757 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002758 mWindowManager.continueSurfaceLayout();
2759 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2760 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002761 }
2762
Robert Carr0d00c2e2016-02-29 17:45:02 -08002763 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002764 // TODO(multi-display): Pinned stack display should be passed in.
2765 final PinnedActivityStack stack = getDefaultDisplay().getStack(
2766 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002767 if (stack == null) {
2768 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2769 return;
2770 }
Winson Chung19953ca2017-04-11 11:19:23 -07002771
2772 // It is possible for the bounds animation from the WM to call this but be delayed by
2773 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2774 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2775 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2776 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002777 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002778 return;
2779 }
2780
Robert Carr0d00c2e2016-02-29 17:45:02 -08002781 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2782 mWindowManager.deferSurfaceLayout();
2783 try {
2784 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002785 Rect insetBounds = null;
2786 if (tempPinnedTaskBounds != null) {
2787 // We always use 0,0 as the position for the inset rect because
2788 // if we are getting insets at all in the pinned stack it must mean
2789 // we are headed for fullscreen.
2790 insetBounds = tempRect;
2791 insetBounds.top = 0;
2792 insetBounds.left = 0;
2793 insetBounds.right = tempPinnedTaskBounds.width();
2794 insetBounds.bottom = tempPinnedTaskBounds.height();
2795 }
2796 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002797 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002798 } finally {
2799 mWindowManager.continueSurfaceLayout();
2800 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2801 }
2802 }
2803
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002804 private void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002805 final ActivityStack stack = getStack(stackId);
2806 if (stack == null) {
2807 return;
2808 }
2809
2810 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002811 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002812 /**
2813 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2814 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2815 * that the client receives onStop() before it is reparented. We do this by detaching
2816 * the stack from the display so that it will be considered invisible when
2817 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2818 * invisible as well and added to the stopping list. After which we process the
2819 * stopping list by handling the idle.
2820 */
2821 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2822 pinnedStack.mForceHidden = true;
2823 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2824 pinnedStack.mForceHidden = false;
2825 activityIdleInternalLocked(null, false /* fromTimeout */,
2826 true /* processPausingActivites */, null /* configuration */);
2827
2828 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002829 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002830 } else {
2831 for (int i = tasks.size() - 1; i >= 0; i--) {
2832 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2833 REMOVE_FROM_RECENTS);
2834 }
2835 }
2836 }
2837
2838 /**
Robert Carr6914f082017-03-20 19:04:30 -07002839 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2840 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2841 * instead moved back onto the fullscreen stack.
2842 */
2843 void removeStackLocked(int stackId) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002844 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stackId));
2845 }
2846
Wale Ogunwale68278562017-09-23 17:13:55 -07002847 /**
2848 * Removes stacks in the input windowing modes from the system if they are of activity type
2849 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2850 */
2851 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002852 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002853 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2854 }
2855 }
2856
2857 void removeStacksWithActivityTypes(int... activityTypes) {
2858 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2859 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002860 }
Robert Carr6914f082017-03-20 19:04:30 -07002861 }
2862
2863 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002864 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2865 */
2866 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2867 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2868 }
2869
2870 /**
Winson Chung010927a2016-12-15 16:12:35 -08002871 * Removes the task with the specified task id.
2872 *
2873 * @param taskId Identifier of the task to be removed.
2874 * @param killProcess Kill any process associated with the task if possible.
2875 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002876 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2877 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002878 * @return Returns true if the given task was found and removed.
2879 */
Winson Chung6954fc92017-03-24 16:22:12 -07002880 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2881 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002882 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002883 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002884 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002885 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2886 if (tr.isPersistable) {
2887 mService.notifyTaskPersisterLocked(null, true);
2888 }
2889 return true;
2890 }
2891 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2892 return false;
2893 }
2894
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002895 void addRecentActivity(ActivityRecord r) {
2896 if (r == null) {
2897 return;
2898 }
2899 final TaskRecord task = r.getTask();
2900 mRecentTasks.addLocked(task);
2901 task.touchActiveTime();
2902 }
2903
2904 void removeTaskFromRecents(TaskRecord task) {
2905 mRecentTasks.remove(task);
2906 task.removedFromRecents();
2907 }
2908
Winson Chung010927a2016-12-15 16:12:35 -08002909 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2910 if (removeFromRecents) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002911 removeTaskFromRecents(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002912 }
2913 ComponentName component = tr.getBaseIntent().getComponent();
2914 if (component == null) {
2915 Slog.w(TAG, "No component for base intent of task: " + tr);
2916 return;
2917 }
2918
2919 // Find any running services associated with this app and stop if needed.
2920 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2921
2922 if (!killProcess) {
2923 return;
2924 }
2925
2926 // Determine if the process(es) for this task should be killed.
2927 final String pkg = component.getPackageName();
2928 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2929 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2930 for (int i = 0; i < pmap.size(); i++) {
2931
2932 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2933 for (int j = 0; j < uids.size(); j++) {
2934 ProcessRecord proc = uids.valueAt(j);
2935 if (proc.userId != tr.userId) {
2936 // Don't kill process for a different user.
2937 continue;
2938 }
2939 if (proc == mService.mHomeProcess) {
2940 // Don't kill the home process along with tasks from the same package.
2941 continue;
2942 }
2943 if (!proc.pkgList.containsKey(pkg)) {
2944 // Don't kill process that is not associated with this task.
2945 continue;
2946 }
2947
2948 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002949 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002950 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2951 // Don't kill process(es) that has an activity in a different task that is
2952 // also in recents.
2953 return;
2954 }
2955 }
2956
2957 if (proc.foregroundServices) {
2958 // Don't kill process(es) with foreground service.
2959 return;
2960 }
2961
2962 // Add process to kill list.
2963 procsToKill.add(proc);
2964 }
2965 }
2966
2967 // Kill the running processes.
2968 for (int i = 0; i < procsToKill.size(); i++) {
2969 ProcessRecord pr = procsToKill.get(i);
2970 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2971 && pr.curReceivers.isEmpty()) {
2972 pr.kill("remove task", true);
2973 } else {
2974 // We delay killing processes that are not in the background or running a receiver.
2975 pr.waitingToKill = "remove task";
2976 }
2977 }
2978 }
2979
Craig Mautner4a1cb222013-12-04 16:14:06 -08002980 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002981 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002982 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2983 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002984 break;
2985 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002986 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002987 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002988 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002989 }
2990
Chong Zhang5dcb2752015-08-18 13:50:26 -07002991 /**
2992 * Restores a recent task to a stack
2993 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002994 * @param aOptions The activity options to use for restoration.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002995 * @return true if the task has been restored successfully.
2996 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002997 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002998 final ActivityStack stack = getLaunchStack(null, aOptions, task, !ON_TOP);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002999 final ActivityStack currentStack = task.getStack();
3000 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003001 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003002 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003003 // Nothing else to do since it is already restored in the right stack.
3004 return true;
3005 }
3006 // Remove current stack association, so we can re-associate the task with the
3007 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07003008 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003009 }
3010
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003011 stack.addTask(task, !ON_TOP, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003012 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003013 task.createWindowContainer(!ON_TOP, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003014 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3015 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003016 final ArrayList<ActivityRecord> activities = task.mActivities;
3017 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003018 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003019 }
3020 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003021 }
3022
Wale Ogunwale040b4702015-08-06 18:10:50 -07003023 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003024 * Move stack with all its existing content to specified display.
3025 * @param stackId Id of stack to move.
3026 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003027 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003028 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003029 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003030 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003031 if (activityDisplay == null) {
3032 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3033 + displayId);
3034 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003035 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003036 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003037 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3038 + stackId);
3039 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003040
3041 final ActivityDisplay currentDisplay = stack.getDisplay();
3042 if (currentDisplay == null) {
3043 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3044 + " is not attached to any display.");
3045 }
3046
3047 if (currentDisplay.mDisplayId == displayId) {
3048 throw new IllegalArgumentException("Trying to move stack=" + stack
3049 + " to its current displayId=" + displayId);
3050 }
3051
3052 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003053 // TODO(multi-display): resize stacks properly if moved from split-screen.
3054 }
3055
3056 /**
Winson Chung74666102017-02-22 17:49:24 -08003057 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3058 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003059 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003060 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003061 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003062 final int stackId = stack.mStackId;
3063 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003064
Winson Chung74666102017-02-22 17:49:24 -08003065 // Check that we aren't reparenting to the same stack that the task is already in
3066 if (prevStack != null && prevStack.mStackId == stackId) {
3067 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3068 + " already in stackId=" + stackId);
3069 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003070 }
3071
Winson Chung74666102017-02-22 17:49:24 -08003072 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3073 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003074 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003075 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003076 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003077 }
3078
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003079 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3080 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003081 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003082 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3083 + " reparent task=" + task + " to stackId=" + stackId);
3084 }
3085
Winson Chung74666102017-02-22 17:49:24 -08003086 // Ensure that we aren't trying to move into a freeform stack without freeform
3087 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003088 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3089 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003090 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3091 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003092 }
3093
Winson Chung74666102017-02-22 17:49:24 -08003094 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
3095 // used for split-screen mode and will cause things like the docked divider to show up. We
3096 // instead leave the task in its current stack or move it to the fullscreen stack if it
3097 // isn't currently in a stack.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003098 if (inMultiWindowMode && !task.isResizeable()) {
Winson Chung74666102017-02-22 17:49:24 -08003099 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
3100 + " Moving to stackId=" + stackId + " instead.");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003101 // Temporarily disable resizeablility of the task as we don't want it to be resized if,
3102 // for example, a docked stack is created which will lead to the stack we are moving
3103 // from being resized and and its resizeable tasks being resized.
3104 try {
3105 task.mTemporarilyUnresizable = true;
3106 stack = stack.getDisplay().createStack(
3107 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
3108 } finally {
3109 task.mTemporarilyUnresizable = false;
3110 }
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003111 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003112 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003113 }
3114
Winson Chung08f81892017-03-02 15:40:51 -08003115 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003116 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003117 if (stack == null) {
3118 throw new IllegalArgumentException(
3119 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3120 }
3121
3122 final ActivityRecord r = stack.topRunningActivityLocked();
3123 if (r == null) {
3124 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3125 + " in stack=" + stack);
3126 return false;
3127 }
3128
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003129 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003130 Slog.w(TAG,
3131 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003132 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003133 return false;
3134 }
3135
Winson Chung3a682872017-04-10 14:38:05 -07003136 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08003137 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003138 return true;
3139 }
3140
Winson Chung8bca9e42017-04-16 15:59:43 -07003141 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08003142 boolean moveHomeStackToFront, String reason) {
3143
Wale Ogunwale480dca02016-02-06 13:58:29 -08003144 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003145
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003146 final ActivityDisplay display = r.getStack().getDisplay();
3147 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003148
Bryce Lee04ab3462017-04-10 15:06:33 -07003149 // This will clear the pinned stack by moving an existing task to the full screen stack,
3150 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003151 if (stack != null) {
3152 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3153 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003154
Wale Ogunwale1666e312016-12-16 11:27:18 -08003155 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003156 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003157
Wale Ogunwale480dca02016-02-06 13:58:29 -08003158 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003159 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003160 // Resize the pinned stack to match the current size of the task the activity we are
3161 // going to be moving is currently contained in. We do this to have the right starting
3162 // animation bounds for the pinned stack to the desired bounds the caller wants.
3163 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
3164 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003165 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003166
3167 if (task.mActivities.size() == 1) {
3168 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003169 // the stack without re-parenting the activity in a different task. We don't
3170 // move the home stack forward if we are currently entering picture-in-picture
3171 // while pausing because that changes the focused stack and may prevent the new
3172 // starting activity from resuming.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003173 if (moveHomeStackToFront && task.returnsToHomeTask()
Winson Chungf7e03e12017-08-22 11:32:16 -07003174 && (r.state == RESUMED || !r.supportsEnterPipOnTaskSwitch)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003175 // Move the home stack forward if the task we just moved to the pinned stack
3176 // was launched from home so home should be visible behind it.
3177 moveHomeStackToFront(reason);
3178 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003179 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003180 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3181 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003182 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003183 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003184 // reveal/leave the other activities in their original task.
3185
3186 // Currently, we don't support reparenting activities across tasks in two different
3187 // stacks, so instead, just create a new task in the same stack, reparent the
3188 // activity into that task, and then reparent the whole task to the new stack. This
3189 // ensures that all the necessary work to migrate states in the old and new stacks
3190 // is also done.
3191 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003192 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003193 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3194
Winson Chung5af42fc2017-03-24 17:11:33 -07003195 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003196 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003197 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003198 }
Winson Chungc2baac02017-01-11 13:34:47 -08003199
3200 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3201 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003202 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003203 } finally {
3204 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003205 }
3206
Winson Chunge7ba6862017-05-24 12:13:33 -07003207 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3208 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3209 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003210 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3211
Winson Chunge7ba6862017-05-24 12:13:33 -07003212 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3213 true /* fromFullscreen */);
3214
3215 // Update the visibility of all activities after the they have been reparented to the new
3216 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3217 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3218 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003219 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003220 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003221
Wale Ogunwale89be5762017-10-04 13:27:49 -07003222 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003223 }
3224
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003225 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003226 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3227 if (r == null || !r.isFocusable()) {
3228 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3229 "moveActivityStackToFront: unfocusable r=" + r);
3230 return false;
3231 }
3232
Bryce Leeaf691c02017-03-20 14:20:22 -07003233 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003234 final ActivityStack stack = r.getStack();
3235 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003236 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3237 + r + " task=" + task);
3238 return false;
3239 }
3240
Chong Zhang6cda19c2016-06-14 19:07:56 -07003241 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3242 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3243 "moveActivityStackToFront: already on top, r=" + r);
3244 return false;
3245 }
3246
3247 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3248 "moveActivityStackToFront: r=" + r);
3249
3250 stack.moveToFront(reason, task);
3251 return true;
3252 }
3253
David Stevensc6b91c62017-02-08 14:23:58 -08003254 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003255 mTmpFindTaskResult.r = null;
3256 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003257 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003258 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003259 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3260 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003261 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3262 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003263 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003264 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3265 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003266 continue;
3267 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003268 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003269 // It is possible to have tasks in multiple stacks with the same root affinity, so
3270 // we should keep looking after finding an affinity match to see if there is a
3271 // better match in another stack. Also, task affinity isn't a good enough reason
3272 // to target a display which isn't the source of the intent, so skip any affinity
3273 // matches not on the specified display.
3274 if (mTmpFindTaskResult.r != null) {
3275 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3276 return mTmpFindTaskResult.r;
3277 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003278 // Note: since the traversing through the stacks is top down, the floating
3279 // tasks should always have lower priority than any affinity-matching tasks
3280 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003281 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003282 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3283 Slog.d(TAG_TASKS, "Skipping match on different display "
3284 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003285 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003286 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003287 }
3288 }
Winson Chung5b895b72017-05-01 13:46:25 -07003289
David Stevensc6b91c62017-02-08 14:23:58 -08003290 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3291 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003292 }
3293
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003294 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3295 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003296 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3297 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003298 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003299 final ActivityRecord ar = stacks.get(stackNdx)
3300 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003301 if (ar != null) {
3302 return ar;
3303 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003304 }
3305 }
3306 return null;
3307 }
3308
David Stevens9440dc82017-03-16 19:00:20 -07003309 boolean hasAwakeDisplay() {
3310 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3311 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3312 if (!display.shouldSleep()) {
3313 return true;
3314 }
3315 }
3316 return false;
3317 }
3318
Craig Mautner8d341ef2013-03-26 09:03:27 -07003319 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003320 scheduleSleepTimeout();
3321 if (!mGoingToSleep.isHeld()) {
3322 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003323 if (mLaunchingActivity.isHeld()) {
3324 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3325 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003326 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003327 mLaunchingActivity.release();
3328 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003329 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003330 }
David Stevens9440dc82017-03-16 19:00:20 -07003331
3332 applySleepTokensLocked(false /* applyToStacks */);
3333
3334 checkReadyForSleepLocked(true /* allowDelay */);
3335 }
3336
3337 void prepareForShutdownLocked() {
3338 for (int i = 0; i < mActivityDisplays.size(); i++) {
3339 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3340 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003341 }
3342
3343 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003344 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003345
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003346 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003347 final long endTime = System.currentTimeMillis() + timeout;
3348 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003349 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003350 long timeRemaining = endTime - System.currentTimeMillis();
3351 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003352 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003353 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003354 } catch (InterruptedException e) {
3355 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003356 } else {
3357 Slog.w(TAG, "Activity manager shutdown timed out");
3358 timedout = true;
3359 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003361 } else {
3362 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003363 }
3364 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003365
3366 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003367 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003368
Craig Mautner8d341ef2013-03-26 09:03:27 -07003369 return timedout;
3370 }
3371
3372 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003373 removeSleepTimeouts();
3374 if (mGoingToSleep.isHeld()) {
3375 mGoingToSleep.release();
3376 }
David Stevens9440dc82017-03-16 19:00:20 -07003377 }
3378
3379 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003380 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003381 // Set the sleeping state of the display.
3382 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3383 final boolean displayShouldSleep = display.shouldSleep();
3384 if (displayShouldSleep == display.isSleeping()) {
3385 continue;
3386 }
3387 display.setIsSleeping(displayShouldSleep);
3388
3389 if (!applyToStacks) {
3390 continue;
3391 }
3392
3393 // Set the sleeping state of the stacks on the display.
3394 final ArrayList<ActivityStack> stacks = display.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003395 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3396 final ActivityStack stack = stacks.get(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003397 if (displayShouldSleep) {
3398 stack.goToSleepIfPossible(false /* shuttingDown */);
3399 } else {
3400 stack.awakeFromSleepingLocked();
3401 if (isFocusedStack(stack)) {
3402 resumeFocusedStackTopActivityLocked();
3403 }
3404 }
3405 }
3406
3407 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3408 continue;
3409 }
3410 // The display is awake now, so clean up the going to sleep list.
3411 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3412 final ActivityRecord r = it.next();
3413 if (r.getDisplayId() == display.mDisplayId) {
3414 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 }
Craig Mautner5314a402013-09-26 12:40:16 -07003416 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003417 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003418 }
3419
3420 void activitySleptLocked(ActivityRecord r) {
3421 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003422 final ActivityStack s = r.getStack();
3423 if (s != null) {
3424 s.checkReadyForSleep();
3425 } else {
3426 checkReadyForSleepLocked(true);
3427 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003428 }
3429
David Stevens9440dc82017-03-16 19:00:20 -07003430 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003431 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003432 // Do not care.
3433 return;
3434 }
3435
David Stevens18abd0e2017-08-17 14:55:47 -07003436 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3437 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003438 }
3439
Wei Wang65c7a152016-06-02 18:51:22 -07003440 // Send launch end powerhint before going sleep
3441 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3442
Craig Mautner0eea92c2013-05-16 13:35:39 -07003443 removeSleepTimeouts();
3444
3445 if (mGoingToSleep.isHeld()) {
3446 mGoingToSleep.release();
3447 }
3448 if (mService.mShuttingDown) {
3449 mService.notifyAll();
3450 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003451 }
3452
David Stevens18abd0e2017-08-17 14:55:47 -07003453 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3454 // successfully put to sleep.
3455 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3456 boolean allSleep = true;
3457 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3458 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3459 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3460 if (allowDelay) {
3461 allSleep &= stacks.get(stackNdx).goToSleepIfPossible(shuttingDown);
3462 } else {
3463 stacks.get(stackNdx).goToSleep();
3464 }
3465 }
3466 }
3467 return allSleep;
3468 }
3469
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003470 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003471 // A resumed activity cannot be stopping. remove from list
3472 mStoppingActivities.remove(r);
3473
Andrii Kulian02b7a832016-10-06 23:11:56 -07003474 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003475 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003476 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003477 }
3478 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003479 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003480 mWindowManager.executeAppTransition();
3481 return true;
3482 }
3483 return false;
3484 }
3485
Craig Mautner8d341ef2013-03-26 09:03:27 -07003486 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003487 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3488 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003489 int stackNdx = stacks.size() - 1;
3490 while (stackNdx >= 0) {
3491 stacks.get(stackNdx).handleAppCrashLocked(app);
3492 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003493 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003494 }
3495 }
3496
Craig Mautnerbb742462014-07-07 15:28:55 -07003497 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003498 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003499 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003500 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003501
3502 r.mLaunchTaskBehind = false;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003503 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003504 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003505 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003506
3507 // When launching tasks behind, update the last active time of the top task after the new
3508 // task has been shown briefly
3509 final ActivityRecord top = stack.topActivity();
3510 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003511 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003512 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003513 }
3514
3515 void scheduleLaunchTaskBehindComplete(IBinder token) {
3516 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3517 }
3518
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003519 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3520 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003521 mKeyguardController.beginActivityVisibilityUpdate();
3522 try {
3523 // First the front stacks. In case any are not fullscreen and are in front of home.
3524 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3525 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3526 final int topStackNdx = stacks.size() - 1;
3527 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3528 final ActivityStack stack = stacks.get(stackNdx);
3529 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3530 }
Craig Mautner580ea812013-04-25 12:58:38 -07003531 }
Jorim Jaggife762342016-10-13 14:33:27 +02003532 } finally {
3533 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003534 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003535 }
3536
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003537 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3538 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3539 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3540 final int topStackNdx = stacks.size() - 1;
3541 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3542 final ActivityStack stack = stacks.get(stackNdx);
3543 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3544 }
3545 }
3546 }
3547
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003548 void invalidateTaskLayers() {
3549 mTaskLayersChanged = true;
3550 }
3551
3552 void rankTaskLayersIfNeeded() {
3553 if (!mTaskLayersChanged) {
3554 return;
3555 }
3556 mTaskLayersChanged = false;
3557 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3558 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3559 int baseLayer = 0;
3560 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3561 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3562 }
3563 }
3564 }
3565
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003566 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3567 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3568 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3569 final int topStackNdx = stacks.size() - 1;
3570 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3571 final ActivityStack stack = stacks.get(stackNdx);
3572 stack.clearOtherAppTimeTrackers(except);
3573 }
3574 }
3575 }
3576
Craig Mautner8d341ef2013-03-26 09:03:27 -07003577 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003578 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3579 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003580 final int numStacks = stacks.size();
3581 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3582 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003583 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003585 }
3586 }
3587
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003588 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3589 // Examine all activities currently running in the process.
3590 TaskRecord firstTask = null;
3591 // Tasks is non-null only if two or more tasks are found.
3592 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003593 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3594 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003595 ActivityRecord r = app.activities.get(i);
3596 // First, if we find an activity that is in the process of being destroyed,
3597 // then we just aren't going to do anything for now; we want things to settle
3598 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003599 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003600 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003601 return;
3602 }
3603 // Don't consider any activies that are currently not in a state where they
3604 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003605 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3606 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003607 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003608 continue;
3609 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003610
3611 final TaskRecord task = r.getTask();
3612 if (task != null) {
3613 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003614 + " from " + r);
3615 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003616 firstTask = task;
3617 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003618 if (tasks == null) {
3619 tasks = new ArraySet<>();
3620 tasks.add(firstTask);
3621 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003622 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003623 }
3624 }
3625 }
3626 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003627 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003628 return;
3629 }
3630 // If we have activities in multiple tasks that are in a position to be destroyed,
3631 // let's iterate through the tasks and release the oldest one.
3632 final int numDisplays = mActivityDisplays.size();
3633 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3634 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3635 // Step through all stacks starting from behind, to hit the oldest things first.
3636 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3637 final ActivityStack stack = stacks.get(stackNdx);
3638 // Try to release activities in this stack; if we manage to, we are done.
3639 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3640 return;
3641 }
3642 }
3643 }
3644 }
3645
Amith Yamasani37a40c22015-06-17 13:25:42 -07003646 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003647 final int focusStackId = mFocusedStack.getStackId();
3648 // We dismiss the docked stack whenever we switch users.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003649 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003650 if (dockedStack != null) {
3651 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3652 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003653 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3654 // also cause all tasks to be moved to the fullscreen stack at a position that is
3655 // appropriate.
3656 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003657
3658 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003659 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003660 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003661
Craig Mautner858d8a62013-04-23 17:08:34 -07003662 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3664 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003666 final ActivityStack stack = stacks.get(stackNdx);
3667 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003668 TaskRecord task = stack.topTask();
3669 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003670 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003671 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003672 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003673 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003674
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003675 ActivityStack stack = getStack(restoreStackId);
3676 if (stack == null) {
3677 stack = mHomeStack;
3678 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003679 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003680 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003681 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003682 } else {
3683 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003684 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003685 }
Craig Mautner93529a42013-10-04 15:03:13 -07003686 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003687 }
3688
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003689 /** Checks whether the userid is a profile of the current user. */
3690 boolean isCurrentProfileLocked(int userId) {
3691 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003692 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003693 }
3694
Bryce Leeb7c9b802017-05-02 14:20:24 -07003695 /**
3696 * Returns whether a stopping activity is present that should be stopped after visible, rather
3697 * than idle.
3698 * @return {@code true} if such activity is present. {@code false} otherwise.
3699 */
3700 boolean isStoppingNoHistoryActivity() {
3701 // Activities that are marked as nohistory should be stopped immediately after the resumed
3702 // activity has become visible.
3703 for (ActivityRecord record : mStoppingActivities) {
3704 if (record.isNoHistory()) {
3705 return true;
3706 }
3707 }
3708
3709 return false;
3710 }
3711
Winson Chung4dabf232017-01-25 13:25:22 -08003712 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3713 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003714 ArrayList<ActivityRecord> stops = null;
3715
3716 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003717 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3718 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003719 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003720 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003721 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3722 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003723 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003724 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003725 if (s.finishing) {
3726 // If this activity is finishing, it is sitting on top of
3727 // everyone else but we now know it is no longer needed...
3728 // so get rid of it. Otherwise, we need to go through the
3729 // normal flow and hide it once we determine that it is
3730 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003731 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003732 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003733 }
3734 }
David Stevens9440dc82017-03-16 19:00:20 -07003735 if (remove) {
3736 final ActivityStack stack = s.getStack();
3737 final boolean shouldSleepOrShutDown = stack != null
3738 ? stack.shouldSleepOrShutDownActivities()
3739 : mService.isSleepingOrShuttingDownLocked();
3740 if (!waitingVisible || shouldSleepOrShutDown) {
3741 if (!processPausingActivities && s.state == PAUSING) {
3742 // Defer processing pausing activities in this iteration and reschedule
3743 // a delayed idle to reprocess it again
3744 removeTimeoutsForActivityLocked(idleActivity);
3745 scheduleIdleTimeoutLocked(idleActivity);
3746 continue;
3747 }
Winson Chung4dabf232017-01-25 13:25:22 -08003748
David Stevens9440dc82017-03-16 19:00:20 -07003749 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3750 if (stops == null) {
3751 stops = new ArrayList<>();
3752 }
3753 stops.add(s);
3754 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003755 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003756 }
3757 }
3758
3759 return stops;
3760 }
3761
Craig Mautnercf910b02013-04-23 11:23:27 -07003762 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003763 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3764 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3765 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3766 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003767 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003768 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003769 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003770 if (r == null) Slog.e(TAG,
3771 "validateTop...: null top activity, stack=" + stack);
3772 else {
3773 final ActivityRecord pausing = stack.mPausingActivity;
3774 if (pausing != null && pausing == r) Slog.e(TAG,
3775 "validateTop...: top stack has pausing activity r=" + r
3776 + " state=" + state);
3777 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3778 "validateTop...: activity in front not resumed r=" + r
3779 + " state=" + state);
3780 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003781 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003782 final ActivityRecord resumed = stack.mResumedActivity;
3783 if (resumed != null && resumed == r) Slog.e(TAG,
3784 "validateTop...: back stack has resumed activity r=" + r
3785 + " state=" + state);
3786 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3787 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003788 }
3789 }
3790 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003791 }
3792
Craig Mautner27084302013-03-25 08:05:25 -07003793 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003794 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003795 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003796 pw.print(prefix);
3797 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003798 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003799 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3800 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3801 pw.println(prefix + "displayId=" + display.mDisplayId + " mStacks=" + display.mStacks);
3802 }
Bryce Lee4a194382017-04-04 14:32:48 -07003803 if (!mWaitingForActivityVisible.isEmpty()) {
3804 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3805 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3806 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3807 }
3808 }
3809
Jorim Jaggi8d786932016-10-26 19:08:36 -07003810 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003811 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003812 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003813
Yi Jin129fc6c2017-09-28 15:48:38 -07003814 public void writeToProto(ProtoOutputStream proto) {
Steven Timotius4346f0a2017-09-12 11:07:21 -07003815 super.writeToProto(proto, CONFIGURATION_CONTAINER);
3816 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3817 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3818 activityDisplay.writeToProto(proto, DISPLAYS);
3819 }
3820 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3821 if (mFocusedStack != null) {
3822 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3823 ActivityRecord focusedActivity = getResumedActivityLocked();
3824 if (focusedActivity != null) {
3825 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3826 }
3827 } else {
3828 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3829 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003830 }
3831
Winson43d1f262016-06-14 16:05:55 -07003832 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003833 * Dump all connected displays' configurations.
3834 * @param prefix Prefix to apply to each line of the dump.
3835 */
3836 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3837 pw.print(prefix); pw.println("Display override configurations:");
3838 final int displayCount = mActivityDisplays.size();
3839 for (int i = 0; i < displayCount; i++) {
3840 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3841 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3842 pw.println(activityDisplay.getOverrideConfiguration());
3843 }
3844 }
3845
3846 /**
Winson43d1f262016-06-14 16:05:55 -07003847 * Dumps the activities matching the given {@param name} in the either the focused stack
3848 * or all visible stacks if {@param dumpVisibleStacks} is true.
3849 */
Winson Chung6998bc42017-02-28 17:07:05 -08003850 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3851 boolean dumpFocusedStackOnly) {
3852 if (dumpFocusedStackOnly) {
3853 return mFocusedStack.getDumpActivitiesLocked(name);
3854 } else {
Winson43d1f262016-06-14 16:05:55 -07003855 ArrayList<ActivityRecord> activities = new ArrayList<>();
3856 int numDisplays = mActivityDisplays.size();
3857 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3858 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3859 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3860 ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003861 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003862 activities.addAll(stack.getDumpActivitiesLocked(name));
3863 }
3864 }
3865 }
3866 return activities;
Winson43d1f262016-06-14 16:05:55 -07003867 }
Craig Mautner20e72272013-04-01 13:45:53 -07003868 }
3869
Dianne Hackborn390517b2013-05-30 15:03:32 -07003870 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3871 boolean needSep, String prefix) {
3872 if (activity != null) {
3873 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3874 if (needSep) {
3875 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003876 }
3877 pw.print(prefix);
3878 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003879 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003880 }
3881 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003882 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003883 }
3884
Craig Mautner8d341ef2013-03-26 09:03:27 -07003885 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3886 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003887 boolean printed = false;
3888 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003889 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3890 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003891 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003892 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003893 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003894 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003895 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003896 pw.println();
3897 pw.println(" Stack #" + stack.mStackId + ":");
3898 pw.println(" mFullscreen=" + stack.mFullscreen);
3899 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3900 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003901
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003902 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3903 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003904
Craig Mautner4a1cb222013-12-04 16:14:06 -08003905 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3906 !dumpAll, false, dumpPackage, true,
3907 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003908
Craig Mautner4a1cb222013-12-04 16:14:06 -08003909 needSep = printed;
3910 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3911 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003912 if (pr) {
3913 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003914 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003915 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003916 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3917 " mResumedActivity: ");
3918 if (pr) {
3919 printed = true;
3920 needSep = false;
3921 }
3922 if (dumpAll) {
3923 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3924 " mLastPausedActivity: ");
3925 if (pr) {
3926 printed = true;
3927 needSep = true;
3928 }
3929 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3930 needSep, " mLastNoHistoryActivity: ");
3931 }
3932 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003933 }
3934 }
3935
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003936 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3937 false, dumpPackage, true, " Activities waiting to finish:", null);
3938 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3939 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003940 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3941 false, !dumpAll, false, dumpPackage, true,
3942 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003943 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3944 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003945
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003946 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003947 }
3948
Dianne Hackborn390517b2013-05-30 15:03:32 -07003949 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003950 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003951 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003952 String innerPrefix = null;
3953 String[] args = null;
3954 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003955 for (int i=list.size()-1; i>=0; i--) {
3956 final ActivityRecord r = list.get(i);
3957 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3958 continue;
3959 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003960 if (innerPrefix == null) {
3961 innerPrefix = prefix + " ";
3962 args = new String[0];
3963 }
3964 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003965 final boolean full = !brief && (complete || !r.isInHistory());
3966 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003967 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003968 needNL = false;
3969 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003970 if (header != null) {
3971 pw.println(header);
3972 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003973 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003974 if (lastTask != r.getTask()) {
3975 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003976 pw.print(prefix);
3977 pw.print(full ? "* " : " ");
3978 pw.println(lastTask);
3979 if (full) {
3980 lastTask.dump(pw, prefix + " ");
3981 } else if (complete) {
3982 // Complete + brief == give a summary. Isn't that obvious?!?
3983 if (lastTask.intent != null) {
3984 pw.print(prefix); pw.print(" ");
3985 pw.println(lastTask.intent.toInsecureStringWithClip());
3986 }
3987 }
3988 }
3989 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3990 pw.print(" #"); pw.print(i); pw.print(": ");
3991 pw.println(r);
3992 if (full) {
3993 r.dump(pw, innerPrefix);
3994 } else if (complete) {
3995 // Complete + brief == give a summary. Isn't that obvious?!?
3996 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3997 if (r.app != null) {
3998 pw.print(innerPrefix); pw.println(r.app);
3999 }
4000 }
4001 if (client && r.app != null && r.app.thread != null) {
4002 // flush anything that is already in the PrintWriter since the thread is going
4003 // to write to the file descriptor directly
4004 pw.flush();
4005 try {
4006 TransferPipe tp = new TransferPipe();
4007 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004008 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004009 // Short timeout, since blocking here can
4010 // deadlock with the application.
4011 tp.go(fd, 2000);
4012 } finally {
4013 tp.kill();
4014 }
4015 } catch (IOException e) {
4016 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4017 } catch (RemoteException e) {
4018 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4019 }
4020 needNL = true;
4021 }
4022 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004023 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004024 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004025
Craig Mautnerf3333272013-04-22 10:55:53 -07004026 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004027 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4028 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004029 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4030 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004031 }
4032
4033 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004034 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004035 }
4036
4037 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004038 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4039 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004040 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4041 }
4042
Craig Mautner05d29032013-05-03 13:40:13 -07004043 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004044 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4045 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4046 }
Craig Mautner05d29032013-05-03 13:40:13 -07004047 }
4048
Craig Mautner0eea92c2013-05-16 13:35:39 -07004049 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004050 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4051 }
4052
4053 final void scheduleSleepTimeout() {
4054 removeSleepTimeouts();
4055 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4056 }
4057
Craig Mautner4a1cb222013-12-04 16:14:06 -08004058 @Override
4059 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004060 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004061 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4062 }
4063
4064 @Override
4065 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004066 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004067 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4068 }
4069
4070 @Override
4071 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004072 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4074 }
4075
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004076 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004078 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004079 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080 }
4081
Andrii Kulianca007a62016-11-22 16:33:28 -08004082 /** Check if display with specified id is added to the list. */
4083 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004084 return getActivityDisplayOrCreateLocked(displayId) != null;
4085 }
4086
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004087 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004088 ActivityDisplay getActivityDisplay(int displayId) {
4089 return mActivityDisplays.get(displayId);
4090 }
4091
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004092 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4093 ActivityDisplay getDefaultDisplay() {
4094 return mActivityDisplays.get(DEFAULT_DISPLAY);
4095 }
4096
Andrii Kulian62e6f252017-05-30 22:46:53 -07004097 /**
4098 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4099 * corresponding record in display manager.
4100 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004101 // TODO: Look into consolidating with getActivityDisplay()
4102 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004103 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4104 if (activityDisplay != null) {
4105 return activityDisplay;
4106 }
4107 if (mDisplayManager == null) {
4108 // The system isn't fully initialized yet.
4109 return null;
4110 }
4111 final Display display = mDisplayManager.getDisplay(displayId);
4112 if (display == null) {
4113 // The display is not registered in DisplayManager.
4114 return null;
4115 }
4116 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004117 activityDisplay = new ActivityDisplay(this, displayId);
4118 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004119 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4120 mWindowManager.onDisplayAdded(displayId);
4121 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004122 }
4123
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004124 @VisibleForTesting
4125 void attachDisplay(ActivityDisplay display) {
4126 mActivityDisplays.put(display.mDisplayId, display);
4127 }
4128
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004129 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004130 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004131 mService.mContext.getResources().getDimensionPixelSize(
4132 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004133 }
4134
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004135 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004136 if (displayId == DEFAULT_DISPLAY) {
4137 throw new IllegalArgumentException("Can't remove the primary display.");
4138 }
4139
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004141 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4142 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08004143 final boolean destroyContentOnRemoval
4144 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08004145 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
4146 while (!stacks.isEmpty()) {
4147 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08004148 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004149 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
4150 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004151 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08004152 } else {
4153 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4154 // a valid launch stack for all activities. This way the task history from
4155 // external display will be preserved on primary after move.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004156 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004157 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004158 }
David Stevens9440dc82017-03-16 19:00:20 -07004159
4160 releaseSleepTokens(activityDisplay);
4161
Craig Mautnere0a38842013-12-16 16:14:02 -08004162 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07004163 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004164 }
4165 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004166 }
4167
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004168 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004169 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004170 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4171 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004172 // The window policy is responsible for stopping activities on the default display
4173 if (displayId != Display.DEFAULT_DISPLAY) {
4174 int displayState = activityDisplay.mDisplay.getState();
4175 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4176 activityDisplay.mOffToken =
4177 mService.acquireSleepToken("Display-off", displayId);
4178 } else if (displayState == Display.STATE_ON
4179 && activityDisplay.mOffToken != null) {
4180 activityDisplay.mOffToken.release();
4181 activityDisplay.mOffToken = null;
4182 }
4183 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004184 // TODO: Update the bounds.
4185 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004186 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004187 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 }
4189
David Stevens9440dc82017-03-16 19:00:20 -07004190 SleepToken createSleepTokenLocked(String tag, int displayId) {
4191 ActivityDisplay display = mActivityDisplays.get(displayId);
4192 if (display == null) {
4193 throw new IllegalArgumentException("Invalid display: " + displayId);
4194 }
4195
4196 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4197 mSleepTokens.add(token);
4198 display.mAllSleepTokens.add(token);
4199 return token;
4200 }
4201
4202 private void removeSleepTokenLocked(SleepTokenImpl token) {
4203 mSleepTokens.remove(token);
4204
4205 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4206 if (display != null) {
4207 display.mAllSleepTokens.remove(token);
4208 if (display.mAllSleepTokens.isEmpty()) {
4209 mService.updateSleepIfNeededLocked();
4210 }
4211 }
4212 }
4213
4214 private void releaseSleepTokens(ActivityDisplay display) {
4215 if (display.mAllSleepTokens.isEmpty()) {
4216 return;
4217 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004218 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004219 mSleepTokens.remove(token);
4220 }
4221 display.mAllSleepTokens.clear();
4222
4223 mService.updateSleepIfNeededLocked();
4224 }
4225
Wale Ogunwale68278562017-09-23 17:13:55 -07004226 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004227 final int displayId = stack.mDisplayId;
4228 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004229 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004230 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004231 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004232 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004233 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004234 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004235 // A stack might be not attached to a display.
Wale Ogunwale68278562017-09-23 17:13:55 -07004236 info.position = display != null ? display.mStacks.indexOf(stack) : 0;
4237 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004238
4239 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4240 final int numTasks = tasks.size();
4241 int[] taskIds = new int[numTasks];
4242 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004243 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004244 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004245 for (int i = 0; i < numTasks; ++i) {
4246 final TaskRecord task = tasks.get(i);
4247 taskIds[i] = task.taskId;
4248 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4249 : task.realActivity != null ? task.realActivity.flattenToString()
4250 : task.getTopActivity() != null ? task.getTopActivity().packageName
4251 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004252 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004253 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004254 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004255 }
4256 info.taskIds = taskIds;
4257 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004258 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004259 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004260
4261 final ActivityRecord top = stack.topRunningActivityLocked();
4262 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004263 return info;
4264 }
4265
Wale Ogunwale68278562017-09-23 17:13:55 -07004266 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004267 ActivityStack stack = getStack(stackId);
4268 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004269 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004270 }
4271 return null;
4272 }
4273
Wale Ogunwale68278562017-09-23 17:13:55 -07004274 StackInfo getStackInfo(int windowingMode, int activityType) {
4275 final ActivityStack stack = getStack(windowingMode, activityType);
4276 return (stack != null) ? getStackInfo(stack) : null;
4277 }
4278
Craig Mautner4a1cb222013-12-04 16:14:06 -08004279 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004280 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004281 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4282 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004283 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004284 list.add(getStackInfo(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004285 }
4286 }
4287 return list;
4288 }
4289
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004290 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004291 int preferredDisplayId, int actualStackId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004292 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
4293 actualStackId, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004294 }
4295
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004296 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004297 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4298 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004299 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004300 final ActivityStack actualStack = getStack(actualStackId);
4301 final boolean inSplitScreenMode = actualStack != null
4302 && actualStack.inSplitScreenWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004303 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004304 && !isSecondaryDisplayPreferred) || task.isActivityTypeHome()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004305 return;
4306 }
4307
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004308 // Handle incorrect launch/move to secondary display if needed.
4309 final boolean launchOnSecondaryDisplayFailed;
4310 if (isSecondaryDisplayPreferred) {
4311 final int actualDisplayId = task.getStack().mDisplayId;
4312 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4313 // The task landed on an inappropriate display somehow, move it to the default
4314 // display.
4315 // TODO(multi-display): Find proper stack for the task on the default display.
4316 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4317 true /* toTop */);
4318 launchOnSecondaryDisplayFailed = true;
4319 } else {
4320 // The task might have landed on a display different from requested.
4321 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4322 || (preferredDisplayId != INVALID_DISPLAY
4323 && preferredDisplayId != actualDisplayId);
4324 }
4325 } else {
4326 // The task wasn't requested to be on a secondary display.
4327 launchOnSecondaryDisplayFailed = false;
4328 }
4329
Jorim Jaggicd13d332016-04-27 15:40:20 -07004330 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004331 if (launchOnSecondaryDisplayFailed
4332 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004333 if (launchOnSecondaryDisplayFailed) {
4334 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4335 // display with config different from global config.
4336 mService.mTaskChangeNotificationController
4337 .notifyActivityLaunchOnSecondaryDisplayFailed();
4338 } else {
4339 // Display a warning toast that we tried to put a non-dockable task in the docked
4340 // stack.
4341 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4342 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004343
Andrii Kulianc27916642016-04-12 17:59:27 -07004344 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4345 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004346
4347 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004348 if (dockedStack != null) {
4349 moveTasksToFullscreenStackLocked(dockedStack,
4350 actualStackId == dockedStack.getStackId());
4351 }
Winson Chungd3395382016-12-13 11:49:09 -08004352 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004353 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004354 final String packageName = topActivity.appInfo.packageName;
4355 final int reason = isSecondaryDisplayPreferred
4356 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4357 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004358 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004359 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004360 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004361 }
4362
Jorim Jaggife89d122015-12-22 16:28:44 +01004363 void activityRelaunchedLocked(IBinder token) {
4364 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004365 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4366 if (r != null) {
4367 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004368 r.setSleeping(true, true);
4369 }
4370 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004371 }
4372
4373 void activityRelaunchingLocked(ActivityRecord r) {
4374 mWindowManager.notifyAppRelaunching(r.appToken);
4375 }
4376
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004377 void logStackState() {
4378 mActivityMetricsLogger.logWindowState();
4379 }
4380
Winson Chung5af42fc2017-03-24 17:11:33 -07004381 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004382 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4383 // end, then ensure that we defer all in between multi-window mode changes
4384 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4385 return;
4386 }
4387
Wale Ogunwale22e25262016-02-01 10:32:02 -08004388 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4389 final ActivityRecord r = task.mActivities.get(i);
4390 if (r.app != null && r.app.thread != null) {
4391 mMultiWindowModeChangedActivities.add(r);
4392 }
4393 }
4394
4395 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4396 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4397 }
4398 }
4399
Winson Chung5af42fc2017-03-24 17:11:33 -07004400 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004401 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004402 if (prevStack == null || prevStack == stack
4403 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4404 return;
4405 }
4406
Winson Chungab76bbc2017-08-14 13:33:51 -07004407 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004408 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004409
Winson Chungab76bbc2017-08-14 13:33:51 -07004410 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4411 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4412 final ActivityRecord r = task.mActivities.get(i);
4413 if (r.app != null && r.app.thread != null) {
4414 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004415 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004416 }
4417 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004418
Winson Chungab76bbc2017-08-14 13:33:51 -07004419 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4420 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4421 }
4422 }
4423
4424 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4425 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4426 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4427 final ActivityRecord r = task.mActivities.get(i);
4428 if (r.app != null && r.app.thread != null) {
4429 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004430 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004431 }
4432 }
4433
Tony Mak853304c2016-04-18 15:17:41 +01004434 void setDockedStackMinimized(boolean minimized) {
4435 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004436 }
4437
chaviw59b98852017-06-13 12:05:44 -07004438 void wakeUp(String reason) {
4439 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4440 }
4441
4442 /**
4443 * Begin deferring resume to avoid duplicate resumes in one pass.
4444 */
4445 private void beginDeferResume() {
4446 mDeferResumeCount++;
4447 }
4448
4449 /**
4450 * End deferring resume and determine if resume can be called.
4451 */
4452 private void endDeferResume() {
4453 mDeferResumeCount--;
4454 }
4455
4456 /**
4457 * @return True if resume can be called.
4458 */
4459 private boolean readyToResume() {
4460 return mDeferResumeCount == 0;
4461 }
4462
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004463 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004464
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004465 public ActivityStackSupervisorHandler(Looper looper) {
4466 super(looper);
4467 }
4468
Winson Chung4dabf232017-01-25 13:25:22 -08004469 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004470 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004471 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4472 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004473 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004474 }
4475
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004476 @Override
4477 public void handleMessage(Message msg) {
4478 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004479 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4480 synchronized (mService) {
4481 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4482 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004483 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004484 }
4485 }
4486 } break;
4487 case REPORT_PIP_MODE_CHANGED_MSG: {
4488 synchronized (mService) {
4489 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4490 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004491 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4492 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004493 }
4494 }
4495 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004496 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004497 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4498 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004499 // We don't at this point know if the activity is fullscreen,
4500 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004501 activityIdleInternal((ActivityRecord) msg.obj,
4502 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004503 } break;
4504 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004505 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004506 activityIdleInternal((ActivityRecord) msg.obj,
4507 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004508 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004509 case RESUME_TOP_ACTIVITY_MSG: {
4510 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004511 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004512 }
4513 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004514 case SLEEP_TIMEOUT_MSG: {
4515 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004516 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004517 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004518 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004519 }
4520 }
4521 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004522 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004523 synchronized (mService) {
4524 if (mLaunchingActivity.isHeld()) {
4525 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4526 if (VALIDATE_WAKE_LOCK_CALLER
4527 && Binder.getCallingUid() != Process.myUid()) {
4528 throw new IllegalStateException("Calling must be system uid");
4529 }
4530 mLaunchingActivity.release();
4531 }
4532 }
4533 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004534 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004535 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004536 } break;
4537 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004538 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004539 } break;
4540 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004541 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004542 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004543 case LAUNCH_TASK_BEHIND_COMPLETE: {
4544 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004545 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004546 if (r != null) {
4547 handleLaunchTaskBehindCompleteLocked(r);
4548 }
4549 }
4550 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004551
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004552 }
4553 }
4554 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004555
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004556 ActivityStack findStackBehind(ActivityStack stack) {
4557 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004558 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004559 if (display == null) {
4560 return null;
4561 }
4562 final ArrayList<ActivityStack> stacks = display.mStacks;
4563 for (int i = stacks.size() - 1; i >= 0; i--) {
4564 if (stacks.get(i) == stack && i > 0) {
4565 return stacks.get(i - 1);
4566 }
4567 }
4568 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4569 + " in=" + stacks);
4570 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004571
Jorim Jaggic69bd222016-03-15 14:38:37 +01004572 /**
4573 * Puts a task into resizing mode during the next app transition.
4574 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004575 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004576 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004577 private void setResizingDuringAnimation(TaskRecord task) {
4578 mResizingTasksDuringAnimation.add(task.taskId);
4579 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004580 }
4581
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004582 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4583 final TaskRecord task;
4584 final int callingUid;
4585 final String callingPackage;
4586 final Intent intent;
4587 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004588 int activityType = ACTIVITY_TYPE_UNDEFINED;
4589 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004590 final ActivityOptions activityOptions = (bOptions != null)
4591 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004592 if (activityOptions != null) {
4593 activityType = activityOptions.getLaunchActivityType();
4594 windowingMode = activityOptions.getLaunchWindowingMode();
4595 }
4596 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004597 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004598 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004599 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004600
Matthew Ng606dd802017-06-05 14:06:32 -07004601 mWindowManager.deferSurfaceLayout();
4602 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004603 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004604 mWindowManager.setDockedStackCreateState(
4605 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004606
Matthew Ng606dd802017-06-05 14:06:32 -07004607 // Defer updating the stack in which recents is until the app transition is done, to
4608 // not run into issues where we still need to draw the task in recents but the
4609 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004610 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004611 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004612 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004613
Matthew Ng606dd802017-06-05 14:06:32 -07004614 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004615 activityOptions);
Matthew Ng606dd802017-06-05 14:06:32 -07004616 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004617 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004618 mWindowManager.executeAppTransition();
4619 throw new IllegalArgumentException(
4620 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4621 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004622
Matthew Ng606dd802017-06-05 14:06:32 -07004623 // Since we don't have an actual source record here, we assume that the currently
4624 // focused activity was the source.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004625 final ActivityStack stack = getLaunchStack(null, activityOptions, task, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004626
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004627 if (stack != null && task.getStack() != stack) {
4628 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004629 "startActivityFromRecents");
Matthew Ng606dd802017-06-05 14:06:32 -07004630 }
4631
4632 // If the user must confirm credentials (e.g. when first launching a work app and the
4633 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4634 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4635 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004636 final ActivityRecord targetActivity = task.getTopActivity();
4637
4638 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4639 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004640 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07004641 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07004642 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07004643 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004644
4645 // If we are launching the task in the docked stack, put it into resizing mode so
4646 // the window renders full-screen with the background filling the void. Also only
4647 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004648 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004649 setResizingDuringAnimation(task);
4650 }
4651
4652 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004653 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004654 return ActivityManager.START_TASK_TO_FRONT;
4655 }
4656 callingUid = task.mCallingUid;
4657 callingPackage = task.mCallingPackage;
4658 intent = task.intent;
4659 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4660 userId = task.userId;
4661 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004662 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004663 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004664 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004665 }
Matthew Ng606dd802017-06-05 14:06:32 -07004666 return result;
4667 } finally {
4668 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004669 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004670 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004671
4672 /**
4673 * @return a list of activities which are the top ones in each visible stack. The first
4674 * entry will be the focused activity.
4675 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004676 List<IBinder> getTopVisibleActivities() {
4677 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4678 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004679 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004680 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4681 // Traverse all stacks on a display.
4682 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4683 final ActivityStack stack = display.mStacks.get(j);
4684 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004685 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004686 final ActivityRecord top = stack.topActivity();
4687 if (top != null) {
4688 if (stack == mFocusedStack) {
4689 topActivityTokens.add(0, top.appToken);
4690 } else {
4691 topActivityTokens.add(top.appToken);
4692 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004693 }
4694 }
4695 }
4696 }
4697 return topActivityTokens;
4698 }
Bryce Lee4a194382017-04-04 14:32:48 -07004699
4700 /**
4701 * Internal container to store a match qualifier alongside a WaitResult.
4702 */
4703 static class WaitInfo {
4704 private final ComponentName mTargetComponent;
4705 private final WaitResult mResult;
4706
4707 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4708 this.mTargetComponent = targetComponent;
4709 this.mResult = result;
4710 }
4711
Wale Ogunwale3270f172017-04-26 07:29:42 -07004712 public boolean matches(ComponentName targetComponent) {
4713 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004714 }
4715
4716 public WaitResult getResult() {
4717 return mResult;
4718 }
4719
4720 public ComponentName getComponent() {
4721 return mTargetComponent;
4722 }
4723
4724 public void dump(PrintWriter pw, String prefix) {
4725 pw.println(prefix + "WaitInfo:");
4726 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4727 pw.println(prefix + " mResult=");
4728 mResult.dump(pw, prefix);
4729 }
4730 }
David Stevens9440dc82017-03-16 19:00:20 -07004731
4732 private final class SleepTokenImpl extends SleepToken {
4733 private final String mTag;
4734 private final long mAcquireTime;
4735 private final int mDisplayId;
4736
4737 public SleepTokenImpl(String tag, int displayId) {
4738 mTag = tag;
4739 mDisplayId = displayId;
4740 mAcquireTime = SystemClock.uptimeMillis();
4741 }
4742
4743 @Override
4744 public void release() {
4745 synchronized (mService) {
4746 removeSleepTokenLocked(this);
4747 }
4748 }
4749
4750 @Override
4751 public String toString() {
4752 return "{\"" + mTag + "\", display " + mDisplayId
4753 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4754 }
4755 }
4756
Craig Mautner27084302013-03-25 08:05:25 -07004757}