blob: fa279c227d7985838faa4f928eed6d4bc4cf68dc [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;
chaviw59b98852017-06-13 12:05:44 -070043import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Jorim Jaggife762342016-10-13 14:33:27 +020044import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
45import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070046import static android.view.Display.FLAG_PRIVATE;
47import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080048import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070049import static android.view.Display.TYPE_VIRTUAL;
Jorim Jaggife762342016-10-13 14:33:27 +020050
Winson Chung47900652017-04-06 18:44:25 -070051import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020052import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020064import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_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
Andrii Kulian1779e612016-10-12 21:58:25 -0700460 @Override
461 protected int getChildCount() {
462 return mActivityDisplays.size();
463 }
464
465 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700466 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700467 return mActivityDisplays.valueAt(index);
468 }
469
470 @Override
471 protected ConfigurationContainer getParent() {
472 return null;
473 }
474
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700475 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700476 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700477 if (activityDisplay == null) {
478 throw new IllegalArgumentException("No display found with id: " + displayId);
479 }
480
481 return activityDisplay.getOverrideConfiguration();
482 }
483
484 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700485 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700486 if (activityDisplay == null) {
487 throw new IllegalArgumentException("No display found with id: " + displayId);
488 }
489
490 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
491 }
492
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700493 /** Check if placing task or activity on specified display is allowed. */
494 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable) {
495 return displayId == DEFAULT_DISPLAY || (mService.mSupportsMultiDisplay
496 && (resizeable || displayConfigMatchesGlobal(displayId)));
497 }
498
499 /**
500 * Check if configuration of specified display matches current global config.
501 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
502 * the same config as on the default display.
503 * @param displayId Id of the display to check.
504 * @return {@code true} if configuration matches.
505 */
506 private boolean displayConfigMatchesGlobal(int displayId) {
507 if (displayId == DEFAULT_DISPLAY) {
508 return true;
509 }
510 if (displayId == INVALID_DISPLAY) {
511 return false;
512 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700513 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700514 if (targetDisplay == null) {
515 throw new IllegalArgumentException("No display found with id: " + displayId);
516 }
517 return getConfiguration().equals(targetDisplay.getConfiguration());
518 }
519
Wale Ogunwale39381972015-12-17 17:15:29 -0800520 static class FindTaskResult {
521 ActivityRecord r;
522 boolean matchedByRootAffinity;
523 }
524 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
525
Craig Mautneree36c772014-07-16 14:56:05 -0700526 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800527 * Temp storage for display ids sorted in focus order.
528 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
529 * it's more efficient, as the number of displays is usually small.
530 */
531 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
532
533 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100534 * Used to keep track whether app visibilities got changed since the last pause. Useful to
535 * determine whether to invoke the task stack change listener after pausing.
536 */
537 boolean mAppVisibilitiesChangedSinceLastPause;
538
539 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100540 * Set of tasks that are in resizing mode during an app transition to fill the "void".
541 */
542 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
543
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700544
545 /**
546 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
547 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
548 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
549 * like the docked stack going empty.
550 */
551 private boolean mAllowDockedStackResize = true;
552
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100553 /**
Tony Mak853304c2016-04-18 15:17:41 +0100554 * Is dock currently minimized.
555 */
556 boolean mIsDockMinimized;
557
Jorim Jaggife762342016-10-13 14:33:27 +0200558 final KeyguardController mKeyguardController;
559
chaviw59b98852017-06-13 12:05:44 -0700560 private PowerManager mPowerManager;
561 private int mDeferResumeCount;
562
Tony Mak853304c2016-04-18 15:17:41 +0100563 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700564 * Description of a request to start a new activity, which has been held
565 * due to app switches being disabled.
566 */
567 static class PendingActivityLaunch {
568 final ActivityRecord r;
569 final ActivityRecord sourceRecord;
570 final int startFlags;
571 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700572 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700573
574 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700575 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700576 r = _r;
577 sourceRecord = _sourceRecord;
578 startFlags = _startFlags;
579 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700580 callerApp = _callerApp;
581 }
582
583 void sendErrorResult(String message) {
584 try {
585 if (callerApp.thread != null) {
586 callerApp.thread.scheduleCrash(message);
587 }
588 } catch (RemoteException e) {
589 Slog.e(TAG, "Exception scheduling crash of failed "
590 + "activity launcher sourceRecord=" + sourceRecord, e);
591 }
Craig Mautneree36c772014-07-16 14:56:05 -0700592 }
593 }
594
Bryce Leeaf691c02017-03-20 14:20:22 -0700595 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700596 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700597 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800598 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200599 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700600 }
601
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800602 void setRecentTasks(RecentTasks recentTasks) {
603 mRecentTasks = recentTasks;
604 }
605
Jeff Brown2c43c332014-06-12 22:38:59 -0700606 /**
607 * At the time when the constructor runs, the power manager has not yet been
608 * initialized. So we initialize our wakelocks afterwards.
609 */
610 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700611 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
612 mGoingToSleep = mPowerManager
613 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
614 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700615 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700616 }
617
justinzhang5286d3f2014-05-12 17:06:01 -0400618 // This function returns a IStatusBarService. The value is from ServiceManager.
619 // getService and is cached.
620 private IStatusBarService getStatusBarService() {
621 synchronized (mService) {
622 if (mStatusBarService == null) {
623 mStatusBarService = IStatusBarService.Stub.asInterface(
624 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
625 if (mStatusBarService == null) {
626 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
627 }
628 }
629 return mStatusBarService;
630 }
631 }
632
Jason Monk35c62a42014-06-17 10:24:47 -0400633 private IDevicePolicyManager getDevicePolicyManager() {
634 synchronized (mService) {
635 if (mDevicePolicyManager == null) {
636 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
637 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
638 if (mDevicePolicyManager == null) {
639 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
640 }
641 }
642 return mDevicePolicyManager;
643 }
644 }
645
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700646 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647 synchronized (mService) {
648 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200649 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650
651 mDisplayManager =
652 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
653 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800654 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655
656 Display[] displays = mDisplayManager.getDisplays();
657 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
658 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800659 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700660 if (activityDisplay.mDisplay == null) {
661 throw new IllegalStateException("Default Display does not exist");
662 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800663 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700664 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 }
666
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800667 mHomeStack = mFocusedStack = mLastFocusedStack =
668 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800669
670 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 }
Craig Mautner27084302013-03-25 08:05:25 -0700672 }
673
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700674 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700676 }
677
Craig Mautnerde4ef022013-04-07 19:01:33 -0700678 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700680 }
681
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700682 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700683 if (stack == null) {
684 return false;
685 }
686
Craig Mautnerdf88d732014-01-27 09:21:32 -0800687 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
688 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700689 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800690 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800691 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700692 }
693
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800694 /** The top most stack on its display. */
695 boolean isFrontStackOnDisplay(ActivityStack stack) {
696 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
697 }
698
699 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700700 if (stack == null) {
701 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800702 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700703
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700704 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
705 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700706 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800707 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800708 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700709 }
710
Wale Ogunwaled046a012015-12-24 13:05:59 -0800711 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800712 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
713 if (!focusCandidate.isFocusable()) {
714 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800715 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800716 }
717
718 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800719 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800720 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800721
Wale Ogunwaled046a012015-12-24 13:05:59 -0800722 EventLogTags.writeAmFocusedStack(
723 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
724 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
725 }
726
727 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800728 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800729 if (r != null && r.idle) {
730 checkFinishBootingLocked();
731 }
732 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700733 }
734
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700735 void moveHomeStackToFront(String reason) {
736 mHomeStack.moveToFront(reason);
737 }
738
Matthew Ng330757d2017-02-28 14:19:17 -0800739 void moveRecentsStackToFront(String reason) {
740 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
741 if (recentsStack != null) {
742 recentsStack.moveToFront(reason);
743 }
744 }
745
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700746 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800747 boolean moveHomeStackTaskToTop(String reason) {
748 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700749
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700750 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700751 if (top == null) {
752 return false;
753 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700754 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700755 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700756 }
757
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800758 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700759 if (!mService.mBooting && !mService.mBooted) {
760 // Not ready yet!
761 return false;
762 }
763
Craig Mautner84984fa2014-06-19 11:19:20 -0700764 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700765 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700766 }
767
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800768 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700769 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800770 final String myReason = reason + " resumeHomeStackTask";
771
Mark Lua56ea122015-10-08 13:31:01 +0800772 // Only resume home activity if isn't finishing.
773 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700774 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800775 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700776 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800777 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700778 }
779
Craig Mautner8d341ef2013-03-26 09:03:27 -0700780 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800781 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
782 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700783 }
784
785 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700786 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700787 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800788 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700789 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800790 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
791 * valid if the matchMode is
792 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700793 */
Winson Chung7900bee2017-03-10 08:59:25 -0800794 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
795 // If there is a stack id set, ensure that we are attempting to actually restore a task
796 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
797 stackId != INVALID_STACK_ID) {
798 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
799 }
800
Craig Mautnere0a38842013-12-16 16:14:02 -0800801 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800803 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
805 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700806 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 if (task != null) {
808 return task;
809 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700810 }
811 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800812
Winson Chung7900bee2017-03-10 08:59:25 -0800813 // If we are matching stack tasks only, return now
814 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800815 return null;
816 }
817
Winson Chung7900bee2017-03-10 08:59:25 -0800818 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
819 // the task from recents
820 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700821 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
822
823 if (task == null) {
824 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800825 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
826 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700827
828 return null;
829 }
830
831 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700832 return task;
833 }
834
Winson Chung7900bee2017-03-10 08:59:25 -0800835 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700836 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700837 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
838 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800839 return null;
840 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700841 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800842 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700843 }
844
Craig Mautner6170f732013-04-02 13:05:23 -0700845 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800846 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800847 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800848 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
850 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
851 if (r != null) {
852 return r;
853 }
Craig Mautner6170f732013-04-02 13:05:23 -0700854 }
855 }
856 return null;
857 }
858
Tony Mak853304c2016-04-18 15:17:41 +0100859 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000860 * Detects whether we should show a lock screen in front of this task for a locked user.
861 * <p>
862 * We'll do this if either of the following holds:
863 * <ul>
864 * <li>The top activity explicitly belongs to {@param userId}.</li>
865 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
866 * </ul>
867 *
868 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100869 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000870 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
871 // To handle the case that work app is in the task but just is not the top one.
872 final ActivityRecord activityRecord = task.getTopActivity();
873 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
874
875 return (activityRecord != null && activityRecord.userId == userId)
876 || (resultTo != null && resultTo.userId == userId);
877 }
878
879 /**
880 * Find all visible task stacks containing {@param userId} and intercept them with an activity
881 * to block out the contents and possibly start a credential-confirming intent.
882 *
883 * @param userId user handle for the locked managed profile.
884 */
885 void lockAllProfileTasks(@UserIdInt int userId) {
886 mWindowManager.deferSurfaceLayout();
887 try {
888 final List<ActivityStack> stacks = getStacks();
889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
890 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
891 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
892 final TaskRecord task = tasks.get(taskNdx);
893
894 // Check the task for a top activity belonging to userId, or returning a result
895 // to an activity belonging to userId. Example case: a document picker for
896 // personal files, opened by a work app, should still get locked.
897 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000898 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
899 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000900 }
Tony Mak853304c2016-04-18 15:17:41 +0100901 }
902 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000903 } finally {
904 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100905 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000906 }
907
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800908 void setNextTaskIdForUserLocked(int taskId, int userId) {
909 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
910 if (taskId > currentTaskId) {
911 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700912 }
913 }
914
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700915 static int nextTaskIdForUser(int taskId, int userId) {
916 int nextTaskId = taskId + 1;
917 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
918 // Wrap around as there will be smaller task ids that are available now.
919 nextTaskId -= MAX_TASK_IDS_PER_USER;
920 }
921 return nextTaskId;
922 }
923
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800924 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800925 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
926 // for a userId u, a taskId can only be in the range
927 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
928 // 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 -0700929 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800930 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800931 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800932 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700933 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800934 if (candidateTaskId == currentTaskId) {
935 // Something wrong!
936 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
937 throw new IllegalStateException("Cannot get an available task id."
938 + " Reached limit of " + MAX_TASK_IDS_PER_USER
939 + " running tasks per user.");
940 }
941 }
942 mCurTaskIdForUser.put(userId, candidateTaskId);
943 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700944 }
945
Chong Zhang6cda19c2016-06-14 19:07:56 -0700946 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800947 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700948 if (stack == null) {
949 return null;
950 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700951 ActivityRecord resumedActivity = stack.mResumedActivity;
952 if (resumedActivity == null || resumedActivity.app == null) {
953 resumedActivity = stack.mPausingActivity;
954 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700955 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700956 }
957 }
958 return resumedActivity;
959 }
960
Dianne Hackbornff072722014-09-24 10:56:28 -0700961 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700962 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
965 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
967 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700968 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 continue;
970 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700971 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 if (hr != null) {
973 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
974 && processName.equals(hr.processName)) {
975 try {
George Mount2c92c972014-03-20 09:38:23 -0700976 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800977 didSomething = true;
978 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700979 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800980 Slog.w(TAG, "Exception in new application when starting activity "
981 + hr.intent.getComponent().flattenToShortString(), e);
982 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700983 }
Craig Mautner20e72272013-04-01 13:45:53 -0700984 }
Craig Mautner20e72272013-04-01 13:45:53 -0700985 }
986 }
987 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700988 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700989 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700990 }
Craig Mautner20e72272013-04-01 13:45:53 -0700991 return didSomething;
992 }
993
994 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800995 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
996 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800997 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
998 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700999 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 continue;
1001 }
1002 final ActivityRecord resumedActivity = stack.mResumedActivity;
1003 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001004 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -08001005 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001006 return false;
1007 }
Craig Mautner20e72272013-04-01 13:45:53 -07001008 }
1009 }
Wei Wang65c7a152016-06-02 18:51:22 -07001010 // Send launch end powerhint when idle
1011 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001012 return true;
1013 }
1014
Craig Mautnerde4ef022013-04-07 19:01:33 -07001015 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1017 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001018 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1019 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001020 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001021 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001022 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001023 return false;
1024 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 }
1026 }
1027 }
1028 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001029 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001030 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1031 mLastFocusedStack + " to=" + mFocusedStack);
1032 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001033 return true;
1034 }
1035
1036 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001037 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001038 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1039 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001040 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1041 final ActivityStack stack = stacks.get(stackNdx);
1042 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001043 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001044 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001045 return false;
1046 }
1047 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001048 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001049 }
1050 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001051 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001052 }
1053
Craig Mautner2acc3892013-09-23 10:28:14 -07001054 /**
1055 * Pause all activities in either all of the stacks or just the back stacks.
1056 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001057 * @param resuming The resuming activity.
1058 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1059 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001060 * @return true if any activity was paused as a result of this call.
1061 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001062 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001063 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001064 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1065 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001066 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1067 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001068 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001069 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001070 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001071 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1072 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001073 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001074 }
1075 }
1076 return someActivityPaused;
1077 }
1078
Craig Mautnerde4ef022013-04-07 19:01:33 -07001079 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001080 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001081 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1082 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001083 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1084 final ActivityStack stack = stacks.get(stackNdx);
1085 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001086 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001087 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001088 Slog.d(TAG_STATES,
1089 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001090 pausing = false;
1091 } else {
1092 return false;
1093 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001094 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001095 }
1096 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001097 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001098 }
1099
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001100 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001101 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001102 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001103 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1104 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1105 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1106 final ActivityStack stack = stacks.get(stackNdx);
1107 if (stack.mResumedActivity != null &&
1108 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001109 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001110 }
1111 }
1112 }
1113 }
1114
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001115 void cancelInitializingActivities() {
1116 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1117 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1118 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1119 stacks.get(stackNdx).cancelInitializingActivities();
1120 }
1121 }
1122 }
1123
Bryce Lee4a194382017-04-04 14:32:48 -07001124 void waitActivityVisible(ComponentName name, WaitResult result) {
1125 final WaitInfo waitInfo = new WaitInfo(name, result);
1126 mWaitingForActivityVisible.add(waitInfo);
1127 }
1128
1129 void cleanupActivity(ActivityRecord r) {
1130 // Make sure this record is no longer in the pending finishes list.
1131 // This could happen, for example, if we are trimming activities
1132 // down to the max limit while they are still waiting to finish.
1133 mFinishingActivities.remove(r);
1134 mActivitiesWaitingForVisibleActivity.remove(r);
1135
1136 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001137 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001138 mWaitingForActivityVisible.remove(i);
1139 }
1140 }
1141 }
1142
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001143 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001144 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001145 }
1146
1147 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1148 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001149 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1150 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001151 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001152 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001153 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001154 result.timeout = false;
1155 result.who = w.getComponent();
1156 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1157 result.thisTime = result.totalTime;
1158 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001159 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001160 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001161 if (changed) {
1162 mService.notifyAll();
1163 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001164 }
1165
Chong Zhang5022da32016-06-21 16:31:37 -07001166 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1167 boolean changed = false;
1168 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1169 WaitResult w = mWaitingActivityLaunched.remove(i);
1170 if (w.who == null) {
1171 changed = true;
1172 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1173 // the starting activity ends up moving another activity to front, and it should
1174 // wait for this new activity to become visible instead.
1175 // Do not modify other fields.
1176 w.result = START_TASK_TO_FRONT;
1177 }
1178 }
1179 if (changed) {
1180 mService.notifyAll();
1181 }
1182 }
1183
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001184 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1185 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001186 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001187 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001188 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001189 if (w.who == null) {
1190 changed = true;
1191 w.timeout = timeout;
1192 if (r != null) {
1193 w.who = new ComponentName(r.info.packageName, r.info.name);
1194 }
1195 w.thisTime = thisTime;
1196 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001197 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001198 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001199 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001200 if (changed) {
1201 mService.notifyAll();
1202 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001203 }
1204
Craig Mautner29219d92013-04-16 20:19:12 -07001205 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001206 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001207 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001208 if (r != null) {
1209 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001210 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001211
Andrii Kulian7318d632016-07-20 18:59:28 -07001212 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001213 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1214
1215 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1216 final int displayId = mTmpOrderedDisplayIds.get(i);
1217 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1218 if (stacks == null) {
1219 continue;
1220 }
1221 for (int j = stacks.size() - 1; j >= 0; --j) {
1222 final ActivityStack stack = stacks.get(j);
1223 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1224 r = stack.topRunningActivityLocked();
1225 if (r != null) {
1226 return r;
1227 }
Craig Mautner29219d92013-04-16 20:19:12 -07001228 }
1229 }
1230 }
1231 return null;
1232 }
1233
Dianne Hackborn09233282014-04-30 11:33:59 -07001234 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001235 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001236 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1237 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001238 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001239 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001240 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001241 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1242 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001243 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001244 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001245 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001246 }
1247 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001248
1249 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1250 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1251 while (maxNum > 0) {
1252 long mostRecentActiveTime = Long.MIN_VALUE;
1253 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001254 final int numTaskLists = runningTaskLists.size();
1255 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1256 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001257 if (!stackTaskList.isEmpty()) {
1258 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1259 if (lastActiveTime > mostRecentActiveTime) {
1260 mostRecentActiveTime = lastActiveTime;
1261 selectedStackList = stackTaskList;
1262 }
1263 }
1264 }
1265 if (selectedStackList != null) {
1266 list.add(selectedStackList.remove(0));
1267 --maxNum;
1268 } else {
1269 break;
1270 }
1271 }
Craig Mautner20e72272013-04-01 13:45:53 -07001272 }
1273
Todd Kennedy7440f172015-12-09 14:31:22 -08001274 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1275 ProfilerInfo profilerInfo) {
1276 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001277 if (aInfo != null) {
1278 // Store the found target back into the intent, because now that
1279 // we have it we never want to do this again. For example, if the
1280 // user navigates back to this point in the history, we should
1281 // always restart the exact same activity.
1282 intent.setComponent(new ComponentName(
1283 aInfo.applicationInfo.packageName, aInfo.name));
1284
1285 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001286 if (!aInfo.processName.equals("system")) {
1287 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001288 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001289 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001290
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001291 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1292 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1293 }
1294
Man Caocfa78b22015-06-11 20:14:34 -07001295 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1296 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1297 }
1298
1299 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001300 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001301 }
1302 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001303 final String intentLaunchToken = intent.getLaunchToken();
1304 if (aInfo.launchToken == null && intentLaunchToken != null) {
1305 aInfo.launchToken = intentLaunchToken;
1306 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001307 }
1308 return aInfo;
1309 }
1310
Todd Kennedy7440f172015-12-09 14:31:22 -08001311 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001312 return resolveIntent(intent, resolvedType, userId, 0);
1313 }
1314
1315 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001316 synchronized (mService) {
1317 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001318 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001319 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001320 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001321 }
1322
1323 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1324 ProfilerInfo profilerInfo, int userId) {
1325 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1326 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1327 }
1328
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001329 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1330 boolean andResume, boolean checkConfig) throws RemoteException {
1331
1332 if (!allPausedActivitiesComplete()) {
1333 // While there are activities pausing we skipping starting any new activities until
1334 // pauses are complete. NOTE: that we also do this for activities that are starting in
1335 // the paused state because they will first be resumed then paused on the client side.
1336 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1337 "realStartActivityLocked: Skipping start of r=" + r
1338 + " some activities pausing...");
1339 return false;
1340 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001341
Bryce Leeaf691c02017-03-20 14:20:22 -07001342 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001343 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001344
1345 beginDeferResume();
1346
Craig Mautner2420ead2013-04-01 17:13:20 -07001347 try {
chaviw59b98852017-06-13 12:05:44 -07001348 r.startFreezingScreenLocked(app, 0);
1349
1350 // schedule launch ticks to collect information about slow apps.
1351 r.startLaunchTickingLocked();
1352
1353 r.app = app;
1354
1355 // Have the window manager re-evaluate the orientation of the screen based on the new
1356 // activity order. Note that as a result of this, it can call back into the activity
1357 // manager with a new orientation. We don't care about that, because the activity is
1358 // not currently running so we are just restarting it anyway.
1359 if (checkConfig) {
1360 final int displayId = r.getDisplayId();
1361 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1362 getDisplayOverrideConfiguration(displayId),
1363 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1364 // Deferring resume here because we're going to launch new activity shortly.
1365 // We don't want to perform a redundant launch of the same record while ensuring
1366 // configurations and trying to resume top activity of focused stack.
1367 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1368 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001369 }
chaviw59b98852017-06-13 12:05:44 -07001370
1371 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1372 true /* isTop */)) {
1373 // We only set the visibility to true if the activity is allowed to be visible
1374 // based on
1375 // keyguard state. This avoids setting this into motion in window manager that is
1376 // later cancelled due to later calls to ensure visible activities that set
1377 // visibility back to false.
1378 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 }
chaviw59b98852017-06-13 12:05:44 -07001380
1381 if (mKeyguardController.isKeyguardLocked()) {
1382 r.notifyUnknownVisibilityLaunched();
Craig Mautner2420ead2013-04-01 17:13:20 -07001383 }
chaviw59b98852017-06-13 12:05:44 -07001384 final int applicationInfoUid =
1385 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1386 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1387 Slog.wtf(TAG,
1388 "User ID for activity changing for " + r
1389 + " appInfo.uid=" + r.appInfo.uid
1390 + " info.ai.uid=" + applicationInfoUid
1391 + " old=" + r.app + " new=" + app);
1392 }
1393
1394 app.waitingToKill = null;
1395 r.launchCount++;
1396 r.lastLaunchTime = SystemClock.uptimeMillis();
1397
1398 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1399
1400 int idx = app.activities.indexOf(r);
1401 if (idx < 0) {
1402 app.activities.add(r);
1403 }
1404 mService.updateLruProcessLocked(app, true, null);
1405 mService.updateOomAdjLocked();
1406
1407 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1408 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
1409 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE",
1410 false);
1411 }
1412
1413 try {
1414 if (app.thread == null) {
1415 throw new RemoteException();
1416 }
1417 List<ResultInfo> results = null;
1418 List<ReferrerIntent> newIntents = null;
1419 if (andResume) {
1420 // We don't need to deliver new intents and/or set results if activity is going
1421 // to pause immediately after launch.
1422 results = r.results;
1423 newIntents = r.newIntents;
1424 }
1425 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1426 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1427 + " newIntents=" + newIntents + " andResume=" + andResume);
1428 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1429 System.identityHashCode(r), task.taskId, r.shortComponentName);
1430 if (r.isHomeActivity()) {
1431 // Home process is the root process of the task.
1432 mService.mHomeProcess = task.mActivities.get(0).app;
1433 }
1434 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1435 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1436 r.sleeping = false;
1437 r.forceNewConfig = false;
1438 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1439 mService.showAskCompatModeDialogLocked(r);
1440 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1441 ProfilerInfo profilerInfo = null;
1442 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1443 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1444 mService.mProfileProc = app;
1445 final String profileFile = mService.mProfileFile;
1446 if (profileFile != null) {
1447 ParcelFileDescriptor profileFd = mService.mProfileFd;
1448 if (profileFd != null) {
1449 try {
1450 profileFd = profileFd.dup();
1451 } catch (IOException e) {
1452 if (profileFd != null) {
1453 try {
1454 profileFd.close();
1455 } catch (IOException o) {
1456 }
1457 profileFd = null;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001458 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001459 }
1460 }
chaviw59b98852017-06-13 12:05:44 -07001461
1462 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1463 mService.mSamplingInterval, mService.mAutoStopProfiler,
1464 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001465 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001466 }
1467 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001468
chaviw59b98852017-06-13 12:05:44 -07001469 app.hasShownUi = true;
1470 app.pendingUiClean = true;
1471 app.forceProcessStateUpTo(mService.mTopProcessState);
1472 // Because we could be starting an Activity in the system process this may not go
1473 // across a Binder interface which would create a new Configuration. Consequently
1474 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001475
chaviw59b98852017-06-13 12:05:44 -07001476 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1477 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1478 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001479
chaviw59b98852017-06-13 12:05:44 -07001480 logIfTransactionTooLarge(r.intent, r.icicle);
1481 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1482 System.identityHashCode(r), r.info,
1483 // TODO: Have this take the merged configuration instead of separate global
1484 // and override configs.
1485 mergedConfiguration.getGlobalConfiguration(),
1486 mergedConfiguration.getOverrideConfiguration(), r.compat,
1487 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1488 r.persistentState, results, newIntents, !andResume,
1489 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001490
chaviw59b98852017-06-13 12:05:44 -07001491 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1492 // This may be a heavy-weight process! Note that the package
1493 // manager will ensure that only activity can run in the main
1494 // process of the .apk, which is the only thing that will be
1495 // considered heavy-weight.
1496 if (app.processName.equals(app.info.packageName)) {
1497 if (mService.mHeavyWeightProcess != null
1498 && mService.mHeavyWeightProcess != app) {
1499 Slog.w(TAG, "Starting new heavy weight process " + app
1500 + " when already running "
1501 + mService.mHeavyWeightProcess);
1502 }
1503 mService.mHeavyWeightProcess = app;
1504 Message msg = mService.mHandler.obtainMessage(
1505 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1506 msg.obj = r;
1507 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001508 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001509 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001510
chaviw59b98852017-06-13 12:05:44 -07001511 } catch (RemoteException e) {
1512 if (r.launchFailed) {
1513 // This is the second time we failed -- finish activity
1514 // and give up.
1515 Slog.e(TAG, "Second failure launching "
1516 + r.intent.getComponent().flattenToShortString()
1517 + ", giving up", e);
1518 mService.appDiedLocked(app);
1519 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1520 "2nd-crash", false);
1521 return false;
1522 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001523
chaviw59b98852017-06-13 12:05:44 -07001524 // This is the first time we failed -- restart process and
1525 // retry.
1526 r.launchFailed = true;
1527 app.activities.remove(r);
1528 throw e;
1529 }
1530 } finally {
1531 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001532 }
1533
1534 r.launchFailed = false;
1535 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001536 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001537 }
1538
chaviw59b98852017-06-13 12:05:44 -07001539 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001540 // As part of the process of launching, ActivityThread also performs
1541 // a resume.
1542 stack.minimalResumeActivityLocked(r);
1543 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001544 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001545 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001546 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001547 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001548 "Moving to PAUSED: " + r + " (starting in paused state)");
1549 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001550 }
1551
1552 // Launch the new version setup screen if needed. We do this -after-
1553 // launching the initial activity (that is, home), so that it can have
1554 // a chance to initialize itself while in the background, making the
1555 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001556 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001557 mService.startSetupActivityLocked();
1558 }
1559
Dianne Hackborn465fa392014-09-14 14:21:18 -07001560 // Update any services we are bound to that might care about whether
1561 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001562 if (r.app != null) {
1563 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1564 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001565
Craig Mautner2420ead2013-04-01 17:13:20 -07001566 return true;
1567 }
1568
Sudheer Shankafab200f2017-05-17 20:41:53 -07001569 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1570 int extrasSize = 0;
1571 if (intent != null) {
1572 final Bundle extras = intent.getExtras();
1573 if (extras != null) {
1574 extrasSize = extras.getSize();
1575 }
1576 }
1577 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1578 if (extrasSize + icicleSize > 200000) {
1579 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1580 + ", icicle size: " + icicleSize);
1581 }
1582 }
1583
Craig Mautnere79d42682013-04-01 19:01:53 -07001584 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001585 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001586 // Is this activity's application already running?
1587 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001588 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001589
Andrii Kulian02b7a832016-10-06 23:11:56 -07001590 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001591
1592 if (app != null && app.thread != null) {
1593 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001594 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1595 || !"android".equals(r.info.packageName)) {
1596 // Don't add this if it is a platform component that is marked
1597 // to run in multiple processes, because this is actually
1598 // part of the framework so doesn't make sense to track as a
1599 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001600 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1601 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001602 }
George Mount2c92c972014-03-20 09:38:23 -07001603 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001604 return;
1605 } catch (RemoteException e) {
1606 Slog.w(TAG, "Exception when starting activity "
1607 + r.intent.getComponent().flattenToShortString(), e);
1608 }
1609
1610 // If a dead object exception was thrown -- fall through to
1611 // restart the application.
1612 }
1613
1614 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001615 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001616 }
1617
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001618 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001619 String resultWho, int requestCode, int callingPid, int callingUid,
1620 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001621 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001622 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1623 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001624 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001625 return true;
1626 }
1627 final int componentRestriction = getComponentRestrictionForCallingPackage(
1628 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1629 final int actionRestriction = getActionRestrictionForCallingPackage(
1630 intent.getAction(), callingPackage, callingPid, callingUid);
1631 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1632 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1633 if (resultRecord != null) {
1634 resultStack.sendActivityResultLocked(-1,
1635 resultRecord, resultWho, requestCode,
1636 Activity.RESULT_CANCELED, null);
1637 }
1638 final String msg;
1639 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1640 msg = "Permission Denial: starting " + intent.toString()
1641 + " from " + callerApp + " (pid=" + callingPid
1642 + ", uid=" + callingUid + ")" + " with revoked permission "
1643 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1644 } else if (!aInfo.exported) {
1645 msg = "Permission Denial: starting " + intent.toString()
1646 + " from " + callerApp + " (pid=" + callingPid
1647 + ", uid=" + callingUid + ")"
1648 + " not exported from uid " + aInfo.applicationInfo.uid;
1649 } else {
1650 msg = "Permission Denial: starting " + intent.toString()
1651 + " from " + callerApp + " (pid=" + callingPid
1652 + ", uid=" + callingUid + ")"
1653 + " requires " + aInfo.permission;
1654 }
1655 Slog.w(TAG, msg);
1656 throw new SecurityException(msg);
1657 }
1658
1659 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1660 final String message = "Appop Denial: starting " + intent.toString()
1661 + " from " + callerApp + " (pid=" + callingPid
1662 + ", uid=" + callingUid + ")"
1663 + " requires " + AppOpsManager.permissionToOp(
1664 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1665 Slog.w(TAG, message);
1666 return false;
1667 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1668 final String message = "Appop Denial: starting " + intent.toString()
1669 + " from " + callerApp + " (pid=" + callingPid
1670 + ", uid=" + callingUid + ")"
1671 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1672 Slog.w(TAG, message);
1673 return false;
1674 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001675 if (options != null) {
1676 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1677 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1678 callingPid, callingUid);
1679 if (startInTaskPerm != PERMISSION_GRANTED) {
1680 final String msg = "Permission Denial: starting " + intent.toString()
1681 + " from " + callerApp + " (pid=" + callingPid
1682 + ", uid=" + callingUid + ") with launchTaskId="
1683 + options.getLaunchTaskId();
1684 Slog.w(TAG, msg);
1685 throw new SecurityException(msg);
1686 }
1687 }
1688 // Check if someone tries to launch an activity on a private display with a different
1689 // owner.
1690 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001691 if (launchDisplayId != INVALID_DISPLAY
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001692 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001693 final String msg = "Permission Denial: starting " + intent.toString()
1694 + " from " + callerApp + " (pid=" + callingPid
1695 + ", uid=" + callingUid + ") with launchDisplayId="
1696 + launchDisplayId;
1697 Slog.w(TAG, msg);
1698 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001699 }
1700 }
1701
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001702 return true;
1703 }
1704
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001705 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001706 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001707 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1708 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1709
Andrii Kulian62e6f252017-05-30 22:46:53 -07001710 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001711 if (activityDisplay == null) {
1712 Slog.w(TAG, "Launch on display check: display not found");
1713 return false;
1714 }
1715
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001716 // Check if the caller can manage activity stacks.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001717 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001718 callingUid);
1719 if (startAnyPerm == PERMISSION_GRANTED) {
1720 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1721 + " allow launch any on display");
1722 return true;
1723 }
1724
1725 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001726 && activityDisplay.mDisplay.getOwnerUid() != SYSTEM_UID) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001727 // Limit launching on virtual displays, because their contents can be read from Surface
1728 // by apps that created them.
1729 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1730 + " disallow launch on virtual display for not-embedded activity");
1731 return false;
1732 }
1733
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001734 if (!activityDisplay.isPrivate()) {
1735 // Anyone can launch on a public display.
1736 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1737 + " allow launch on public display");
1738 return true;
1739 }
1740
1741 // Check if the caller is the owner of the display.
1742 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1743 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1744 + " allow launch for owner of the display");
1745 return true;
1746 }
1747
1748 // Check if caller is present on display
1749 if (activityDisplay.isUidPresent(callingUid)) {
1750 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1751 + " allow launch for caller present on the display");
1752 return true;
1753 }
1754
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001755 Slog.w(TAG, "Launch on display check: denied");
1756 return false;
1757 }
1758
1759 /** Update lists of UIDs that are present on displays and have access to them. */
1760 void updateUIDsPresentOnDisplay() {
1761 mDisplayAccessUIDs.clear();
1762 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1763 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001764 // Only bother calculating the whitelist for private displays
1765 if (activityDisplay.isPrivate()) {
1766 mDisplayAccessUIDs.append(
1767 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1768 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001769 }
1770 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1771 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1772 }
1773
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001774 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001775 final long identity = Binder.clearCallingIdentity();
1776 try {
1777 return UserManager.get(mService.mContext).getUserInfo(userId);
1778 } finally {
1779 Binder.restoreCallingIdentity(identity);
1780 }
1781 }
1782
Svet Ganov99b60432015-06-27 13:15:22 -07001783 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001784 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001785 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1786 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001787 == PackageManager.PERMISSION_DENIED) {
1788 return ACTIVITY_RESTRICTION_PERMISSION;
1789 }
1790
Christopher Tateff7add02015-08-17 10:23:22 -07001791 if (activityInfo.permission == null) {
1792 return ACTIVITY_RESTRICTION_NONE;
1793 }
1794
Svet Ganov99b60432015-06-27 13:15:22 -07001795 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1796 if (opCode == AppOpsManager.OP_NONE) {
1797 return ACTIVITY_RESTRICTION_NONE;
1798 }
1799
1800 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1801 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001802 if (!ignoreTargetSecurity) {
1803 return ACTIVITY_RESTRICTION_APPOP;
1804 }
Svet Ganov99b60432015-06-27 13:15:22 -07001805 }
1806
1807 return ACTIVITY_RESTRICTION_NONE;
1808 }
1809
Svetoslav7008b512015-06-24 18:47:07 -07001810 private int getActionRestrictionForCallingPackage(String action,
1811 String callingPackage, int callingPid, int callingUid) {
1812 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001813 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001814 }
1815
1816 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1817 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001818 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001819 }
1820
1821 final PackageInfo packageInfo;
1822 try {
1823 packageInfo = mService.mContext.getPackageManager()
1824 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1825 } catch (PackageManager.NameNotFoundException e) {
1826 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001827 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001828 }
1829
1830 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001831 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001832 }
1833
1834 if (mService.checkPermission(permission, callingPid, callingUid) ==
1835 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001836 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001837 }
1838
1839 final int opCode = AppOpsManager.permissionToOpCode(permission);
1840 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001841 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001842 }
1843
1844 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1845 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001846 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001847 }
1848
Svet Ganov99b60432015-06-27 13:15:22 -07001849 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001850 }
1851
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001852 void setLaunchSource(int uid) {
1853 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1854 }
1855
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001856 void acquireLaunchWakelock() {
1857 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1858 throw new IllegalStateException("Calling must be system uid");
1859 }
1860 mLaunchingActivity.acquire();
1861 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1862 // To be safe, don't allow the wake lock to be held for too long.
1863 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1864 }
1865 }
1866
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001867 /**
1868 * Called when the frontmost task is idle.
1869 * @return the state of mService.mBooting before this was called.
1870 */
1871 private boolean checkFinishBootingLocked() {
1872 final boolean booting = mService.mBooting;
1873 boolean enableScreen = false;
1874 mService.mBooting = false;
1875 if (!mService.mBooted) {
1876 mService.mBooted = true;
1877 enableScreen = true;
1878 }
1879 if (booting || enableScreen) {
1880 mService.postFinishBooting(booting, enableScreen);
1881 }
1882 return booting;
1883 }
1884
Craig Mautnerf3333272013-04-22 10:55:53 -07001885 // Checked.
1886 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001887 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001888 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001889
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001891 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001892 int NS = 0;
1893 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001894 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 boolean activityRemoved = false;
1896
Wale Ogunwale7d701172015-03-11 15:36:30 -07001897 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001899 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1900 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001901 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1902 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001903 if (fromTimeout) {
1904 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001905 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001906
1907 // This is a hack to semi-deal with a race condition
1908 // in the client where it can be constructed with a
1909 // newer configuration from when we asked it to launch.
1910 // We'll update with whatever configuration it now says
1911 // it used to launch.
1912 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001913 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001914 }
1915
1916 // We are now idle. If someone is waiting for a thumbnail from
1917 // us, we can now deliver.
1918 r.idle = true;
1919
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001920 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001921 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001922 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001923 }
1924 }
1925
1926 if (allResumedActivitiesIdle()) {
1927 if (r != null) {
1928 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001929 }
1930
1931 if (mLaunchingActivity.isHeld()) {
1932 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1933 if (VALIDATE_WAKE_LOCK_CALLER &&
1934 Binder.getCallingUid() != Process.myUid()) {
1935 throw new IllegalStateException("Calling must be system uid");
1936 }
1937 mLaunchingActivity.release();
1938 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001939 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001940 }
1941
1942 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001943 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1944 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001945 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001946 if ((NF = mFinishingActivities.size()) > 0) {
1947 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001948 mFinishingActivities.clear();
1949 }
1950
Craig Mautnerf3333272013-04-22 10:55:53 -07001951 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001952 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001953 mStartingUsers.clear();
1954 }
1955
Craig Mautnerf3333272013-04-22 10:55:53 -07001956 // Stop any activities that are scheduled to do so but have been
1957 // waiting for the next one to start.
1958 for (int i = 0; i < NS; i++) {
1959 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001960 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001961 if (stack != null) {
1962 if (r.finishing) {
1963 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1964 } else {
1965 stack.stopActivityLocked(r);
1966 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001967 }
1968 }
1969
1970 // Finish any activities that are scheduled to do so but have been
1971 // waiting for the next one to start.
1972 for (int i = 0; i < NF; i++) {
1973 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001974 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001975 if (stack != null) {
1976 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1977 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001978 }
1979
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001980 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001981 // Complete user switch
1982 if (startingUsers != null) {
1983 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001984 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001985 }
1986 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001987 }
1988
1989 mService.trimApplications();
1990 //dump();
1991 //mWindowManager.dump();
1992
Craig Mautnerf3333272013-04-22 10:55:53 -07001993 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001994 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001995 }
1996
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001997 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001998 }
1999
Craig Mautner8e569572013-10-11 17:36:59 -07002000 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002001 boolean hasVisibleActivities = false;
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 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2006 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002007 }
Craig Mautner19091252013-10-05 00:03:53 -07002008 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002009 }
2010
2011 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2013 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002014 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2015 stacks.get(stackNdx).closeSystemDialogsLocked();
2016 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002017 }
2018 }
2019
Craig Mautner93529a42013-10-04 15:03:13 -07002020 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002021 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002022 }
2023
Craig Mautner8d341ef2013-03-26 09:03:27 -07002024 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002025 * Update the last used stack id for non-current user (current user's last
2026 * used stack is the focused stack)
2027 */
2028 void updateUserStackLocked(int userId, ActivityStack stack) {
2029 if (userId != mCurrentUser) {
2030 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2031 }
2032 }
2033
2034 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002035 * @return true if some activity was finished (or would have finished if doit were true).
2036 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002037 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2038 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002039 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002040 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2041 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002042 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002043 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002044 if (stack.finishDisabledPackageActivitiesLocked(
2045 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002046 didSomething = true;
2047 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002048 }
2049 }
2050 return didSomething;
2051 }
2052
Dianne Hackborna413dc02013-07-12 12:02:55 -07002053 void updatePreviousProcessLocked(ActivityRecord r) {
2054 // Now that this process has stopped, we may want to consider
2055 // it to be the previous app to try to keep around in case
2056 // the user wants to return to it.
2057
2058 // First, found out what is currently the foreground app, so that
2059 // we don't blow away the previous app if this activity is being
2060 // hosted by the process that is actually still the foreground.
2061 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002062 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2063 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002064 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2065 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002066 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002067 if (stack.mResumedActivity != null) {
2068 fgApp = stack.mResumedActivity.app;
2069 } else if (stack.mPausingActivity != null) {
2070 fgApp = stack.mPausingActivity.app;
2071 }
2072 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002073 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002074 }
2075 }
2076
2077 // Now set this one as the previous process, only if that really
2078 // makes sense to.
2079 if (r.app != null && fgApp != null && r.app != fgApp
2080 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002081 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002082 mService.mPreviousProcess = r.app;
2083 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2084 }
2085 }
2086
Wale Ogunwaled046a012015-12-24 13:05:59 -08002087 boolean resumeFocusedStackTopActivityLocked() {
2088 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002089 }
2090
Wale Ogunwaled046a012015-12-24 13:05:59 -08002091 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002092 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002093
2094 if (!readyToResume()) {
2095 return false;
2096 }
2097
Wale Ogunwaled046a012015-12-24 13:05:59 -08002098 if (targetStack != null && isFocusedStack(targetStack)) {
2099 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002100 }
chaviw59b98852017-06-13 12:05:44 -07002101
Wale Ogunwale06579d62016-04-30 15:29:06 -07002102 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2103 if (r == null || r.state != RESUMED) {
2104 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002105 } else if (r.state == RESUMED) {
2106 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2107 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002108 }
chaviw59b98852017-06-13 12:05:44 -07002109
Wale Ogunwaled046a012015-12-24 13:05:59 -08002110 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002111 }
2112
Todd Kennedy39bfee52016-02-24 10:28:21 -08002113 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2114 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2115 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2116 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2117 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2118 }
2119 }
2120 }
2121
Adrian Roos20d7df32016-01-12 18:59:43 +01002122 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2123 TaskRecord finishedTask = null;
2124 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002125 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2126 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002127 final int numStacks = stacks.size();
2128 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2129 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002130 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2131 if (stack == focusedStack || finishedTask == null) {
2132 finishedTask = t;
2133 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002134 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002135 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002136 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002137 }
2138
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002139 void finishVoiceTask(IVoiceInteractionSession session) {
2140 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2141 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2142 final int numStacks = stacks.size();
2143 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2144 final ActivityStack stack = stacks.get(stackNdx);
2145 stack.finishVoiceTask(session);
2146 }
2147 }
2148 }
2149
Andrii Kulianc27916642016-04-12 17:59:27 -07002150 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2151 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002152 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2153 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002154 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002155 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2156 // Caller wants the home activity moved with it. To accomplish this,
2157 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002158 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002159 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002160 ActivityStack currentStack = task.getStack();
2161 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002162 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2163 + task + " to front. Stack is null");
2164 return;
2165 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002166
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002167 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002168 int stackId = options.getLaunchStackId();
2169 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002170 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002171 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002172 if (stackId == INVALID_STACK_ID) {
2173 stackId = task.getLaunchStackId();
2174 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002175 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002176 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2177 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002178 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002179 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2180 // still need moveTaskToFrontLocked() below for any transition settings.
2181 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002182 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2183 resizeStackLocked(stackId, bounds,
2184 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002185 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002186 } else {
2187 // WM resizeTask must be done after the task is moved to the correct stack,
2188 // because Task's setBounds() also updates dim layer's bounds, but that has
2189 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002190 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002191 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002192 }
2193 }
2194
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002195 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002196 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002197 r == null ? null : r.appTimeTracker, reason);
2198
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002199 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002200 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002201
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002202 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
2203 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002204 }
2205
Wale Ogunwale854809c2015-12-27 16:18:19 -08002206 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002207 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002208 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002209 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002210 return false;
2211 }
2212 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2213 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002214 }
2215
Winson Chung55893332017-02-17 17:13:10 -08002216 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002217 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002218 }
2219
Winson Chung55893332017-02-17 17:13:10 -08002220 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2221 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002222 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002223 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002224 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002225 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002226 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002227 return null;
2228 }
Matthew Ng330757d2017-02-28 14:19:17 -08002229 if (stackId == DOCKED_STACK_ID) {
2230 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2231 // the other side of the docked stack and we make visibility decisions based on that.
2232 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2233 }
Winson Chung55893332017-02-17 17:13:10 -08002234 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002235 }
2236
Andrii Kulian16802aa2016-11-02 12:21:33 -07002237 /**
2238 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2239 * If there is no such stack, new dynamic stack can be created.
2240 * @param displayId Target display.
2241 * @param r Activity that should be launched there.
2242 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2243 */
2244 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002245 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002246 if (activityDisplay == null) {
2247 throw new IllegalArgumentException(
2248 "Display with displayId=" + displayId + " not found.");
2249 }
2250
2251 // Return the topmost valid stack on the display.
2252 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2253 final ActivityStack stack = activityDisplay.mStacks.get(i);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002254 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002255 return stack;
2256 }
2257 }
2258
2259 // If there is no valid stack on the external display - check if new dynamic stack will do.
2260 if (displayId != Display.DEFAULT_DISPLAY) {
2261 final int newDynamicStackId = getNextStackId();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002262 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002263 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2264 }
2265 }
2266
2267 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2268 return null;
2269 }
2270
Craig Mautner967212c2013-04-13 21:10:58 -07002271 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002272 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002273 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2274 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002275 }
2276 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002277 }
2278
Jorim Jaggife762342016-10-13 14:33:27 +02002279 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2280 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2281 }
2282
Andrii Kulian7fc22812016-12-28 13:04:11 -08002283 /**
2284 * Get next focusable stack in the system. This will search across displays and stacks
2285 * in last-focused order for a focusable and visible stack, different from the target stack.
2286 *
2287 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2288 * searching for next candidate.
2289 * @return Next focusable {@link ActivityStack}, null if not found.
2290 */
2291 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2292 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2293
2294 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2295 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002296 // If a display is registered in WM, it must also be available in AM.
2297 @SuppressWarnings("ConstantConditions")
2298 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002299 for (int j = stacks.size() - 1; j >= 0; --j) {
2300 final ActivityStack stack = stacks.get(j);
2301 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002302 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002303 return stack;
2304 }
2305 }
2306 }
2307
2308 return null;
2309 }
2310
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002311 /**
2312 * Get next valid stack for launching provided activity in the system. This will search across
2313 * displays and stacks in last-focused order for a focusable and visible stack, except those
2314 * that are on a currently focused display.
2315 *
2316 * @param r The activity that is being launched.
2317 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2318 * searching for the next candidate.
2319 * @return Next valid {@link ActivityStack}, null if not found.
2320 */
2321 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2322 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2323 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2324 final int displayId = mTmpOrderedDisplayIds.get(i);
2325 if (displayId == currentFocus) {
2326 continue;
2327 }
2328 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2329 if (stack != null) {
2330 return stack;
2331 }
2332 }
2333 return null;
2334 }
2335
Craig Mautneree2e45a2014-06-27 12:10:03 -07002336 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002337 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002338 }
2339
2340 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002341 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2342 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2343 final TaskRecord task = tasks.get(taskNdx);
2344 if (task.isHomeTask()) {
2345 final ArrayList<ActivityRecord> activities = task.mActivities;
2346 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2347 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002348 if (r.isHomeActivity()
2349 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002350 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002351 }
2352 }
2353 }
2354 }
2355 return null;
2356 }
2357
Chong Zhangb15758a2015-11-17 12:12:03 -08002358 /**
2359 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2360 * the docked stack itself, or if it's side-by-side to the docked stack.
2361 */
2362 boolean isStackDockedInEffect(int stackId) {
2363 return stackId == DOCKED_STACK_ID ||
2364 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2365 }
2366
Todd Kennedyca4d8422015-01-15 15:19:22 -08002367 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002368 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002369 ActivityContainer activityContainer =
2370 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002371 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002372 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2373 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002374 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002375 return activityContainer;
2376 }
2377
Craig Mautner34b73df2014-01-12 21:11:08 -08002378 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002379 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2380 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2381 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002382 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2383 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002384 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002385 }
2386 }
2387
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002388 void deleteActivityContainerRecord(int stackId) {
2389 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2390 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2391 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002392 }
2393
Jorim Jaggidc249c42015-12-15 14:57:31 -08002394 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002395 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002396 if (stackId == DOCKED_STACK_ID) {
2397 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002398 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002399 return;
2400 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002401 final ActivityStack stack = getStack(stackId);
2402 if (stack == null) {
2403 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2404 return;
2405 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002406
Winson Chunga2249ac2017-03-30 18:15:30 -07002407 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2408 getStack(DOCKED_STACK_ID) != null) {
2409 // If the docked stack exists, don't resize non-floating stacks independently of the
2410 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002411 return;
2412 }
2413
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002414 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002415 mWindowManager.deferSurfaceLayout();
2416 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002417 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002418 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002419 stack.ensureVisibleActivitiesConfigurationLocked(
2420 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002421 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002422 } finally {
2423 mWindowManager.continueSurfaceLayout();
2424 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002425 }
2426 }
2427
Jorim Jaggi192086e2016-03-11 17:17:03 +01002428 void deferUpdateBounds(int stackId) {
2429 final ActivityStack stack = getStack(stackId);
2430 if (stack != null) {
2431 stack.deferUpdateBounds();
2432 }
2433 }
2434
2435 void continueUpdateBounds(int stackId) {
2436 final ActivityStack stack = getStack(stackId);
2437 if (stack != null) {
2438 stack.continueUpdateBounds();
2439 }
2440 }
2441
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002442 void notifyAppTransitionDone() {
Matthew Ng606dd802017-06-05 14:06:32 -07002443 continueUpdateBounds(RECENTS_STACK_ID);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002444 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2445 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002446 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002447 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002448 if (task != null) {
2449 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002450 }
2451 }
2452 mResizingTasksDuringAnimation.clear();
2453 }
2454
Robert Carre7cc44d2017-03-20 19:04:30 -07002455 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2456 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002457
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002458 final ActivityStack stack = getStack(fromStackId);
2459 if (stack == null) {
2460 return;
2461 }
2462
2463 mWindowManager.deferSurfaceLayout();
2464 try {
2465 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002466 // We are moving all tasks from the docked stack to the fullscreen stack,
2467 // which is dismissing the docked stack, so resize all other stacks to
2468 // fullscreen here already so we don't end up with resize trashing.
2469 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2470 if (StackId.isResizeableByDockedStack(i)) {
2471 ActivityStack otherStack = getStack(i);
2472 if (otherStack != null) {
2473 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2474 true /* allowResizeInDockedMode */, DEFER_RESUME);
2475 }
2476 }
2477 }
2478
2479 // Also disable docked stack resizing since we have manually adjusted the
2480 // size of other stacks above and we don't want to trigger a docked stack
2481 // resize when we remove task from it below and it is detached from the
2482 // display because it no longer contains any tasks.
2483 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002484 } else if (fromStackId == PINNED_STACK_ID) {
2485 if (onTop) {
2486 // Log if we are expanding the PiP to fullscreen
2487 MetricsLogger.action(mService.mContext,
2488 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2489 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002490 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002491 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002492 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002493 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002494 // If we are moving from the pinned stack, then the animation takes care of updating
2495 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002496 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002497 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2498 final int size = tasks.size();
2499 if (onTop) {
2500 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002501 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002502 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002503 if (fromStackId == PINNED_STACK_ID) {
2504 // Update the return-to to reflect where the pinned stack task was moved
2505 // from so that we retain the stack that was previously visible if the
2506 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002507 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2508 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002509 }
Winson Chung74666102017-02-22 17:49:24 -08002510 // Defer resume until all the tasks have been moved to the fullscreen stack
2511 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2512 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002513 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002514 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002515 }
2516 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002517 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002518 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002519 // Position the tasks in the fullscreen stack in order at the bottom of the
2520 // stack. Also defer resume until all the tasks have been moved to the
2521 // fullscreen stack.
2522 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002523 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2524 schedulePictureInPictureModeChange,
2525 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002526 }
2527 }
Winson Chung74666102017-02-22 17:49:24 -08002528
2529 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2530 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002531 } finally {
2532 mAllowDockedStackResize = true;
2533 mWindowManager.continueSurfaceLayout();
2534 }
2535 }
2536
Robert Carr6914f082017-03-20 19:04:30 -07002537 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002538 mWindowManager.inSurfaceTransaction(
2539 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002540 }
2541
Jorim Jaggidc249c42015-12-15 14:57:31 -08002542 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002543 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2544 boolean preserveWindows) {
2545 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2546 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2547 false /* deferResume */);
2548 }
2549
2550 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2551 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2552 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002553
2554 if (!mAllowDockedStackResize) {
2555 // Docked stack resize currently disabled.
2556 return;
2557 }
2558
Jorim Jaggidc249c42015-12-15 14:57:31 -08002559 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2560 if (stack == null) {
2561 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2562 return;
2563 }
2564
2565 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2566 mWindowManager.deferSurfaceLayout();
2567 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002568 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2569 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002570 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002571 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002572
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002573 // TODO: Checking for isAttached might not be needed as if the user passes in null
2574 // dockedBounds then they want the docked stack to be dismissed.
2575 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2576 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002577 // In this case we make all other static stacks fullscreen and move all
2578 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002579 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002580
2581 // stack shouldn't contain anymore activities, so nothing to resume.
2582 r = null;
2583 } else {
2584 // Docked stacks occupy a dedicated region on screen so the size of all other
2585 // static stacks need to be adjusted so they don't overlap with the docked stack.
2586 // We get the bounds to use from window manager which has been adjusted for any
2587 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002588 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002589 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002590 final ActivityStack current = getStack(i);
2591 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002592 current.getStackDockedModeBounds(
2593 tempOtherTaskBounds /* currentTempTaskBounds */,
2594 tempRect /* outStackBounds */,
2595 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2596
Andrii Kuliane48b7d82017-04-28 14:51:05 -07002597 resizeStackLocked(i, !tempRect.isEmpty() ? tempRect : null,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002598 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2599 tempOtherTaskInsetBounds, preserveWindows,
2600 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002601 }
2602 }
2603 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002604 if (!deferResume) {
2605 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2606 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002607 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002608 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002609 mWindowManager.continueSurfaceLayout();
2610 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2611 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002612 }
2613
Robert Carr0d00c2e2016-02-29 17:45:02 -08002614 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002615 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002616 if (stack == null) {
2617 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2618 return;
2619 }
Winson Chung19953ca2017-04-11 11:19:23 -07002620
2621 // It is possible for the bounds animation from the WM to call this but be delayed by
2622 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2623 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2624 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2625 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002626 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002627 return;
2628 }
2629
Robert Carr0d00c2e2016-02-29 17:45:02 -08002630 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2631 mWindowManager.deferSurfaceLayout();
2632 try {
2633 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002634 Rect insetBounds = null;
2635 if (tempPinnedTaskBounds != null) {
2636 // We always use 0,0 as the position for the inset rect because
2637 // if we are getting insets at all in the pinned stack it must mean
2638 // we are headed for fullscreen.
2639 insetBounds = tempRect;
2640 insetBounds.top = 0;
2641 insetBounds.left = 0;
2642 insetBounds.right = tempPinnedTaskBounds.width();
2643 insetBounds.bottom = tempPinnedTaskBounds.height();
2644 }
2645 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002646 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002647 } finally {
2648 mWindowManager.continueSurfaceLayout();
2649 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2650 }
2651 }
2652
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002653 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002654 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002655 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002656 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002657 }
2658
Wale Ogunwale1666e312016-12-16 11:27:18 -08002659 final ActivityContainer activityContainer =
2660 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002661 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002662 }
2663
Robert Carr6914f082017-03-20 19:04:30 -07002664
Robert Carre7cc44d2017-03-20 19:04:30 -07002665 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002666 final ActivityStack stack = getStack(stackId);
2667 if (stack == null) {
2668 return;
2669 }
2670
2671 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2672 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002673 /**
2674 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2675 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2676 * that the client receives onStop() before it is reparented. We do this by detaching
2677 * the stack from the display so that it will be considered invisible when
2678 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2679 * invisible as well and added to the stopping list. After which we process the
2680 * stopping list by handling the idle.
2681 */
2682 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2683 pinnedStack.mForceHidden = true;
2684 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2685 pinnedStack.mForceHidden = false;
2686 activityIdleInternalLocked(null, false /* fromTimeout */,
2687 true /* processPausingActivites */, null /* configuration */);
2688
2689 // Move all the tasks to the bottom of the fullscreen stack
2690 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002691 } else {
2692 for (int i = tasks.size() - 1; i >= 0; i--) {
2693 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2694 REMOVE_FROM_RECENTS);
2695 }
2696 }
2697 }
2698
2699 /**
Robert Carr6914f082017-03-20 19:04:30 -07002700 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2701 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2702 * instead moved back onto the fullscreen stack.
2703 */
2704 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002705 mWindowManager.inSurfaceTransaction(
2706 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002707 }
2708
2709 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002710 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2711 */
2712 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2713 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2714 }
2715
2716 /**
Winson Chung010927a2016-12-15 16:12:35 -08002717 * Removes the task with the specified task id.
2718 *
2719 * @param taskId Identifier of the task to be removed.
2720 * @param killProcess Kill any process associated with the task if possible.
2721 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002722 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2723 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002724 * @return Returns true if the given task was found and removed.
2725 */
Winson Chung6954fc92017-03-24 16:22:12 -07002726 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2727 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002728 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2729 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002730 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002731 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002732 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2733 if (tr.isPersistable) {
2734 mService.notifyTaskPersisterLocked(null, true);
2735 }
2736 return true;
2737 }
2738 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2739 return false;
2740 }
2741
2742 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2743 if (removeFromRecents) {
2744 mRecentTasks.remove(tr);
2745 tr.removedFromRecents();
2746 }
2747 ComponentName component = tr.getBaseIntent().getComponent();
2748 if (component == null) {
2749 Slog.w(TAG, "No component for base intent of task: " + tr);
2750 return;
2751 }
2752
2753 // Find any running services associated with this app and stop if needed.
2754 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2755
2756 if (!killProcess) {
2757 return;
2758 }
2759
2760 // Determine if the process(es) for this task should be killed.
2761 final String pkg = component.getPackageName();
2762 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2763 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2764 for (int i = 0; i < pmap.size(); i++) {
2765
2766 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2767 for (int j = 0; j < uids.size(); j++) {
2768 ProcessRecord proc = uids.valueAt(j);
2769 if (proc.userId != tr.userId) {
2770 // Don't kill process for a different user.
2771 continue;
2772 }
2773 if (proc == mService.mHomeProcess) {
2774 // Don't kill the home process along with tasks from the same package.
2775 continue;
2776 }
2777 if (!proc.pkgList.containsKey(pkg)) {
2778 // Don't kill process that is not associated with this task.
2779 continue;
2780 }
2781
2782 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002783 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002784 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2785 // Don't kill process(es) that has an activity in a different task that is
2786 // also in recents.
2787 return;
2788 }
2789 }
2790
2791 if (proc.foregroundServices) {
2792 // Don't kill process(es) with foreground service.
2793 return;
2794 }
2795
2796 // Add process to kill list.
2797 procsToKill.add(proc);
2798 }
2799 }
2800
2801 // Kill the running processes.
2802 for (int i = 0; i < procsToKill.size(); i++) {
2803 ProcessRecord pr = procsToKill.get(i);
2804 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2805 && pr.curReceivers.isEmpty()) {
2806 pr.kill("remove task", true);
2807 } else {
2808 // We delay killing processes that are not in the background or running a receiver.
2809 pr.waitingToKill = "remove task";
2810 }
2811 }
2812 }
2813
Craig Mautner4a1cb222013-12-04 16:14:06 -08002814 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002815 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002816 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2817 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002818 break;
2819 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002820 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002821 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002822 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002823 }
2824
Chong Zhang5dcb2752015-08-18 13:50:26 -07002825 /**
2826 * Restores a recent task to a stack
2827 * @param task The recent task to be restored.
2828 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002829 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2830 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002831 * @return true if the task has been restored successfully.
2832 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002833 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002834 if (!StackId.isStaticStack(stackId)) {
2835 // If stack is not static (or stack id is invalid) - use the default one.
2836 // This means that tasks that were on external displays will be restored on the
2837 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002838 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002839 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002840 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2841 // Put it in the fullscreen stack.
2842 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002843 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002844
Andrii Kulian02b7a832016-10-06 23:11:56 -07002845 final ActivityStack currentStack = task.getStack();
2846 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002847 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002848 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002849 // Nothing else to do since it is already restored in the right stack.
2850 return true;
2851 }
2852 // Remove current stack association, so we can re-associate the task with the
2853 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002854 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002855 }
2856
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002857 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002858 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002859
2860 if (stack == null) {
2861 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002862 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2863 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002864 return false;
2865 }
2866
Wale Ogunwale72919d22016-12-08 18:58:50 -08002867 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002868 // TODO: move call for creation here and other place into Stack.addTask()
2869 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002870 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2871 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002872 final ArrayList<ActivityRecord> activities = task.mActivities;
2873 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002874 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002875 }
2876 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002877 }
2878
Wale Ogunwale040b4702015-08-06 18:10:50 -07002879 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002880 * Move stack with all its existing content to specified display.
2881 * @param stackId Id of stack to move.
2882 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002883 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002884 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002885 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002886 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002887 if (activityDisplay == null) {
2888 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2889 + displayId);
2890 }
2891 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2892 if (activityContainer != null) {
2893 if (activityContainer.isAttachedLocked()) {
2894 if (activityContainer.getDisplayId() == displayId) {
2895 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2896 + " to its current displayId=" + displayId);
2897 }
2898
Andrii Kulian250d6532017-02-08 23:30:45 -08002899 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002900 } else {
2901 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2902 + stackId + " is not attached to any display.");
2903 }
2904 } else {
2905 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2906 + stackId);
2907 }
Andrii Kulian839def92016-11-02 10:58:58 -07002908 // TODO(multi-display): resize stacks properly if moved from split-screen.
2909 }
2910
2911 /**
Winson Chung74666102017-02-22 17:49:24 -08002912 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2913 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002914 */
Winson Chung74666102017-02-22 17:49:24 -08002915 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002916 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002917
Winson Chung74666102017-02-22 17:49:24 -08002918 // Check that we aren't reparenting to the same stack that the task is already in
2919 if (prevStack != null && prevStack.mStackId == stackId) {
2920 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2921 + " already in stackId=" + stackId);
2922 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002923 }
2924
Winson Chung74666102017-02-22 17:49:24 -08002925 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2926 // support
2927 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2928 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2929 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002930 }
2931
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002932 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2933 // multi-display.
2934 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2935 // TODO: Check ActivityView after fixing b/35349678.
2936 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2937 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2938 + " reparent task=" + task + " to stackId=" + stackId);
2939 }
2940
Winson Chung74666102017-02-22 17:49:24 -08002941 // Ensure that we aren't trying to move into a freeform stack without freeform
2942 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002943 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002944 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2945 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002946 }
2947
Winson Chung74666102017-02-22 17:49:24 -08002948 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2949 // used for split-screen mode and will cause things like the docked divider to show up. We
2950 // instead leave the task in its current stack or move it to the fullscreen stack if it
2951 // isn't currently in a stack.
2952 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2953 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2954 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2955 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002956 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002957
Winson Chung74666102017-02-22 17:49:24 -08002958 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2959 // example, a docked stack is created which will lead to the stack we are moving from being
2960 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002961 try {
Winson Chung74666102017-02-22 17:49:24 -08002962 task.mTemporarilyUnresizable = true;
2963 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002964 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002965 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002966 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002967 }
2968
Winson Chung08f81892017-03-02 15:40:51 -08002969 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002970 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2971 if (stack == null) {
2972 throw new IllegalArgumentException(
2973 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2974 }
2975
2976 final ActivityRecord r = stack.topRunningActivityLocked();
2977 if (r == null) {
2978 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2979 + " in stack=" + stack);
2980 return false;
2981 }
2982
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002983 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002984 Slog.w(TAG,
2985 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002986 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002987 return false;
2988 }
2989
Winson Chung3a682872017-04-10 14:38:05 -07002990 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002991 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002992 return true;
2993 }
2994
Winson Chung8bca9e42017-04-16 15:59:43 -07002995 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002996 boolean moveHomeStackToFront, String reason) {
2997
Wale Ogunwale480dca02016-02-06 13:58:29 -08002998 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002999
Bryce Lee04ab3462017-04-10 15:06:33 -07003000 // This will clear the pinned stack by moving an existing task to the full screen stack,
3001 // ensuring only one task is present.
3002 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
3003
Wale Ogunwale1666e312016-12-16 11:27:18 -08003004 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08003005 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003006
Wale Ogunwale480dca02016-02-06 13:58:29 -08003007 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003008 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003009 // Resize the pinned stack to match the current size of the task the activity we are
3010 // going to be moving is currently contained in. We do this to have the right starting
3011 // animation bounds for the pinned stack to the desired bounds the caller wants.
3012 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
3013 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003014 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003015
3016 if (task.mActivities.size() == 1) {
3017 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003018 // the stack without re-parenting the activity in a different task. We don't
3019 // move the home stack forward if we are currently entering picture-in-picture
3020 // while pausing because that changes the focused stack and may prevent the new
3021 // starting activity from resuming.
3022 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08003023 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003024 // Move the home stack forward if the task we just moved to the pinned stack
3025 // was launched from home so home should be visible behind it.
3026 moveHomeStackToFront(reason);
3027 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003028 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003029 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003030 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003031 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003032 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003033 // reveal/leave the other activities in their original task.
3034
3035 // Currently, we don't support reparenting activities across tasks in two different
3036 // stacks, so instead, just create a new task in the same stack, reparent the
3037 // activity into that task, and then reparent the whole task to the new stack. This
3038 // ensures that all the necessary work to migrate states in the old and new stacks
3039 // is also done.
3040 final TaskRecord newTask = task.getStack().createTaskRecord(
3041 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
3042 r.mActivityType);
3043 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3044
Winson Chung5af42fc2017-03-24 17:11:33 -07003045 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003046 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003047 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003048 }
Winson Chungc2baac02017-01-11 13:34:47 -08003049
3050 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3051 // to the pinned stack
3052 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003053 } finally {
3054 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003055 }
3056
Winson Chunge7ba6862017-05-24 12:13:33 -07003057 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3058 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3059 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003060 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3061
Winson Chunge7ba6862017-05-24 12:13:33 -07003062 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3063 true /* fromFullscreen */);
3064
3065 // Update the visibility of all activities after the they have been reparented to the new
3066 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3067 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3068 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003069 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003070 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003071
Winson Chungb5026902017-05-03 12:45:13 -07003072 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName,
3073 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003074 }
3075
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003076 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003077 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3078 if (r == null || !r.isFocusable()) {
3079 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3080 "moveActivityStackToFront: unfocusable r=" + r);
3081 return false;
3082 }
3083
Bryce Leeaf691c02017-03-20 14:20:22 -07003084 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003085 final ActivityStack stack = r.getStack();
3086 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003087 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3088 + r + " task=" + task);
3089 return false;
3090 }
3091
Chong Zhang6cda19c2016-06-14 19:07:56 -07003092 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3093 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3094 "moveActivityStackToFront: already on top, r=" + r);
3095 return false;
3096 }
3097
3098 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3099 "moveActivityStackToFront: r=" + r);
3100
3101 stack.moveToFront(reason, task);
3102 return true;
3103 }
3104
David Stevensc6b91c62017-02-08 14:23:58 -08003105 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003106 mTmpFindTaskResult.r = null;
3107 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003108 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003109 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003110 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3111 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003112 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3113 final ActivityStack stack = stacks.get(stackNdx);
Winson Chung91e5c882017-04-21 14:44:38 -07003114 if (!checkActivityBelongsInStack(r, stack)) {
3115 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3116 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003117 continue;
3118 }
3119 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003120 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3121 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003122 continue;
3123 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003124 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003125 // It is possible to have tasks in multiple stacks with the same root affinity, so
3126 // we should keep looking after finding an affinity match to see if there is a
3127 // better match in another stack. Also, task affinity isn't a good enough reason
3128 // to target a display which isn't the source of the intent, so skip any affinity
3129 // matches not on the specified display.
3130 if (mTmpFindTaskResult.r != null) {
3131 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3132 return mTmpFindTaskResult.r;
3133 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003134 // Note: since the traversing through the stacks is top down, the floating
3135 // tasks should always have lower priority than any affinity-matching tasks
3136 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003137 affinityMatch = mTmpFindTaskResult.r;
3138 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003139 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003140 }
3141 }
Winson Chung5b895b72017-05-01 13:46:25 -07003142
David Stevensc6b91c62017-02-08 14:23:58 -08003143 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3144 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003145 }
3146
Winson Chung91e5c882017-04-21 14:44:38 -07003147 /**
3148 * Checks that for the given activity {@param r}, its activity type matches the {@param stack}
3149 * type.
3150 */
3151 private boolean checkActivityBelongsInStack(ActivityRecord r, ActivityStack stack) {
3152 if (r.isHomeActivity()) {
3153 return stack.isHomeStack();
3154 } else if (r.isRecentsActivity()) {
3155 return stack.isRecentsStack();
3156 } else if (r.isAssistantActivity()) {
3157 return stack.isAssistantStack();
3158 }
3159 return true;
3160 }
3161
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003162 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3163 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003164 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3165 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003166 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003167 final ActivityRecord ar = stacks.get(stackNdx)
3168 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003169 if (ar != null) {
3170 return ar;
3171 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003172 }
3173 }
3174 return null;
3175 }
3176
Craig Mautner8d341ef2013-03-26 09:03:27 -07003177 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003178 scheduleSleepTimeout();
3179 if (!mGoingToSleep.isHeld()) {
3180 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003181 if (mLaunchingActivity.isHeld()) {
3182 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3183 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003184 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003185 mLaunchingActivity.release();
3186 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003187 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003188 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003189 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003190 }
3191
3192 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003193 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003194
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003195 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003196 final long endTime = System.currentTimeMillis() + timeout;
3197 while (true) {
3198 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003199 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3200 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003201 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3202 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3203 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003204 }
3205 if (cantShutdown) {
3206 long timeRemaining = endTime - System.currentTimeMillis();
3207 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003208 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003209 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003210 } catch (InterruptedException e) {
3211 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003212 } else {
3213 Slog.w(TAG, "Activity manager shutdown timed out");
3214 timedout = true;
3215 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003216 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003217 } else {
3218 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003219 }
3220 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003221
3222 // Force checkReadyForSleep to complete.
3223 mSleepTimeout = true;
3224 checkReadyForSleepLocked();
3225
Craig Mautner8d341ef2013-03-26 09:03:27 -07003226 return timedout;
3227 }
3228
3229 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003230 removeSleepTimeouts();
3231 if (mGoingToSleep.isHeld()) {
3232 mGoingToSleep.release();
3233 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003234 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3235 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003236 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3237 final ActivityStack stack = stacks.get(stackNdx);
3238 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003239 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003240 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003241 }
Craig Mautner5314a402013-09-26 12:40:16 -07003242 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003243 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003244 mGoingToSleepActivities.clear();
3245 }
3246
3247 void activitySleptLocked(ActivityRecord r) {
3248 mGoingToSleepActivities.remove(r);
3249 checkReadyForSleepLocked();
3250 }
3251
3252 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003253 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003254 // Do not care.
3255 return;
3256 }
3257
3258 if (!mSleepTimeout) {
3259 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003260 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3261 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003262 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3263 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3264 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003265 }
3266
3267 if (mStoppingActivities.size() > 0) {
3268 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003269 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003270 + mStoppingActivities.size() + " activities");
3271 scheduleIdleLocked();
3272 dontSleep = true;
3273 }
3274
3275 if (mGoingToSleepActivities.size() > 0) {
3276 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003277 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003278 + mGoingToSleepActivities.size() + " activities");
3279 dontSleep = true;
3280 }
3281
3282 if (dontSleep) {
3283 return;
3284 }
3285 }
3286
Wei Wang65c7a152016-06-02 18:51:22 -07003287 // Send launch end powerhint before going sleep
3288 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3289
Craig Mautnere0a38842013-12-16 16:14:02 -08003290 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3291 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003292 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3293 stacks.get(stackNdx).goToSleep();
3294 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003295 }
3296
3297 removeSleepTimeouts();
3298
3299 if (mGoingToSleep.isHeld()) {
3300 mGoingToSleep.release();
3301 }
3302 if (mService.mShuttingDown) {
3303 mService.notifyAll();
3304 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003305 }
3306
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003307 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003308 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003309 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003310 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003311 }
3312 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003313 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003314 mWindowManager.executeAppTransition();
3315 return true;
3316 }
3317 return false;
3318 }
3319
Craig Mautner8d341ef2013-03-26 09:03:27 -07003320 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003321 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3322 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003323 int stackNdx = stacks.size() - 1;
3324 while (stackNdx >= 0) {
3325 stacks.get(stackNdx).handleAppCrashLocked(app);
3326 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003327 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003328 }
3329 }
3330
Craig Mautnerbb742462014-07-07 15:28:55 -07003331 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003332 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003333 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003334 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003335
3336 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003337 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003338 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003339 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003340 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003341
3342 // When launching tasks behind, update the last active time of the top task after the new
3343 // task has been shown briefly
3344 final ActivityRecord top = stack.topActivity();
3345 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003346 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003347 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003348 }
3349
3350 void scheduleLaunchTaskBehindComplete(IBinder token) {
3351 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3352 }
3353
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003354 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3355 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003356 mKeyguardController.beginActivityVisibilityUpdate();
3357 try {
3358 // First the front stacks. In case any are not fullscreen and are in front of home.
3359 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3360 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3361 final int topStackNdx = stacks.size() - 1;
3362 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3363 final ActivityStack stack = stacks.get(stackNdx);
3364 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3365 }
Craig Mautner580ea812013-04-25 12:58:38 -07003366 }
Jorim Jaggife762342016-10-13 14:33:27 +02003367 } finally {
3368 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003369 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003370 }
3371
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003372 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3373 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3374 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3375 final int topStackNdx = stacks.size() - 1;
3376 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3377 final ActivityStack stack = stacks.get(stackNdx);
3378 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3379 }
3380 }
3381 }
3382
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003383 void invalidateTaskLayers() {
3384 mTaskLayersChanged = true;
3385 }
3386
3387 void rankTaskLayersIfNeeded() {
3388 if (!mTaskLayersChanged) {
3389 return;
3390 }
3391 mTaskLayersChanged = false;
3392 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3393 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3394 int baseLayer = 0;
3395 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3396 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3397 }
3398 }
3399 }
3400
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003401 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3402 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3403 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3404 final int topStackNdx = stacks.size() - 1;
3405 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3406 final ActivityStack stack = stacks.get(stackNdx);
3407 stack.clearOtherAppTimeTrackers(except);
3408 }
3409 }
3410 }
3411
Craig Mautner8d341ef2013-03-26 09:03:27 -07003412 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003413 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3414 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 final int numStacks = stacks.size();
3416 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3417 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003418 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003419 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003420 }
3421 }
3422
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003423 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3424 // Examine all activities currently running in the process.
3425 TaskRecord firstTask = null;
3426 // Tasks is non-null only if two or more tasks are found.
3427 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003428 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3429 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003430 ActivityRecord r = app.activities.get(i);
3431 // First, if we find an activity that is in the process of being destroyed,
3432 // then we just aren't going to do anything for now; we want things to settle
3433 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003434 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003435 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003436 return;
3437 }
3438 // Don't consider any activies that are currently not in a state where they
3439 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003440 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3441 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003442 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003443 continue;
3444 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003445
3446 final TaskRecord task = r.getTask();
3447 if (task != null) {
3448 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003449 + " from " + r);
3450 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003451 firstTask = task;
3452 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003453 if (tasks == null) {
3454 tasks = new ArraySet<>();
3455 tasks.add(firstTask);
3456 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003457 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003458 }
3459 }
3460 }
3461 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003462 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003463 return;
3464 }
3465 // If we have activities in multiple tasks that are in a position to be destroyed,
3466 // let's iterate through the tasks and release the oldest one.
3467 final int numDisplays = mActivityDisplays.size();
3468 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3469 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3470 // Step through all stacks starting from behind, to hit the oldest things first.
3471 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3472 final ActivityStack stack = stacks.get(stackNdx);
3473 // Try to release activities in this stack; if we manage to, we are done.
3474 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3475 return;
3476 }
3477 }
3478 }
3479 }
3480
Amith Yamasani37a40c22015-06-17 13:25:42 -07003481 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003482 final int focusStackId = mFocusedStack.getStackId();
3483 // We dismiss the docked stack whenever we switch users.
3484 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003485 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3486 // also cause all tasks to be moved to the fullscreen stack at a position that is
3487 // appropriate.
3488 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003489
3490 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003491 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003492 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003493
Craig Mautner858d8a62013-04-23 17:08:34 -07003494 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003495 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3496 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003497 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003498 final ActivityStack stack = stacks.get(stackNdx);
3499 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003500 TaskRecord task = stack.topTask();
3501 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003502 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003503 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003504 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003505 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003506
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003507 ActivityStack stack = getStack(restoreStackId);
3508 if (stack == null) {
3509 stack = mHomeStack;
3510 }
3511 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003512 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003513 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003514 } else {
3515 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003516 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003517 }
Craig Mautner93529a42013-10-04 15:03:13 -07003518 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003519 }
3520
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003521 /** Checks whether the userid is a profile of the current user. */
3522 boolean isCurrentProfileLocked(int userId) {
3523 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003524 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003525 }
3526
Bryce Leeb7c9b802017-05-02 14:20:24 -07003527 /**
3528 * Returns whether a stopping activity is present that should be stopped after visible, rather
3529 * than idle.
3530 * @return {@code true} if such activity is present. {@code false} otherwise.
3531 */
3532 boolean isStoppingNoHistoryActivity() {
3533 // Activities that are marked as nohistory should be stopped immediately after the resumed
3534 // activity has become visible.
3535 for (ActivityRecord record : mStoppingActivities) {
3536 if (record.isNoHistory()) {
3537 return true;
3538 }
3539 }
3540
3541 return false;
3542 }
3543
Winson Chung4dabf232017-01-25 13:25:22 -08003544 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3545 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003546 ArrayList<ActivityRecord> stops = null;
3547
3548 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003549 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3550 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003551 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003552 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003553 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3554 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003555 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003556 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003557 if (s.finishing) {
3558 // If this activity is finishing, it is sitting on top of
3559 // everyone else but we now know it is no longer needed...
3560 // so get rid of it. Otherwise, we need to go through the
3561 // normal flow and hide it once we determine that it is
3562 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003563 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003564 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003565 }
3566 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003567 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003568 if (!processPausingActivities && s.state == PAUSING) {
3569 // Defer processing pausing activities in this iteration and reschedule
3570 // a delayed idle to reprocess it again
3571 removeTimeoutsForActivityLocked(idleActivity);
3572 scheduleIdleTimeoutLocked(idleActivity);
3573 continue;
3574 }
3575
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003576 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003577 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003578 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003579 }
3580 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003581 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003582 }
3583 }
3584
3585 return stops;
3586 }
3587
Craig Mautnercf910b02013-04-23 11:23:27 -07003588 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3590 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3592 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003593 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003594 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003595 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003596 if (r == null) Slog.e(TAG,
3597 "validateTop...: null top activity, stack=" + stack);
3598 else {
3599 final ActivityRecord pausing = stack.mPausingActivity;
3600 if (pausing != null && pausing == r) Slog.e(TAG,
3601 "validateTop...: top stack has pausing activity r=" + r
3602 + " state=" + state);
3603 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3604 "validateTop...: activity in front not resumed r=" + r
3605 + " state=" + state);
3606 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003607 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003608 final ActivityRecord resumed = stack.mResumedActivity;
3609 if (resumed != null && resumed == r) Slog.e(TAG,
3610 "validateTop...: back stack has resumed activity r=" + r
3611 + " state=" + state);
3612 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3613 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003614 }
3615 }
3616 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003617 }
3618
Craig Mautnere0570202015-05-13 13:06:11 -07003619 private String lockTaskModeToString() {
3620 switch (mLockTaskModeState) {
3621 case LOCK_TASK_MODE_LOCKED:
3622 return "LOCKED";
3623 case LOCK_TASK_MODE_PINNED:
3624 return "PINNED";
3625 case LOCK_TASK_MODE_NONE:
3626 return "NONE";
3627 default: return "unknown=" + mLockTaskModeState;
3628 }
3629 }
3630
Craig Mautner27084302013-03-25 08:05:25 -07003631 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003632 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003633 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003634 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003635 pw.print(prefix);
3636 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003637 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003638 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003639 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003640 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3641 if (packages.size() > 0) {
3642 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3643 for (int i = 0; i < packages.size(); ++i) {
3644 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3645 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3646 }
3647 }
Bryce Lee4a194382017-04-04 14:32:48 -07003648 if (!mWaitingForActivityVisible.isEmpty()) {
3649 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3650 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3651 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3652 }
3653 }
3654
Jorim Jaggi8d786932016-10-26 19:08:36 -07003655 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3656 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003657 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003658
Winson43d1f262016-06-14 16:05:55 -07003659 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003660 * Dump all connected displays' configurations.
3661 * @param prefix Prefix to apply to each line of the dump.
3662 */
3663 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3664 pw.print(prefix); pw.println("Display override configurations:");
3665 final int displayCount = mActivityDisplays.size();
3666 for (int i = 0; i < displayCount; i++) {
3667 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3668 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3669 pw.println(activityDisplay.getOverrideConfiguration());
3670 }
3671 }
3672
3673 /**
Winson43d1f262016-06-14 16:05:55 -07003674 * Dumps the activities matching the given {@param name} in the either the focused stack
3675 * or all visible stacks if {@param dumpVisibleStacks} is true.
3676 */
Winson Chung6998bc42017-02-28 17:07:05 -08003677 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3678 boolean dumpFocusedStackOnly) {
3679 if (dumpFocusedStackOnly) {
3680 return mFocusedStack.getDumpActivitiesLocked(name);
3681 } else {
Winson43d1f262016-06-14 16:05:55 -07003682 ArrayList<ActivityRecord> activities = new ArrayList<>();
3683 int numDisplays = mActivityDisplays.size();
3684 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3685 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3686 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3687 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003688 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003689 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003690 activities.addAll(stack.getDumpActivitiesLocked(name));
3691 }
3692 }
3693 }
3694 return activities;
Winson43d1f262016-06-14 16:05:55 -07003695 }
Craig Mautner20e72272013-04-01 13:45:53 -07003696 }
3697
Dianne Hackborn390517b2013-05-30 15:03:32 -07003698 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3699 boolean needSep, String prefix) {
3700 if (activity != null) {
3701 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3702 if (needSep) {
3703 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003704 }
3705 pw.print(prefix);
3706 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003707 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003708 }
3709 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003710 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003711 }
3712
Craig Mautner8d341ef2013-03-26 09:03:27 -07003713 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3714 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003715 boolean printed = false;
3716 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003717 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3718 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003719 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003720 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003721 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003723 final ActivityStack stack = stacks.get(stackNdx);
3724 StringBuilder stackHeader = new StringBuilder(128);
3725 stackHeader.append(" Stack #");
3726 stackHeader.append(stack.mStackId);
3727 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003728 stackHeader.append("\n");
3729 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3730 stackHeader.append("\n");
3731 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003732
3733 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3734 dumpClient, dumpPackage, needSep, stackHeader.toString());
3735 printed |= printedStackHeader;
3736 if (!printedStackHeader) {
3737 // Ensure we always dump the stack header even if there are no activities
3738 pw.println();
3739 pw.println(stackHeader);
3740 }
3741
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3743 !dumpAll, false, dumpPackage, true,
3744 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003745
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 needSep = printed;
3747 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3748 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003749 if (pr) {
3750 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003751 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003752 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003753 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3754 " mResumedActivity: ");
3755 if (pr) {
3756 printed = true;
3757 needSep = false;
3758 }
3759 if (dumpAll) {
3760 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3761 " mLastPausedActivity: ");
3762 if (pr) {
3763 printed = true;
3764 needSep = true;
3765 }
3766 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3767 needSep, " mLastNoHistoryActivity: ");
3768 }
3769 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003770 }
3771 }
3772
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003773 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3774 false, dumpPackage, true, " Activities waiting to finish:", null);
3775 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3776 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003777 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3778 false, !dumpAll, false, dumpPackage, true,
3779 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003780 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3781 false, dumpPackage, true, " Activities waiting to sleep:", null);
3782 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3783 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003784
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003785 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003786 }
3787
Dianne Hackborn390517b2013-05-30 15:03:32 -07003788 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003789 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003790 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003791 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003792 String innerPrefix = null;
3793 String[] args = null;
3794 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003795 for (int i=list.size()-1; i>=0; i--) {
3796 final ActivityRecord r = list.get(i);
3797 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3798 continue;
3799 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003800 if (innerPrefix == null) {
3801 innerPrefix = prefix + " ";
3802 args = new String[0];
3803 }
3804 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003805 final boolean full = !brief && (complete || !r.isInHistory());
3806 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003807 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003808 needNL = false;
3809 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003810 if (header1 != null) {
3811 pw.println(header1);
3812 header1 = null;
3813 }
3814 if (header2 != null) {
3815 pw.println(header2);
3816 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003817 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003818 if (lastTask != r.getTask()) {
3819 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003820 pw.print(prefix);
3821 pw.print(full ? "* " : " ");
3822 pw.println(lastTask);
3823 if (full) {
3824 lastTask.dump(pw, prefix + " ");
3825 } else if (complete) {
3826 // Complete + brief == give a summary. Isn't that obvious?!?
3827 if (lastTask.intent != null) {
3828 pw.print(prefix); pw.print(" ");
3829 pw.println(lastTask.intent.toInsecureStringWithClip());
3830 }
3831 }
3832 }
3833 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3834 pw.print(" #"); pw.print(i); pw.print(": ");
3835 pw.println(r);
3836 if (full) {
3837 r.dump(pw, innerPrefix);
3838 } else if (complete) {
3839 // Complete + brief == give a summary. Isn't that obvious?!?
3840 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3841 if (r.app != null) {
3842 pw.print(innerPrefix); pw.println(r.app);
3843 }
3844 }
3845 if (client && r.app != null && r.app.thread != null) {
3846 // flush anything that is already in the PrintWriter since the thread is going
3847 // to write to the file descriptor directly
3848 pw.flush();
3849 try {
3850 TransferPipe tp = new TransferPipe();
3851 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003852 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003853 // Short timeout, since blocking here can
3854 // deadlock with the application.
3855 tp.go(fd, 2000);
3856 } finally {
3857 tp.kill();
3858 }
3859 } catch (IOException e) {
3860 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3861 } catch (RemoteException e) {
3862 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3863 }
3864 needNL = true;
3865 }
3866 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003867 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003868 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003869
Craig Mautnerf3333272013-04-22 10:55:53 -07003870 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003871 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3872 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003873 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3874 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003875 }
3876
3877 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003878 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003879 }
3880
3881 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003882 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3883 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003884 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3885 }
3886
Craig Mautner05d29032013-05-03 13:40:13 -07003887 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003888 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3889 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3890 }
Craig Mautner05d29032013-05-03 13:40:13 -07003891 }
3892
Craig Mautner0eea92c2013-05-16 13:35:39 -07003893 void removeSleepTimeouts() {
3894 mSleepTimeout = false;
3895 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3896 }
3897
3898 final void scheduleSleepTimeout() {
3899 removeSleepTimeouts();
3900 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3901 }
3902
Craig Mautner4a1cb222013-12-04 16:14:06 -08003903 @Override
3904 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003905 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003906 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3907 }
3908
3909 @Override
3910 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003911 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003912 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3913 }
3914
3915 @Override
3916 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003917 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003918 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3919 }
3920
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003921 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003922 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003923 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003925 }
3926
Andrii Kulianca007a62016-11-22 16:33:28 -08003927 /** Check if display with specified id is added to the list. */
3928 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003929 return getActivityDisplayOrCreateLocked(displayId) != null;
3930 }
3931
3932 /**
3933 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3934 * corresponding record in display manager.
3935 */
3936 private ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
3937 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3938 if (activityDisplay != null) {
3939 return activityDisplay;
3940 }
3941 if (mDisplayManager == null) {
3942 // The system isn't fully initialized yet.
3943 return null;
3944 }
3945 final Display display = mDisplayManager.getDisplay(displayId);
3946 if (display == null) {
3947 // The display is not registered in DisplayManager.
3948 return null;
3949 }
3950 // The display hasn't been added to ActivityManager yet, create a new record now.
3951 activityDisplay = new ActivityDisplay(displayId);
3952 if (activityDisplay.mDisplay == null) {
3953 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3954 return null;
3955 }
3956 mActivityDisplays.put(displayId, activityDisplay);
3957 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
3958 mWindowManager.onDisplayAdded(displayId);
3959 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08003960 }
3961
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003962 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003963 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003964 mService.mContext.getResources().getDimensionPixelSize(
3965 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003966 }
3967
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003968 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003969 if (displayId == DEFAULT_DISPLAY) {
3970 throw new IllegalArgumentException("Can't remove the primary display.");
3971 }
3972
Craig Mautner4a1cb222013-12-04 16:14:06 -08003973 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003974 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3975 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003976 final boolean destroyContentOnRemoval
3977 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003978 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3979 while (!stacks.isEmpty()) {
3980 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003981 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003982 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3983 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003984 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003985 } else {
3986 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3987 // a valid launch stack for all activities. This way the task history from
3988 // external display will be preserved on primary after move.
3989 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003990 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003992 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003993 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003994 }
3995 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003996 }
3997
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003998 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003999 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004000 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4001 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004002 // TODO: Update the bounds.
4003 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004004 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004005 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006 }
4007
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004008 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004009 final int displayId = stack.mDisplayId;
4010 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004011 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004012 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004013 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004014 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004015 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004016 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Andrii Kulian62e6f252017-05-30 22:46:53 -07004017 // A stack might be not attached to a display.
Winson529c8e42016-05-17 11:08:40 -07004018 info.position = display != null
4019 ? display.mStacks.indexOf(stack)
4020 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004021
4022 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4023 final int numTasks = tasks.size();
4024 int[] taskIds = new int[numTasks];
4025 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004026 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004027 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004028 for (int i = 0; i < numTasks; ++i) {
4029 final TaskRecord task = tasks.get(i);
4030 taskIds[i] = task.taskId;
4031 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4032 : task.realActivity != null ? task.realActivity.flattenToString()
4033 : task.getTopActivity() != null ? task.getTopActivity().packageName
4034 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004035 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004036 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004037 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004038 }
4039 info.taskIds = taskIds;
4040 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004041 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004042 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004043
4044 final ActivityRecord top = stack.topRunningActivityLocked();
4045 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004046 return info;
4047 }
4048
4049 StackInfo getStackInfoLocked(int stackId) {
4050 ActivityStack stack = getStack(stackId);
4051 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004052 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 }
4054 return null;
4055 }
4056
4057 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004058 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004059 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4060 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004061 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004062 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004063 }
4064 }
4065 return list;
4066 }
4067
Craig Mautner15df08a2015-04-01 12:17:18 -07004068 TaskRecord getLockedTaskLocked() {
4069 final int top = mLockTaskModeTasks.size() - 1;
4070 if (top >= 0) {
4071 return mLockTaskModeTasks.get(top);
4072 }
4073 return null;
4074 }
4075
4076 boolean isLockedTask(TaskRecord task) {
4077 return mLockTaskModeTasks.contains(task);
4078 }
4079
4080 boolean isLastLockedTask(TaskRecord task) {
4081 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4082 }
4083
4084 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004085 if (!mLockTaskModeTasks.remove(task)) {
4086 return;
4087 }
4088 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4089 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004090 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004091 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4092 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004093 final Message lockTaskMsg = Message.obtain();
4094 lockTaskMsg.arg1 = task.userId;
4095 lockTaskMsg.what = LOCK_TASK_END_MSG;
4096 mHandler.sendMessage(lockTaskMsg);
4097 }
4098 }
4099
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004100 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4101 int preferredDisplayId, int actualStackId) {
4102 handleNonResizableTaskIfNeeded(task, preferredStackId, preferredDisplayId, actualStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07004103 false /* forceNonResizable */);
4104 }
4105
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004106 private void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4107 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4108 final boolean isSecondaryDisplayPreferred =
4109 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY)
4110 || StackId.isDynamicStack(preferredStackId);
4111 if (((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
4112 && !isSecondaryDisplayPreferred) || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004113 return;
4114 }
4115
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004116 // Handle incorrect launch/move to secondary display if needed.
4117 final boolean launchOnSecondaryDisplayFailed;
4118 if (isSecondaryDisplayPreferred) {
4119 final int actualDisplayId = task.getStack().mDisplayId;
4120 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4121 // The task landed on an inappropriate display somehow, move it to the default
4122 // display.
4123 // TODO(multi-display): Find proper stack for the task on the default display.
4124 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4125 true /* toTop */);
4126 launchOnSecondaryDisplayFailed = true;
4127 } else {
4128 // The task might have landed on a display different from requested.
4129 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4130 || (preferredDisplayId != INVALID_DISPLAY
4131 && preferredDisplayId != actualDisplayId);
4132 }
4133 } else {
4134 // The task wasn't requested to be on a secondary display.
4135 launchOnSecondaryDisplayFailed = false;
4136 }
4137
Jorim Jaggicd13d332016-04-27 15:40:20 -07004138 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004139 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4140 if (launchOnSecondaryDisplayFailed) {
4141 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4142 // display with config different from global config.
4143 mService.mTaskChangeNotificationController
4144 .notifyActivityLaunchOnSecondaryDisplayFailed();
4145 } else {
4146 // Display a warning toast that we tried to put a non-dockable task in the docked
4147 // stack.
4148 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4149 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004150
Andrii Kulianc27916642016-04-12 17:59:27 -07004151 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4152 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004153 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004154 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004155 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004156 final String packageName = topActivity.appInfo.packageName;
4157 final int reason = isSecondaryDisplayPreferred
4158 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4159 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004160 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004161 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004162 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004163 }
4164
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004165 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004166 if (mLockTaskNotify != null) {
4167 mLockTaskNotify.showToast(mLockTaskModeState);
4168 }
Jason Monka8f569c2014-07-07 12:15:21 -04004169 }
4170
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004171 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4172 if (mLockTaskModeTasks.contains(task)) {
4173 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4174 }
4175 }
4176
Craig Mautner432f64e2015-05-20 14:59:57 -07004177 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4178 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004179 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004180 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004181 final TaskRecord lockedTask = getLockedTaskLocked();
4182 if (lockedTask != null) {
4183 removeLockedTaskLocked(lockedTask);
4184 if (!mLockTaskModeTasks.isEmpty()) {
4185 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004186 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4187 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004188 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004189 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004190 return;
4191 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004192 }
Craig Mautnere0570202015-05-13 13:06:11 -07004193 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4194 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004195 return;
4196 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004197
4198 // Should have already been checked, but do it again.
4199 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004200 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4201 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004202 return;
4203 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004204 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004205 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004206 return;
4207 }
4208
4209 if (mLockTaskModeTasks.isEmpty()) {
4210 // First locktask.
4211 final Message lockTaskMsg = Message.obtain();
4212 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4213 lockTaskMsg.arg1 = task.userId;
4214 lockTaskMsg.what = LOCK_TASK_START_MSG;
4215 lockTaskMsg.arg2 = lockTaskModeState;
4216 mHandler.sendMessage(lockTaskMsg);
4217 }
4218 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004219 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4220 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004221 mLockTaskModeTasks.remove(task);
4222 mLockTaskModeTasks.add(task);
4223
4224 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004225 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004226 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004227
4228 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004229 findTaskToMoveToFrontLocked(task, 0, null, reason,
4230 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004231 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004232 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004233 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004234 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
4235 task.getStackId(), true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004236 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004237 }
4238
4239 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004240 return isLockTaskModeViolation(task, false);
4241 }
4242
4243 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4244 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004245 return false;
4246 }
4247 final int lockTaskAuth = task.mLockTaskAuth;
4248 switch (lockTaskAuth) {
4249 case LOCK_TASK_AUTH_DONT_LOCK:
4250 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004251 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004252 case LOCK_TASK_AUTH_LAUNCHABLE:
4253 case LOCK_TASK_AUTH_WHITELISTED:
4254 return false;
4255 case LOCK_TASK_AUTH_PINNABLE:
4256 // Pinnable tasks can't be launched on top of locktask tasks.
4257 return !mLockTaskModeTasks.isEmpty();
4258 default:
4259 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4260 return true;
4261 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004262 }
4263
Craig Mautner15df08a2015-04-01 12:17:18 -07004264 void onLockTaskPackagesUpdatedLocked() {
4265 boolean didSomething = false;
4266 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4267 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004268 final boolean wasWhitelisted =
4269 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4270 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004271 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004272 final boolean isWhitelisted =
4273 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4274 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4275 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004276 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004277 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4278 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004279 removeLockedTaskLocked(lockedTask);
4280 lockedTask.performClearTaskLocked();
4281 didSomething = true;
4282 }
4283 }
4284 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4285 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4286 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4287 final ActivityStack stack = stacks.get(stackNdx);
4288 stack.onLockTaskPackagesUpdatedLocked();
4289 }
4290 }
Craig Mautnere0570202015-05-13 13:06:11 -07004291 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004292 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004293 if (mLockTaskModeTasks.isEmpty() && task != null
4294 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4295 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004296 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4297 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4298 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4299 false);
4300 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004301 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004302 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004303 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004304 }
4305 }
4306
Benjamin Franz43261142015-02-11 15:59:44 +00004307 int getLockTaskModeState() {
4308 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004309 }
4310
Jorim Jaggife89d122015-12-22 16:28:44 +01004311 void activityRelaunchedLocked(IBinder token) {
4312 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004313 if (mService.isSleepingOrShuttingDownLocked()) {
4314 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4315 if (r != null) {
4316 r.setSleeping(true, true);
4317 }
4318 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004319 }
4320
4321 void activityRelaunchingLocked(ActivityRecord r) {
4322 mWindowManager.notifyAppRelaunching(r.appToken);
4323 }
4324
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004325 void logStackState() {
4326 mActivityMetricsLogger.logWindowState();
4327 }
4328
Winson Chung5af42fc2017-03-24 17:11:33 -07004329 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004330 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4331 // end, then ensure that we defer all in between multi-window mode changes
4332 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4333 return;
4334 }
4335
Wale Ogunwale22e25262016-02-01 10:32:02 -08004336 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4337 final ActivityRecord r = task.mActivities.get(i);
4338 if (r.app != null && r.app.thread != null) {
4339 mMultiWindowModeChangedActivities.add(r);
4340 }
4341 }
4342
4343 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4344 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4345 }
4346 }
4347
Winson Chung5af42fc2017-03-24 17:11:33 -07004348 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004349 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004350 if (prevStack == null || prevStack == stack
4351 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4352 return;
4353 }
4354
Winson Chung5af42fc2017-03-24 17:11:33 -07004355 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4356 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004357
Winson Chung5af42fc2017-03-24 17:11:33 -07004358 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4359 boolean immediate) {
4360
4361 if (immediate) {
4362 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4363 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4364 final ActivityRecord r = task.mActivities.get(i);
4365 if (r.app != null && r.app.thread != null) {
4366 r.updatePictureInPictureMode(targetStackBounds);
4367 }
4368 }
4369 } else {
4370 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4371 final ActivityRecord r = task.mActivities.get(i);
4372 if (r.app != null && r.app.thread != null) {
4373 mPipModeChangedActivities.add(r);
4374 }
4375 }
4376 mPipModeChangedTargetStackBounds = targetStackBounds;
4377
4378 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4379 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4380 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004381 }
4382 }
4383
Tony Mak853304c2016-04-18 15:17:41 +01004384 void setDockedStackMinimized(boolean minimized) {
4385 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004386 }
4387
chaviw59b98852017-06-13 12:05:44 -07004388 void wakeUp(String reason) {
4389 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4390 }
4391
4392 /**
4393 * Begin deferring resume to avoid duplicate resumes in one pass.
4394 */
4395 private void beginDeferResume() {
4396 mDeferResumeCount++;
4397 }
4398
4399 /**
4400 * End deferring resume and determine if resume can be called.
4401 */
4402 private void endDeferResume() {
4403 mDeferResumeCount--;
4404 }
4405
4406 /**
4407 * @return True if resume can be called.
4408 */
4409 private boolean readyToResume() {
4410 return mDeferResumeCount == 0;
4411 }
4412
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004413 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004414
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004415 public ActivityStackSupervisorHandler(Looper looper) {
4416 super(looper);
4417 }
4418
Winson Chung4dabf232017-01-25 13:25:22 -08004419 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004420 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004421 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4422 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004423 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004424 }
4425
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004426 @Override
4427 public void handleMessage(Message msg) {
4428 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004429 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4430 synchronized (mService) {
4431 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4432 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004433 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004434 }
4435 }
4436 } break;
4437 case REPORT_PIP_MODE_CHANGED_MSG: {
4438 synchronized (mService) {
4439 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4440 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004441 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004442 }
4443 }
4444 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004445 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004446 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4447 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004448 // We don't at this point know if the activity is fullscreen,
4449 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004450 activityIdleInternal((ActivityRecord) msg.obj,
4451 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004452 } break;
4453 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004454 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004455 activityIdleInternal((ActivityRecord) msg.obj,
4456 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004457 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004458 case RESUME_TOP_ACTIVITY_MSG: {
4459 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004460 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004461 }
4462 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004463 case SLEEP_TIMEOUT_MSG: {
4464 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004465 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004466 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4467 mSleepTimeout = true;
4468 checkReadyForSleepLocked();
4469 }
4470 }
4471 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004472 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004473 synchronized (mService) {
4474 if (mLaunchingActivity.isHeld()) {
4475 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4476 if (VALIDATE_WAKE_LOCK_CALLER
4477 && Binder.getCallingUid() != Process.myUid()) {
4478 throw new IllegalStateException("Calling must be system uid");
4479 }
4480 mLaunchingActivity.release();
4481 }
4482 }
4483 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004484 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004485 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004486 } break;
4487 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004488 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004489 } break;
4490 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004491 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004492 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004493 case CONTAINER_CALLBACK_VISIBILITY: {
4494 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004495 final IActivityContainerCallback callback = container.mCallback;
4496 if (callback != null) {
4497 try {
4498 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4499 } catch (RemoteException e) {
4500 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004501 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004502 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004503 case LOCK_TASK_START_MSG: {
4504 // When lock task starts, we disable the status bars.
4505 try {
Jason Monk62515be2014-05-21 16:06:19 -04004506 if (mLockTaskNotify == null) {
4507 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004508 }
Jason Monk62515be2014-05-21 16:06:19 -04004509 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004510 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004511 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004512 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004513 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004514 flags = StatusBarManager.DISABLE_MASK
4515 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004516 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004517 flags = StatusBarManager.DISABLE_MASK
4518 & (~StatusBarManager.DISABLE_BACK)
4519 & (~StatusBarManager.DISABLE_HOME)
4520 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004521 }
4522 getStatusBarService().disable(flags, mToken,
4523 mService.mContext.getPackageName());
4524 }
4525 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004526 if (getDevicePolicyManager() != null) {
4527 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004528 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004529 }
justinzhang5286d3f2014-05-12 17:06:01 -04004530 } catch (RemoteException ex) {
4531 throw new RuntimeException(ex);
4532 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004533 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004534 case LOCK_TASK_END_MSG: {
4535 // When lock task ends, we enable the status bars.
4536 try {
Jason Monk62515be2014-05-21 16:06:19 -04004537 if (getStatusBarService() != null) {
4538 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4539 mService.mContext.getPackageName());
4540 }
4541 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004542 if (getDevicePolicyManager() != null) {
4543 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4544 msg.arg1);
4545 }
Jason Monk62515be2014-05-21 16:06:19 -04004546 if (mLockTaskNotify == null) {
4547 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4548 }
4549 mLockTaskNotify.show(false);
4550 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004551 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004552 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004553 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004554 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004555 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004556 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004557 new LockPatternUtils(mService.mContext)
4558 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004559 }
4560 } catch (SettingNotFoundException e) {
4561 // No setting, don't lock.
4562 }
justinzhang5286d3f2014-05-12 17:06:01 -04004563 } catch (RemoteException ex) {
4564 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004565 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004566 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004567 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004568 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004569 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4570 if (mLockTaskNotify == null) {
4571 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4572 }
4573 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4574 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004575 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4576 final ActivityContainer container = (ActivityContainer) msg.obj;
4577 final IActivityContainerCallback callback = container.mCallback;
4578 if (callback != null) {
4579 try {
4580 callback.onAllActivitiesComplete(container.asBinder());
4581 } catch (RemoteException e) {
4582 }
4583 }
4584 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004585 case LAUNCH_TASK_BEHIND_COMPLETE: {
4586 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004587 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004588 if (r != null) {
4589 handleLaunchTaskBehindCompleteLocked(r);
4590 }
4591 }
4592 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004593
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004594 }
4595 }
4596 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004597
Ying Wangb081a592014-04-22 15:20:16 -07004598 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004599 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4600 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004601 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004602 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004603 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004604 ActivityRecord mParentActivity = null;
4605 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004606
Craig Mautnere3a00d72014-04-16 08:31:19 -07004607 boolean mVisible = true;
4608
Craig Mautner4a1cb222013-12-04 16:14:06 -08004609 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004610 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004611
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004612 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4613 final static int CONTAINER_STATE_NO_SURFACE = 1;
4614 final static int CONTAINER_STATE_FINISHING = 2;
4615 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4616
Wale Ogunwale1666e312016-12-16 11:27:18 -08004617 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004618 synchronized (mService) {
4619 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004620 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004621 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004622
4623 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004624 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004625 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004626 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004627
Bryce Leeaf691c02017-03-20 14:20:22 -07004628 protected void createStack(int stackId, boolean onTop) {
4629 switch (stackId) {
4630 case PINNED_STACK_ID:
4631 new PinnedActivityStack(this, mRecentTasks, onTop);
4632 break;
4633 default:
4634 new ActivityStack(this, mRecentTasks, onTop);
4635 break;
4636 }
4637 }
4638
Andrii Kulian839def92016-11-02 10:58:58 -07004639 /**
4640 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004641 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004642 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004643 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004644 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004645 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004646 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004647 if (mActivityDisplay != null) {
4648 throw new IllegalStateException("ActivityContainer is already attached, " +
4649 "displayId=" + mActivityDisplay.mDisplayId);
4650 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004651 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004652 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004653 }
4654
4655 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004656 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004657 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004658 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004659 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004660 return;
4661 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004662 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004663 }
4664 }
4665
4666 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004667 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004668 synchronized (mService) {
4669 if (mActivityDisplay != null) {
4670 return mActivityDisplay.mDisplayId;
4671 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004672 }
4673 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004674 }
4675
Jeff Brownca9bc702014-02-11 14:32:56 -08004676 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004677 public int getStackId() {
4678 synchronized (mService) {
4679 return mStackId;
4680 }
4681 }
4682
4683 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004684 public boolean injectEvent(InputEvent event) {
4685 final long origId = Binder.clearCallingIdentity();
4686 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004687 synchronized (mService) {
4688 if (mActivityDisplay != null) {
4689 return mInputManagerInternal.injectInputEvent(event,
4690 mActivityDisplay.mDisplayId,
4691 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4692 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004693 }
4694 return false;
4695 } finally {
4696 Binder.restoreCallingIdentity(origId);
4697 }
4698 }
4699
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004700 @Override
4701 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004702 synchronized (mService) {
4703 if (mContainerState == CONTAINER_STATE_FINISHING) {
4704 return;
4705 }
4706 mContainerState = CONTAINER_STATE_FINISHING;
4707
Craig Mautnerd163e752014-06-13 17:18:47 -07004708 long origId = Binder.clearCallingIdentity();
4709 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004710 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004711 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004712 } finally {
4713 Binder.restoreCallingIdentity(origId);
4714 }
4715 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004716 }
4717
Andrii Kulian03c403d2016-11-11 11:14:12 -08004718 /**
4719 * Remove the stack completely. Must be called only when there are no tasks left in it,
4720 * as this method does not finish running activities.
4721 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004722 void removeLocked() {
4723 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004724 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004725 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004726 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004727 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004728 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004729 }
4730
Andrii Kulian839def92016-11-02 10:58:58 -07004731 /**
4732 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4733 * completely or re-parented.
4734 */
4735 private void removeFromDisplayLocked() {
4736 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4737 + " current displayId=" + mActivityDisplay.mDisplayId);
4738
Wale Ogunwale1666e312016-12-16 11:27:18 -08004739 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004740 mActivityDisplay = null;
4741 }
4742
4743 /**
4744 * Move the stack to specified display.
4745 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004746 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004747 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004748 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004749 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4750 + mActivityDisplay + " to display=" + activityDisplay
4751 + " Callers=" + Debug.getCallers(2));
4752
4753 removeFromDisplayLocked();
4754
4755 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004756 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004757 }
4758
Craig Mautner4a1cb222013-12-04 16:14:06 -08004759 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004760 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004761 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004762 }
4763
4764 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004765 public final int startActivityIntentSender(IIntentSender intentSender)
4766 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004767 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4768
4769 if (!(intentSender instanceof PendingIntentRecord)) {
4770 throw new IllegalArgumentException("Bad PendingIntent object");
4771 }
4772
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004773 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004774 Binder.getCallingUid(), mCurrentUser, false,
4775 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004776
4777 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4778 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4779 pendingIntent.key.requestResolvedType);
4780
Dianne Hackborn98305522017-05-05 17:53:53 -07004781 return pendingIntent.sendInner(0, null, null, null, null, null, null, null, 0,
Craig Mautnerb9168362015-02-26 20:40:19 -08004782 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4783 }
4784
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004785 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004786 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004787 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004788 throw new SecurityException(
4789 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4790 }
4791 }
4792
Craig Mautnerdf88d732014-01-27 09:21:32 -08004793 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004794 public IBinder asBinder() {
4795 return this;
4796 }
4797
Craig Mautner4504de52013-12-20 09:06:56 -08004798 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004799 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004800 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004801 }
4802
Craig Mautner4a1cb222013-12-04 16:14:06 -08004803 ActivityStackSupervisor getOuter() {
4804 return ActivityStackSupervisor.this;
4805 }
4806
Craig Mautnerd163e752014-06-13 17:18:47 -07004807 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004808 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004809 }
4810
Craig Mautner6985bad2014-04-21 15:22:06 -07004811 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004812 void setVisible(boolean visible) {
4813 if (mVisible != visible) {
4814 mVisible = visible;
4815 if (mCallback != null) {
4816 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4817 0 /* unused */, this).sendToTarget();
4818 }
4819 }
4820 }
4821
Craig Mautner6985bad2014-04-21 15:22:06 -07004822 void setDrawn() {
4823 }
4824
Craig Mautner1b4bf852014-05-26 15:06:32 -07004825 // You can always start a new task on a regular ActivityStack.
4826 boolean isEligibleForNewTasks() {
4827 return true;
4828 }
4829
Craig Mautnerd163e752014-06-13 17:18:47 -07004830 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004831 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004832 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004833 }
4834
Craig Mautner34b73df2014-01-12 21:11:08 -08004835 @Override
4836 public String toString() {
4837 return mIdString + (mActivityDisplay == null ? "N" : "A");
4838 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004839 }
4840
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004841 private class VirtualActivityContainer extends ActivityContainer {
4842 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004843 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004844
4845 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004846 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4847 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004848 mParentActivity = parent;
4849 mCallback = callback;
4850 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004851 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004852 }
4853
4854 @Override
4855 public void setSurface(Surface surface, int width, int height, int density) {
4856 super.setSurface(surface, width, height, density);
4857
4858 synchronized (mService) {
4859 final long origId = Binder.clearCallingIdentity();
4860 try {
4861 setSurfaceLocked(surface, width, height, density);
4862 } finally {
4863 Binder.restoreCallingIdentity(origId);
4864 }
4865 }
4866 }
4867
4868 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4869 if (mContainerState == CONTAINER_STATE_FINISHING) {
4870 return;
4871 }
4872 VirtualActivityDisplay virtualActivityDisplay =
4873 (VirtualActivityDisplay) mActivityDisplay;
4874 if (virtualActivityDisplay == null) {
4875 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004876 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004877 mActivityDisplay = virtualActivityDisplay;
4878 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004879 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004880 }
4881
4882 if (mSurface != null) {
4883 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004884 }
4885
Craig Mautner6985bad2014-04-21 15:22:06 -07004886 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004887 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004888 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004889 } else {
4890 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004891 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004892 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004893 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004894 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004895 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004896
Craig Mautnerd163e752014-06-13 17:18:47 -07004897 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004898
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004899 if (DEBUG_STACK) Slog.d(TAG_STACK,
4900 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004901 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004902
Craig Mautner6985bad2014-04-21 15:22:06 -07004903 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004904 boolean isAttachedLocked() {
4905 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004906 }
4907
4908 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004909 void setDrawn() {
4910 synchronized (mService) {
4911 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004912 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004913 }
4914 }
4915
Craig Mautner1b4bf852014-05-26 15:06:32 -07004916 // Never start a new task on an ActivityView if it isn't explicitly specified.
4917 @Override
4918 boolean isEligibleForNewTasks() {
4919 return false;
4920 }
4921
Craig Mautnerd163e752014-06-13 17:18:47 -07004922 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004923 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004924 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4925 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4926 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4927 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4928 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004929 }
4930 }
4931
Craig Mautner4a1cb222013-12-04 16:14:06 -08004932 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4933 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004934 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004935 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004936 int mDisplayId;
4937 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004938
Craig Mautner4a1cb222013-12-04 16:14:06 -08004939 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4940 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004941 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004942
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004943 /** Array of all UIDs that are present on the display. */
4944 private IntArray mDisplayAccessUIDs = new IntArray();
4945
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004946 ActivityDisplay() {
4947 }
Craig Mautner4504de52013-12-20 09:06:56 -08004948
Craig Mautner1a70a162014-09-13 12:09:31 -07004949 // After instantiation, check that mDisplay is not null before using this. The alternative
4950 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004951 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004952 final Display display = mDisplayManager.getDisplay(displayId);
4953 if (display == null) {
4954 return;
4955 }
4956 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004957 }
4958
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004959 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004960 mDisplay = display;
4961 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004962 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004963
Winson Chung7c3ede32017-03-14 14:18:34 -07004964 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004965 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004966 + " to displayId=" + mDisplayId + " position=" + position);
4967 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004968 }
4969
Wale Ogunwale1666e312016-12-16 11:27:18 -08004970 void detachStack(ActivityStack stack) {
4971 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004972 + " from displayId=" + mDisplayId);
4973 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004974 }
4975
Craig Mautner34b73df2014-01-12 21:11:08 -08004976 @Override
4977 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004978 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4979 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004980
4981 @Override
4982 protected int getChildCount() {
4983 return mStacks.size();
4984 }
4985
4986 @Override
4987 protected ConfigurationContainer getChildAt(int index) {
4988 return mStacks.get(index);
4989 }
4990
4991 @Override
4992 protected ConfigurationContainer getParent() {
4993 return ActivityStackSupervisor.this;
4994 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004995
4996 boolean isPrivate() {
4997 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4998 }
4999
5000 boolean isUidPresent(int uid) {
5001 for (ActivityStack stack : mStacks) {
5002 if (stack.isUidPresent(uid)) {
5003 return true;
5004 }
5005 }
5006 return false;
5007 }
5008
5009 /** Update and get all UIDs that are present on the display and have access to it. */
5010 private IntArray getPresentUIDs() {
5011 mDisplayAccessUIDs.clear();
5012 for (ActivityStack stack : mStacks) {
5013 stack.getPresentUIDs(mDisplayAccessUIDs);
5014 }
5015 return mDisplayAccessUIDs;
5016 }
Andrii Kulian250d6532017-02-08 23:30:45 -08005017
5018 boolean shouldDestroyContentOnRemove() {
5019 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
5020 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005021 }
5022
5023 class VirtualActivityDisplay extends ActivityDisplay {
5024 VirtualDisplay mVirtualDisplay;
5025
Craig Mautner6985bad2014-04-21 15:22:06 -07005026 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005027 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Santos Cordonee8931e2017-04-05 10:31:15 -07005028 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null /* projection */,
5029 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null /* surface */,
Michael Wrightc39d47a2014-07-08 18:07:36 -07005030 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Santos Cordonee8931e2017-04-05 10:31:15 -07005031 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null /* callback */,
5032 null /* handler */, null /* uniqueId */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005033
5034 init(mVirtualDisplay.getDisplay());
5035
Bryce Leeaf3a4002017-03-20 10:37:12 -07005036 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005037 }
5038
5039 void setSurface(Surface surface) {
5040 if (mVirtualDisplay != null) {
5041 mVirtualDisplay.setSurface(surface);
5042 }
5043 }
5044
5045 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08005046 void detachStack(ActivityStack stack) {
5047 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005048 if (mVirtualDisplay != null) {
5049 mVirtualDisplay.release();
5050 mVirtualDisplay = null;
5051 }
5052 }
5053
5054 @Override
5055 public String toString() {
5056 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005057 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005058 }
Jose Lima58e66d62014-05-27 20:00:27 -07005059
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005060 ActivityStack findStackBehind(ActivityStack stack) {
5061 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02005062 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005063 if (display == null) {
5064 return null;
5065 }
5066 final ArrayList<ActivityStack> stacks = display.mStacks;
5067 for (int i = stacks.size() - 1; i >= 0; i--) {
5068 if (stacks.get(i) == stack && i > 0) {
5069 return stacks.get(i - 1);
5070 }
5071 }
5072 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5073 + " in=" + stacks);
5074 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005075
Jorim Jaggic69bd222016-03-15 14:38:37 +01005076 /**
5077 * Puts a task into resizing mode during the next app transition.
5078 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005079 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01005080 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005081 private void setResizingDuringAnimation(TaskRecord task) {
5082 mResizingTasksDuringAnimation.add(task.taskId);
5083 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01005084 }
5085
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005086 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
5087 final TaskRecord task;
5088 final int callingUid;
5089 final String callingPackage;
5090 final Intent intent;
5091 final int userId;
5092 final ActivityOptions activityOptions = (bOptions != null)
5093 ? new ActivityOptions(bOptions) : null;
5094 final int launchStackId = (activityOptions != null)
5095 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005096 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005097 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005098 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005099 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005100
Matthew Ng606dd802017-06-05 14:06:32 -07005101 mWindowManager.deferSurfaceLayout();
5102 try {
5103 if (launchStackId == DOCKED_STACK_ID) {
5104 mWindowManager.setDockedStackCreateState(
5105 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005106
Matthew Ng606dd802017-06-05 14:06:32 -07005107 // Defer updating the stack in which recents is until the app transition is done, to
5108 // not run into issues where we still need to draw the task in recents but the
5109 // docked stack is already created.
5110 deferUpdateBounds(RECENTS_STACK_ID);
5111 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005112 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005113
Matthew Ng606dd802017-06-05 14:06:32 -07005114 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
5115 launchStackId);
5116 if (task == null) {
5117 continueUpdateBounds(RECENTS_STACK_ID);
5118 mWindowManager.executeAppTransition();
5119 throw new IllegalArgumentException(
5120 "startActivityFromRecentsInner: Task " + taskId + " not found.");
5121 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005122
Matthew Ng606dd802017-06-05 14:06:32 -07005123 // Since we don't have an actual source record here, we assume that the currently
5124 // focused activity was the source.
5125 final ActivityStack focusedStack = getFocusedStack();
5126 final ActivityRecord sourceRecord =
5127 focusedStack != null ? focusedStack.topActivity() : null;
5128
5129 if (launchStackId != INVALID_STACK_ID) {
5130 if (task.getStackId() != launchStackId) {
5131 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
5132 DEFER_RESUME, "startActivityFromRecents");
5133 }
5134 }
5135
5136 // If the user must confirm credentials (e.g. when first launching a work app and the
5137 // Work Challenge is present) let startActivityInPackage handle the intercepting.
5138 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
5139 && task.getRootActivity() != null) {
5140 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
5141 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07005142 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07005143 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
5144 task.getTopActivity());
5145
5146 // If we are launching the task in the docked stack, put it into resizing mode so
5147 // the window renders full-screen with the background filling the void. Also only
5148 // call this at the end to make sure that tasks exists on the window manager side.
5149 if (launchStackId == DOCKED_STACK_ID) {
5150 setResizingDuringAnimation(task);
5151 }
5152
5153 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
5154 ActivityManager.START_TASK_TO_FRONT,
5155 sourceRecord != null
5156 ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
5157 sourceRecord, task.getStack());
5158 return ActivityManager.START_TASK_TO_FRONT;
5159 }
5160 callingUid = task.mCallingUid;
5161 callingPackage = task.mCallingPackage;
5162 intent = task.intent;
5163 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5164 userId = task.userId;
5165 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Wale Ogunwale692dcd62017-06-20 13:38:14 -07005166 null, null, 0, 0, bOptions, userId, null, task, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005167 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005168 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005169 }
Matthew Ng606dd802017-06-05 14:06:32 -07005170 return result;
5171 } finally {
5172 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005173 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005174 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005175
5176 /**
5177 * @return a list of activities which are the top ones in each visible stack. The first
5178 * entry will be the focused activity.
5179 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005180 List<IBinder> getTopVisibleActivities() {
5181 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5182 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005183 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005184 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5185 // Traverse all stacks on a display.
5186 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5187 final ActivityStack stack = display.mStacks.get(j);
5188 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005189 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005190 == ActivityStack.STACK_VISIBLE) {
5191 final ActivityRecord top = stack.topActivity();
5192 if (top != null) {
5193 if (stack == mFocusedStack) {
5194 topActivityTokens.add(0, top.appToken);
5195 } else {
5196 topActivityTokens.add(top.appToken);
5197 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005198 }
5199 }
5200 }
5201 }
5202 return topActivityTokens;
5203 }
Bryce Lee4a194382017-04-04 14:32:48 -07005204
5205 /**
5206 * Internal container to store a match qualifier alongside a WaitResult.
5207 */
5208 static class WaitInfo {
5209 private final ComponentName mTargetComponent;
5210 private final WaitResult mResult;
5211
5212 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5213 this.mTargetComponent = targetComponent;
5214 this.mResult = result;
5215 }
5216
Wale Ogunwale3270f172017-04-26 07:29:42 -07005217 public boolean matches(ComponentName targetComponent) {
5218 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07005219 }
5220
5221 public WaitResult getResult() {
5222 return mResult;
5223 }
5224
5225 public ComponentName getComponent() {
5226 return mTargetComponent;
5227 }
5228
5229 public void dump(PrintWriter pw, String prefix) {
5230 pw.println(prefix + "WaitInfo:");
5231 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5232 pw.println(prefix + " mResult=");
5233 mResult.dump(pw, prefix);
5234 }
5235 }
Craig Mautner27084302013-03-25 08:05:25 -07005236}