blob: ce86624dfe4e813a01c53e9e6ba49bfe116c3b22 [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 Kulian3a95edc2017-06-28 16:21:07 -070019import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Andrii Kuliane6ac20e2017-03-17 10:30:50 -070020import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020021import static android.Manifest.permission.START_ANY_ACTIVITY;
22import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
23import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
24import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
25import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020026import static android.app.ActivityManager.START_TASK_TO_FRONT;
27import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
30import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
32import static android.app.ActivityManager.StackId.HOME_STACK_ID;
33import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
34import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
35import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080036import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070037import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
38import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020039import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
40import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
41import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070042import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020043import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
44import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070045import static android.view.Display.FLAG_PRIVATE;
46import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080047import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070048import static android.view.Display.TYPE_VIRTUAL;
Jorim Jaggife762342016-10-13 14:33:27 +020049
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;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
74import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
75import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
76import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
77import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
78import static com.android.server.am.ActivityManagerService.ANIMATE;
79import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
80import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
81import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020082import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
83import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
84import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
85import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
86import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
87import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
88import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
89import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
90import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
91import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
92import static com.android.server.am.ActivityStack.STACK_VISIBLE;
93import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
94import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
95import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
96import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
97import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080098import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
99import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
100import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +0200101import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800102import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200103
Svetoslav7008b512015-06-24 18:47:07 -0700104import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800105import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700106import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100107import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800110import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700111import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800112import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700114import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800115import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700116import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400118import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700119import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400120import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700122import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700124import android.content.Intent;
125import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700127import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.content.pm.PackageManager;
129import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100130import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800132import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800133import android.hardware.display.DisplayManager;
134import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800135import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800136import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800137import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800138import android.hardware.input.InputManager;
139import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700140import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100141import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700142import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700143import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700144import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700145import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700146import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700147import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700148import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700149import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700150import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400151import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700152import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700153import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700154import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700155import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100156import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700157import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700158import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400159import android.provider.Settings;
160import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700161import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700162import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700163import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700164import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800165import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700166import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700167import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800168import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800170import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800171import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800172import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800173
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800174import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800175import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700176import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400177import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700178import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400179import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800180import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700181import com.android.server.am.ActivityStack.ActivityState;
Winson Chung19953ca2017-04-11 11:19:23 -0700182import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700183import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700184
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185import java.io.FileDescriptor;
186import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700187import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800188import java.lang.annotation.Retention;
189import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700190import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700191import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700192import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700193import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700194
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800195public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800196 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700198 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700200 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700202 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700204 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700206 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800207 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700208 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800209
Craig Mautnerf3333272013-04-22 10:55:53 -0700210 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700211 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700212
Craig Mautner0eea92c2013-05-16 13:35:39 -0700213 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700214 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700217 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700218
Craig Mautner05d29032013-05-03 13:40:13 -0700219 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
220 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
221 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700222 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700223 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800224 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
225 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
226 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700227 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400228 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
229 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700230 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700231 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700232 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800233 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
234 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700237
Jason Monk62515be2014-05-21 16:06:19 -0400238 private static final String LOCK_TASK_TAG = "Lock-to-App";
239
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240 // Used to indicate if an object (e.g. stack) that we are trying to get
241 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800242 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700243
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700244 // Used to indicate that windows of activities should be preserved during the resize.
245 static final boolean PRESERVE_WINDOWS = true;
246
Wale Ogunwale040b4702015-08-06 18:10:50 -0700247 // Used to indicate if an object (e.g. task) should be moved/created
248 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700249 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700250
251 // Used to indicate that an objects (e.g. task) removal from its container
252 // (e.g. stack) is due to it moving to another container.
253 static final boolean MOVING = true;
254
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700255 // Force the focus to change to the stack we are moving a task to..
256 static final boolean FORCE_FOCUS = true;
257
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700258 // Don't execute any calls to resume.
259 static final boolean DEFER_RESUME = true;
260
Winson Chung010927a2016-12-15 16:12:35 -0800261 // Used to indicate that a task is removed it should also be removed from recents.
262 static final boolean REMOVE_FROM_RECENTS = true;
263
Winson Chung6954fc92017-03-24 16:22:12 -0700264 // Used to indicate that pausing an activity should occur immediately without waiting for
265 // the activity callback indicating that it has completed pausing
266 static final boolean PAUSE_IMMEDIATELY = true;
267
Winson Chung7900bee2017-03-10 08:59:25 -0800268 /**
269 * The modes which affect which tasks are returned when calling
270 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
271 */
272 @Retention(RetentionPolicy.SOURCE)
273 @IntDef({
274 MATCH_TASK_IN_STACKS_ONLY,
275 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
276 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
277 })
278 public @interface AnyTaskForIdMatchTaskMode {}
279 // Match only tasks in the current stacks
280 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
281 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
282 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
283 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
284 // provided stack id
285 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
286
Svetoslav7008b512015-06-24 18:47:07 -0700287 // Activity actions an app cannot start if it uses a permission which is not granted.
288 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
289 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700290
Svetoslav7008b512015-06-24 18:47:07 -0700291 static {
292 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
293 Manifest.permission.CAMERA);
294 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
295 Manifest.permission.CAMERA);
296 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
297 Manifest.permission.CALL_PHONE);
298 }
299
Svet Ganov99b60432015-06-27 13:15:22 -0700300 /** Action restriction: launching the activity is not restricted. */
301 private static final int ACTIVITY_RESTRICTION_NONE = 0;
302 /** Action restriction: launching the activity is restricted by a permission. */
303 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
304 /** Action restriction: launching the activity is restricted by an app op. */
305 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700306
justinzhang5286d3f2014-05-12 17:06:01 -0400307 /** Status Bar Service **/
308 private IBinder mToken = new Binder();
309 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400310 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400311
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700312 // For debugging to make sure the caller when acquiring/releasing our
313 // wake lock is the system process.
314 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800315 /** The number of distinct task ids that can be assigned to the tasks of a single user */
316 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700317
Craig Mautner27084302013-03-25 08:05:25 -0700318 final ActivityManagerService mService;
319
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800320 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800321
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322 final ActivityStackSupervisorHandler mHandler;
323
324 /** Short cut */
325 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700328 /** Counter for next free stack ID to use for dynamic activity stacks. */
329 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700330
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800331 /**
332 * Maps the task identifier that activities are currently being started in to the userId of the
333 * task. Each time a new task is created, the entry for the userId of the task is incremented
334 */
335 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700336
Craig Mautner2420ead2013-04-01 17:13:20 -0700337 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800338 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700339
Craig Mautnere0a38842013-12-16 16:14:02 -0800340 /** The stack containing the launcher app. Assumed to always be attached to
341 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800342 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700343
Craig Mautnere0a38842013-12-16 16:14:02 -0800344 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800345 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700346
Craig Mautner4a1cb222013-12-04 16:14:06 -0800347 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
348 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800349 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800350 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351
352 /** List of activities that are waiting for a new activity to become visible before completing
353 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700354 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
355 // mStoppingActivities when something else comes up.
356 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700357
Bryce Lee4a194382017-04-04 14:32:48 -0700358 /** List of processes waiting to find out when a specific activity becomes visible. */
359 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700360
361 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700362 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700363
Craig Mautnerde4ef022013-04-07 19:01:33 -0700364 /** List of activities that are ready to be stopped, but waiting for the next activity to
365 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800366 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700367
Craig Mautnerf3333272013-04-22 10:55:53 -0700368 /** List of activities that are ready to be finished, but waiting for the previous activity to
369 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800370 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700371
Craig Mautner0eea92c2013-05-16 13:35:39 -0700372 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800373 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700374
Wale Ogunwale22e25262016-02-01 10:32:02 -0800375 /** List of activities whose multi-window mode changed that we need to report to the
376 * application */
377 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
378
379 /** List of activities whose picture-in-picture mode changed that we need to report to the
380 * application */
381 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
382
Winson Chung5af42fc2017-03-24 17:11:33 -0700383 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
384 * the application */
385 Rect mPipModeChangedTargetStackBounds;
386
Craig Mautnerf3333272013-04-22 10:55:53 -0700387 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700388 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700389
Craig Mautnerde4ef022013-04-07 19:01:33 -0700390 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
391 * is being brought in front of us. */
392 boolean mUserLeaving = false;
393
Craig Mautner0eea92c2013-05-16 13:35:39 -0700394 /** Set when we have taken too long waiting to go to sleep. */
395 boolean mSleepTimeout = false;
396
397 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700398 * We don't want to allow the device to go to sleep while in the process
399 * of launching an activity. This is primarily to allow alarm intent
400 * receivers to launch an activity and get that to run before the device
401 * goes back to sleep.
402 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700403 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700404
405 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700406 * Set when the system is going to sleep, until we have
407 * successfully paused the current activity and released our wake lock.
408 * At that point the system is allowed to actually sleep.
409 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700410 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700411
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700412 /** Stack id of the front stack when user switched, indexed by userId. */
413 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700414
Craig Mautner4504de52013-12-20 09:06:56 -0800415 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800417 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418
Bryce Leeaf3a4002017-03-20 10:37:12 -0700419 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700421 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800422
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800423 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
424
425 private DisplayManagerInternal mDisplayManagerInternal;
426 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800427
Craig Mautner15df08a2015-04-01 12:17:18 -0700428 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
429 * may be finished until there is only one entry left. If this is empty the system is not
430 * in lockTask mode. */
431 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000432 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700433 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
434 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000435 */
436 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400437 /**
438 * Notifies the user when entering/exiting lock-task.
439 */
440 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800441
Wale Ogunwaled046a012015-12-24 13:05:59 -0800442 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800443 boolean inResumeTopActivity;
444
Andrii Kulian1e32e022016-09-16 15:29:34 -0700445 /**
446 * Temporary rect used during docked stack resize calculation so we don't need to create a new
447 * object each time.
448 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700449 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700450
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700451 // The default minimal size that will be used if the activity doesn't specify its minimal size.
452 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700453 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700454
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700455 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
456 private boolean mTaskLayersChanged = true;
457
Jorim Jaggi275561a2016-02-23 10:11:02 -0500458 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800459
Jorim Jaggiea039a82017-08-02 14:37:49 +0200460 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
461
Andrii Kulian1779e612016-10-12 21:58:25 -0700462 @Override
463 protected int getChildCount() {
464 return mActivityDisplays.size();
465 }
466
467 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700468 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700469 return mActivityDisplays.valueAt(index);
470 }
471
472 @Override
473 protected ConfigurationContainer getParent() {
474 return null;
475 }
476
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700477 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700478 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700479 if (activityDisplay == null) {
480 throw new IllegalArgumentException("No display found with id: " + displayId);
481 }
482
483 return activityDisplay.getOverrideConfiguration();
484 }
485
486 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700487 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700488 if (activityDisplay == null) {
489 throw new IllegalArgumentException("No display found with id: " + displayId);
490 }
491
492 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
493 }
494
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700495 /** Check if placing task or activity on specified display is allowed. */
496 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable) {
497 return displayId == DEFAULT_DISPLAY || (mService.mSupportsMultiDisplay
498 && (resizeable || displayConfigMatchesGlobal(displayId)));
499 }
500
501 /**
502 * Check if configuration of specified display matches current global config.
503 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
504 * the same config as on the default display.
505 * @param displayId Id of the display to check.
506 * @return {@code true} if configuration matches.
507 */
508 private boolean displayConfigMatchesGlobal(int displayId) {
509 if (displayId == DEFAULT_DISPLAY) {
510 return true;
511 }
512 if (displayId == INVALID_DISPLAY) {
513 return false;
514 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700515 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700516 if (targetDisplay == null) {
517 throw new IllegalArgumentException("No display found with id: " + displayId);
518 }
519 return getConfiguration().equals(targetDisplay.getConfiguration());
520 }
521
Wale Ogunwale39381972015-12-17 17:15:29 -0800522 static class FindTaskResult {
523 ActivityRecord r;
524 boolean matchedByRootAffinity;
525 }
526 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
527
Craig Mautneree36c772014-07-16 14:56:05 -0700528 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800529 * Temp storage for display ids sorted in focus order.
530 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
531 * it's more efficient, as the number of displays is usually small.
532 */
533 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
534
535 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100536 * Used to keep track whether app visibilities got changed since the last pause. Useful to
537 * determine whether to invoke the task stack change listener after pausing.
538 */
539 boolean mAppVisibilitiesChangedSinceLastPause;
540
541 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100542 * Set of tasks that are in resizing mode during an app transition to fill the "void".
543 */
544 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
545
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700546
547 /**
548 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
549 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
550 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
551 * like the docked stack going empty.
552 */
553 private boolean mAllowDockedStackResize = true;
554
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100555 /**
Tony Mak853304c2016-04-18 15:17:41 +0100556 * Is dock currently minimized.
557 */
558 boolean mIsDockMinimized;
559
Jorim Jaggife762342016-10-13 14:33:27 +0200560 final KeyguardController mKeyguardController;
561
Tony Mak853304c2016-04-18 15:17:41 +0100562 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700563 * Description of a request to start a new activity, which has been held
564 * due to app switches being disabled.
565 */
566 static class PendingActivityLaunch {
567 final ActivityRecord r;
568 final ActivityRecord sourceRecord;
569 final int startFlags;
570 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700571 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700572
573 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700574 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700575 r = _r;
576 sourceRecord = _sourceRecord;
577 startFlags = _startFlags;
578 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700579 callerApp = _callerApp;
580 }
581
582 void sendErrorResult(String message) {
583 try {
584 if (callerApp.thread != null) {
585 callerApp.thread.scheduleCrash(message);
586 }
587 } catch (RemoteException e) {
588 Slog.e(TAG, "Exception scheduling crash of failed "
589 + "activity launcher sourceRecord=" + sourceRecord, e);
590 }
Craig Mautneree36c772014-07-16 14:56:05 -0700591 }
592 }
593
Bryce Leeaf691c02017-03-20 14:20:22 -0700594 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700595 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700596 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800597 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200598 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700599 }
600
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800601 void setRecentTasks(RecentTasks recentTasks) {
602 mRecentTasks = recentTasks;
603 }
604
Jeff Brown2c43c332014-06-12 22:38:59 -0700605 /**
606 * At the time when the constructor runs, the power manager has not yet been
607 * initialized. So we initialize our wakelocks afterwards.
608 */
609 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700611 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700612 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700613 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700614 }
615
justinzhang5286d3f2014-05-12 17:06:01 -0400616 // This function returns a IStatusBarService. The value is from ServiceManager.
617 // getService and is cached.
618 private IStatusBarService getStatusBarService() {
619 synchronized (mService) {
620 if (mStatusBarService == null) {
621 mStatusBarService = IStatusBarService.Stub.asInterface(
622 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
623 if (mStatusBarService == null) {
624 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
625 }
626 }
627 return mStatusBarService;
628 }
629 }
630
Jason Monk35c62a42014-06-17 10:24:47 -0400631 private IDevicePolicyManager getDevicePolicyManager() {
632 synchronized (mService) {
633 if (mDevicePolicyManager == null) {
634 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
635 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
636 if (mDevicePolicyManager == null) {
637 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
638 }
639 }
640 return mDevicePolicyManager;
641 }
642 }
643
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700644 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 synchronized (mService) {
646 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200647 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800648
649 mDisplayManager =
650 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
651 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800652 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800653
654 Display[] displays = mDisplayManager.getDisplays();
655 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
656 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800657 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700658 if (activityDisplay.mDisplay == null) {
659 throw new IllegalStateException("Default Display does not exist");
660 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800661 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700662 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 }
664
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800665 mHomeStack = mFocusedStack = mLastFocusedStack =
666 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800667
668 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800669 }
Craig Mautner27084302013-03-25 08:05:25 -0700670 }
671
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700672 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700674 }
675
Craig Mautnerde4ef022013-04-07 19:01:33 -0700676 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700678 }
679
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700680 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700681 if (stack == null) {
682 return false;
683 }
684
Craig Mautnerdf88d732014-01-27 09:21:32 -0800685 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
686 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700687 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800688 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800689 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700690 }
691
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800692 /** The top most stack on its display. */
693 boolean isFrontStackOnDisplay(ActivityStack stack) {
694 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
695 }
696
697 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700698 if (stack == null) {
699 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800700 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700701
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700702 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
703 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700704 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800705 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800706 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700707 }
708
Wale Ogunwaled046a012015-12-24 13:05:59 -0800709 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800710 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
711 if (!focusCandidate.isFocusable()) {
712 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800713 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800714 }
715
716 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800717 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800718 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800719
Wale Ogunwaled046a012015-12-24 13:05:59 -0800720 EventLogTags.writeAmFocusedStack(
721 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
722 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
723 }
724
725 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800726 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800727 if (r != null && r.idle) {
728 checkFinishBootingLocked();
729 }
730 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700731 }
732
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700733 void moveHomeStackToFront(String reason) {
734 mHomeStack.moveToFront(reason);
735 }
736
Matthew Ng330757d2017-02-28 14:19:17 -0800737 void moveRecentsStackToFront(String reason) {
738 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
739 if (recentsStack != null) {
740 recentsStack.moveToFront(reason);
741 }
742 }
743
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700744 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800745 boolean moveHomeStackTaskToTop(String reason) {
746 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700747
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700748 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700749 if (top == null) {
750 return false;
751 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700752 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700753 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700754 }
755
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800756 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700757 if (!mService.mBooting && !mService.mBooted) {
758 // Not ready yet!
759 return false;
760 }
761
Craig Mautner84984fa2014-06-19 11:19:20 -0700762 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700763 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700764 }
765
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800766 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700767 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800768 final String myReason = reason + " resumeHomeStackTask";
769
Mark Lua56ea122015-10-08 13:31:01 +0800770 // Only resume home activity if isn't finishing.
771 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700772 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800773 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700774 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800775 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700776 }
777
Craig Mautner8d341ef2013-03-26 09:03:27 -0700778 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800779 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
780 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700781 }
782
783 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700784 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700785 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800786 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700787 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800788 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
789 * valid if the matchMode is
790 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700791 */
Winson Chung7900bee2017-03-10 08:59:25 -0800792 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
793 // If there is a stack id set, ensure that we are attempting to actually restore a task
794 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
795 stackId != INVALID_STACK_ID) {
796 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
797 }
798
Craig Mautnere0a38842013-12-16 16:14:02 -0800799 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800800 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800801 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
803 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700804 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800805 if (task != null) {
806 return task;
807 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700808 }
809 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800810
Winson Chung7900bee2017-03-10 08:59:25 -0800811 // If we are matching stack tasks only, return now
812 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800813 return null;
814 }
815
Winson Chung7900bee2017-03-10 08:59:25 -0800816 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
817 // the task from recents
818 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700819 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
820
821 if (task == null) {
822 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800823 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
824 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700825
826 return null;
827 }
828
829 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700830 return task;
831 }
832
Winson Chung7900bee2017-03-10 08:59:25 -0800833 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700834 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700835 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
836 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800837 return null;
838 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700839 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800840 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700841 }
842
Craig Mautner6170f732013-04-02 13:05:23 -0700843 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800844 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800845 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800846 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800847 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
848 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
849 if (r != null) {
850 return r;
851 }
Craig Mautner6170f732013-04-02 13:05:23 -0700852 }
853 }
854 return null;
855 }
856
Tony Mak853304c2016-04-18 15:17:41 +0100857 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000858 * Detects whether we should show a lock screen in front of this task for a locked user.
859 * <p>
860 * We'll do this if either of the following holds:
861 * <ul>
862 * <li>The top activity explicitly belongs to {@param userId}.</li>
863 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
864 * </ul>
865 *
866 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100867 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000868 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
869 // To handle the case that work app is in the task but just is not the top one.
870 final ActivityRecord activityRecord = task.getTopActivity();
871 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
872
873 return (activityRecord != null && activityRecord.userId == userId)
874 || (resultTo != null && resultTo.userId == userId);
875 }
876
877 /**
878 * Find all visible task stacks containing {@param userId} and intercept them with an activity
879 * to block out the contents and possibly start a credential-confirming intent.
880 *
881 * @param userId user handle for the locked managed profile.
882 */
883 void lockAllProfileTasks(@UserIdInt int userId) {
884 mWindowManager.deferSurfaceLayout();
885 try {
886 final List<ActivityStack> stacks = getStacks();
887 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
888 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
889 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
890 final TaskRecord task = tasks.get(taskNdx);
891
892 // Check the task for a top activity belonging to userId, or returning a result
893 // to an activity belonging to userId. Example case: a document picker for
894 // personal files, opened by a work app, should still get locked.
895 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000896 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
897 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000898 }
Tony Mak853304c2016-04-18 15:17:41 +0100899 }
900 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000901 } finally {
902 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100903 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000904 }
905
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800906 void setNextTaskIdForUserLocked(int taskId, int userId) {
907 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
908 if (taskId > currentTaskId) {
909 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700910 }
911 }
912
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700913 static int nextTaskIdForUser(int taskId, int userId) {
914 int nextTaskId = taskId + 1;
915 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
916 // Wrap around as there will be smaller task ids that are available now.
917 nextTaskId -= MAX_TASK_IDS_PER_USER;
918 }
919 return nextTaskId;
920 }
921
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800922 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800923 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
924 // for a userId u, a taskId can only be in the range
925 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
926 // 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 -0700927 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800928 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800929 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800930 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700931 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800932 if (candidateTaskId == currentTaskId) {
933 // Something wrong!
934 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
935 throw new IllegalStateException("Cannot get an available task id."
936 + " Reached limit of " + MAX_TASK_IDS_PER_USER
937 + " running tasks per user.");
938 }
939 }
940 mCurTaskIdForUser.put(userId, candidateTaskId);
941 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700942 }
943
Chong Zhang6cda19c2016-06-14 19:07:56 -0700944 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800945 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700946 if (stack == null) {
947 return null;
948 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700949 ActivityRecord resumedActivity = stack.mResumedActivity;
950 if (resumedActivity == null || resumedActivity.app == null) {
951 resumedActivity = stack.mPausingActivity;
952 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700953 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700954 }
955 }
956 return resumedActivity;
957 }
958
Dianne Hackbornff072722014-09-24 10:56:28 -0700959 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700960 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800961 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800962 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
963 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800964 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
965 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700966 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800967 continue;
968 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200969 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
970 final ActivityRecord top = stack.topRunningActivityLocked();
971 final int size = mTmpActivityList.size();
972 for (int i = 0; i < size; i++) {
973 final ActivityRecord activity = mTmpActivityList.get(i);
974 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
975 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200977 if (realStartActivityLocked(activity, app,
978 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 didSomething = true;
980 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700981 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200983 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700985 }
Craig Mautner20e72272013-04-01 13:45:53 -0700986 }
Craig Mautner20e72272013-04-01 13:45:53 -0700987 }
988 }
989 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700990 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700991 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700992 }
Craig Mautner20e72272013-04-01 13:45:53 -0700993 return didSomething;
994 }
995
996 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800997 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
998 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800999 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1000 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001001 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001002 continue;
1003 }
1004 final ActivityRecord resumedActivity = stack.mResumedActivity;
1005 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001006 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -08001007 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001008 return false;
1009 }
Craig Mautner20e72272013-04-01 13:45:53 -07001010 }
1011 }
Wei Wang65c7a152016-06-02 18:51:22 -07001012 // Send launch end powerhint when idle
1013 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001014 return true;
1015 }
1016
Craig Mautnerde4ef022013-04-07 19:01:33 -07001017 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001018 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1019 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001020 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1021 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001022 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001023 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001024 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001025 return false;
1026 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001027 }
1028 }
1029 }
1030 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001031 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001032 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1033 mLastFocusedStack + " to=" + mFocusedStack);
1034 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001035 return true;
1036 }
1037
1038 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001039 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001040 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1041 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001042 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1043 final ActivityStack stack = stacks.get(stackNdx);
1044 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001045 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001046 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001047 return false;
1048 }
1049 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001050 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001051 }
1052 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001053 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001054 }
1055
Craig Mautner2acc3892013-09-23 10:28:14 -07001056 /**
1057 * Pause all activities in either all of the stacks or just the back stacks.
1058 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001059 * @param resuming The resuming activity.
1060 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1061 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001062 * @return true if any activity was paused as a result of this call.
1063 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001064 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001065 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001066 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1067 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001068 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1069 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001070 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001071 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001072 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001073 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1074 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001075 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001076 }
1077 }
1078 return someActivityPaused;
1079 }
1080
Craig Mautnerde4ef022013-04-07 19:01:33 -07001081 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001082 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001083 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1084 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001085 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1086 final ActivityStack stack = stacks.get(stackNdx);
1087 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001088 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001089 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001090 Slog.d(TAG_STATES,
1091 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001092 pausing = false;
1093 } else {
1094 return false;
1095 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001096 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001097 }
1098 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001099 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001100 }
1101
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001102 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001103 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001104 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001105 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1106 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1107 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1108 final ActivityStack stack = stacks.get(stackNdx);
1109 if (stack.mResumedActivity != null &&
1110 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001111 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001112 }
1113 }
1114 }
1115 }
1116
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001117 void cancelInitializingActivities() {
1118 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1119 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1120 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1121 stacks.get(stackNdx).cancelInitializingActivities();
1122 }
1123 }
1124 }
1125
Bryce Lee4a194382017-04-04 14:32:48 -07001126 void waitActivityVisible(ComponentName name, WaitResult result) {
1127 final WaitInfo waitInfo = new WaitInfo(name, result);
1128 mWaitingForActivityVisible.add(waitInfo);
1129 }
1130
1131 void cleanupActivity(ActivityRecord r) {
1132 // Make sure this record is no longer in the pending finishes list.
1133 // This could happen, for example, if we are trimming activities
1134 // down to the max limit while they are still waiting to finish.
1135 mFinishingActivities.remove(r);
1136 mActivitiesWaitingForVisibleActivity.remove(r);
1137
1138 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001139 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001140 mWaitingForActivityVisible.remove(i);
1141 }
1142 }
1143 }
1144
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001145 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001146 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001147 }
1148
1149 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1150 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001151 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1152 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001153 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001154 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001155 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001156 result.timeout = false;
1157 result.who = w.getComponent();
1158 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1159 result.thisTime = result.totalTime;
1160 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001161 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001162 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001163 if (changed) {
1164 mService.notifyAll();
1165 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001166 }
1167
Chong Zhang5022da32016-06-21 16:31:37 -07001168 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1169 boolean changed = false;
1170 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1171 WaitResult w = mWaitingActivityLaunched.remove(i);
1172 if (w.who == null) {
1173 changed = true;
1174 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1175 // the starting activity ends up moving another activity to front, and it should
1176 // wait for this new activity to become visible instead.
1177 // Do not modify other fields.
1178 w.result = START_TASK_TO_FRONT;
1179 }
1180 }
1181 if (changed) {
1182 mService.notifyAll();
1183 }
1184 }
1185
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001186 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1187 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001188 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001189 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001190 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001191 if (w.who == null) {
1192 changed = true;
1193 w.timeout = timeout;
1194 if (r != null) {
1195 w.who = new ComponentName(r.info.packageName, r.info.name);
1196 }
1197 w.thisTime = thisTime;
1198 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001199 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001200 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001201 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001202 if (changed) {
1203 mService.notifyAll();
1204 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001205 }
1206
Craig Mautner29219d92013-04-16 20:19:12 -07001207 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001208 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001209 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001210 if (r != null) {
1211 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001212 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001213
Andrii Kulian7318d632016-07-20 18:59:28 -07001214 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001215 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1216
1217 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1218 final int displayId = mTmpOrderedDisplayIds.get(i);
1219 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1220 if (stacks == null) {
1221 continue;
1222 }
1223 for (int j = stacks.size() - 1; j >= 0; --j) {
1224 final ActivityStack stack = stacks.get(j);
1225 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1226 r = stack.topRunningActivityLocked();
1227 if (r != null) {
1228 return r;
1229 }
Craig Mautner29219d92013-04-16 20:19:12 -07001230 }
1231 }
1232 }
1233 return null;
1234 }
1235
Dianne Hackborn09233282014-04-30 11:33:59 -07001236 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001237 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001238 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1239 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001240 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001241 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001242 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001243 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1244 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001245 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001246 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001247 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001248 }
1249 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001250
1251 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1252 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1253 while (maxNum > 0) {
1254 long mostRecentActiveTime = Long.MIN_VALUE;
1255 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001256 final int numTaskLists = runningTaskLists.size();
1257 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1258 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001259 if (!stackTaskList.isEmpty()) {
1260 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1261 if (lastActiveTime > mostRecentActiveTime) {
1262 mostRecentActiveTime = lastActiveTime;
1263 selectedStackList = stackTaskList;
1264 }
1265 }
1266 }
1267 if (selectedStackList != null) {
1268 list.add(selectedStackList.remove(0));
1269 --maxNum;
1270 } else {
1271 break;
1272 }
1273 }
Craig Mautner20e72272013-04-01 13:45:53 -07001274 }
1275
Todd Kennedy7440f172015-12-09 14:31:22 -08001276 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1277 ProfilerInfo profilerInfo) {
1278 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001279 if (aInfo != null) {
1280 // Store the found target back into the intent, because now that
1281 // we have it we never want to do this again. For example, if the
1282 // user navigates back to this point in the history, we should
1283 // always restart the exact same activity.
1284 intent.setComponent(new ComponentName(
1285 aInfo.applicationInfo.packageName, aInfo.name));
1286
1287 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001288 if (!aInfo.processName.equals("system")) {
1289 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001290 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001291 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001292
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001293 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1294 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1295 }
1296
Man Caocfa78b22015-06-11 20:14:34 -07001297 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1298 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1299 }
1300
1301 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001302 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001303 }
1304 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001305 final String intentLaunchToken = intent.getLaunchToken();
1306 if (aInfo.launchToken == null && intentLaunchToken != null) {
1307 aInfo.launchToken = intentLaunchToken;
1308 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001309 }
1310 return aInfo;
1311 }
1312
Todd Kennedy7440f172015-12-09 14:31:22 -08001313 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001314 return resolveIntent(intent, resolvedType, userId, 0);
1315 }
1316
1317 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001318 synchronized (mService) {
1319 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001320 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001321 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001322 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001323 }
1324
1325 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1326 ProfilerInfo profilerInfo, int userId) {
1327 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1328 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1329 }
1330
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001331 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1332 boolean andResume, boolean checkConfig) throws RemoteException {
1333
1334 if (!allPausedActivitiesComplete()) {
1335 // While there are activities pausing we skipping starting any new activities until
1336 // pauses are complete. NOTE: that we also do this for activities that are starting in
1337 // the paused state because they will first be resumed then paused on the client side.
1338 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1339 "realStartActivityLocked: Skipping start of r=" + r
1340 + " some activities pausing...");
1341 return false;
1342 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001343
Andrii Kulianada10292017-02-09 23:19:29 -08001344 r.startFreezingScreenLocked(app, 0);
Wale Ogunwaled334a052017-05-12 07:17:36 -07001345 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */, true /* isTop */)) {
1346 // We only set the visibility to true if the activity is allowed to be visible based on
1347 // keyguard state. This avoids setting this into motion in window manager that is later
1348 // cancelled due to later calls to ensure visible activities that set visibility back to
1349 // false.
1350 r.setVisibility(true);
1351 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001352
Andrii Kulianada10292017-02-09 23:19:29 -08001353 // schedule launch ticks to collect information about slow apps.
1354 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001355
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001356 // Have the window manager re-evaluate the orientation of the screen based on the new
1357 // activity order. Note that as a result of this, it can call back into the activity
1358 // manager with a new orientation. We don't care about that, because the activity is not
1359 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001360 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001361 final int displayId = r.getDisplayId();
1362 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1363 getDisplayOverrideConfiguration(displayId),
1364 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001365 // Deferring resume here because we're going to launch new activity shortly.
1366 // We don't want to perform a redundant launch of the same record while ensuring
1367 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001368 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1369 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001370 }
1371
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001372 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001373 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001374 }
Makoto Onuki7a622782017-05-10 16:32:39 -07001375 final int applicationInfoUid =
1376 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1377 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1378 Slog.wtf(TAG,
1379 "User ID for activity changing for " + r
1380 + " appInfo.uid=" + r.appInfo.uid
1381 + " info.ai.uid=" + applicationInfoUid
1382 + " old=" + r.app + " new=" + app);
1383 }
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001384
Craig Mautner2420ead2013-04-01 17:13:20 -07001385 r.app = app;
1386 app.waitingToKill = null;
1387 r.launchCount++;
1388 r.lastLaunchTime = SystemClock.uptimeMillis();
1389
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001390 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001391
1392 int idx = app.activities.indexOf(r);
1393 if (idx < 0) {
1394 app.activities.add(r);
1395 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001396 mService.updateLruProcessLocked(app, true, null);
1397 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001398
Bryce Leeaf691c02017-03-20 14:20:22 -07001399 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001400 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1401 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001402 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001403 }
1404
Andrii Kulian02b7a832016-10-06 23:11:56 -07001405 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001406 try {
1407 if (app.thread == null) {
1408 throw new RemoteException();
1409 }
1410 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001411 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001412 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001413 // We don't need to deliver new intents and/or set results if activity is going
1414 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001415 results = r.results;
1416 newIntents = r.newIntents;
1417 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001418 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1419 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1420 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001421 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1422 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001423 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001424 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001425 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001426 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001427 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1428 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001429 r.sleeping = false;
1430 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001431 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001432 mService.showAskCompatModeDialogLocked(r);
1433 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001434 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001435 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1436 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1437 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001438 final String profileFile = mService.mProfileFile;
1439 if (profileFile != null) {
1440 ParcelFileDescriptor profileFd = mService.mProfileFd;
1441 if (profileFd != null) {
1442 try {
1443 profileFd = profileFd.dup();
1444 } catch (IOException e) {
1445 if (profileFd != null) {
1446 try {
1447 profileFd.close();
1448 } catch (IOException o) {
1449 }
1450 profileFd = null;
1451 }
1452 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001453 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001454
1455 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001456 mService.mSamplingInterval, mService.mAutoStopProfiler,
1457 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001458 }
1459 }
1460 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001461
Andrii Kulianada10292017-02-09 23:19:29 -08001462 app.hasShownUi = true;
1463 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001464 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001465 // Because we could be starting an Activity in the system process this may not go across
1466 // a Binder interface which would create a new Configuration. Consequently we have to
1467 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001468
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07001469 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1470 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1471 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001472
Sudheer Shankafab200f2017-05-17 20:41:53 -07001473 logIfTransactionTooLarge(r.intent, r.icicle);
Craig Mautner2420ead2013-04-01 17:13:20 -07001474 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001475 System.identityHashCode(r), r.info,
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07001476 // TODO: Have this take the merged configuration instead of separate global and
1477 // override configs.
1478 mergedConfiguration.getGlobalConfiguration(),
1479 mergedConfiguration.getOverrideConfiguration(), r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001480 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1481 r.persistentState, results, newIntents, !andResume,
1482 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001483
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001484 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001485 // This may be a heavy-weight process! Note that the package
1486 // manager will ensure that only activity can run in the main
1487 // process of the .apk, which is the only thing that will be
1488 // considered heavy-weight.
1489 if (app.processName.equals(app.info.packageName)) {
1490 if (mService.mHeavyWeightProcess != null
1491 && mService.mHeavyWeightProcess != app) {
1492 Slog.w(TAG, "Starting new heavy weight process " + app
1493 + " when already running "
1494 + mService.mHeavyWeightProcess);
1495 }
1496 mService.mHeavyWeightProcess = app;
1497 Message msg = mService.mHandler.obtainMessage(
1498 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1499 msg.obj = r;
1500 mService.mHandler.sendMessage(msg);
1501 }
1502 }
1503
1504 } catch (RemoteException e) {
1505 if (r.launchFailed) {
1506 // This is the second time we failed -- finish activity
1507 // and give up.
1508 Slog.e(TAG, "Second failure launching "
1509 + r.intent.getComponent().flattenToShortString()
1510 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001511 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001512 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1513 "2nd-crash", false);
1514 return false;
1515 }
1516
1517 // This is the first time we failed -- restart process and
1518 // retry.
Sudheer Shankae49e7b82017-05-05 16:39:32 -07001519 r.launchFailed = true;
Craig Mautner2420ead2013-04-01 17:13:20 -07001520 app.activities.remove(r);
1521 throw e;
1522 }
1523
1524 r.launchFailed = false;
1525 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001526 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001527 }
1528
1529 if (andResume) {
1530 // As part of the process of launching, ActivityThread also performs
1531 // a resume.
1532 stack.minimalResumeActivityLocked(r);
1533 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001534 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001535 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001536 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001537 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001538 "Moving to PAUSED: " + r + " (starting in paused state)");
1539 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001540 }
1541
1542 // Launch the new version setup screen if needed. We do this -after-
1543 // launching the initial activity (that is, home), so that it can have
1544 // a chance to initialize itself while in the background, making the
1545 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001546 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001547 mService.startSetupActivityLocked();
1548 }
1549
Dianne Hackborn465fa392014-09-14 14:21:18 -07001550 // Update any services we are bound to that might care about whether
1551 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001552 if (r.app != null) {
1553 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1554 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001555
Craig Mautner2420ead2013-04-01 17:13:20 -07001556 return true;
1557 }
1558
Sudheer Shankafab200f2017-05-17 20:41:53 -07001559 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1560 int extrasSize = 0;
1561 if (intent != null) {
1562 final Bundle extras = intent.getExtras();
1563 if (extras != null) {
1564 extrasSize = extras.getSize();
1565 }
1566 }
1567 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1568 if (extrasSize + icicleSize > 200000) {
1569 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1570 + ", icicle size: " + icicleSize);
1571 }
1572 }
1573
Craig Mautnere79d42682013-04-01 19:01:53 -07001574 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001575 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001576 // Is this activity's application already running?
1577 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001578 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001579
Andrii Kulian02b7a832016-10-06 23:11:56 -07001580 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001581
1582 if (app != null && app.thread != null) {
1583 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001584 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1585 || !"android".equals(r.info.packageName)) {
1586 // Don't add this if it is a platform component that is marked
1587 // to run in multiple processes, because this is actually
1588 // part of the framework so doesn't make sense to track as a
1589 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001590 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1591 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001592 }
George Mount2c92c972014-03-20 09:38:23 -07001593 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001594 return;
1595 } catch (RemoteException e) {
1596 Slog.w(TAG, "Exception when starting activity "
1597 + r.intent.getComponent().flattenToShortString(), e);
1598 }
1599
1600 // If a dead object exception was thrown -- fall through to
1601 // restart the application.
1602 }
1603
1604 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001605 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001606 }
1607
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001608 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001609 String resultWho, int requestCode, int callingPid, int callingUid,
1610 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001611 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001612 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1613 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001614 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001615 return true;
1616 }
1617 final int componentRestriction = getComponentRestrictionForCallingPackage(
1618 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1619 final int actionRestriction = getActionRestrictionForCallingPackage(
1620 intent.getAction(), callingPackage, callingPid, callingUid);
1621 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1622 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1623 if (resultRecord != null) {
1624 resultStack.sendActivityResultLocked(-1,
1625 resultRecord, resultWho, requestCode,
1626 Activity.RESULT_CANCELED, null);
1627 }
1628 final String msg;
1629 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1630 msg = "Permission Denial: starting " + intent.toString()
1631 + " from " + callerApp + " (pid=" + callingPid
1632 + ", uid=" + callingUid + ")" + " with revoked permission "
1633 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1634 } else if (!aInfo.exported) {
1635 msg = "Permission Denial: starting " + intent.toString()
1636 + " from " + callerApp + " (pid=" + callingPid
1637 + ", uid=" + callingUid + ")"
1638 + " not exported from uid " + aInfo.applicationInfo.uid;
1639 } else {
1640 msg = "Permission Denial: starting " + intent.toString()
1641 + " from " + callerApp + " (pid=" + callingPid
1642 + ", uid=" + callingUid + ")"
1643 + " requires " + aInfo.permission;
1644 }
1645 Slog.w(TAG, msg);
1646 throw new SecurityException(msg);
1647 }
1648
1649 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1650 final String message = "Appop Denial: starting " + intent.toString()
1651 + " from " + callerApp + " (pid=" + callingPid
1652 + ", uid=" + callingUid + ")"
1653 + " requires " + AppOpsManager.permissionToOp(
1654 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1655 Slog.w(TAG, message);
1656 return false;
1657 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1658 final String message = "Appop Denial: starting " + intent.toString()
1659 + " from " + callerApp + " (pid=" + callingPid
1660 + ", uid=" + callingUid + ")"
1661 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1662 Slog.w(TAG, message);
1663 return false;
1664 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001665 if (options != null) {
1666 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1667 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1668 callingPid, callingUid);
1669 if (startInTaskPerm != PERMISSION_GRANTED) {
1670 final String msg = "Permission Denial: starting " + intent.toString()
1671 + " from " + callerApp + " (pid=" + callingPid
1672 + ", uid=" + callingUid + ") with launchTaskId="
1673 + options.getLaunchTaskId();
1674 Slog.w(TAG, msg);
1675 throw new SecurityException(msg);
1676 }
1677 }
1678 // Check if someone tries to launch an activity on a private display with a different
1679 // owner.
1680 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001681 if (launchDisplayId != INVALID_DISPLAY
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001682 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001683 final String msg = "Permission Denial: starting " + intent.toString()
1684 + " from " + callerApp + " (pid=" + callingPid
1685 + ", uid=" + callingUid + ") with launchDisplayId="
1686 + launchDisplayId;
1687 Slog.w(TAG, msg);
1688 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001689 }
1690 }
1691
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001692 return true;
1693 }
1694
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001695 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001696 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001697 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1698 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1699
Andrii Kulian62e6f252017-05-30 22:46:53 -07001700 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001701 if (activityDisplay == null) {
1702 Slog.w(TAG, "Launch on display check: display not found");
1703 return false;
1704 }
1705
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001706 // Check if the caller can manage activity stacks.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001707 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001708 callingUid);
1709 if (startAnyPerm == PERMISSION_GRANTED) {
1710 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1711 + " allow launch any on display");
1712 return true;
1713 }
1714
1715 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001716 && activityDisplay.mDisplay.getOwnerUid() != SYSTEM_UID) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001717 // Limit launching on virtual displays, because their contents can be read from Surface
1718 // by apps that created them.
1719 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1720 + " disallow launch on virtual display for not-embedded activity");
1721 return false;
1722 }
1723
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001724 if (!activityDisplay.isPrivate()) {
1725 // Anyone can launch on a public display.
1726 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1727 + " allow launch on public display");
1728 return true;
1729 }
1730
1731 // Check if the caller is the owner of the display.
1732 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1733 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1734 + " allow launch for owner of the display");
1735 return true;
1736 }
1737
1738 // Check if caller is present on display
1739 if (activityDisplay.isUidPresent(callingUid)) {
1740 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1741 + " allow launch for caller present on the display");
1742 return true;
1743 }
1744
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001745 Slog.w(TAG, "Launch on display check: denied");
1746 return false;
1747 }
1748
1749 /** Update lists of UIDs that are present on displays and have access to them. */
1750 void updateUIDsPresentOnDisplay() {
1751 mDisplayAccessUIDs.clear();
1752 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1753 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001754 // Only bother calculating the whitelist for private displays
1755 if (activityDisplay.isPrivate()) {
1756 mDisplayAccessUIDs.append(
1757 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1758 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001759 }
1760 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1761 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1762 }
1763
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001764 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001765 final long identity = Binder.clearCallingIdentity();
1766 try {
1767 return UserManager.get(mService.mContext).getUserInfo(userId);
1768 } finally {
1769 Binder.restoreCallingIdentity(identity);
1770 }
1771 }
1772
Svet Ganov99b60432015-06-27 13:15:22 -07001773 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001774 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001775 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1776 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001777 == PackageManager.PERMISSION_DENIED) {
1778 return ACTIVITY_RESTRICTION_PERMISSION;
1779 }
1780
Christopher Tateff7add02015-08-17 10:23:22 -07001781 if (activityInfo.permission == null) {
1782 return ACTIVITY_RESTRICTION_NONE;
1783 }
1784
Svet Ganov99b60432015-06-27 13:15:22 -07001785 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1786 if (opCode == AppOpsManager.OP_NONE) {
1787 return ACTIVITY_RESTRICTION_NONE;
1788 }
1789
1790 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1791 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001792 if (!ignoreTargetSecurity) {
1793 return ACTIVITY_RESTRICTION_APPOP;
1794 }
Svet Ganov99b60432015-06-27 13:15:22 -07001795 }
1796
1797 return ACTIVITY_RESTRICTION_NONE;
1798 }
1799
Svetoslav7008b512015-06-24 18:47:07 -07001800 private int getActionRestrictionForCallingPackage(String action,
1801 String callingPackage, int callingPid, int callingUid) {
1802 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001803 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001804 }
1805
1806 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1807 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001808 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001809 }
1810
1811 final PackageInfo packageInfo;
1812 try {
1813 packageInfo = mService.mContext.getPackageManager()
1814 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1815 } catch (PackageManager.NameNotFoundException e) {
1816 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001817 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001818 }
1819
1820 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001821 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001822 }
1823
1824 if (mService.checkPermission(permission, callingPid, callingUid) ==
1825 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001826 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001827 }
1828
1829 final int opCode = AppOpsManager.permissionToOpCode(permission);
1830 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001831 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001832 }
1833
1834 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1835 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001836 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001837 }
1838
Svet Ganov99b60432015-06-27 13:15:22 -07001839 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001840 }
1841
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001842 void setLaunchSource(int uid) {
1843 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1844 }
1845
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001846 void acquireLaunchWakelock() {
1847 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1848 throw new IllegalStateException("Calling must be system uid");
1849 }
1850 mLaunchingActivity.acquire();
1851 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1852 // To be safe, don't allow the wake lock to be held for too long.
1853 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1854 }
1855 }
1856
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001857 /**
1858 * Called when the frontmost task is idle.
1859 * @return the state of mService.mBooting before this was called.
1860 */
1861 private boolean checkFinishBootingLocked() {
1862 final boolean booting = mService.mBooting;
1863 boolean enableScreen = false;
1864 mService.mBooting = false;
1865 if (!mService.mBooted) {
1866 mService.mBooted = true;
1867 enableScreen = true;
1868 }
1869 if (booting || enableScreen) {
1870 mService.postFinishBooting(booting, enableScreen);
1871 }
1872 return booting;
1873 }
1874
Craig Mautnerf3333272013-04-22 10:55:53 -07001875 // Checked.
1876 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001877 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001878 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001879
Craig Mautnerf3333272013-04-22 10:55:53 -07001880 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001881 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001882 int NS = 0;
1883 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001884 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001885 boolean activityRemoved = false;
1886
Wale Ogunwale7d701172015-03-11 15:36:30 -07001887 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001888 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001889 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1890 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001891 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1892 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001893 if (fromTimeout) {
1894 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001896
1897 // This is a hack to semi-deal with a race condition
1898 // in the client where it can be constructed with a
1899 // newer configuration from when we asked it to launch.
1900 // We'll update with whatever configuration it now says
1901 // it used to launch.
1902 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001903 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001904 }
1905
1906 // We are now idle. If someone is waiting for a thumbnail from
1907 // us, we can now deliver.
1908 r.idle = true;
1909
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001910 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001911 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001912 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001913 }
1914 }
1915
1916 if (allResumedActivitiesIdle()) {
1917 if (r != null) {
1918 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001919 }
1920
1921 if (mLaunchingActivity.isHeld()) {
1922 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1923 if (VALIDATE_WAKE_LOCK_CALLER &&
1924 Binder.getCallingUid() != Process.myUid()) {
1925 throw new IllegalStateException("Calling must be system uid");
1926 }
1927 mLaunchingActivity.release();
1928 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001929 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001930 }
1931
1932 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001933 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1934 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001935 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001936 if ((NF = mFinishingActivities.size()) > 0) {
1937 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001938 mFinishingActivities.clear();
1939 }
1940
Craig Mautnerf3333272013-04-22 10:55:53 -07001941 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001942 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001943 mStartingUsers.clear();
1944 }
1945
Craig Mautnerf3333272013-04-22 10:55:53 -07001946 // Stop any activities that are scheduled to do so but have been
1947 // waiting for the next one to start.
1948 for (int i = 0; i < NS; i++) {
1949 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001950 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001951 if (stack != null) {
1952 if (r.finishing) {
1953 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1954 } else {
1955 stack.stopActivityLocked(r);
1956 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001957 }
1958 }
1959
1960 // Finish any activities that are scheduled to do so but have been
1961 // waiting for the next one to start.
1962 for (int i = 0; i < NF; i++) {
1963 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001964 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001965 if (stack != null) {
1966 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1967 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001968 }
1969
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001970 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001971 // Complete user switch
1972 if (startingUsers != null) {
1973 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001974 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001975 }
1976 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001977 }
1978
1979 mService.trimApplications();
1980 //dump();
1981 //mWindowManager.dump();
1982
Craig Mautnerf3333272013-04-22 10:55:53 -07001983 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001984 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001985 }
1986
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001987 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001988 }
1989
Craig Mautner8e569572013-10-11 17:36:59 -07001990 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001991 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001992 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1993 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001994 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1995 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1996 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001997 }
Craig Mautner19091252013-10-05 00:03:53 -07001998 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001999 }
2000
2001 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002002 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2003 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002004 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2005 stacks.get(stackNdx).closeSystemDialogsLocked();
2006 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002007 }
2008 }
2009
Craig Mautner93529a42013-10-04 15:03:13 -07002010 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002011 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002012 }
2013
Craig Mautner8d341ef2013-03-26 09:03:27 -07002014 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002015 * Update the last used stack id for non-current user (current user's last
2016 * used stack is the focused stack)
2017 */
2018 void updateUserStackLocked(int userId, ActivityStack stack) {
2019 if (userId != mCurrentUser) {
2020 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2021 }
2022 }
2023
2024 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002025 * @return true if some activity was finished (or would have finished if doit were true).
2026 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002027 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2028 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002029 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2031 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002032 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002033 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002034 if (stack.finishDisabledPackageActivitiesLocked(
2035 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002036 didSomething = true;
2037 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002038 }
2039 }
2040 return didSomething;
2041 }
2042
Dianne Hackborna413dc02013-07-12 12:02:55 -07002043 void updatePreviousProcessLocked(ActivityRecord r) {
2044 // Now that this process has stopped, we may want to consider
2045 // it to be the previous app to try to keep around in case
2046 // the user wants to return to it.
2047
2048 // First, found out what is currently the foreground app, so that
2049 // we don't blow away the previous app if this activity is being
2050 // hosted by the process that is actually still the foreground.
2051 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002052 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2053 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002054 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2055 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002056 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002057 if (stack.mResumedActivity != null) {
2058 fgApp = stack.mResumedActivity.app;
2059 } else if (stack.mPausingActivity != null) {
2060 fgApp = stack.mPausingActivity.app;
2061 }
2062 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002063 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002064 }
2065 }
2066
2067 // Now set this one as the previous process, only if that really
2068 // makes sense to.
2069 if (r.app != null && fgApp != null && r.app != fgApp
2070 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002071 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002072 mService.mPreviousProcess = r.app;
2073 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2074 }
2075 }
2076
Wale Ogunwaled046a012015-12-24 13:05:59 -08002077 boolean resumeFocusedStackTopActivityLocked() {
2078 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002079 }
2080
Wale Ogunwaled046a012015-12-24 13:05:59 -08002081 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002082 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002083 if (targetStack != null && isFocusedStack(targetStack)) {
2084 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002085 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002086 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2087 if (r == null || r.state != RESUMED) {
2088 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002089 } else if (r.state == RESUMED) {
2090 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2091 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002092 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002093 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002094 }
2095
Todd Kennedy39bfee52016-02-24 10:28:21 -08002096 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2097 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2098 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2099 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2100 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2101 }
2102 }
2103 }
2104
Adrian Roos20d7df32016-01-12 18:59:43 +01002105 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2106 TaskRecord finishedTask = null;
2107 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002108 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2109 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002110 final int numStacks = stacks.size();
2111 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2112 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002113 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2114 if (stack == focusedStack || finishedTask == null) {
2115 finishedTask = t;
2116 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002117 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002118 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002119 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002120 }
2121
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002122 void finishVoiceTask(IVoiceInteractionSession session) {
2123 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2124 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2125 final int numStacks = stacks.size();
2126 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2127 final ActivityStack stack = stacks.get(stackNdx);
2128 stack.finishVoiceTask(session);
2129 }
2130 }
2131 }
2132
Andrii Kulianc27916642016-04-12 17:59:27 -07002133 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2134 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002135 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2136 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002137 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002138 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2139 // Caller wants the home activity moved with it. To accomplish this,
2140 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002141 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002142 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002143 ActivityStack currentStack = task.getStack();
2144 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002145 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2146 + task + " to front. Stack is null");
2147 return;
2148 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002149
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002150 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002151 int stackId = options.getLaunchStackId();
2152 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002153 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002154 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002155 if (stackId == INVALID_STACK_ID) {
2156 stackId = task.getLaunchStackId();
2157 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002158 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002159 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2160 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002161 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002162 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2163 // still need moveTaskToFrontLocked() below for any transition settings.
2164 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002165 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2166 resizeStackLocked(stackId, bounds,
2167 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002168 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002169 } else {
2170 // WM resizeTask must be done after the task is moved to the correct stack,
2171 // because Task's setBounds() also updates dim layer's bounds, but that has
2172 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002173 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002174 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002175 }
2176 }
2177
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002178 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002179 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002180 r == null ? null : r.appTimeTracker, reason);
2181
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002182 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002183 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002184
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002185 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
2186 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002187 }
2188
Wale Ogunwale854809c2015-12-27 16:18:19 -08002189 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002190 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002191 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002192 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002193 return false;
2194 }
2195 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2196 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002197 }
2198
Winson Chung55893332017-02-17 17:13:10 -08002199 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002200 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002201 }
2202
Winson Chung55893332017-02-17 17:13:10 -08002203 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2204 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002205 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002206 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002207 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002208 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002209 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002210 return null;
2211 }
Matthew Ng330757d2017-02-28 14:19:17 -08002212 if (stackId == DOCKED_STACK_ID) {
2213 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2214 // the other side of the docked stack and we make visibility decisions based on that.
2215 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2216 }
Winson Chung55893332017-02-17 17:13:10 -08002217 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002218 }
2219
Andrii Kulian16802aa2016-11-02 12:21:33 -07002220 /**
2221 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2222 * If there is no such stack, new dynamic stack can be created.
2223 * @param displayId Target display.
2224 * @param r Activity that should be launched there.
2225 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2226 */
2227 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002228 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002229 if (activityDisplay == null) {
2230 throw new IllegalArgumentException(
2231 "Display with displayId=" + displayId + " not found.");
2232 }
2233
2234 // Return the topmost valid stack on the display.
2235 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2236 final ActivityStack stack = activityDisplay.mStacks.get(i);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002237 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002238 return stack;
2239 }
2240 }
2241
2242 // If there is no valid stack on the external display - check if new dynamic stack will do.
2243 if (displayId != Display.DEFAULT_DISPLAY) {
2244 final int newDynamicStackId = getNextStackId();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002245 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002246 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2247 }
2248 }
2249
2250 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2251 return null;
2252 }
2253
Craig Mautner967212c2013-04-13 21:10:58 -07002254 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002255 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002256 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2257 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002258 }
2259 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002260 }
2261
Jorim Jaggife762342016-10-13 14:33:27 +02002262 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2263 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2264 }
2265
Andrii Kulian7fc22812016-12-28 13:04:11 -08002266 /**
2267 * Get next focusable stack in the system. This will search across displays and stacks
2268 * in last-focused order for a focusable and visible stack, different from the target stack.
2269 *
2270 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2271 * searching for next candidate.
2272 * @return Next focusable {@link ActivityStack}, null if not found.
2273 */
2274 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2275 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2276
2277 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2278 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002279 // If a display is registered in WM, it must also be available in AM.
2280 @SuppressWarnings("ConstantConditions")
2281 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002282 for (int j = stacks.size() - 1; j >= 0; --j) {
2283 final ActivityStack stack = stacks.get(j);
2284 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002285 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002286 return stack;
2287 }
2288 }
2289 }
2290
2291 return null;
2292 }
2293
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002294 /**
2295 * Get next valid stack for launching provided activity in the system. This will search across
2296 * displays and stacks in last-focused order for a focusable and visible stack, except those
2297 * that are on a currently focused display.
2298 *
2299 * @param r The activity that is being launched.
2300 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2301 * searching for the next candidate.
2302 * @return Next valid {@link ActivityStack}, null if not found.
2303 */
2304 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2305 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2306 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2307 final int displayId = mTmpOrderedDisplayIds.get(i);
2308 if (displayId == currentFocus) {
2309 continue;
2310 }
2311 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2312 if (stack != null) {
2313 return stack;
2314 }
2315 }
2316 return null;
2317 }
2318
Craig Mautneree2e45a2014-06-27 12:10:03 -07002319 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002320 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002321 }
2322
2323 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002324 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2325 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2326 final TaskRecord task = tasks.get(taskNdx);
2327 if (task.isHomeTask()) {
2328 final ArrayList<ActivityRecord> activities = task.mActivities;
2329 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2330 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002331 if (r.isHomeActivity()
2332 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002333 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002334 }
2335 }
2336 }
2337 }
2338 return null;
2339 }
2340
Chong Zhangb15758a2015-11-17 12:12:03 -08002341 /**
2342 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2343 * the docked stack itself, or if it's side-by-side to the docked stack.
2344 */
2345 boolean isStackDockedInEffect(int stackId) {
2346 return stackId == DOCKED_STACK_ID ||
2347 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2348 }
2349
Todd Kennedyca4d8422015-01-15 15:19:22 -08002350 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002351 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002352 ActivityContainer activityContainer =
2353 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002354 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002355 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2356 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002357 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002358 return activityContainer;
2359 }
2360
Craig Mautner34b73df2014-01-12 21:11:08 -08002361 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002362 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2363 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2364 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002365 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2366 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002367 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002368 }
2369 }
2370
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002371 void deleteActivityContainerRecord(int stackId) {
2372 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2373 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2374 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002375 }
2376
Jorim Jaggidc249c42015-12-15 14:57:31 -08002377 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002378 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002379 if (stackId == DOCKED_STACK_ID) {
2380 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002381 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002382 return;
2383 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002384 final ActivityStack stack = getStack(stackId);
2385 if (stack == null) {
2386 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2387 return;
2388 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002389
Winson Chunga2249ac2017-03-30 18:15:30 -07002390 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2391 getStack(DOCKED_STACK_ID) != null) {
2392 // If the docked stack exists, don't resize non-floating stacks independently of the
2393 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002394 return;
2395 }
2396
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002397 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002398 mWindowManager.deferSurfaceLayout();
2399 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002400 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002401 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002402 stack.ensureVisibleActivitiesConfigurationLocked(
2403 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002404 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002405 } finally {
2406 mWindowManager.continueSurfaceLayout();
2407 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002408 }
2409 }
2410
Jorim Jaggi192086e2016-03-11 17:17:03 +01002411 void deferUpdateBounds(int stackId) {
2412 final ActivityStack stack = getStack(stackId);
2413 if (stack != null) {
2414 stack.deferUpdateBounds();
2415 }
2416 }
2417
2418 void continueUpdateBounds(int stackId) {
2419 final ActivityStack stack = getStack(stackId);
2420 if (stack != null) {
2421 stack.continueUpdateBounds();
2422 }
2423 }
2424
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002425 void notifyAppTransitionDone() {
Matthew Ng606dd802017-06-05 14:06:32 -07002426 continueUpdateBounds(RECENTS_STACK_ID);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002427 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2428 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002429 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002430 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002431 if (task != null) {
2432 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002433 }
2434 }
2435 mResizingTasksDuringAnimation.clear();
2436 }
2437
Robert Carre7cc44d2017-03-20 19:04:30 -07002438 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2439 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002440
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002441 final ActivityStack stack = getStack(fromStackId);
2442 if (stack == null) {
2443 return;
2444 }
2445
2446 mWindowManager.deferSurfaceLayout();
2447 try {
2448 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002449 // We are moving all tasks from the docked stack to the fullscreen stack,
2450 // which is dismissing the docked stack, so resize all other stacks to
2451 // fullscreen here already so we don't end up with resize trashing.
2452 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2453 if (StackId.isResizeableByDockedStack(i)) {
2454 ActivityStack otherStack = getStack(i);
2455 if (otherStack != null) {
2456 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2457 true /* allowResizeInDockedMode */, DEFER_RESUME);
2458 }
2459 }
2460 }
2461
2462 // Also disable docked stack resizing since we have manually adjusted the
2463 // size of other stacks above and we don't want to trigger a docked stack
2464 // resize when we remove task from it below and it is detached from the
2465 // display because it no longer contains any tasks.
2466 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002467 } else if (fromStackId == PINNED_STACK_ID) {
2468 if (onTop) {
2469 // Log if we are expanding the PiP to fullscreen
2470 MetricsLogger.action(mService.mContext,
2471 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2472 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002473 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002474 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002475 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002476 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002477 // If we are moving from the pinned stack, then the animation takes care of updating
2478 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002479 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002480 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2481 final int size = tasks.size();
2482 if (onTop) {
2483 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002484 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002485 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002486 if (fromStackId == PINNED_STACK_ID) {
2487 // Update the return-to to reflect where the pinned stack task was moved
2488 // from so that we retain the stack that was previously visible if the
2489 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002490 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2491 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002492 }
Winson Chung74666102017-02-22 17:49:24 -08002493 // Defer resume until all the tasks have been moved to the fullscreen stack
2494 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2495 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002496 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002497 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002498 }
2499 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002500 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002501 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002502 // Position the tasks in the fullscreen stack in order at the bottom of the
2503 // stack. Also defer resume until all the tasks have been moved to the
2504 // fullscreen stack.
2505 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002506 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2507 schedulePictureInPictureModeChange,
2508 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002509 }
2510 }
Winson Chung74666102017-02-22 17:49:24 -08002511
2512 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2513 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002514 } finally {
2515 mAllowDockedStackResize = true;
2516 mWindowManager.continueSurfaceLayout();
2517 }
2518 }
2519
Robert Carr6914f082017-03-20 19:04:30 -07002520 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002521 mWindowManager.inSurfaceTransaction(
2522 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002523 }
2524
Jorim Jaggidc249c42015-12-15 14:57:31 -08002525 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002526 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2527 boolean preserveWindows) {
2528 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2529 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2530 false /* deferResume */);
2531 }
2532
2533 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2534 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2535 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002536
2537 if (!mAllowDockedStackResize) {
2538 // Docked stack resize currently disabled.
2539 return;
2540 }
2541
Jorim Jaggidc249c42015-12-15 14:57:31 -08002542 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2543 if (stack == null) {
2544 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2545 return;
2546 }
2547
2548 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2549 mWindowManager.deferSurfaceLayout();
2550 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002551 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2552 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002553 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002554 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002555
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002556 // TODO: Checking for isAttached might not be needed as if the user passes in null
2557 // dockedBounds then they want the docked stack to be dismissed.
2558 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2559 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002560 // In this case we make all other static stacks fullscreen and move all
2561 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002562 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002563
2564 // stack shouldn't contain anymore activities, so nothing to resume.
2565 r = null;
2566 } else {
2567 // Docked stacks occupy a dedicated region on screen so the size of all other
2568 // static stacks need to be adjusted so they don't overlap with the docked stack.
2569 // We get the bounds to use from window manager which has been adjusted for any
2570 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002571 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002572 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002573 final ActivityStack current = getStack(i);
2574 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002575 current.getStackDockedModeBounds(
2576 tempOtherTaskBounds /* currentTempTaskBounds */,
2577 tempRect /* outStackBounds */,
2578 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2579
Andrii Kuliane48b7d82017-04-28 14:51:05 -07002580 resizeStackLocked(i, !tempRect.isEmpty() ? tempRect : null,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002581 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2582 tempOtherTaskInsetBounds, preserveWindows,
2583 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002584 }
2585 }
2586 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002587 if (!deferResume) {
2588 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2589 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002590 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002591 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002592 mWindowManager.continueSurfaceLayout();
2593 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2594 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002595 }
2596
Robert Carr0d00c2e2016-02-29 17:45:02 -08002597 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002598 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002599 if (stack == null) {
2600 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2601 return;
2602 }
Winson Chung19953ca2017-04-11 11:19:23 -07002603
2604 // It is possible for the bounds animation from the WM to call this but be delayed by
2605 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2606 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2607 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2608 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002609 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002610 return;
2611 }
2612
Robert Carr0d00c2e2016-02-29 17:45:02 -08002613 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2614 mWindowManager.deferSurfaceLayout();
2615 try {
2616 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002617 Rect insetBounds = null;
2618 if (tempPinnedTaskBounds != null) {
2619 // We always use 0,0 as the position for the inset rect because
2620 // if we are getting insets at all in the pinned stack it must mean
2621 // we are headed for fullscreen.
2622 insetBounds = tempRect;
2623 insetBounds.top = 0;
2624 insetBounds.left = 0;
2625 insetBounds.right = tempPinnedTaskBounds.width();
2626 insetBounds.bottom = tempPinnedTaskBounds.height();
2627 }
2628 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002629 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002630 } finally {
2631 mWindowManager.continueSurfaceLayout();
2632 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2633 }
2634 }
2635
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002636 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002637 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002638 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002639 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002640 }
2641
Wale Ogunwale1666e312016-12-16 11:27:18 -08002642 final ActivityContainer activityContainer =
2643 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002644 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002645 }
2646
Robert Carr6914f082017-03-20 19:04:30 -07002647
Robert Carre7cc44d2017-03-20 19:04:30 -07002648 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002649 final ActivityStack stack = getStack(stackId);
2650 if (stack == null) {
2651 return;
2652 }
2653
2654 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2655 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002656 /**
2657 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2658 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2659 * that the client receives onStop() before it is reparented. We do this by detaching
2660 * the stack from the display so that it will be considered invisible when
2661 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2662 * invisible as well and added to the stopping list. After which we process the
2663 * stopping list by handling the idle.
2664 */
2665 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2666 pinnedStack.mForceHidden = true;
2667 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2668 pinnedStack.mForceHidden = false;
2669 activityIdleInternalLocked(null, false /* fromTimeout */,
2670 true /* processPausingActivites */, null /* configuration */);
2671
2672 // Move all the tasks to the bottom of the fullscreen stack
2673 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002674 } else {
2675 for (int i = tasks.size() - 1; i >= 0; i--) {
2676 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2677 REMOVE_FROM_RECENTS);
2678 }
2679 }
2680 }
2681
2682 /**
Robert Carr6914f082017-03-20 19:04:30 -07002683 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2684 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2685 * instead moved back onto the fullscreen stack.
2686 */
2687 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002688 mWindowManager.inSurfaceTransaction(
2689 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002690 }
2691
2692 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002693 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2694 */
2695 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2696 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2697 }
2698
2699 /**
Winson Chung010927a2016-12-15 16:12:35 -08002700 * Removes the task with the specified task id.
2701 *
2702 * @param taskId Identifier of the task to be removed.
2703 * @param killProcess Kill any process associated with the task if possible.
2704 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002705 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2706 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002707 * @return Returns true if the given task was found and removed.
2708 */
Winson Chung6954fc92017-03-24 16:22:12 -07002709 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2710 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002711 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2712 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002713 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002714 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002715 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2716 if (tr.isPersistable) {
2717 mService.notifyTaskPersisterLocked(null, true);
2718 }
2719 return true;
2720 }
2721 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2722 return false;
2723 }
2724
2725 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2726 if (removeFromRecents) {
2727 mRecentTasks.remove(tr);
2728 tr.removedFromRecents();
2729 }
2730 ComponentName component = tr.getBaseIntent().getComponent();
2731 if (component == null) {
2732 Slog.w(TAG, "No component for base intent of task: " + tr);
2733 return;
2734 }
2735
2736 // Find any running services associated with this app and stop if needed.
2737 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2738
2739 if (!killProcess) {
2740 return;
2741 }
2742
2743 // Determine if the process(es) for this task should be killed.
2744 final String pkg = component.getPackageName();
2745 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2746 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2747 for (int i = 0; i < pmap.size(); i++) {
2748
2749 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2750 for (int j = 0; j < uids.size(); j++) {
2751 ProcessRecord proc = uids.valueAt(j);
2752 if (proc.userId != tr.userId) {
2753 // Don't kill process for a different user.
2754 continue;
2755 }
2756 if (proc == mService.mHomeProcess) {
2757 // Don't kill the home process along with tasks from the same package.
2758 continue;
2759 }
2760 if (!proc.pkgList.containsKey(pkg)) {
2761 // Don't kill process that is not associated with this task.
2762 continue;
2763 }
2764
2765 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002766 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002767 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2768 // Don't kill process(es) that has an activity in a different task that is
2769 // also in recents.
2770 return;
2771 }
2772 }
2773
2774 if (proc.foregroundServices) {
2775 // Don't kill process(es) with foreground service.
2776 return;
2777 }
2778
2779 // Add process to kill list.
2780 procsToKill.add(proc);
2781 }
2782 }
2783
2784 // Kill the running processes.
2785 for (int i = 0; i < procsToKill.size(); i++) {
2786 ProcessRecord pr = procsToKill.get(i);
2787 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2788 && pr.curReceivers.isEmpty()) {
2789 pr.kill("remove task", true);
2790 } else {
2791 // We delay killing processes that are not in the background or running a receiver.
2792 pr.waitingToKill = "remove task";
2793 }
2794 }
2795 }
2796
Craig Mautner4a1cb222013-12-04 16:14:06 -08002797 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002798 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002799 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2800 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002801 break;
2802 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002803 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002804 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002805 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002806 }
2807
Chong Zhang5dcb2752015-08-18 13:50:26 -07002808 /**
2809 * Restores a recent task to a stack
2810 * @param task The recent task to be restored.
2811 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002812 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2813 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002814 * @return true if the task has been restored successfully.
2815 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002816 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002817 if (!StackId.isStaticStack(stackId)) {
2818 // If stack is not static (or stack id is invalid) - use the default one.
2819 // This means that tasks that were on external displays will be restored on the
2820 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002821 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002822 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002823 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2824 // Put it in the fullscreen stack.
2825 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002826 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002827
Andrii Kulian02b7a832016-10-06 23:11:56 -07002828 final ActivityStack currentStack = task.getStack();
2829 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002830 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002831 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002832 // Nothing else to do since it is already restored in the right stack.
2833 return true;
2834 }
2835 // Remove current stack association, so we can re-associate the task with the
2836 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002837 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002838 }
2839
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002840 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002841 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002842
2843 if (stack == null) {
2844 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002845 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2846 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002847 return false;
2848 }
2849
Wale Ogunwale72919d22016-12-08 18:58:50 -08002850 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002851 // TODO: move call for creation here and other place into Stack.addTask()
2852 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002853 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2854 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002855 final ArrayList<ActivityRecord> activities = task.mActivities;
2856 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002857 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002858 }
2859 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002860 }
2861
Wale Ogunwale040b4702015-08-06 18:10:50 -07002862 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002863 * Move stack with all its existing content to specified display.
2864 * @param stackId Id of stack to move.
2865 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002866 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002867 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002868 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002869 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002870 if (activityDisplay == null) {
2871 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2872 + displayId);
2873 }
2874 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2875 if (activityContainer != null) {
2876 if (activityContainer.isAttachedLocked()) {
2877 if (activityContainer.getDisplayId() == displayId) {
2878 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2879 + " to its current displayId=" + displayId);
2880 }
2881
Andrii Kulian250d6532017-02-08 23:30:45 -08002882 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002883 } else {
2884 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2885 + stackId + " is not attached to any display.");
2886 }
2887 } else {
2888 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2889 + stackId);
2890 }
Andrii Kulian839def92016-11-02 10:58:58 -07002891 // TODO(multi-display): resize stacks properly if moved from split-screen.
2892 }
2893
2894 /**
Winson Chung74666102017-02-22 17:49:24 -08002895 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2896 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002897 */
Winson Chung74666102017-02-22 17:49:24 -08002898 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002899 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002900
Winson Chung74666102017-02-22 17:49:24 -08002901 // Check that we aren't reparenting to the same stack that the task is already in
2902 if (prevStack != null && prevStack.mStackId == stackId) {
2903 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2904 + " already in stackId=" + stackId);
2905 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002906 }
2907
Winson Chung74666102017-02-22 17:49:24 -08002908 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2909 // support
2910 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2911 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2912 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002913 }
2914
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002915 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2916 // multi-display.
2917 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2918 // TODO: Check ActivityView after fixing b/35349678.
2919 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2920 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2921 + " reparent task=" + task + " to stackId=" + stackId);
2922 }
2923
Winson Chung74666102017-02-22 17:49:24 -08002924 // Ensure that we aren't trying to move into a freeform stack without freeform
2925 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002926 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002927 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2928 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002929 }
2930
Winson Chung74666102017-02-22 17:49:24 -08002931 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2932 // used for split-screen mode and will cause things like the docked divider to show up. We
2933 // instead leave the task in its current stack or move it to the fullscreen stack if it
2934 // isn't currently in a stack.
2935 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2936 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2937 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2938 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002939 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002940
Winson Chung74666102017-02-22 17:49:24 -08002941 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2942 // example, a docked stack is created which will lead to the stack we are moving from being
2943 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002944 try {
Winson Chung74666102017-02-22 17:49:24 -08002945 task.mTemporarilyUnresizable = true;
2946 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002947 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002948 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002949 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002950 }
2951
Winson Chung08f81892017-03-02 15:40:51 -08002952 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002953 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2954 if (stack == null) {
2955 throw new IllegalArgumentException(
2956 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2957 }
2958
2959 final ActivityRecord r = stack.topRunningActivityLocked();
2960 if (r == null) {
2961 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2962 + " in stack=" + stack);
2963 return false;
2964 }
2965
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002966 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002967 Slog.w(TAG,
2968 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002969 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002970 return false;
2971 }
2972
Winson Chung3a682872017-04-10 14:38:05 -07002973 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002974 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002975 return true;
2976 }
2977
Winson Chung8bca9e42017-04-16 15:59:43 -07002978 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002979 boolean moveHomeStackToFront, String reason) {
2980
Wale Ogunwale480dca02016-02-06 13:58:29 -08002981 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002982
Bryce Lee04ab3462017-04-10 15:06:33 -07002983 // This will clear the pinned stack by moving an existing task to the full screen stack,
2984 // ensuring only one task is present.
2985 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
2986
Wale Ogunwale1666e312016-12-16 11:27:18 -08002987 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002988 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002989
Wale Ogunwale480dca02016-02-06 13:58:29 -08002990 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002991 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002992
Andrii Kulian02b7a832016-10-06 23:11:56 -07002993 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002994 // An activity can't be pinned and visible behind at the same time. Go ahead and
2995 // release it from been visible behind before pinning.
2996 requestVisibleBehindLocked(r, false);
2997 }
2998
Wale Ogunwale480dca02016-02-06 13:58:29 -08002999 // Resize the pinned stack to match the current size of the task the activity we are
3000 // going to be moving is currently contained in. We do this to have the right starting
3001 // animation bounds for the pinned stack to the desired bounds the caller wants.
3002 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
3003 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003004 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003005
3006 if (task.mActivities.size() == 1) {
3007 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003008 // the stack without re-parenting the activity in a different task. We don't
3009 // move the home stack forward if we are currently entering picture-in-picture
3010 // while pausing because that changes the focused stack and may prevent the new
3011 // starting activity from resuming.
3012 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08003013 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003014 // Move the home stack forward if the task we just moved to the pinned stack
3015 // was launched from home so home should be visible behind it.
3016 moveHomeStackToFront(reason);
3017 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003018 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003019 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003020 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003021 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003022 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003023 // reveal/leave the other activities in their original task.
3024
3025 // Currently, we don't support reparenting activities across tasks in two different
3026 // stacks, so instead, just create a new task in the same stack, reparent the
3027 // activity into that task, and then reparent the whole task to the new stack. This
3028 // ensures that all the necessary work to migrate states in the old and new stacks
3029 // is also done.
3030 final TaskRecord newTask = task.getStack().createTaskRecord(
3031 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
3032 r.mActivityType);
3033 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3034
Winson Chung5af42fc2017-03-24 17:11:33 -07003035 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003036 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003037 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003038 }
Winson Chungc2baac02017-01-11 13:34:47 -08003039
3040 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3041 // to the pinned stack
3042 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003043 } finally {
3044 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003045 }
3046
Winson Chunge7ba6862017-05-24 12:13:33 -07003047 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3048 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3049 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003050 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3051
Winson Chunge7ba6862017-05-24 12:13:33 -07003052 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3053 true /* fromFullscreen */);
3054
3055 // Update the visibility of all activities after the they have been reparented to the new
3056 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3057 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3058 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003059 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003060 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003061
Winson Chungb5026902017-05-03 12:45:13 -07003062 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName,
3063 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003064 }
3065
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003066 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003067 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3068 if (r == null || !r.isFocusable()) {
3069 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3070 "moveActivityStackToFront: unfocusable r=" + r);
3071 return false;
3072 }
3073
Bryce Leeaf691c02017-03-20 14:20:22 -07003074 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003075 final ActivityStack stack = r.getStack();
3076 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003077 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3078 + r + " task=" + task);
3079 return false;
3080 }
3081
Chong Zhang6cda19c2016-06-14 19:07:56 -07003082 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3083 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3084 "moveActivityStackToFront: already on top, r=" + r);
3085 return false;
3086 }
3087
3088 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3089 "moveActivityStackToFront: r=" + r);
3090
3091 stack.moveToFront(reason, task);
3092 return true;
3093 }
3094
David Stevensc6b91c62017-02-08 14:23:58 -08003095 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003096 mTmpFindTaskResult.r = null;
3097 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003098 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003099 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003100 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3101 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003102 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3103 final ActivityStack stack = stacks.get(stackNdx);
Winson Chung91e5c882017-04-21 14:44:38 -07003104 if (!checkActivityBelongsInStack(r, stack)) {
3105 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3106 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003107 continue;
3108 }
3109 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003110 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3111 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003112 continue;
3113 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003114 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003115 // It is possible to have tasks in multiple stacks with the same root affinity, so
3116 // we should keep looking after finding an affinity match to see if there is a
3117 // better match in another stack. Also, task affinity isn't a good enough reason
3118 // to target a display which isn't the source of the intent, so skip any affinity
3119 // matches not on the specified display.
3120 if (mTmpFindTaskResult.r != null) {
3121 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3122 return mTmpFindTaskResult.r;
3123 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003124 // Note: since the traversing through the stacks is top down, the floating
3125 // tasks should always have lower priority than any affinity-matching tasks
3126 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003127 affinityMatch = mTmpFindTaskResult.r;
3128 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003129 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003130 }
3131 }
Winson Chung5b895b72017-05-01 13:46:25 -07003132
David Stevensc6b91c62017-02-08 14:23:58 -08003133 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3134 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003135 }
3136
Winson Chung91e5c882017-04-21 14:44:38 -07003137 /**
3138 * Checks that for the given activity {@param r}, its activity type matches the {@param stack}
3139 * type.
3140 */
3141 private boolean checkActivityBelongsInStack(ActivityRecord r, ActivityStack stack) {
3142 if (r.isHomeActivity()) {
3143 return stack.isHomeStack();
3144 } else if (r.isRecentsActivity()) {
3145 return stack.isRecentsStack();
3146 } else if (r.isAssistantActivity()) {
3147 return stack.isAssistantStack();
3148 }
3149 return true;
3150 }
3151
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003152 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3153 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003154 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3155 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003156 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003157 final ActivityRecord ar = stacks.get(stackNdx)
3158 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003159 if (ar != null) {
3160 return ar;
3161 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003162 }
3163 }
3164 return null;
3165 }
3166
Craig Mautner8d341ef2013-03-26 09:03:27 -07003167 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003168 scheduleSleepTimeout();
3169 if (!mGoingToSleep.isHeld()) {
3170 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003171 if (mLaunchingActivity.isHeld()) {
3172 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3173 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003174 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003175 mLaunchingActivity.release();
3176 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003177 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003178 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003179 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003180 }
3181
3182 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003183 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003184
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003185 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003186 final long endTime = System.currentTimeMillis() + timeout;
3187 while (true) {
3188 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003189 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3190 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003191 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3192 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3193 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003194 }
3195 if (cantShutdown) {
3196 long timeRemaining = endTime - System.currentTimeMillis();
3197 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003198 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003199 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003200 } catch (InterruptedException e) {
3201 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003202 } else {
3203 Slog.w(TAG, "Activity manager shutdown timed out");
3204 timedout = true;
3205 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003206 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003207 } else {
3208 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003209 }
3210 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003211
3212 // Force checkReadyForSleep to complete.
3213 mSleepTimeout = true;
3214 checkReadyForSleepLocked();
3215
Craig Mautner8d341ef2013-03-26 09:03:27 -07003216 return timedout;
3217 }
3218
3219 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003220 removeSleepTimeouts();
3221 if (mGoingToSleep.isHeld()) {
3222 mGoingToSleep.release();
3223 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003224 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3225 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003226 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3227 final ActivityStack stack = stacks.get(stackNdx);
3228 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003229 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003230 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003231 }
Craig Mautner5314a402013-09-26 12:40:16 -07003232 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003233 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003234 mGoingToSleepActivities.clear();
3235 }
3236
3237 void activitySleptLocked(ActivityRecord r) {
3238 mGoingToSleepActivities.remove(r);
3239 checkReadyForSleepLocked();
3240 }
3241
3242 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003243 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003244 // Do not care.
3245 return;
3246 }
3247
3248 if (!mSleepTimeout) {
3249 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003250 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3251 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003252 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3253 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3254 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003255 }
3256
3257 if (mStoppingActivities.size() > 0) {
3258 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003259 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003260 + mStoppingActivities.size() + " activities");
3261 scheduleIdleLocked();
3262 dontSleep = true;
3263 }
3264
3265 if (mGoingToSleepActivities.size() > 0) {
3266 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003267 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003268 + mGoingToSleepActivities.size() + " activities");
3269 dontSleep = true;
3270 }
3271
3272 if (dontSleep) {
3273 return;
3274 }
3275 }
3276
Wei Wang65c7a152016-06-02 18:51:22 -07003277 // Send launch end powerhint before going sleep
3278 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3279
Craig Mautnere0a38842013-12-16 16:14:02 -08003280 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3281 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003282 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3283 stacks.get(stackNdx).goToSleep();
3284 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003285 }
3286
3287 removeSleepTimeouts();
3288
3289 if (mGoingToSleep.isHeld()) {
3290 mGoingToSleep.release();
3291 }
3292 if (mService.mShuttingDown) {
3293 mService.notifyAll();
3294 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003295 }
3296
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003297 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003298 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003299 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003300 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003301 }
3302 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003303 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003304 mWindowManager.executeAppTransition();
3305 return true;
3306 }
3307 return false;
3308 }
3309
Craig Mautner8d341ef2013-03-26 09:03:27 -07003310 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003311 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3312 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003313 int stackNdx = stacks.size() - 1;
3314 while (stackNdx >= 0) {
3315 stacks.get(stackNdx).handleAppCrashLocked(app);
3316 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003317 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003318 }
3319 }
3320
Jose Lima4b6c6692014-08-12 17:41:12 -07003321 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003322 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003323 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003324 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3325 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003326 return false;
3327 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003328
3329 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3330 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3331 + " visible=" + visible + " stackId=" + stack.mStackId
3332 + " can't contain visible behind activities");
3333 return false;
3334 }
3335
Jose Lima4b6c6692014-08-12 17:41:12 -07003336 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003337 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3338 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003339
3340 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003341 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003342 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003343 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003344 return true;
3345 }
3346
3347 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003348 if (visible && top.fullscreen) {
3349 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003350 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3351 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3352 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3353 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003354 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003355 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3356 // Only the activity set as currently visible behind should actively reset its
3357 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003358 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3359 "requestVisibleBehind: returning visible=" + visible
3360 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3361 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003362 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003363 }
3364
Jose Lima4b6c6692014-08-12 17:41:12 -07003365 stack.setVisibleBehindActivity(visible ? r : null);
3366 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003367 // If there is a translucent home activity, we need to force it stop being translucent,
3368 // because we can't depend on the application to necessarily perform that operation.
3369 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003370 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003371 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003372 mService.convertFromTranslucent(next.appToken);
3373 }
3374 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003375 if (top.app != null && top.app.thread != null) {
3376 // Notify the top app of the change.
3377 try {
3378 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3379 } catch (RemoteException e) {
3380 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003381 }
3382 return true;
3383 }
3384
Craig Mautnerbb742462014-07-07 15:28:55 -07003385 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003386 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003387 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003388 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003389
3390 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003391 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003392 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003393 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003394 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003395
3396 // When launching tasks behind, update the last active time of the top task after the new
3397 // task has been shown briefly
3398 final ActivityRecord top = stack.topActivity();
3399 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003400 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003401 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003402 }
3403
3404 void scheduleLaunchTaskBehindComplete(IBinder token) {
3405 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3406 }
3407
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003408 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3409 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003410 mKeyguardController.beginActivityVisibilityUpdate();
3411 try {
3412 // First the front stacks. In case any are not fullscreen and are in front of home.
3413 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3414 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3415 final int topStackNdx = stacks.size() - 1;
3416 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3417 final ActivityStack stack = stacks.get(stackNdx);
3418 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3419 }
Craig Mautner580ea812013-04-25 12:58:38 -07003420 }
Jorim Jaggife762342016-10-13 14:33:27 +02003421 } finally {
3422 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003423 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003424 }
3425
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003426 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3427 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3428 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3429 final int topStackNdx = stacks.size() - 1;
3430 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3431 final ActivityStack stack = stacks.get(stackNdx);
3432 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3433 }
3434 }
3435 }
3436
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003437 void invalidateTaskLayers() {
3438 mTaskLayersChanged = true;
3439 }
3440
3441 void rankTaskLayersIfNeeded() {
3442 if (!mTaskLayersChanged) {
3443 return;
3444 }
3445 mTaskLayersChanged = false;
3446 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3447 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3448 int baseLayer = 0;
3449 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3450 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3451 }
3452 }
3453 }
3454
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003455 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3456 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3457 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3458 final int topStackNdx = stacks.size() - 1;
3459 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3460 final ActivityStack stack = stacks.get(stackNdx);
3461 stack.clearOtherAppTimeTrackers(except);
3462 }
3463 }
3464 }
3465
Craig Mautner8d341ef2013-03-26 09:03:27 -07003466 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003467 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3468 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003469 final int numStacks = stacks.size();
3470 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3471 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003472 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003473 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003474 }
3475 }
3476
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003477 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3478 // Examine all activities currently running in the process.
3479 TaskRecord firstTask = null;
3480 // Tasks is non-null only if two or more tasks are found.
3481 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003482 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3483 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003484 ActivityRecord r = app.activities.get(i);
3485 // First, if we find an activity that is in the process of being destroyed,
3486 // then we just aren't going to do anything for now; we want things to settle
3487 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003488 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003489 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003490 return;
3491 }
3492 // Don't consider any activies that are currently not in a state where they
3493 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003494 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3495 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003496 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003497 continue;
3498 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003499
3500 final TaskRecord task = r.getTask();
3501 if (task != null) {
3502 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003503 + " from " + r);
3504 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003505 firstTask = task;
3506 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003507 if (tasks == null) {
3508 tasks = new ArraySet<>();
3509 tasks.add(firstTask);
3510 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003511 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003512 }
3513 }
3514 }
3515 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003516 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003517 return;
3518 }
3519 // If we have activities in multiple tasks that are in a position to be destroyed,
3520 // let's iterate through the tasks and release the oldest one.
3521 final int numDisplays = mActivityDisplays.size();
3522 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3523 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3524 // Step through all stacks starting from behind, to hit the oldest things first.
3525 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3526 final ActivityStack stack = stacks.get(stackNdx);
3527 // Try to release activities in this stack; if we manage to, we are done.
3528 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3529 return;
3530 }
3531 }
3532 }
3533 }
3534
Amith Yamasani37a40c22015-06-17 13:25:42 -07003535 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003536 final int focusStackId = mFocusedStack.getStackId();
3537 // We dismiss the docked stack whenever we switch users.
3538 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003539 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3540 // also cause all tasks to be moved to the fullscreen stack at a position that is
3541 // appropriate.
3542 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003543
3544 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003545 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003546 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003547
Craig Mautner858d8a62013-04-23 17:08:34 -07003548 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003549 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3550 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003551 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003552 final ActivityStack stack = stacks.get(stackNdx);
3553 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003554 TaskRecord task = stack.topTask();
3555 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003556 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003557 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003558 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003559 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003560
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003561 ActivityStack stack = getStack(restoreStackId);
3562 if (stack == null) {
3563 stack = mHomeStack;
3564 }
3565 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003566 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003567 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003568 } else {
3569 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003570 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003571 }
Craig Mautner93529a42013-10-04 15:03:13 -07003572 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003573 }
3574
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003575 /** Checks whether the userid is a profile of the current user. */
3576 boolean isCurrentProfileLocked(int userId) {
3577 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003578 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003579 }
3580
Bryce Leeb7c9b802017-05-02 14:20:24 -07003581 /**
3582 * Returns whether a stopping activity is present that should be stopped after visible, rather
3583 * than idle.
3584 * @return {@code true} if such activity is present. {@code false} otherwise.
3585 */
3586 boolean isStoppingNoHistoryActivity() {
3587 // Activities that are marked as nohistory should be stopped immediately after the resumed
3588 // activity has become visible.
3589 for (ActivityRecord record : mStoppingActivities) {
3590 if (record.isNoHistory()) {
3591 return true;
3592 }
3593 }
3594
3595 return false;
3596 }
3597
Winson Chung4dabf232017-01-25 13:25:22 -08003598 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3599 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003600 ArrayList<ActivityRecord> stops = null;
3601
3602 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003603 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3604 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003605 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003606 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003607 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3608 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003609 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003610 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003611 if (s.finishing) {
3612 // If this activity is finishing, it is sitting on top of
3613 // everyone else but we now know it is no longer needed...
3614 // so get rid of it. Otherwise, we need to go through the
3615 // normal flow and hide it once we determine that it is
3616 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003617 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003618 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003619 }
3620 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003621 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003622 if (!processPausingActivities && s.state == PAUSING) {
3623 // Defer processing pausing activities in this iteration and reschedule
3624 // a delayed idle to reprocess it again
3625 removeTimeoutsForActivityLocked(idleActivity);
3626 scheduleIdleTimeoutLocked(idleActivity);
3627 continue;
3628 }
3629
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003630 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003631 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003632 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003633 }
3634 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003635 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003636 }
3637 }
3638
3639 return stops;
3640 }
3641
Craig Mautnercf910b02013-04-23 11:23:27 -07003642 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003643 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3644 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3645 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3646 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003647 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003648 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003649 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003650 if (r == null) Slog.e(TAG,
3651 "validateTop...: null top activity, stack=" + stack);
3652 else {
3653 final ActivityRecord pausing = stack.mPausingActivity;
3654 if (pausing != null && pausing == r) Slog.e(TAG,
3655 "validateTop...: top stack has pausing activity r=" + r
3656 + " state=" + state);
3657 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3658 "validateTop...: activity in front not resumed r=" + r
3659 + " state=" + state);
3660 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003661 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003662 final ActivityRecord resumed = stack.mResumedActivity;
3663 if (resumed != null && resumed == r) Slog.e(TAG,
3664 "validateTop...: back stack has resumed activity r=" + r
3665 + " state=" + state);
3666 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3667 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003668 }
3669 }
3670 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003671 }
3672
Craig Mautnere0570202015-05-13 13:06:11 -07003673 private String lockTaskModeToString() {
3674 switch (mLockTaskModeState) {
3675 case LOCK_TASK_MODE_LOCKED:
3676 return "LOCKED";
3677 case LOCK_TASK_MODE_PINNED:
3678 return "PINNED";
3679 case LOCK_TASK_MODE_NONE:
3680 return "NONE";
3681 default: return "unknown=" + mLockTaskModeState;
3682 }
3683 }
3684
Craig Mautner27084302013-03-25 08:05:25 -07003685 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003686 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003687 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003688 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003689 pw.print(prefix);
3690 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003691 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003692 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003693 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003694 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3695 if (packages.size() > 0) {
3696 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3697 for (int i = 0; i < packages.size(); ++i) {
3698 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3699 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3700 }
3701 }
Bryce Lee4a194382017-04-04 14:32:48 -07003702 if (!mWaitingForActivityVisible.isEmpty()) {
3703 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3704 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3705 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3706 }
3707 }
3708
Jorim Jaggi8d786932016-10-26 19:08:36 -07003709 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3710 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003711 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003712
Winson43d1f262016-06-14 16:05:55 -07003713 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003714 * Dump all connected displays' configurations.
3715 * @param prefix Prefix to apply to each line of the dump.
3716 */
3717 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3718 pw.print(prefix); pw.println("Display override configurations:");
3719 final int displayCount = mActivityDisplays.size();
3720 for (int i = 0; i < displayCount; i++) {
3721 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3722 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3723 pw.println(activityDisplay.getOverrideConfiguration());
3724 }
3725 }
3726
3727 /**
Winson43d1f262016-06-14 16:05:55 -07003728 * Dumps the activities matching the given {@param name} in the either the focused stack
3729 * or all visible stacks if {@param dumpVisibleStacks} is true.
3730 */
Winson Chung6998bc42017-02-28 17:07:05 -08003731 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3732 boolean dumpFocusedStackOnly) {
3733 if (dumpFocusedStackOnly) {
3734 return mFocusedStack.getDumpActivitiesLocked(name);
3735 } else {
Winson43d1f262016-06-14 16:05:55 -07003736 ArrayList<ActivityRecord> activities = new ArrayList<>();
3737 int numDisplays = mActivityDisplays.size();
3738 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3739 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3740 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3741 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003742 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003743 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003744 activities.addAll(stack.getDumpActivitiesLocked(name));
3745 }
3746 }
3747 }
3748 return activities;
Winson43d1f262016-06-14 16:05:55 -07003749 }
Craig Mautner20e72272013-04-01 13:45:53 -07003750 }
3751
Dianne Hackborn390517b2013-05-30 15:03:32 -07003752 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3753 boolean needSep, String prefix) {
3754 if (activity != null) {
3755 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3756 if (needSep) {
3757 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003758 }
3759 pw.print(prefix);
3760 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003761 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003762 }
3763 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003764 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003765 }
3766
Craig Mautner8d341ef2013-03-26 09:03:27 -07003767 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3768 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003769 boolean printed = false;
3770 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003771 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3772 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003773 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003774 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003775 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003776 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003777 final ActivityStack stack = stacks.get(stackNdx);
3778 StringBuilder stackHeader = new StringBuilder(128);
3779 stackHeader.append(" Stack #");
3780 stackHeader.append(stack.mStackId);
3781 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003782 stackHeader.append("\n");
3783 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3784 stackHeader.append("\n");
3785 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003786
3787 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3788 dumpClient, dumpPackage, needSep, stackHeader.toString());
3789 printed |= printedStackHeader;
3790 if (!printedStackHeader) {
3791 // Ensure we always dump the stack header even if there are no activities
3792 pw.println();
3793 pw.println(stackHeader);
3794 }
3795
Craig Mautner4a1cb222013-12-04 16:14:06 -08003796 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3797 !dumpAll, false, dumpPackage, true,
3798 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003799
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800 needSep = printed;
3801 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3802 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003803 if (pr) {
3804 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003805 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003806 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003807 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3808 " mResumedActivity: ");
3809 if (pr) {
3810 printed = true;
3811 needSep = false;
3812 }
3813 if (dumpAll) {
3814 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3815 " mLastPausedActivity: ");
3816 if (pr) {
3817 printed = true;
3818 needSep = true;
3819 }
3820 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3821 needSep, " mLastNoHistoryActivity: ");
3822 }
3823 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003824 }
3825 }
3826
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003827 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3828 false, dumpPackage, true, " Activities waiting to finish:", null);
3829 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3830 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003831 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3832 false, !dumpAll, false, dumpPackage, true,
3833 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003834 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3835 false, dumpPackage, true, " Activities waiting to sleep:", null);
3836 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3837 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003838
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003839 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003840 }
3841
Dianne Hackborn390517b2013-05-30 15:03:32 -07003842 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003843 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003844 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003845 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003846 String innerPrefix = null;
3847 String[] args = null;
3848 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003849 for (int i=list.size()-1; i>=0; i--) {
3850 final ActivityRecord r = list.get(i);
3851 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3852 continue;
3853 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003854 if (innerPrefix == null) {
3855 innerPrefix = prefix + " ";
3856 args = new String[0];
3857 }
3858 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003859 final boolean full = !brief && (complete || !r.isInHistory());
3860 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003861 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003862 needNL = false;
3863 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003864 if (header1 != null) {
3865 pw.println(header1);
3866 header1 = null;
3867 }
3868 if (header2 != null) {
3869 pw.println(header2);
3870 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003871 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003872 if (lastTask != r.getTask()) {
3873 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003874 pw.print(prefix);
3875 pw.print(full ? "* " : " ");
3876 pw.println(lastTask);
3877 if (full) {
3878 lastTask.dump(pw, prefix + " ");
3879 } else if (complete) {
3880 // Complete + brief == give a summary. Isn't that obvious?!?
3881 if (lastTask.intent != null) {
3882 pw.print(prefix); pw.print(" ");
3883 pw.println(lastTask.intent.toInsecureStringWithClip());
3884 }
3885 }
3886 }
3887 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3888 pw.print(" #"); pw.print(i); pw.print(": ");
3889 pw.println(r);
3890 if (full) {
3891 r.dump(pw, innerPrefix);
3892 } else if (complete) {
3893 // Complete + brief == give a summary. Isn't that obvious?!?
3894 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3895 if (r.app != null) {
3896 pw.print(innerPrefix); pw.println(r.app);
3897 }
3898 }
3899 if (client && r.app != null && r.app.thread != null) {
3900 // flush anything that is already in the PrintWriter since the thread is going
3901 // to write to the file descriptor directly
3902 pw.flush();
3903 try {
3904 TransferPipe tp = new TransferPipe();
3905 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003906 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003907 // Short timeout, since blocking here can
3908 // deadlock with the application.
3909 tp.go(fd, 2000);
3910 } finally {
3911 tp.kill();
3912 }
3913 } catch (IOException e) {
3914 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3915 } catch (RemoteException e) {
3916 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3917 }
3918 needNL = true;
3919 }
3920 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003921 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003922 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003923
Craig Mautnerf3333272013-04-22 10:55:53 -07003924 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003925 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3926 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003927 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3928 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003929 }
3930
3931 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003932 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003933 }
3934
3935 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003936 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3937 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003938 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3939 }
3940
Craig Mautner05d29032013-05-03 13:40:13 -07003941 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003942 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3943 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3944 }
Craig Mautner05d29032013-05-03 13:40:13 -07003945 }
3946
Craig Mautner0eea92c2013-05-16 13:35:39 -07003947 void removeSleepTimeouts() {
3948 mSleepTimeout = false;
3949 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3950 }
3951
3952 final void scheduleSleepTimeout() {
3953 removeSleepTimeouts();
3954 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3955 }
3956
Craig Mautner4a1cb222013-12-04 16:14:06 -08003957 @Override
3958 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003959 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003960 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3961 }
3962
3963 @Override
3964 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003965 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003966 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3967 }
3968
3969 @Override
3970 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003971 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003972 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3973 }
3974
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003975 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003976 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003977 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003978 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003979 }
3980
Andrii Kulianca007a62016-11-22 16:33:28 -08003981 /** Check if display with specified id is added to the list. */
3982 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003983 return getActivityDisplayOrCreateLocked(displayId) != null;
3984 }
3985
3986 /**
3987 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3988 * corresponding record in display manager.
3989 */
3990 private ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
3991 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3992 if (activityDisplay != null) {
3993 return activityDisplay;
3994 }
3995 if (mDisplayManager == null) {
3996 // The system isn't fully initialized yet.
3997 return null;
3998 }
3999 final Display display = mDisplayManager.getDisplay(displayId);
4000 if (display == null) {
4001 // The display is not registered in DisplayManager.
4002 return null;
4003 }
4004 // The display hasn't been added to ActivityManager yet, create a new record now.
4005 activityDisplay = new ActivityDisplay(displayId);
4006 if (activityDisplay.mDisplay == null) {
4007 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4008 return null;
4009 }
4010 mActivityDisplays.put(displayId, activityDisplay);
4011 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4012 mWindowManager.onDisplayAdded(displayId);
4013 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004014 }
4015
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004016 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004017 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004018 mService.mContext.getResources().getDimensionPixelSize(
4019 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004020 }
4021
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004022 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004023 if (displayId == DEFAULT_DISPLAY) {
4024 throw new IllegalArgumentException("Can't remove the primary display.");
4025 }
4026
Craig Mautner4a1cb222013-12-04 16:14:06 -08004027 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004028 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4029 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08004030 final boolean destroyContentOnRemoval
4031 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08004032 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
4033 while (!stacks.isEmpty()) {
4034 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08004035 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004036 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
4037 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004038 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08004039 } else {
4040 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4041 // a valid launch stack for all activities. This way the task history from
4042 // external display will be preserved on primary after move.
4043 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004044 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004046 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07004047 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 }
4049 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 }
4051
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004052 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004054 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4055 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004056 // TODO: Update the bounds.
4057 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004058 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004060 }
4061
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004062 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004063 final int displayId = stack.mDisplayId;
4064 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004065 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004066 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004067 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004068 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004069 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004070 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Andrii Kulian62e6f252017-05-30 22:46:53 -07004071 // A stack might be not attached to a display.
Winson529c8e42016-05-17 11:08:40 -07004072 info.position = display != null
4073 ? display.mStacks.indexOf(stack)
4074 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004075
4076 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4077 final int numTasks = tasks.size();
4078 int[] taskIds = new int[numTasks];
4079 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004080 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004081 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004082 for (int i = 0; i < numTasks; ++i) {
4083 final TaskRecord task = tasks.get(i);
4084 taskIds[i] = task.taskId;
4085 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4086 : task.realActivity != null ? task.realActivity.flattenToString()
4087 : task.getTopActivity() != null ? task.getTopActivity().packageName
4088 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004089 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004090 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004091 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004092 }
4093 info.taskIds = taskIds;
4094 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004095 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004096 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004097
4098 final ActivityRecord top = stack.topRunningActivityLocked();
4099 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 return info;
4101 }
4102
4103 StackInfo getStackInfoLocked(int stackId) {
4104 ActivityStack stack = getStack(stackId);
4105 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004106 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004107 }
4108 return null;
4109 }
4110
4111 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004112 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004113 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4114 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004115 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004116 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004117 }
4118 }
4119 return list;
4120 }
4121
Craig Mautner15df08a2015-04-01 12:17:18 -07004122 TaskRecord getLockedTaskLocked() {
4123 final int top = mLockTaskModeTasks.size() - 1;
4124 if (top >= 0) {
4125 return mLockTaskModeTasks.get(top);
4126 }
4127 return null;
4128 }
4129
4130 boolean isLockedTask(TaskRecord task) {
4131 return mLockTaskModeTasks.contains(task);
4132 }
4133
4134 boolean isLastLockedTask(TaskRecord task) {
4135 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4136 }
4137
4138 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004139 if (!mLockTaskModeTasks.remove(task)) {
4140 return;
4141 }
4142 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4143 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004144 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004145 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4146 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004147 final Message lockTaskMsg = Message.obtain();
4148 lockTaskMsg.arg1 = task.userId;
4149 lockTaskMsg.what = LOCK_TASK_END_MSG;
4150 mHandler.sendMessage(lockTaskMsg);
4151 }
4152 }
4153
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004154 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4155 int preferredDisplayId, int actualStackId) {
4156 handleNonResizableTaskIfNeeded(task, preferredStackId, preferredDisplayId, actualStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07004157 false /* forceNonResizable */);
4158 }
4159
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004160 private void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4161 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4162 final boolean isSecondaryDisplayPreferred =
4163 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY)
4164 || StackId.isDynamicStack(preferredStackId);
4165 if (((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
4166 && !isSecondaryDisplayPreferred) || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004167 return;
4168 }
4169
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004170 // Handle incorrect launch/move to secondary display if needed.
4171 final boolean launchOnSecondaryDisplayFailed;
4172 if (isSecondaryDisplayPreferred) {
4173 final int actualDisplayId = task.getStack().mDisplayId;
4174 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4175 // The task landed on an inappropriate display somehow, move it to the default
4176 // display.
4177 // TODO(multi-display): Find proper stack for the task on the default display.
4178 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4179 true /* toTop */);
4180 launchOnSecondaryDisplayFailed = true;
4181 } else {
4182 // The task might have landed on a display different from requested.
4183 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4184 || (preferredDisplayId != INVALID_DISPLAY
4185 && preferredDisplayId != actualDisplayId);
4186 }
4187 } else {
4188 // The task wasn't requested to be on a secondary display.
4189 launchOnSecondaryDisplayFailed = false;
4190 }
4191
Jorim Jaggicd13d332016-04-27 15:40:20 -07004192 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004193 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4194 if (launchOnSecondaryDisplayFailed) {
4195 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4196 // display with config different from global config.
4197 mService.mTaskChangeNotificationController
4198 .notifyActivityLaunchOnSecondaryDisplayFailed();
4199 } else {
4200 // Display a warning toast that we tried to put a non-dockable task in the docked
4201 // stack.
4202 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4203 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004204
Andrii Kulianc27916642016-04-12 17:59:27 -07004205 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4206 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004207 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004208 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004209 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004210 final String packageName = topActivity.appInfo.packageName;
4211 final int reason = isSecondaryDisplayPreferred
4212 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4213 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004214 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004215 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004216 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004217 }
4218
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004219 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004220 if (mLockTaskNotify != null) {
4221 mLockTaskNotify.showToast(mLockTaskModeState);
4222 }
Jason Monka8f569c2014-07-07 12:15:21 -04004223 }
4224
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004225 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4226 if (mLockTaskModeTasks.contains(task)) {
4227 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4228 }
4229 }
4230
Craig Mautner432f64e2015-05-20 14:59:57 -07004231 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4232 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004233 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004234 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004235 final TaskRecord lockedTask = getLockedTaskLocked();
4236 if (lockedTask != null) {
4237 removeLockedTaskLocked(lockedTask);
4238 if (!mLockTaskModeTasks.isEmpty()) {
4239 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004240 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4241 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004242 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004243 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004244 return;
4245 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004246 }
Craig Mautnere0570202015-05-13 13:06:11 -07004247 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4248 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004249 return;
4250 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004251
4252 // Should have already been checked, but do it again.
4253 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004254 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4255 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004256 return;
4257 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004258 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004259 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004260 return;
4261 }
4262
4263 if (mLockTaskModeTasks.isEmpty()) {
4264 // First locktask.
4265 final Message lockTaskMsg = Message.obtain();
4266 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4267 lockTaskMsg.arg1 = task.userId;
4268 lockTaskMsg.what = LOCK_TASK_START_MSG;
4269 lockTaskMsg.arg2 = lockTaskModeState;
4270 mHandler.sendMessage(lockTaskMsg);
4271 }
4272 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004273 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4274 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004275 mLockTaskModeTasks.remove(task);
4276 mLockTaskModeTasks.add(task);
4277
4278 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004279 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004280 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004281
4282 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004283 findTaskToMoveToFrontLocked(task, 0, null, reason,
4284 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004285 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004286 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004287 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004288 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
4289 task.getStackId(), true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004290 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004291 }
4292
4293 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004294 return isLockTaskModeViolation(task, false);
4295 }
4296
4297 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4298 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004299 return false;
4300 }
4301 final int lockTaskAuth = task.mLockTaskAuth;
4302 switch (lockTaskAuth) {
4303 case LOCK_TASK_AUTH_DONT_LOCK:
4304 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004305 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004306 case LOCK_TASK_AUTH_LAUNCHABLE:
4307 case LOCK_TASK_AUTH_WHITELISTED:
4308 return false;
4309 case LOCK_TASK_AUTH_PINNABLE:
4310 // Pinnable tasks can't be launched on top of locktask tasks.
4311 return !mLockTaskModeTasks.isEmpty();
4312 default:
4313 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4314 return true;
4315 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004316 }
4317
Craig Mautner15df08a2015-04-01 12:17:18 -07004318 void onLockTaskPackagesUpdatedLocked() {
4319 boolean didSomething = false;
4320 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4321 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004322 final boolean wasWhitelisted =
4323 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4324 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004325 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004326 final boolean isWhitelisted =
4327 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4328 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4329 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004330 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004331 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4332 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004333 removeLockedTaskLocked(lockedTask);
4334 lockedTask.performClearTaskLocked();
4335 didSomething = true;
4336 }
4337 }
4338 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4339 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4340 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4341 final ActivityStack stack = stacks.get(stackNdx);
4342 stack.onLockTaskPackagesUpdatedLocked();
4343 }
4344 }
Craig Mautnere0570202015-05-13 13:06:11 -07004345 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004346 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004347 if (mLockTaskModeTasks.isEmpty() && task != null
4348 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4349 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004350 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4351 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4352 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4353 false);
4354 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004355 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004356 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004357 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004358 }
4359 }
4360
Benjamin Franz43261142015-02-11 15:59:44 +00004361 int getLockTaskModeState() {
4362 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004363 }
4364
Jorim Jaggife89d122015-12-22 16:28:44 +01004365 void activityRelaunchedLocked(IBinder token) {
4366 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004367 if (mService.isSleepingOrShuttingDownLocked()) {
4368 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4369 if (r != null) {
4370 r.setSleeping(true, true);
4371 }
4372 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004373 }
4374
4375 void activityRelaunchingLocked(ActivityRecord r) {
4376 mWindowManager.notifyAppRelaunching(r.appToken);
4377 }
4378
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004379 void logStackState() {
4380 mActivityMetricsLogger.logWindowState();
4381 }
4382
Winson Chung5af42fc2017-03-24 17:11:33 -07004383 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004384 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4385 // end, then ensure that we defer all in between multi-window mode changes
4386 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4387 return;
4388 }
4389
Wale Ogunwale22e25262016-02-01 10:32:02 -08004390 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4391 final ActivityRecord r = task.mActivities.get(i);
4392 if (r.app != null && r.app.thread != null) {
4393 mMultiWindowModeChangedActivities.add(r);
4394 }
4395 }
4396
4397 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4398 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4399 }
4400 }
4401
Winson Chung5af42fc2017-03-24 17:11:33 -07004402 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004403 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004404 if (prevStack == null || prevStack == stack
4405 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4406 return;
4407 }
4408
Winson Chung5af42fc2017-03-24 17:11:33 -07004409 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4410 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004411
Winson Chung5af42fc2017-03-24 17:11:33 -07004412 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4413 boolean immediate) {
4414
4415 if (immediate) {
4416 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4417 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4418 final ActivityRecord r = task.mActivities.get(i);
4419 if (r.app != null && r.app.thread != null) {
4420 r.updatePictureInPictureMode(targetStackBounds);
4421 }
4422 }
4423 } else {
4424 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4425 final ActivityRecord r = task.mActivities.get(i);
4426 if (r.app != null && r.app.thread != null) {
4427 mPipModeChangedActivities.add(r);
4428 }
4429 }
4430 mPipModeChangedTargetStackBounds = targetStackBounds;
4431
4432 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4433 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4434 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004435 }
4436 }
4437
Tony Mak853304c2016-04-18 15:17:41 +01004438 void setDockedStackMinimized(boolean minimized) {
4439 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004440 }
4441
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004442 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004443
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004444 public ActivityStackSupervisorHandler(Looper looper) {
4445 super(looper);
4446 }
4447
Winson Chung4dabf232017-01-25 13:25:22 -08004448 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004449 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004450 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4451 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004452 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004453 }
4454
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004455 @Override
4456 public void handleMessage(Message msg) {
4457 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004458 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4459 synchronized (mService) {
4460 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4461 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004462 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004463 }
4464 }
4465 } break;
4466 case REPORT_PIP_MODE_CHANGED_MSG: {
4467 synchronized (mService) {
4468 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4469 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004470 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004471 }
4472 }
4473 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004474 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004475 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4476 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004477 // We don't at this point know if the activity is fullscreen,
4478 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004479 activityIdleInternal((ActivityRecord) msg.obj,
4480 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004481 } break;
4482 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004483 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004484 activityIdleInternal((ActivityRecord) msg.obj,
4485 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004486 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004487 case RESUME_TOP_ACTIVITY_MSG: {
4488 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004489 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004490 }
4491 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004492 case SLEEP_TIMEOUT_MSG: {
4493 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004494 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004495 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4496 mSleepTimeout = true;
4497 checkReadyForSleepLocked();
4498 }
4499 }
4500 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004501 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004502 synchronized (mService) {
4503 if (mLaunchingActivity.isHeld()) {
4504 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4505 if (VALIDATE_WAKE_LOCK_CALLER
4506 && Binder.getCallingUid() != Process.myUid()) {
4507 throw new IllegalStateException("Calling must be system uid");
4508 }
4509 mLaunchingActivity.release();
4510 }
4511 }
4512 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004513 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004514 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004515 } break;
4516 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004517 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004518 } break;
4519 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004520 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004521 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004522 case CONTAINER_CALLBACK_VISIBILITY: {
4523 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004524 final IActivityContainerCallback callback = container.mCallback;
4525 if (callback != null) {
4526 try {
4527 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4528 } catch (RemoteException e) {
4529 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004530 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004531 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004532 case LOCK_TASK_START_MSG: {
4533 // When lock task starts, we disable the status bars.
4534 try {
Jason Monk62515be2014-05-21 16:06:19 -04004535 if (mLockTaskNotify == null) {
4536 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004537 }
Jason Monk62515be2014-05-21 16:06:19 -04004538 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004539 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004540 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004541 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004542 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004543 flags = StatusBarManager.DISABLE_MASK
4544 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004545 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004546 flags = StatusBarManager.DISABLE_MASK
4547 & (~StatusBarManager.DISABLE_BACK)
4548 & (~StatusBarManager.DISABLE_HOME)
4549 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004550 }
4551 getStatusBarService().disable(flags, mToken,
4552 mService.mContext.getPackageName());
4553 }
4554 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004555 if (getDevicePolicyManager() != null) {
4556 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004557 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004558 }
justinzhang5286d3f2014-05-12 17:06:01 -04004559 } catch (RemoteException ex) {
4560 throw new RuntimeException(ex);
4561 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004562 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004563 case LOCK_TASK_END_MSG: {
4564 // When lock task ends, we enable the status bars.
4565 try {
Jason Monk62515be2014-05-21 16:06:19 -04004566 if (getStatusBarService() != null) {
4567 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4568 mService.mContext.getPackageName());
4569 }
4570 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004571 if (getDevicePolicyManager() != null) {
4572 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4573 msg.arg1);
4574 }
Jason Monk62515be2014-05-21 16:06:19 -04004575 if (mLockTaskNotify == null) {
4576 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4577 }
4578 mLockTaskNotify.show(false);
4579 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004580 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004581 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004582 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004583 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004584 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004585 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004586 new LockPatternUtils(mService.mContext)
4587 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004588 }
4589 } catch (SettingNotFoundException e) {
4590 // No setting, don't lock.
4591 }
justinzhang5286d3f2014-05-12 17:06:01 -04004592 } catch (RemoteException ex) {
4593 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004594 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004595 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004596 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004597 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004598 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4599 if (mLockTaskNotify == null) {
4600 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4601 }
4602 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4603 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004604 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4605 final ActivityContainer container = (ActivityContainer) msg.obj;
4606 final IActivityContainerCallback callback = container.mCallback;
4607 if (callback != null) {
4608 try {
4609 callback.onAllActivitiesComplete(container.asBinder());
4610 } catch (RemoteException e) {
4611 }
4612 }
4613 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004614 case LAUNCH_TASK_BEHIND_COMPLETE: {
4615 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004616 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004617 if (r != null) {
4618 handleLaunchTaskBehindCompleteLocked(r);
4619 }
4620 }
4621 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004622
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004623 }
4624 }
4625 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004626
Ying Wangb081a592014-04-22 15:20:16 -07004627 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004628 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4629 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004630 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004631 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004632 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004633 ActivityRecord mParentActivity = null;
4634 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004635
Craig Mautnere3a00d72014-04-16 08:31:19 -07004636 boolean mVisible = true;
4637
Craig Mautner4a1cb222013-12-04 16:14:06 -08004638 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004639 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004640
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004641 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4642 final static int CONTAINER_STATE_NO_SURFACE = 1;
4643 final static int CONTAINER_STATE_FINISHING = 2;
4644 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4645
Wale Ogunwale1666e312016-12-16 11:27:18 -08004646 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004647 synchronized (mService) {
4648 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004649 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004650 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004651
4652 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004653 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004654 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004655 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004656
Bryce Leeaf691c02017-03-20 14:20:22 -07004657 protected void createStack(int stackId, boolean onTop) {
4658 switch (stackId) {
4659 case PINNED_STACK_ID:
4660 new PinnedActivityStack(this, mRecentTasks, onTop);
4661 break;
4662 default:
4663 new ActivityStack(this, mRecentTasks, onTop);
4664 break;
4665 }
4666 }
4667
Andrii Kulian839def92016-11-02 10:58:58 -07004668 /**
4669 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004670 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004671 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004672 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004673 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004674 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004675 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004676 if (mActivityDisplay != null) {
4677 throw new IllegalStateException("ActivityContainer is already attached, " +
4678 "displayId=" + mActivityDisplay.mDisplayId);
4679 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004680 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004681 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004682 }
4683
4684 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004685 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004686 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004687 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004688 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004689 return;
4690 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004691 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004692 }
4693 }
4694
4695 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004696 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004697 synchronized (mService) {
4698 if (mActivityDisplay != null) {
4699 return mActivityDisplay.mDisplayId;
4700 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004701 }
4702 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004703 }
4704
Jeff Brownca9bc702014-02-11 14:32:56 -08004705 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004706 public int getStackId() {
4707 synchronized (mService) {
4708 return mStackId;
4709 }
4710 }
4711
4712 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004713 public boolean injectEvent(InputEvent event) {
4714 final long origId = Binder.clearCallingIdentity();
4715 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004716 synchronized (mService) {
4717 if (mActivityDisplay != null) {
4718 return mInputManagerInternal.injectInputEvent(event,
4719 mActivityDisplay.mDisplayId,
4720 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4721 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004722 }
4723 return false;
4724 } finally {
4725 Binder.restoreCallingIdentity(origId);
4726 }
4727 }
4728
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004729 @Override
4730 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004731 synchronized (mService) {
4732 if (mContainerState == CONTAINER_STATE_FINISHING) {
4733 return;
4734 }
4735 mContainerState = CONTAINER_STATE_FINISHING;
4736
Craig Mautnerd163e752014-06-13 17:18:47 -07004737 long origId = Binder.clearCallingIdentity();
4738 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004739 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004740 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004741 } finally {
4742 Binder.restoreCallingIdentity(origId);
4743 }
4744 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004745 }
4746
Andrii Kulian03c403d2016-11-11 11:14:12 -08004747 /**
4748 * Remove the stack completely. Must be called only when there are no tasks left in it,
4749 * as this method does not finish running activities.
4750 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004751 void removeLocked() {
4752 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004753 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004754 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004755 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004756 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004757 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004758 }
4759
Andrii Kulian839def92016-11-02 10:58:58 -07004760 /**
4761 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4762 * completely or re-parented.
4763 */
4764 private void removeFromDisplayLocked() {
4765 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4766 + " current displayId=" + mActivityDisplay.mDisplayId);
4767
Wale Ogunwale1666e312016-12-16 11:27:18 -08004768 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004769 mActivityDisplay = null;
4770 }
4771
4772 /**
4773 * Move the stack to specified display.
4774 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004775 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004776 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004777 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004778 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4779 + mActivityDisplay + " to display=" + activityDisplay
4780 + " Callers=" + Debug.getCallers(2));
4781
4782 removeFromDisplayLocked();
4783
4784 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004785 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004786 }
4787
Craig Mautner4a1cb222013-12-04 16:14:06 -08004788 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004789 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004790 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004791 }
4792
4793 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004794 public final int startActivityIntentSender(IIntentSender intentSender)
4795 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004796 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4797
4798 if (!(intentSender instanceof PendingIntentRecord)) {
4799 throw new IllegalArgumentException("Bad PendingIntent object");
4800 }
4801
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004802 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004803 Binder.getCallingUid(), mCurrentUser, false,
4804 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004805
4806 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4807 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4808 pendingIntent.key.requestResolvedType);
4809
Dianne Hackborn98305522017-05-05 17:53:53 -07004810 return pendingIntent.sendInner(0, null, null, null, null, null, null, null, 0,
Craig Mautnerb9168362015-02-26 20:40:19 -08004811 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4812 }
4813
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004814 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004815 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004816 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004817 throw new SecurityException(
4818 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4819 }
4820 }
4821
Craig Mautnerdf88d732014-01-27 09:21:32 -08004822 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004823 public IBinder asBinder() {
4824 return this;
4825 }
4826
Craig Mautner4504de52013-12-20 09:06:56 -08004827 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004828 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004829 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004830 }
4831
Craig Mautner4a1cb222013-12-04 16:14:06 -08004832 ActivityStackSupervisor getOuter() {
4833 return ActivityStackSupervisor.this;
4834 }
4835
Craig Mautnerd163e752014-06-13 17:18:47 -07004836 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004837 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004838 }
4839
Craig Mautner6985bad2014-04-21 15:22:06 -07004840 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004841 void setVisible(boolean visible) {
4842 if (mVisible != visible) {
4843 mVisible = visible;
4844 if (mCallback != null) {
4845 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4846 0 /* unused */, this).sendToTarget();
4847 }
4848 }
4849 }
4850
Craig Mautner6985bad2014-04-21 15:22:06 -07004851 void setDrawn() {
4852 }
4853
Craig Mautner1b4bf852014-05-26 15:06:32 -07004854 // You can always start a new task on a regular ActivityStack.
4855 boolean isEligibleForNewTasks() {
4856 return true;
4857 }
4858
Craig Mautnerd163e752014-06-13 17:18:47 -07004859 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004860 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004861 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004862 }
4863
Craig Mautner34b73df2014-01-12 21:11:08 -08004864 @Override
4865 public String toString() {
4866 return mIdString + (mActivityDisplay == null ? "N" : "A");
4867 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004868 }
4869
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004870 private class VirtualActivityContainer extends ActivityContainer {
4871 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004872 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004873
4874 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004875 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4876 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004877 mParentActivity = parent;
4878 mCallback = callback;
4879 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004880 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004881 }
4882
4883 @Override
4884 public void setSurface(Surface surface, int width, int height, int density) {
4885 super.setSurface(surface, width, height, density);
4886
4887 synchronized (mService) {
4888 final long origId = Binder.clearCallingIdentity();
4889 try {
4890 setSurfaceLocked(surface, width, height, density);
4891 } finally {
4892 Binder.restoreCallingIdentity(origId);
4893 }
4894 }
4895 }
4896
4897 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4898 if (mContainerState == CONTAINER_STATE_FINISHING) {
4899 return;
4900 }
4901 VirtualActivityDisplay virtualActivityDisplay =
4902 (VirtualActivityDisplay) mActivityDisplay;
4903 if (virtualActivityDisplay == null) {
4904 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004905 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004906 mActivityDisplay = virtualActivityDisplay;
4907 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004908 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004909 }
4910
4911 if (mSurface != null) {
4912 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004913 }
4914
Craig Mautner6985bad2014-04-21 15:22:06 -07004915 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004916 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004917 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004918 } else {
4919 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004920 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004921 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004922 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004923 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004924 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004925
Craig Mautnerd163e752014-06-13 17:18:47 -07004926 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004927
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004928 if (DEBUG_STACK) Slog.d(TAG_STACK,
4929 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004930 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004931
Craig Mautner6985bad2014-04-21 15:22:06 -07004932 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004933 boolean isAttachedLocked() {
4934 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004935 }
4936
4937 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004938 void setDrawn() {
4939 synchronized (mService) {
4940 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004941 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004942 }
4943 }
4944
Craig Mautner1b4bf852014-05-26 15:06:32 -07004945 // Never start a new task on an ActivityView if it isn't explicitly specified.
4946 @Override
4947 boolean isEligibleForNewTasks() {
4948 return false;
4949 }
4950
Craig Mautnerd163e752014-06-13 17:18:47 -07004951 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004952 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004953 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4954 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4955 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4956 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4957 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004958 }
4959 }
4960
Craig Mautner4a1cb222013-12-04 16:14:06 -08004961 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4962 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004963 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004964 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004965 int mDisplayId;
4966 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004967
Craig Mautner4a1cb222013-12-04 16:14:06 -08004968 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4969 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004970 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004971
Jose Lima4b6c6692014-08-12 17:41:12 -07004972 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004973
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004974 /** Array of all UIDs that are present on the display. */
4975 private IntArray mDisplayAccessUIDs = new IntArray();
4976
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004977 ActivityDisplay() {
4978 }
Craig Mautner4504de52013-12-20 09:06:56 -08004979
Craig Mautner1a70a162014-09-13 12:09:31 -07004980 // After instantiation, check that mDisplay is not null before using this. The alternative
4981 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004982 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004983 final Display display = mDisplayManager.getDisplay(displayId);
4984 if (display == null) {
4985 return;
4986 }
4987 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004988 }
4989
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004990 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004991 mDisplay = display;
4992 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004993 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004994
Winson Chung7c3ede32017-03-14 14:18:34 -07004995 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004996 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004997 + " to displayId=" + mDisplayId + " position=" + position);
4998 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004999 }
5000
Wale Ogunwale1666e312016-12-16 11:27:18 -08005001 void detachStack(ActivityStack stack) {
5002 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08005003 + " from displayId=" + mDisplayId);
5004 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08005005 }
5006
Jose Lima4b6c6692014-08-12 17:41:12 -07005007 void setVisibleBehindActivity(ActivityRecord r) {
5008 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005009 }
5010
Jose Lima4b6c6692014-08-12 17:41:12 -07005011 boolean hasVisibleBehindActivity() {
5012 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005013 }
5014
Craig Mautner34b73df2014-01-12 21:11:08 -08005015 @Override
5016 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005017 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
5018 }
Andrii Kulian1779e612016-10-12 21:58:25 -07005019
5020 @Override
5021 protected int getChildCount() {
5022 return mStacks.size();
5023 }
5024
5025 @Override
5026 protected ConfigurationContainer getChildAt(int index) {
5027 return mStacks.get(index);
5028 }
5029
5030 @Override
5031 protected ConfigurationContainer getParent() {
5032 return ActivityStackSupervisor.this;
5033 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005034
5035 boolean isPrivate() {
5036 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
5037 }
5038
5039 boolean isUidPresent(int uid) {
5040 for (ActivityStack stack : mStacks) {
5041 if (stack.isUidPresent(uid)) {
5042 return true;
5043 }
5044 }
5045 return false;
5046 }
5047
5048 /** Update and get all UIDs that are present on the display and have access to it. */
5049 private IntArray getPresentUIDs() {
5050 mDisplayAccessUIDs.clear();
5051 for (ActivityStack stack : mStacks) {
5052 stack.getPresentUIDs(mDisplayAccessUIDs);
5053 }
5054 return mDisplayAccessUIDs;
5055 }
Andrii Kulian250d6532017-02-08 23:30:45 -08005056
5057 boolean shouldDestroyContentOnRemove() {
5058 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
5059 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005060 }
5061
5062 class VirtualActivityDisplay extends ActivityDisplay {
5063 VirtualDisplay mVirtualDisplay;
5064
Craig Mautner6985bad2014-04-21 15:22:06 -07005065 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005066 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Santos Cordonee8931e2017-04-05 10:31:15 -07005067 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null /* projection */,
5068 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null /* surface */,
Michael Wrightc39d47a2014-07-08 18:07:36 -07005069 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Santos Cordonee8931e2017-04-05 10:31:15 -07005070 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null /* callback */,
5071 null /* handler */, null /* uniqueId */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005072
5073 init(mVirtualDisplay.getDisplay());
5074
Bryce Leeaf3a4002017-03-20 10:37:12 -07005075 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005076 }
5077
5078 void setSurface(Surface surface) {
5079 if (mVirtualDisplay != null) {
5080 mVirtualDisplay.setSurface(surface);
5081 }
5082 }
5083
5084 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08005085 void detachStack(ActivityStack stack) {
5086 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005087 if (mVirtualDisplay != null) {
5088 mVirtualDisplay.release();
5089 mVirtualDisplay = null;
5090 }
5091 }
5092
5093 @Override
5094 public String toString() {
5095 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005096 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005097 }
Jose Lima58e66d62014-05-27 20:00:27 -07005098
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005099 ActivityStack findStackBehind(ActivityStack stack) {
5100 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02005101 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005102 if (display == null) {
5103 return null;
5104 }
5105 final ArrayList<ActivityStack> stacks = display.mStacks;
5106 for (int i = stacks.size() - 1; i >= 0; i--) {
5107 if (stacks.get(i) == stack && i > 0) {
5108 return stacks.get(i - 1);
5109 }
5110 }
5111 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5112 + " in=" + stacks);
5113 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005114
Jorim Jaggic69bd222016-03-15 14:38:37 +01005115 /**
5116 * Puts a task into resizing mode during the next app transition.
5117 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005118 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01005119 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005120 private void setResizingDuringAnimation(TaskRecord task) {
5121 mResizingTasksDuringAnimation.add(task.taskId);
5122 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01005123 }
5124
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005125 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
5126 final TaskRecord task;
5127 final int callingUid;
5128 final String callingPackage;
5129 final Intent intent;
5130 final int userId;
5131 final ActivityOptions activityOptions = (bOptions != null)
5132 ? new ActivityOptions(bOptions) : null;
5133 final int launchStackId = (activityOptions != null)
5134 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005135 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005136 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005137 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005138 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005139
Matthew Ng606dd802017-06-05 14:06:32 -07005140 mWindowManager.deferSurfaceLayout();
5141 try {
5142 if (launchStackId == DOCKED_STACK_ID) {
5143 mWindowManager.setDockedStackCreateState(
5144 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005145
Matthew Ng606dd802017-06-05 14:06:32 -07005146 // Defer updating the stack in which recents is until the app transition is done, to
5147 // not run into issues where we still need to draw the task in recents but the
5148 // docked stack is already created.
5149 deferUpdateBounds(RECENTS_STACK_ID);
5150 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005151 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005152
Matthew Ng606dd802017-06-05 14:06:32 -07005153 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
5154 launchStackId);
5155 if (task == null) {
5156 continueUpdateBounds(RECENTS_STACK_ID);
5157 mWindowManager.executeAppTransition();
5158 throw new IllegalArgumentException(
5159 "startActivityFromRecentsInner: Task " + taskId + " not found.");
5160 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005161
Matthew Ng606dd802017-06-05 14:06:32 -07005162 // Since we don't have an actual source record here, we assume that the currently
5163 // focused activity was the source.
5164 final ActivityStack focusedStack = getFocusedStack();
5165 final ActivityRecord sourceRecord =
5166 focusedStack != null ? focusedStack.topActivity() : null;
5167
5168 if (launchStackId != INVALID_STACK_ID) {
5169 if (task.getStackId() != launchStackId) {
5170 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
5171 DEFER_RESUME, "startActivityFromRecents");
5172 }
5173 }
5174
5175 // If the user must confirm credentials (e.g. when first launching a work app and the
5176 // Work Challenge is present) let startActivityInPackage handle the intercepting.
5177 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
5178 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07005179 final ActivityRecord targetActivity = task.getTopActivity();
5180
5181 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
5182 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07005183 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07005184 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07005185 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07005186 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07005187
5188 // If we are launching the task in the docked stack, put it into resizing mode so
5189 // the window renders full-screen with the background filling the void. Also only
5190 // call this at the end to make sure that tasks exists on the window manager side.
5191 if (launchStackId == DOCKED_STACK_ID) {
5192 setResizingDuringAnimation(task);
5193 }
5194
5195 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
5196 ActivityManager.START_TASK_TO_FRONT,
5197 sourceRecord != null
5198 ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
5199 sourceRecord, task.getStack());
5200 return ActivityManager.START_TASK_TO_FRONT;
5201 }
5202 callingUid = task.mCallingUid;
5203 callingPackage = task.mCallingPackage;
5204 intent = task.intent;
5205 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5206 userId = task.userId;
5207 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Wale Ogunwale692dcd62017-06-20 13:38:14 -07005208 null, null, 0, 0, bOptions, userId, null, task, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005209 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005210 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005211 }
Matthew Ng606dd802017-06-05 14:06:32 -07005212 return result;
5213 } finally {
5214 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005215 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005216 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005217
5218 /**
5219 * @return a list of activities which are the top ones in each visible stack. The first
5220 * entry will be the focused activity.
5221 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005222 List<IBinder> getTopVisibleActivities() {
5223 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5224 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005225 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005226 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5227 // Traverse all stacks on a display.
5228 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5229 final ActivityStack stack = display.mStacks.get(j);
5230 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005231 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005232 == ActivityStack.STACK_VISIBLE) {
5233 final ActivityRecord top = stack.topActivity();
5234 if (top != null) {
5235 if (stack == mFocusedStack) {
5236 topActivityTokens.add(0, top.appToken);
5237 } else {
5238 topActivityTokens.add(top.appToken);
5239 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005240 }
5241 }
5242 }
5243 }
5244 return topActivityTokens;
5245 }
Bryce Lee4a194382017-04-04 14:32:48 -07005246
5247 /**
5248 * Internal container to store a match qualifier alongside a WaitResult.
5249 */
5250 static class WaitInfo {
5251 private final ComponentName mTargetComponent;
5252 private final WaitResult mResult;
5253
5254 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5255 this.mTargetComponent = targetComponent;
5256 this.mResult = result;
5257 }
5258
Wale Ogunwale3270f172017-04-26 07:29:42 -07005259 public boolean matches(ComponentName targetComponent) {
5260 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07005261 }
5262
5263 public WaitResult getResult() {
5264 return mResult;
5265 }
5266
5267 public ComponentName getComponent() {
5268 return mTargetComponent;
5269 }
5270
5271 public void dump(PrintWriter pw, String prefix) {
5272 pw.println(prefix + "WaitInfo:");
5273 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5274 pw.println(prefix + " mResult=");
5275 mResult.dump(pw, prefix);
5276 }
5277 }
Craig Mautner27084302013-03-25 08:05:25 -07005278}