blob: 50233f360a33f6c9381f1d75e8a88c31affc124f [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 Kuliane6ac20e2017-03-17 10:30:50 -070019import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020020import static android.Manifest.permission.START_ANY_ACTIVITY;
21import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
22import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
23import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
24import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.app.ActivityManager.START_TASK_TO_FRONT;
26import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.HOME_STACK_ID;
32import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
33import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
34import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080035import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070036import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
37import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020038import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
39import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
40import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070041import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020042import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
43import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070044import static android.view.Display.FLAG_PRIVATE;
45import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080046import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070047import static android.view.Display.TYPE_VIRTUAL;
Jorim Jaggife762342016-10-13 14:33:27 +020048
Winson Chung47900652017-04-06 18:44:25 -070049import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020050import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
74import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
75import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
76import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
77import static com.android.server.am.ActivityManagerService.ANIMATE;
78import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
79import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
80import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020081import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
82import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
83import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
84import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
85import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
86import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
87import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
88import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
89import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
90import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
91import static com.android.server.am.ActivityStack.STACK_VISIBLE;
92import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
93import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
94import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
95import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
96import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080097import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
98import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
99import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +0200100import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800101import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200102
Svetoslav7008b512015-06-24 18:47:07 -0700103import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800104import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700105import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100106import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700107import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800109import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700110import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800111import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700112import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700113import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800114import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700115import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700116import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400117import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700118import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400119import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700121import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700122import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700123import android.content.Intent;
124import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700126import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700127import android.content.pm.PackageManager;
128import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100129import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700130import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800131import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800132import android.hardware.display.DisplayManager;
133import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800134import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800135import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800136import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800137import android.hardware.input.InputManager;
138import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100140import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700141import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700142import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700143import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700144import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700145import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700146import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700147import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700148import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700149import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400150import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700151import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700152import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700153import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700154import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100155import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700156import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700157import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400158import android.provider.Settings;
159import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700160import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700161import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700162import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700163import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800164import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700165import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700166import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800167import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800168import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800169import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800170import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800171import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800172
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800173import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800174import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700175import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400176import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700177import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400178import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800179import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700180import com.android.server.am.ActivityStack.ActivityState;
Winson Chung19953ca2017-04-11 11:19:23 -0700181import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700182import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700183
Craig Mautner8d341ef2013-03-26 09:03:27 -0700184import java.io.FileDescriptor;
185import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700186import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800187import java.lang.annotation.Retention;
188import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700189import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700190import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700191import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700192import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700193
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800194public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800195 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700197 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700199 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700200 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700203 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700204 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700205 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800206 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700207 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800208
Craig Mautnerf3333272013-04-22 10:55:53 -0700209 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700210 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700211
Craig Mautner0eea92c2013-05-16 13:35:39 -0700212 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700213 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700214
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700215 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700216 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217
Craig Mautner05d29032013-05-03 13:40:13 -0700218 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
219 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
220 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700221 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700222 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800223 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
224 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
225 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700226 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400227 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
228 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700229 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700230 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700231 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800232 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
233 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800234
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700236
Jason Monk62515be2014-05-21 16:06:19 -0400237 private static final String LOCK_TASK_TAG = "Lock-to-App";
238
Wale Ogunwale040b4702015-08-06 18:10:50 -0700239 // Used to indicate if an object (e.g. stack) that we are trying to get
240 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800241 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700242
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700243 // Used to indicate that windows of activities should be preserved during the resize.
244 static final boolean PRESERVE_WINDOWS = true;
245
Wale Ogunwale040b4702015-08-06 18:10:50 -0700246 // Used to indicate if an object (e.g. task) should be moved/created
247 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700248 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700249
250 // Used to indicate that an objects (e.g. task) removal from its container
251 // (e.g. stack) is due to it moving to another container.
252 static final boolean MOVING = true;
253
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700254 // Force the focus to change to the stack we are moving a task to..
255 static final boolean FORCE_FOCUS = true;
256
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700257 // Don't execute any calls to resume.
258 static final boolean DEFER_RESUME = true;
259
Winson Chung010927a2016-12-15 16:12:35 -0800260 // Used to indicate that a task is removed it should also be removed from recents.
261 static final boolean REMOVE_FROM_RECENTS = true;
262
Winson Chung6954fc92017-03-24 16:22:12 -0700263 // Used to indicate that pausing an activity should occur immediately without waiting for
264 // the activity callback indicating that it has completed pausing
265 static final boolean PAUSE_IMMEDIATELY = true;
266
Winson Chung7900bee2017-03-10 08:59:25 -0800267 /**
268 * The modes which affect which tasks are returned when calling
269 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
270 */
271 @Retention(RetentionPolicy.SOURCE)
272 @IntDef({
273 MATCH_TASK_IN_STACKS_ONLY,
274 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
275 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
276 })
277 public @interface AnyTaskForIdMatchTaskMode {}
278 // Match only tasks in the current stacks
279 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
280 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
281 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
282 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
283 // provided stack id
284 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
285
Svetoslav7008b512015-06-24 18:47:07 -0700286 // Activity actions an app cannot start if it uses a permission which is not granted.
287 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
288 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700289
Svetoslav7008b512015-06-24 18:47:07 -0700290 static {
291 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
292 Manifest.permission.CAMERA);
293 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
294 Manifest.permission.CAMERA);
295 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
296 Manifest.permission.CALL_PHONE);
297 }
298
Svet Ganov99b60432015-06-27 13:15:22 -0700299 /** Action restriction: launching the activity is not restricted. */
300 private static final int ACTIVITY_RESTRICTION_NONE = 0;
301 /** Action restriction: launching the activity is restricted by a permission. */
302 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
303 /** Action restriction: launching the activity is restricted by an app op. */
304 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700305
justinzhang5286d3f2014-05-12 17:06:01 -0400306 /** Status Bar Service **/
307 private IBinder mToken = new Binder();
308 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400309 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400310
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700311 // For debugging to make sure the caller when acquiring/releasing our
312 // wake lock is the system process.
313 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800314 /** The number of distinct task ids that can be assigned to the tasks of a single user */
315 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700316
Craig Mautner27084302013-03-25 08:05:25 -0700317 final ActivityManagerService mService;
318
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800319 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800320
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321 final ActivityStackSupervisorHandler mHandler;
322
323 /** Short cut */
324 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800325 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700326
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700327 /** Counter for next free stack ID to use for dynamic activity stacks. */
328 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700329
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800330 /**
331 * Maps the task identifier that activities are currently being started in to the userId of the
332 * task. Each time a new task is created, the entry for the userId of the task is incremented
333 */
334 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700335
Craig Mautner2420ead2013-04-01 17:13:20 -0700336 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800337 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700338
Craig Mautnere0a38842013-12-16 16:14:02 -0800339 /** The stack containing the launcher app. Assumed to always be attached to
340 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800341 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700342
Craig Mautnere0a38842013-12-16 16:14:02 -0800343 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800344 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700345
Craig Mautner4a1cb222013-12-04 16:14:06 -0800346 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
347 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800348 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800349 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700350
351 /** List of activities that are waiting for a new activity to become visible before completing
352 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700353 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
354 // mStoppingActivities when something else comes up.
355 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700356
Bryce Lee4a194382017-04-04 14:32:48 -0700357 /** List of processes waiting to find out when a specific activity becomes visible. */
358 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700359
360 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700361 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700362
Craig Mautnerde4ef022013-04-07 19:01:33 -0700363 /** List of activities that are ready to be stopped, but waiting for the next activity to
364 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800365 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700366
Craig Mautnerf3333272013-04-22 10:55:53 -0700367 /** List of activities that are ready to be finished, but waiting for the previous activity to
368 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800369 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700370
Craig Mautner0eea92c2013-05-16 13:35:39 -0700371 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800372 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373
Wale Ogunwale22e25262016-02-01 10:32:02 -0800374 /** List of activities whose multi-window mode changed that we need to report to the
375 * application */
376 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
377
378 /** List of activities whose picture-in-picture mode changed that we need to report to the
379 * application */
380 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
381
Winson Chung5af42fc2017-03-24 17:11:33 -0700382 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
383 * the application */
384 Rect mPipModeChangedTargetStackBounds;
385
Craig Mautnerf3333272013-04-22 10:55:53 -0700386 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700387 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700388
Craig Mautnerde4ef022013-04-07 19:01:33 -0700389 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
390 * is being brought in front of us. */
391 boolean mUserLeaving = false;
392
Craig Mautner0eea92c2013-05-16 13:35:39 -0700393 /** Set when we have taken too long waiting to go to sleep. */
394 boolean mSleepTimeout = false;
395
396 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700397 * We don't want to allow the device to go to sleep while in the process
398 * of launching an activity. This is primarily to allow alarm intent
399 * receivers to launch an activity and get that to run before the device
400 * goes back to sleep.
401 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700402 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700403
404 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700405 * Set when the system is going to sleep, until we have
406 * successfully paused the current activity and released our wake lock.
407 * At that point the system is allowed to actually sleep.
408 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700409 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700410
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700411 /** Stack id of the front stack when user switched, indexed by userId. */
412 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700413
Craig Mautner4504de52013-12-20 09:06:56 -0800414 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800416 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417
Bryce Leeaf3a4002017-03-20 10:37:12 -0700418 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800419 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700420 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800421
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800422 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
423
424 private DisplayManagerInternal mDisplayManagerInternal;
425 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800426
Craig Mautner15df08a2015-04-01 12:17:18 -0700427 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
428 * may be finished until there is only one entry left. If this is empty the system is not
429 * in lockTask mode. */
430 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000431 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700432 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
433 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000434 */
435 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400436 /**
437 * Notifies the user when entering/exiting lock-task.
438 */
439 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800440
Wale Ogunwaled046a012015-12-24 13:05:59 -0800441 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800442 boolean inResumeTopActivity;
443
Andrii Kulian1e32e022016-09-16 15:29:34 -0700444 /**
445 * Temporary rect used during docked stack resize calculation so we don't need to create a new
446 * object each time.
447 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700448 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700449
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700450 // The default minimal size that will be used if the activity doesn't specify its minimal size.
451 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700452 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700453
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700454 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
455 private boolean mTaskLayersChanged = true;
456
Jorim Jaggi275561a2016-02-23 10:11:02 -0500457 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800458
Andrii Kulian1779e612016-10-12 21:58:25 -0700459 @Override
460 protected int getChildCount() {
461 return mActivityDisplays.size();
462 }
463
464 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700465 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700466 return mActivityDisplays.valueAt(index);
467 }
468
469 @Override
470 protected ConfigurationContainer getParent() {
471 return null;
472 }
473
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700474 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700475 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700476 if (activityDisplay == null) {
477 throw new IllegalArgumentException("No display found with id: " + displayId);
478 }
479
480 return activityDisplay.getOverrideConfiguration();
481 }
482
483 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700484 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700485 if (activityDisplay == null) {
486 throw new IllegalArgumentException("No display found with id: " + displayId);
487 }
488
489 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
490 }
491
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700492 /** Check if placing task or activity on specified display is allowed. */
493 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable) {
494 return displayId == DEFAULT_DISPLAY || (mService.mSupportsMultiDisplay
495 && (resizeable || displayConfigMatchesGlobal(displayId)));
496 }
497
498 /**
499 * Check if configuration of specified display matches current global config.
500 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
501 * the same config as on the default display.
502 * @param displayId Id of the display to check.
503 * @return {@code true} if configuration matches.
504 */
505 private boolean displayConfigMatchesGlobal(int displayId) {
506 if (displayId == DEFAULT_DISPLAY) {
507 return true;
508 }
509 if (displayId == INVALID_DISPLAY) {
510 return false;
511 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700512 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700513 if (targetDisplay == null) {
514 throw new IllegalArgumentException("No display found with id: " + displayId);
515 }
516 return getConfiguration().equals(targetDisplay.getConfiguration());
517 }
518
Wale Ogunwale39381972015-12-17 17:15:29 -0800519 static class FindTaskResult {
520 ActivityRecord r;
521 boolean matchedByRootAffinity;
522 }
523 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
524
Craig Mautneree36c772014-07-16 14:56:05 -0700525 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800526 * Temp storage for display ids sorted in focus order.
527 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
528 * it's more efficient, as the number of displays is usually small.
529 */
530 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
531
532 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100533 * Used to keep track whether app visibilities got changed since the last pause. Useful to
534 * determine whether to invoke the task stack change listener after pausing.
535 */
536 boolean mAppVisibilitiesChangedSinceLastPause;
537
538 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100539 * Set of tasks that are in resizing mode during an app transition to fill the "void".
540 */
541 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
542
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700543
544 /**
545 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
546 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
547 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
548 * like the docked stack going empty.
549 */
550 private boolean mAllowDockedStackResize = true;
551
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100552 /**
Tony Mak853304c2016-04-18 15:17:41 +0100553 * Is dock currently minimized.
554 */
555 boolean mIsDockMinimized;
556
Jorim Jaggife762342016-10-13 14:33:27 +0200557 final KeyguardController mKeyguardController;
558
Tony Mak853304c2016-04-18 15:17:41 +0100559 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700560 * Description of a request to start a new activity, which has been held
561 * due to app switches being disabled.
562 */
563 static class PendingActivityLaunch {
564 final ActivityRecord r;
565 final ActivityRecord sourceRecord;
566 final int startFlags;
567 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700568 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700569
570 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700571 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700572 r = _r;
573 sourceRecord = _sourceRecord;
574 startFlags = _startFlags;
575 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700576 callerApp = _callerApp;
577 }
578
579 void sendErrorResult(String message) {
580 try {
581 if (callerApp.thread != null) {
582 callerApp.thread.scheduleCrash(message);
583 }
584 } catch (RemoteException e) {
585 Slog.e(TAG, "Exception scheduling crash of failed "
586 + "activity launcher sourceRecord=" + sourceRecord, e);
587 }
Craig Mautneree36c772014-07-16 14:56:05 -0700588 }
589 }
590
Bryce Leeaf691c02017-03-20 14:20:22 -0700591 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700592 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700593 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800594 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200595 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700596 }
597
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800598 void setRecentTasks(RecentTasks recentTasks) {
599 mRecentTasks = recentTasks;
600 }
601
Jeff Brown2c43c332014-06-12 22:38:59 -0700602 /**
603 * At the time when the constructor runs, the power manager has not yet been
604 * initialized. So we initialize our wakelocks afterwards.
605 */
606 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700608 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700609 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700610 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700611 }
612
justinzhang5286d3f2014-05-12 17:06:01 -0400613 // This function returns a IStatusBarService. The value is from ServiceManager.
614 // getService and is cached.
615 private IStatusBarService getStatusBarService() {
616 synchronized (mService) {
617 if (mStatusBarService == null) {
618 mStatusBarService = IStatusBarService.Stub.asInterface(
619 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
620 if (mStatusBarService == null) {
621 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
622 }
623 }
624 return mStatusBarService;
625 }
626 }
627
Jason Monk35c62a42014-06-17 10:24:47 -0400628 private IDevicePolicyManager getDevicePolicyManager() {
629 synchronized (mService) {
630 if (mDevicePolicyManager == null) {
631 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
632 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
633 if (mDevicePolicyManager == null) {
634 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
635 }
636 }
637 return mDevicePolicyManager;
638 }
639 }
640
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700641 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800642 synchronized (mService) {
643 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200644 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645
646 mDisplayManager =
647 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
648 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800649 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650
651 Display[] displays = mDisplayManager.getDisplays();
652 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
653 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800654 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700655 if (activityDisplay.mDisplay == null) {
656 throw new IllegalStateException("Default Display does not exist");
657 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800658 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700659 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800660 }
661
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800662 mHomeStack = mFocusedStack = mLastFocusedStack =
663 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800664
665 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 }
Craig Mautner27084302013-03-25 08:05:25 -0700667 }
668
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700669 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700671 }
672
Craig Mautnerde4ef022013-04-07 19:01:33 -0700673 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700675 }
676
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700677 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700678 if (stack == null) {
679 return false;
680 }
681
Craig Mautnerdf88d732014-01-27 09:21:32 -0800682 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
683 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700684 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800685 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800686 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700687 }
688
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800689 /** The top most stack on its display. */
690 boolean isFrontStackOnDisplay(ActivityStack stack) {
691 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
692 }
693
694 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700695 if (stack == null) {
696 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800697 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700698
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700699 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
700 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700701 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800702 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800703 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700704 }
705
Wale Ogunwaled046a012015-12-24 13:05:59 -0800706 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800707 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
708 if (!focusCandidate.isFocusable()) {
709 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800710 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800711 }
712
713 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800714 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800715 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800716
Wale Ogunwaled046a012015-12-24 13:05:59 -0800717 EventLogTags.writeAmFocusedStack(
718 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
719 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
720 }
721
722 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800723 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800724 if (r != null && r.idle) {
725 checkFinishBootingLocked();
726 }
727 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700728 }
729
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700730 void moveHomeStackToFront(String reason) {
731 mHomeStack.moveToFront(reason);
732 }
733
Matthew Ng330757d2017-02-28 14:19:17 -0800734 void moveRecentsStackToFront(String reason) {
735 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
736 if (recentsStack != null) {
737 recentsStack.moveToFront(reason);
738 }
739 }
740
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700741 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800742 boolean moveHomeStackTaskToTop(String reason) {
743 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700744
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700745 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700746 if (top == null) {
747 return false;
748 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700749 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700750 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700751 }
752
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800753 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700754 if (!mService.mBooting && !mService.mBooted) {
755 // Not ready yet!
756 return false;
757 }
758
Craig Mautner84984fa2014-06-19 11:19:20 -0700759 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700760 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700761 }
762
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800763 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700764 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800765 final String myReason = reason + " resumeHomeStackTask";
766
Mark Lua56ea122015-10-08 13:31:01 +0800767 // Only resume home activity if isn't finishing.
768 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700769 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800770 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700771 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800772 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700773 }
774
Craig Mautner8d341ef2013-03-26 09:03:27 -0700775 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800776 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
777 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700778 }
779
780 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700781 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700782 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800783 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700784 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800785 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
786 * valid if the matchMode is
787 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700788 */
Winson Chung7900bee2017-03-10 08:59:25 -0800789 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
790 // If there is a stack id set, ensure that we are attempting to actually restore a task
791 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
792 stackId != INVALID_STACK_ID) {
793 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
794 }
795
Craig Mautnere0a38842013-12-16 16:14:02 -0800796 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800797 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800798 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800799 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
800 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700801 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 if (task != null) {
803 return task;
804 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700805 }
806 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800807
Winson Chung7900bee2017-03-10 08:59:25 -0800808 // If we are matching stack tasks only, return now
809 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800810 return null;
811 }
812
Winson Chung7900bee2017-03-10 08:59:25 -0800813 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
814 // the task from recents
815 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700816 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
817
818 if (task == null) {
819 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800820 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
821 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700822
823 return null;
824 }
825
826 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700827 return task;
828 }
829
Winson Chung7900bee2017-03-10 08:59:25 -0800830 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700831 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700832 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
833 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800834 return null;
835 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700836 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800837 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700838 }
839
Craig Mautner6170f732013-04-02 13:05:23 -0700840 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800841 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800843 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
845 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
846 if (r != null) {
847 return r;
848 }
Craig Mautner6170f732013-04-02 13:05:23 -0700849 }
850 }
851 return null;
852 }
853
Tony Mak853304c2016-04-18 15:17:41 +0100854 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000855 * Detects whether we should show a lock screen in front of this task for a locked user.
856 * <p>
857 * We'll do this if either of the following holds:
858 * <ul>
859 * <li>The top activity explicitly belongs to {@param userId}.</li>
860 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
861 * </ul>
862 *
863 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100864 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000865 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
866 // To handle the case that work app is in the task but just is not the top one.
867 final ActivityRecord activityRecord = task.getTopActivity();
868 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
869
870 return (activityRecord != null && activityRecord.userId == userId)
871 || (resultTo != null && resultTo.userId == userId);
872 }
873
874 /**
875 * Find all visible task stacks containing {@param userId} and intercept them with an activity
876 * to block out the contents and possibly start a credential-confirming intent.
877 *
878 * @param userId user handle for the locked managed profile.
879 */
880 void lockAllProfileTasks(@UserIdInt int userId) {
881 mWindowManager.deferSurfaceLayout();
882 try {
883 final List<ActivityStack> stacks = getStacks();
884 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
885 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
886 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
887 final TaskRecord task = tasks.get(taskNdx);
888
889 // Check the task for a top activity belonging to userId, or returning a result
890 // to an activity belonging to userId. Example case: a document picker for
891 // personal files, opened by a work app, should still get locked.
892 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000893 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
894 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000895 }
Tony Mak853304c2016-04-18 15:17:41 +0100896 }
897 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000898 } finally {
899 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100900 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000901 }
902
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800903 void setNextTaskIdForUserLocked(int taskId, int userId) {
904 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
905 if (taskId > currentTaskId) {
906 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700907 }
908 }
909
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700910 static int nextTaskIdForUser(int taskId, int userId) {
911 int nextTaskId = taskId + 1;
912 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
913 // Wrap around as there will be smaller task ids that are available now.
914 nextTaskId -= MAX_TASK_IDS_PER_USER;
915 }
916 return nextTaskId;
917 }
918
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800919 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800920 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
921 // for a userId u, a taskId can only be in the range
922 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
923 // 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 -0700924 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800925 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800926 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800927 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700928 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800929 if (candidateTaskId == currentTaskId) {
930 // Something wrong!
931 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
932 throw new IllegalStateException("Cannot get an available task id."
933 + " Reached limit of " + MAX_TASK_IDS_PER_USER
934 + " running tasks per user.");
935 }
936 }
937 mCurTaskIdForUser.put(userId, candidateTaskId);
938 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700939 }
940
Chong Zhang6cda19c2016-06-14 19:07:56 -0700941 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800942 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700943 if (stack == null) {
944 return null;
945 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700946 ActivityRecord resumedActivity = stack.mResumedActivity;
947 if (resumedActivity == null || resumedActivity.app == null) {
948 resumedActivity = stack.mPausingActivity;
949 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700950 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700951 }
952 }
953 return resumedActivity;
954 }
955
Dianne Hackbornff072722014-09-24 10:56:28 -0700956 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700957 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800958 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800959 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
960 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800961 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
962 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700963 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800964 continue;
965 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700966 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800967 if (hr != null) {
968 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
969 && processName.equals(hr.processName)) {
970 try {
George Mount2c92c972014-03-20 09:38:23 -0700971 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 didSomething = true;
973 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700974 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 Slog.w(TAG, "Exception in new application when starting activity "
976 + hr.intent.getComponent().flattenToShortString(), e);
977 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700978 }
Craig Mautner20e72272013-04-01 13:45:53 -0700979 }
Craig Mautner20e72272013-04-01 13:45:53 -0700980 }
981 }
982 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700983 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700984 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700985 }
Craig Mautner20e72272013-04-01 13:45:53 -0700986 return didSomething;
987 }
988
989 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
991 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
993 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700994 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800995 continue;
996 }
997 final ActivityRecord resumedActivity = stack.mResumedActivity;
998 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700999 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -08001000 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001001 return false;
1002 }
Craig Mautner20e72272013-04-01 13:45:53 -07001003 }
1004 }
Wei Wang65c7a152016-06-02 18:51:22 -07001005 // Send launch end powerhint when idle
1006 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001007 return true;
1008 }
1009
Craig Mautnerde4ef022013-04-07 19:01:33 -07001010 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001011 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1012 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001013 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1014 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001015 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001016 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001017 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001018 return false;
1019 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001020 }
1021 }
1022 }
1023 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001024 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001025 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1026 mLastFocusedStack + " to=" + mFocusedStack);
1027 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001028 return true;
1029 }
1030
1031 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001032 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001033 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1034 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001035 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1036 final ActivityStack stack = stacks.get(stackNdx);
1037 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001038 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001039 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001040 return false;
1041 }
1042 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001043 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001044 }
1045 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001046 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001047 }
1048
Craig Mautner2acc3892013-09-23 10:28:14 -07001049 /**
1050 * Pause all activities in either all of the stacks or just the back stacks.
1051 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001052 * @param resuming The resuming activity.
1053 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1054 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001055 * @return true if any activity was paused as a result of this call.
1056 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001057 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001058 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001059 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1060 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001061 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1062 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001063 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001064 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001065 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001066 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1067 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001068 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001069 }
1070 }
1071 return someActivityPaused;
1072 }
1073
Craig Mautnerde4ef022013-04-07 19:01:33 -07001074 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001075 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001076 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1077 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001078 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1079 final ActivityStack stack = stacks.get(stackNdx);
1080 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001081 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001082 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001083 Slog.d(TAG_STATES,
1084 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001085 pausing = false;
1086 } else {
1087 return false;
1088 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001089 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001090 }
1091 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001092 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001093 }
1094
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001095 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001096 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001097 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001098 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1099 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1100 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1101 final ActivityStack stack = stacks.get(stackNdx);
1102 if (stack.mResumedActivity != null &&
1103 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001104 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001105 }
1106 }
1107 }
1108 }
1109
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001110 void cancelInitializingActivities() {
1111 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1112 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1113 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1114 stacks.get(stackNdx).cancelInitializingActivities();
1115 }
1116 }
1117 }
1118
Bryce Lee4a194382017-04-04 14:32:48 -07001119 void waitActivityVisible(ComponentName name, WaitResult result) {
1120 final WaitInfo waitInfo = new WaitInfo(name, result);
1121 mWaitingForActivityVisible.add(waitInfo);
1122 }
1123
1124 void cleanupActivity(ActivityRecord r) {
1125 // Make sure this record is no longer in the pending finishes list.
1126 // This could happen, for example, if we are trimming activities
1127 // down to the max limit while they are still waiting to finish.
1128 mFinishingActivities.remove(r);
1129 mActivitiesWaitingForVisibleActivity.remove(r);
1130
1131 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001132 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001133 mWaitingForActivityVisible.remove(i);
1134 }
1135 }
1136 }
1137
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001138 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001139 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001140 }
1141
1142 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1143 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001144 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1145 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001146 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001147 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001148 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001149 result.timeout = false;
1150 result.who = w.getComponent();
1151 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1152 result.thisTime = result.totalTime;
1153 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001154 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001155 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001156 if (changed) {
1157 mService.notifyAll();
1158 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001159 }
1160
Chong Zhang5022da32016-06-21 16:31:37 -07001161 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1162 boolean changed = false;
1163 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1164 WaitResult w = mWaitingActivityLaunched.remove(i);
1165 if (w.who == null) {
1166 changed = true;
1167 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1168 // the starting activity ends up moving another activity to front, and it should
1169 // wait for this new activity to become visible instead.
1170 // Do not modify other fields.
1171 w.result = START_TASK_TO_FRONT;
1172 }
1173 }
1174 if (changed) {
1175 mService.notifyAll();
1176 }
1177 }
1178
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001179 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1180 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001181 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001182 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001183 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001184 if (w.who == null) {
1185 changed = true;
1186 w.timeout = timeout;
1187 if (r != null) {
1188 w.who = new ComponentName(r.info.packageName, r.info.name);
1189 }
1190 w.thisTime = thisTime;
1191 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001192 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001193 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001194 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001195 if (changed) {
1196 mService.notifyAll();
1197 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001198 }
1199
Craig Mautner29219d92013-04-16 20:19:12 -07001200 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001201 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001202 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001203 if (r != null) {
1204 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001205 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001206
Andrii Kulian7318d632016-07-20 18:59:28 -07001207 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001208 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1209
1210 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1211 final int displayId = mTmpOrderedDisplayIds.get(i);
1212 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1213 if (stacks == null) {
1214 continue;
1215 }
1216 for (int j = stacks.size() - 1; j >= 0; --j) {
1217 final ActivityStack stack = stacks.get(j);
1218 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1219 r = stack.topRunningActivityLocked();
1220 if (r != null) {
1221 return r;
1222 }
Craig Mautner29219d92013-04-16 20:19:12 -07001223 }
1224 }
1225 }
1226 return null;
1227 }
1228
Dianne Hackborn09233282014-04-30 11:33:59 -07001229 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001230 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001231 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1232 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001233 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001234 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001235 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001236 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1237 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001238 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001239 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001240 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001241 }
1242 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001243
1244 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1245 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1246 while (maxNum > 0) {
1247 long mostRecentActiveTime = Long.MIN_VALUE;
1248 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001249 final int numTaskLists = runningTaskLists.size();
1250 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1251 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001252 if (!stackTaskList.isEmpty()) {
1253 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1254 if (lastActiveTime > mostRecentActiveTime) {
1255 mostRecentActiveTime = lastActiveTime;
1256 selectedStackList = stackTaskList;
1257 }
1258 }
1259 }
1260 if (selectedStackList != null) {
1261 list.add(selectedStackList.remove(0));
1262 --maxNum;
1263 } else {
1264 break;
1265 }
1266 }
Craig Mautner20e72272013-04-01 13:45:53 -07001267 }
1268
Todd Kennedy7440f172015-12-09 14:31:22 -08001269 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1270 ProfilerInfo profilerInfo) {
1271 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001272 if (aInfo != null) {
1273 // Store the found target back into the intent, because now that
1274 // we have it we never want to do this again. For example, if the
1275 // user navigates back to this point in the history, we should
1276 // always restart the exact same activity.
1277 intent.setComponent(new ComponentName(
1278 aInfo.applicationInfo.packageName, aInfo.name));
1279
1280 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001281 if (!aInfo.processName.equals("system")) {
1282 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001283 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001284 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001285
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001286 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1287 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1288 }
1289
Man Caocfa78b22015-06-11 20:14:34 -07001290 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1291 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1292 }
1293
1294 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001295 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001296 }
1297 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001298 final String intentLaunchToken = intent.getLaunchToken();
1299 if (aInfo.launchToken == null && intentLaunchToken != null) {
1300 aInfo.launchToken = intentLaunchToken;
1301 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001302 }
1303 return aInfo;
1304 }
1305
Todd Kennedy7440f172015-12-09 14:31:22 -08001306 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001307 return resolveIntent(intent, resolvedType, userId, 0);
1308 }
1309
1310 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001311 synchronized (mService) {
1312 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001313 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001314 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001315 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001316 }
1317
1318 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1319 ProfilerInfo profilerInfo, int userId) {
1320 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1321 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1322 }
1323
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001324 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1325 boolean andResume, boolean checkConfig) throws RemoteException {
1326
1327 if (!allPausedActivitiesComplete()) {
1328 // While there are activities pausing we skipping starting any new activities until
1329 // pauses are complete. NOTE: that we also do this for activities that are starting in
1330 // the paused state because they will first be resumed then paused on the client side.
1331 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1332 "realStartActivityLocked: Skipping start of r=" + r
1333 + " some activities pausing...");
1334 return false;
1335 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001336
Andrii Kulianada10292017-02-09 23:19:29 -08001337 r.startFreezingScreenLocked(app, 0);
Wale Ogunwaled334a052017-05-12 07:17:36 -07001338 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */, true /* isTop */)) {
1339 // We only set the visibility to true if the activity is allowed to be visible based on
1340 // keyguard state. This avoids setting this into motion in window manager that is later
1341 // cancelled due to later calls to ensure visible activities that set visibility back to
1342 // false.
1343 r.setVisibility(true);
1344 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001345
Andrii Kulianada10292017-02-09 23:19:29 -08001346 // schedule launch ticks to collect information about slow apps.
1347 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001348
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001349 // Have the window manager re-evaluate the orientation of the screen based on the new
1350 // activity order. Note that as a result of this, it can call back into the activity
1351 // manager with a new orientation. We don't care about that, because the activity is not
1352 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001354 final int displayId = r.getDisplayId();
1355 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1356 getDisplayOverrideConfiguration(displayId),
1357 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001358 // Deferring resume here because we're going to launch new activity shortly.
1359 // We don't want to perform a redundant launch of the same record while ensuring
1360 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001361 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1362 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001363 }
1364
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001365 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001366 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001367 }
Makoto Onuki7a622782017-05-10 16:32:39 -07001368 final int applicationInfoUid =
1369 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1370 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1371 Slog.wtf(TAG,
1372 "User ID for activity changing for " + r
1373 + " appInfo.uid=" + r.appInfo.uid
1374 + " info.ai.uid=" + applicationInfoUid
1375 + " old=" + r.app + " new=" + app);
1376 }
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001377
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 r.app = app;
1379 app.waitingToKill = null;
1380 r.launchCount++;
1381 r.lastLaunchTime = SystemClock.uptimeMillis();
1382
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001383 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001384
1385 int idx = app.activities.indexOf(r);
1386 if (idx < 0) {
1387 app.activities.add(r);
1388 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001389 mService.updateLruProcessLocked(app, true, null);
1390 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001391
Bryce Leeaf691c02017-03-20 14:20:22 -07001392 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001393 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1394 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001395 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001396 }
1397
Andrii Kulian02b7a832016-10-06 23:11:56 -07001398 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001399 try {
1400 if (app.thread == null) {
1401 throw new RemoteException();
1402 }
1403 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001404 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001405 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001406 // We don't need to deliver new intents and/or set results if activity is going
1407 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001408 results = r.results;
1409 newIntents = r.newIntents;
1410 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001411 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1412 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1413 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001414 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1415 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001416 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001417 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001418 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001419 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001420 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1421 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001422 r.sleeping = false;
1423 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001424 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001425 mService.showAskCompatModeDialogLocked(r);
1426 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001427 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001428 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1429 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1430 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001431 final String profileFile = mService.mProfileFile;
1432 if (profileFile != null) {
1433 ParcelFileDescriptor profileFd = mService.mProfileFd;
1434 if (profileFd != null) {
1435 try {
1436 profileFd = profileFd.dup();
1437 } catch (IOException e) {
1438 if (profileFd != null) {
1439 try {
1440 profileFd.close();
1441 } catch (IOException o) {
1442 }
1443 profileFd = null;
1444 }
1445 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001446 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001447
1448 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001449 mService.mSamplingInterval, mService.mAutoStopProfiler,
1450 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001451 }
1452 }
1453 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001454
Andrii Kulianada10292017-02-09 23:19:29 -08001455 app.hasShownUi = true;
1456 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001457 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001458 // Because we could be starting an Activity in the system process this may not go across
1459 // a Binder interface which would create a new Configuration. Consequently we have to
1460 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001461
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07001462 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1463 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1464 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001465
Craig Mautner2420ead2013-04-01 17:13:20 -07001466 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001467 System.identityHashCode(r), r.info,
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07001468 // TODO: Have this take the merged configuration instead of separate global and
1469 // override configs.
1470 mergedConfiguration.getGlobalConfiguration(),
1471 mergedConfiguration.getOverrideConfiguration(), r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001472 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1473 r.persistentState, results, newIntents, !andResume,
1474 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001475
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001476 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001477 // This may be a heavy-weight process! Note that the package
1478 // manager will ensure that only activity can run in the main
1479 // process of the .apk, which is the only thing that will be
1480 // considered heavy-weight.
1481 if (app.processName.equals(app.info.packageName)) {
1482 if (mService.mHeavyWeightProcess != null
1483 && mService.mHeavyWeightProcess != app) {
1484 Slog.w(TAG, "Starting new heavy weight process " + app
1485 + " when already running "
1486 + mService.mHeavyWeightProcess);
1487 }
1488 mService.mHeavyWeightProcess = app;
1489 Message msg = mService.mHandler.obtainMessage(
1490 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1491 msg.obj = r;
1492 mService.mHandler.sendMessage(msg);
1493 }
1494 }
1495
1496 } catch (RemoteException e) {
1497 if (r.launchFailed) {
1498 // This is the second time we failed -- finish activity
1499 // and give up.
1500 Slog.e(TAG, "Second failure launching "
1501 + r.intent.getComponent().flattenToShortString()
1502 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001503 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001504 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1505 "2nd-crash", false);
1506 return false;
1507 }
1508
1509 // This is the first time we failed -- restart process and
1510 // retry.
Sudheer Shankae49e7b82017-05-05 16:39:32 -07001511 r.launchFailed = true;
Craig Mautner2420ead2013-04-01 17:13:20 -07001512 app.activities.remove(r);
1513 throw e;
1514 }
1515
1516 r.launchFailed = false;
1517 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001518 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001519 }
1520
1521 if (andResume) {
1522 // As part of the process of launching, ActivityThread also performs
1523 // a resume.
1524 stack.minimalResumeActivityLocked(r);
1525 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001526 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001527 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001528 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001529 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001530 "Moving to PAUSED: " + r + " (starting in paused state)");
1531 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001532 }
1533
1534 // Launch the new version setup screen if needed. We do this -after-
1535 // launching the initial activity (that is, home), so that it can have
1536 // a chance to initialize itself while in the background, making the
1537 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001538 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001539 mService.startSetupActivityLocked();
1540 }
1541
Dianne Hackborn465fa392014-09-14 14:21:18 -07001542 // Update any services we are bound to that might care about whether
1543 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001544 if (r.app != null) {
1545 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1546 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001547
Craig Mautner2420ead2013-04-01 17:13:20 -07001548 return true;
1549 }
1550
Craig Mautnere79d42682013-04-01 19:01:53 -07001551 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001552 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001553 // Is this activity's application already running?
1554 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001555 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001556
Andrii Kulian02b7a832016-10-06 23:11:56 -07001557 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001558
1559 if (app != null && app.thread != null) {
1560 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001561 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1562 || !"android".equals(r.info.packageName)) {
1563 // Don't add this if it is a platform component that is marked
1564 // to run in multiple processes, because this is actually
1565 // part of the framework so doesn't make sense to track as a
1566 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001567 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1568 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001569 }
George Mount2c92c972014-03-20 09:38:23 -07001570 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001571 return;
1572 } catch (RemoteException e) {
1573 Slog.w(TAG, "Exception when starting activity "
1574 + r.intent.getComponent().flattenToShortString(), e);
1575 }
1576
1577 // If a dead object exception was thrown -- fall through to
1578 // restart the application.
1579 }
1580
1581 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001582 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001583 }
1584
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001585 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001586 String resultWho, int requestCode, int callingPid, int callingUid,
1587 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001588 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001589 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1590 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001591 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001592 return true;
1593 }
1594 final int componentRestriction = getComponentRestrictionForCallingPackage(
1595 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1596 final int actionRestriction = getActionRestrictionForCallingPackage(
1597 intent.getAction(), callingPackage, callingPid, callingUid);
1598 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1599 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1600 if (resultRecord != null) {
1601 resultStack.sendActivityResultLocked(-1,
1602 resultRecord, resultWho, requestCode,
1603 Activity.RESULT_CANCELED, null);
1604 }
1605 final String msg;
1606 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1607 msg = "Permission Denial: starting " + intent.toString()
1608 + " from " + callerApp + " (pid=" + callingPid
1609 + ", uid=" + callingUid + ")" + " with revoked permission "
1610 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1611 } else if (!aInfo.exported) {
1612 msg = "Permission Denial: starting " + intent.toString()
1613 + " from " + callerApp + " (pid=" + callingPid
1614 + ", uid=" + callingUid + ")"
1615 + " not exported from uid " + aInfo.applicationInfo.uid;
1616 } else {
1617 msg = "Permission Denial: starting " + intent.toString()
1618 + " from " + callerApp + " (pid=" + callingPid
1619 + ", uid=" + callingUid + ")"
1620 + " requires " + aInfo.permission;
1621 }
1622 Slog.w(TAG, msg);
1623 throw new SecurityException(msg);
1624 }
1625
1626 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1627 final String message = "Appop Denial: starting " + intent.toString()
1628 + " from " + callerApp + " (pid=" + callingPid
1629 + ", uid=" + callingUid + ")"
1630 + " requires " + AppOpsManager.permissionToOp(
1631 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1632 Slog.w(TAG, message);
1633 return false;
1634 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1635 final String message = "Appop Denial: starting " + intent.toString()
1636 + " from " + callerApp + " (pid=" + callingPid
1637 + ", uid=" + callingUid + ")"
1638 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1639 Slog.w(TAG, message);
1640 return false;
1641 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001642 if (options != null) {
1643 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1644 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1645 callingPid, callingUid);
1646 if (startInTaskPerm != PERMISSION_GRANTED) {
1647 final String msg = "Permission Denial: starting " + intent.toString()
1648 + " from " + callerApp + " (pid=" + callingPid
1649 + ", uid=" + callingUid + ") with launchTaskId="
1650 + options.getLaunchTaskId();
1651 Slog.w(TAG, msg);
1652 throw new SecurityException(msg);
1653 }
1654 }
1655 // Check if someone tries to launch an activity on a private display with a different
1656 // owner.
1657 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001658 if (launchDisplayId != INVALID_DISPLAY
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001659 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId,
1660 aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001661 final String msg = "Permission Denial: starting " + intent.toString()
1662 + " from " + callerApp + " (pid=" + callingPid
1663 + ", uid=" + callingUid + ") with launchDisplayId="
1664 + launchDisplayId;
1665 Slog.w(TAG, msg);
1666 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001667 }
1668 }
1669
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001670 return true;
1671 }
1672
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001673 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001674 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1675 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001676 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1677 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1678
Andrii Kulian62e6f252017-05-30 22:46:53 -07001679 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001680 if (activityDisplay == null) {
1681 Slog.w(TAG, "Launch on display check: display not found");
1682 return false;
1683 }
1684
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001685 // Check if the caller can manage activity stacks.
1686 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
1687 callingUid);
1688 if (startAnyPerm == PERMISSION_GRANTED) {
1689 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1690 + " allow launch any on display");
1691 return true;
1692 }
1693
1694 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL
1695 && activityDisplay.mDisplay.getOwnerUid() != SYSTEM_UID
1696 && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1697 // Limit launching on virtual displays, because their contents can be read from Surface
1698 // by apps that created them.
1699 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1700 + " disallow launch on virtual display for not-embedded activity");
1701 return false;
1702 }
1703
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001704 if (!activityDisplay.isPrivate()) {
1705 // Anyone can launch on a public display.
1706 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1707 + " allow launch on public display");
1708 return true;
1709 }
1710
1711 // Check if the caller is the owner of the display.
1712 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1713 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1714 + " allow launch for owner of the display");
1715 return true;
1716 }
1717
1718 // Check if caller is present on display
1719 if (activityDisplay.isUidPresent(callingUid)) {
1720 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1721 + " allow launch for caller present on the display");
1722 return true;
1723 }
1724
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001725 Slog.w(TAG, "Launch on display check: denied");
1726 return false;
1727 }
1728
1729 /** Update lists of UIDs that are present on displays and have access to them. */
1730 void updateUIDsPresentOnDisplay() {
1731 mDisplayAccessUIDs.clear();
1732 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1733 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001734 // Only bother calculating the whitelist for private displays
1735 if (activityDisplay.isPrivate()) {
1736 mDisplayAccessUIDs.append(
1737 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1738 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001739 }
1740 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1741 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1742 }
1743
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001744 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001745 final long identity = Binder.clearCallingIdentity();
1746 try {
1747 return UserManager.get(mService.mContext).getUserInfo(userId);
1748 } finally {
1749 Binder.restoreCallingIdentity(identity);
1750 }
1751 }
1752
Svet Ganov99b60432015-06-27 13:15:22 -07001753 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001754 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001755 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1756 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001757 == PackageManager.PERMISSION_DENIED) {
1758 return ACTIVITY_RESTRICTION_PERMISSION;
1759 }
1760
Christopher Tateff7add02015-08-17 10:23:22 -07001761 if (activityInfo.permission == null) {
1762 return ACTIVITY_RESTRICTION_NONE;
1763 }
1764
Svet Ganov99b60432015-06-27 13:15:22 -07001765 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1766 if (opCode == AppOpsManager.OP_NONE) {
1767 return ACTIVITY_RESTRICTION_NONE;
1768 }
1769
1770 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1771 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001772 if (!ignoreTargetSecurity) {
1773 return ACTIVITY_RESTRICTION_APPOP;
1774 }
Svet Ganov99b60432015-06-27 13:15:22 -07001775 }
1776
1777 return ACTIVITY_RESTRICTION_NONE;
1778 }
1779
Svetoslav7008b512015-06-24 18:47:07 -07001780 private int getActionRestrictionForCallingPackage(String action,
1781 String callingPackage, int callingPid, int callingUid) {
1782 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001783 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001784 }
1785
1786 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1787 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001788 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001789 }
1790
1791 final PackageInfo packageInfo;
1792 try {
1793 packageInfo = mService.mContext.getPackageManager()
1794 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1795 } catch (PackageManager.NameNotFoundException e) {
1796 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001797 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001798 }
1799
1800 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001801 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001802 }
1803
1804 if (mService.checkPermission(permission, callingPid, callingUid) ==
1805 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001806 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001807 }
1808
1809 final int opCode = AppOpsManager.permissionToOpCode(permission);
1810 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001811 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001812 }
1813
1814 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1815 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001816 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001817 }
1818
Svet Ganov99b60432015-06-27 13:15:22 -07001819 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001820 }
1821
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001822 void setLaunchSource(int uid) {
1823 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1824 }
1825
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001826 void acquireLaunchWakelock() {
1827 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1828 throw new IllegalStateException("Calling must be system uid");
1829 }
1830 mLaunchingActivity.acquire();
1831 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1832 // To be safe, don't allow the wake lock to be held for too long.
1833 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1834 }
1835 }
1836
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001837 /**
1838 * Called when the frontmost task is idle.
1839 * @return the state of mService.mBooting before this was called.
1840 */
1841 private boolean checkFinishBootingLocked() {
1842 final boolean booting = mService.mBooting;
1843 boolean enableScreen = false;
1844 mService.mBooting = false;
1845 if (!mService.mBooted) {
1846 mService.mBooted = true;
1847 enableScreen = true;
1848 }
1849 if (booting || enableScreen) {
1850 mService.postFinishBooting(booting, enableScreen);
1851 }
1852 return booting;
1853 }
1854
Craig Mautnerf3333272013-04-22 10:55:53 -07001855 // Checked.
1856 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001857 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001858 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001859
Craig Mautnerf3333272013-04-22 10:55:53 -07001860 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001861 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001862 int NS = 0;
1863 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001864 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001865 boolean activityRemoved = false;
1866
Wale Ogunwale7d701172015-03-11 15:36:30 -07001867 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001868 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001869 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1870 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001871 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1872 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001873 if (fromTimeout) {
1874 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001875 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001876
1877 // This is a hack to semi-deal with a race condition
1878 // in the client where it can be constructed with a
1879 // newer configuration from when we asked it to launch.
1880 // We'll update with whatever configuration it now says
1881 // it used to launch.
1882 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001883 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001884 }
1885
1886 // We are now idle. If someone is waiting for a thumbnail from
1887 // us, we can now deliver.
1888 r.idle = true;
1889
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001890 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001891 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001892 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001893 }
1894 }
1895
1896 if (allResumedActivitiesIdle()) {
1897 if (r != null) {
1898 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001899 }
1900
1901 if (mLaunchingActivity.isHeld()) {
1902 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1903 if (VALIDATE_WAKE_LOCK_CALLER &&
1904 Binder.getCallingUid() != Process.myUid()) {
1905 throw new IllegalStateException("Calling must be system uid");
1906 }
1907 mLaunchingActivity.release();
1908 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001909 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001910 }
1911
1912 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001913 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1914 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001915 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001916 if ((NF = mFinishingActivities.size()) > 0) {
1917 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001918 mFinishingActivities.clear();
1919 }
1920
Craig Mautnerf3333272013-04-22 10:55:53 -07001921 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001922 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001923 mStartingUsers.clear();
1924 }
1925
Craig Mautnerf3333272013-04-22 10:55:53 -07001926 // Stop any activities that are scheduled to do so but have been
1927 // waiting for the next one to start.
1928 for (int i = 0; i < NS; i++) {
1929 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001930 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001931 if (stack != null) {
1932 if (r.finishing) {
1933 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1934 } else {
1935 stack.stopActivityLocked(r);
1936 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001937 }
1938 }
1939
1940 // Finish any activities that are scheduled to do so but have been
1941 // waiting for the next one to start.
1942 for (int i = 0; i < NF; i++) {
1943 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001944 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001945 if (stack != null) {
1946 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1947 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001948 }
1949
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001950 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001951 // Complete user switch
1952 if (startingUsers != null) {
1953 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001954 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001955 }
1956 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001957 }
1958
1959 mService.trimApplications();
1960 //dump();
1961 //mWindowManager.dump();
1962
Craig Mautnerf3333272013-04-22 10:55:53 -07001963 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001964 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001965 }
1966
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001967 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001968 }
1969
Craig Mautner8e569572013-10-11 17:36:59 -07001970 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001971 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001972 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1973 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001974 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1975 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1976 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001977 }
Craig Mautner19091252013-10-05 00:03:53 -07001978 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001979 }
1980
1981 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001982 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1983 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001984 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1985 stacks.get(stackNdx).closeSystemDialogsLocked();
1986 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001987 }
1988 }
1989
Craig Mautner93529a42013-10-04 15:03:13 -07001990 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001991 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001992 }
1993
Craig Mautner8d341ef2013-03-26 09:03:27 -07001994 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001995 * Update the last used stack id for non-current user (current user's last
1996 * used stack is the focused stack)
1997 */
1998 void updateUserStackLocked(int userId, ActivityStack stack) {
1999 if (userId != mCurrentUser) {
2000 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2001 }
2002 }
2003
2004 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002005 * @return true if some activity was finished (or would have finished if doit were true).
2006 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002007 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2008 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002009 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002010 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2011 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002012 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002013 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002014 if (stack.finishDisabledPackageActivitiesLocked(
2015 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002016 didSomething = true;
2017 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002018 }
2019 }
2020 return didSomething;
2021 }
2022
Dianne Hackborna413dc02013-07-12 12:02:55 -07002023 void updatePreviousProcessLocked(ActivityRecord r) {
2024 // Now that this process has stopped, we may want to consider
2025 // it to be the previous app to try to keep around in case
2026 // the user wants to return to it.
2027
2028 // First, found out what is currently the foreground app, so that
2029 // we don't blow away the previous app if this activity is being
2030 // hosted by the process that is actually still the foreground.
2031 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002032 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2033 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002034 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2035 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002036 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002037 if (stack.mResumedActivity != null) {
2038 fgApp = stack.mResumedActivity.app;
2039 } else if (stack.mPausingActivity != null) {
2040 fgApp = stack.mPausingActivity.app;
2041 }
2042 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002043 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002044 }
2045 }
2046
2047 // Now set this one as the previous process, only if that really
2048 // makes sense to.
2049 if (r.app != null && fgApp != null && r.app != fgApp
2050 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002051 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002052 mService.mPreviousProcess = r.app;
2053 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2054 }
2055 }
2056
Wale Ogunwaled046a012015-12-24 13:05:59 -08002057 boolean resumeFocusedStackTopActivityLocked() {
2058 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002059 }
2060
Wale Ogunwaled046a012015-12-24 13:05:59 -08002061 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002062 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002063 if (targetStack != null && isFocusedStack(targetStack)) {
2064 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002065 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002066 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2067 if (r == null || r.state != RESUMED) {
2068 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002069 } else if (r.state == RESUMED) {
2070 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2071 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002072 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002073 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002074 }
2075
Todd Kennedy39bfee52016-02-24 10:28:21 -08002076 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2077 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2078 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2079 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2080 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2081 }
2082 }
2083 }
2084
Adrian Roos20d7df32016-01-12 18:59:43 +01002085 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2086 TaskRecord finishedTask = null;
2087 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002088 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2089 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002090 final int numStacks = stacks.size();
2091 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2092 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002093 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2094 if (stack == focusedStack || finishedTask == null) {
2095 finishedTask = t;
2096 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002097 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002098 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002099 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002100 }
2101
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002102 void finishVoiceTask(IVoiceInteractionSession session) {
2103 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2104 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2105 final int numStacks = stacks.size();
2106 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2107 final ActivityStack stack = stacks.get(stackNdx);
2108 stack.finishVoiceTask(session);
2109 }
2110 }
2111 }
2112
Andrii Kulianc27916642016-04-12 17:59:27 -07002113 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2114 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002115 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2116 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002117 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002118 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2119 // Caller wants the home activity moved with it. To accomplish this,
2120 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002121 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002122 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002123 ActivityStack currentStack = task.getStack();
2124 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002125 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2126 + task + " to front. Stack is null");
2127 return;
2128 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002129
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002130 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002131 int stackId = options.getLaunchStackId();
2132 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002133 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002134 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002135 if (stackId == INVALID_STACK_ID) {
2136 stackId = task.getLaunchStackId();
2137 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002138 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002139 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2140 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002141 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002142 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2143 // still need moveTaskToFrontLocked() below for any transition settings.
2144 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002145 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2146 resizeStackLocked(stackId, bounds,
2147 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002148 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002149 } else {
2150 // WM resizeTask must be done after the task is moved to the correct stack,
2151 // because Task's setBounds() also updates dim layer's bounds, but that has
2152 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002153 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002154 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002155 }
2156 }
2157
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002158 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002159 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002160 r == null ? null : r.appTimeTracker, reason);
2161
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002162 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002163 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002164
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002165 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
2166 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002167 }
2168
Wale Ogunwale854809c2015-12-27 16:18:19 -08002169 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002170 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002171 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002172 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002173 return false;
2174 }
2175 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2176 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002177 }
2178
Winson Chung55893332017-02-17 17:13:10 -08002179 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002180 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002181 }
2182
Winson Chung55893332017-02-17 17:13:10 -08002183 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2184 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002185 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002186 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002187 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002188 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002189 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002190 return null;
2191 }
Matthew Ng330757d2017-02-28 14:19:17 -08002192 if (stackId == DOCKED_STACK_ID) {
2193 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2194 // the other side of the docked stack and we make visibility decisions based on that.
2195 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2196 }
Winson Chung55893332017-02-17 17:13:10 -08002197 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002198 }
2199
Andrii Kulian16802aa2016-11-02 12:21:33 -07002200 /**
2201 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2202 * If there is no such stack, new dynamic stack can be created.
2203 * @param displayId Target display.
2204 * @param r Activity that should be launched there.
2205 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2206 */
2207 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002208 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002209 if (activityDisplay == null) {
2210 throw new IllegalArgumentException(
2211 "Display with displayId=" + displayId + " not found.");
2212 }
2213
2214 // Return the topmost valid stack on the display.
2215 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2216 final ActivityStack stack = activityDisplay.mStacks.get(i);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002217 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002218 return stack;
2219 }
2220 }
2221
2222 // If there is no valid stack on the external display - check if new dynamic stack will do.
2223 if (displayId != Display.DEFAULT_DISPLAY) {
2224 final int newDynamicStackId = getNextStackId();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002225 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002226 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2227 }
2228 }
2229
2230 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2231 return null;
2232 }
2233
Craig Mautner967212c2013-04-13 21:10:58 -07002234 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002235 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002236 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2237 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002238 }
2239 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002240 }
2241
Jorim Jaggife762342016-10-13 14:33:27 +02002242 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2243 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2244 }
2245
Andrii Kulian7fc22812016-12-28 13:04:11 -08002246 /**
2247 * Get next focusable stack in the system. This will search across displays and stacks
2248 * in last-focused order for a focusable and visible stack, different from the target stack.
2249 *
2250 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2251 * searching for next candidate.
2252 * @return Next focusable {@link ActivityStack}, null if not found.
2253 */
2254 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2255 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2256
2257 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2258 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002259 // If a display is registered in WM, it must also be available in AM.
2260 @SuppressWarnings("ConstantConditions")
2261 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002262 for (int j = stacks.size() - 1; j >= 0; --j) {
2263 final ActivityStack stack = stacks.get(j);
2264 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002265 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002266 return stack;
2267 }
2268 }
2269 }
2270
2271 return null;
2272 }
2273
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002274 /**
2275 * Get next valid stack for launching provided activity in the system. This will search across
2276 * displays and stacks in last-focused order for a focusable and visible stack, except those
2277 * that are on a currently focused display.
2278 *
2279 * @param r The activity that is being launched.
2280 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2281 * searching for the next candidate.
2282 * @return Next valid {@link ActivityStack}, null if not found.
2283 */
2284 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2285 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2286 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2287 final int displayId = mTmpOrderedDisplayIds.get(i);
2288 if (displayId == currentFocus) {
2289 continue;
2290 }
2291 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2292 if (stack != null) {
2293 return stack;
2294 }
2295 }
2296 return null;
2297 }
2298
Craig Mautneree2e45a2014-06-27 12:10:03 -07002299 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002300 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002301 }
2302
2303 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002304 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2305 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2306 final TaskRecord task = tasks.get(taskNdx);
2307 if (task.isHomeTask()) {
2308 final ArrayList<ActivityRecord> activities = task.mActivities;
2309 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2310 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002311 if (r.isHomeActivity()
2312 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002313 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002314 }
2315 }
2316 }
2317 }
2318 return null;
2319 }
2320
Chong Zhangb15758a2015-11-17 12:12:03 -08002321 /**
2322 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2323 * the docked stack itself, or if it's side-by-side to the docked stack.
2324 */
2325 boolean isStackDockedInEffect(int stackId) {
2326 return stackId == DOCKED_STACK_ID ||
2327 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2328 }
2329
Todd Kennedyca4d8422015-01-15 15:19:22 -08002330 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002331 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002332 ActivityContainer activityContainer =
2333 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002334 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002335 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2336 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002337 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002338 return activityContainer;
2339 }
2340
Craig Mautner34b73df2014-01-12 21:11:08 -08002341 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002342 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2343 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2344 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002345 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2346 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002347 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002348 }
2349 }
2350
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002351 void deleteActivityContainerRecord(int stackId) {
2352 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2353 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2354 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002355 }
2356
Jorim Jaggidc249c42015-12-15 14:57:31 -08002357 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002358 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002359 if (stackId == DOCKED_STACK_ID) {
2360 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002361 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002362 return;
2363 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002364 final ActivityStack stack = getStack(stackId);
2365 if (stack == null) {
2366 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2367 return;
2368 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002369
Winson Chunga2249ac2017-03-30 18:15:30 -07002370 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2371 getStack(DOCKED_STACK_ID) != null) {
2372 // If the docked stack exists, don't resize non-floating stacks independently of the
2373 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002374 return;
2375 }
2376
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002377 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002378 mWindowManager.deferSurfaceLayout();
2379 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002380 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002381 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002382 stack.ensureVisibleActivitiesConfigurationLocked(
2383 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002384 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002385 } finally {
2386 mWindowManager.continueSurfaceLayout();
2387 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002388 }
2389 }
2390
Jorim Jaggi192086e2016-03-11 17:17:03 +01002391 void deferUpdateBounds(int stackId) {
2392 final ActivityStack stack = getStack(stackId);
2393 if (stack != null) {
2394 stack.deferUpdateBounds();
2395 }
2396 }
2397
2398 void continueUpdateBounds(int stackId) {
2399 final ActivityStack stack = getStack(stackId);
2400 if (stack != null) {
2401 stack.continueUpdateBounds();
2402 }
2403 }
2404
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002405 void notifyAppTransitionDone() {
Matthew Ng606dd802017-06-05 14:06:32 -07002406 continueUpdateBounds(RECENTS_STACK_ID);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002407 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2408 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002409 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002410 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002411 if (task != null) {
2412 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002413 }
2414 }
2415 mResizingTasksDuringAnimation.clear();
2416 }
2417
Robert Carre7cc44d2017-03-20 19:04:30 -07002418 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2419 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002420
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002421 final ActivityStack stack = getStack(fromStackId);
2422 if (stack == null) {
2423 return;
2424 }
2425
2426 mWindowManager.deferSurfaceLayout();
2427 try {
2428 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002429 // We are moving all tasks from the docked stack to the fullscreen stack,
2430 // which is dismissing the docked stack, so resize all other stacks to
2431 // fullscreen here already so we don't end up with resize trashing.
2432 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2433 if (StackId.isResizeableByDockedStack(i)) {
2434 ActivityStack otherStack = getStack(i);
2435 if (otherStack != null) {
2436 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2437 true /* allowResizeInDockedMode */, DEFER_RESUME);
2438 }
2439 }
2440 }
2441
2442 // Also disable docked stack resizing since we have manually adjusted the
2443 // size of other stacks above and we don't want to trigger a docked stack
2444 // resize when we remove task from it below and it is detached from the
2445 // display because it no longer contains any tasks.
2446 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002447 } else if (fromStackId == PINNED_STACK_ID) {
2448 if (onTop) {
2449 // Log if we are expanding the PiP to fullscreen
2450 MetricsLogger.action(mService.mContext,
2451 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2452 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002453 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002454 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002455 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002456 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002457 // If we are moving from the pinned stack, then the animation takes care of updating
2458 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002459 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002460 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2461 final int size = tasks.size();
2462 if (onTop) {
2463 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002464 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002465 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002466 if (fromStackId == PINNED_STACK_ID) {
2467 // Update the return-to to reflect where the pinned stack task was moved
2468 // from so that we retain the stack that was previously visible if the
2469 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002470 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2471 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002472 }
Winson Chung74666102017-02-22 17:49:24 -08002473 // Defer resume until all the tasks have been moved to the fullscreen stack
2474 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2475 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002476 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002477 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002478 }
2479 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002480 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002481 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002482 // Position the tasks in the fullscreen stack in order at the bottom of the
2483 // stack. Also defer resume until all the tasks have been moved to the
2484 // fullscreen stack.
2485 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002486 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2487 schedulePictureInPictureModeChange,
2488 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002489 }
2490 }
Winson Chung74666102017-02-22 17:49:24 -08002491
2492 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2493 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002494 } finally {
2495 mAllowDockedStackResize = true;
2496 mWindowManager.continueSurfaceLayout();
2497 }
2498 }
2499
Robert Carr6914f082017-03-20 19:04:30 -07002500 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002501 mWindowManager.inSurfaceTransaction(
2502 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002503 }
2504
Jorim Jaggidc249c42015-12-15 14:57:31 -08002505 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002506 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2507 boolean preserveWindows) {
2508 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2509 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2510 false /* deferResume */);
2511 }
2512
2513 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2514 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2515 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002516
2517 if (!mAllowDockedStackResize) {
2518 // Docked stack resize currently disabled.
2519 return;
2520 }
2521
Jorim Jaggidc249c42015-12-15 14:57:31 -08002522 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2523 if (stack == null) {
2524 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2525 return;
2526 }
2527
2528 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2529 mWindowManager.deferSurfaceLayout();
2530 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002531 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2532 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002533 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002534 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002535
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002536 // TODO: Checking for isAttached might not be needed as if the user passes in null
2537 // dockedBounds then they want the docked stack to be dismissed.
2538 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2539 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002540 // In this case we make all other static stacks fullscreen and move all
2541 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002542 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002543
2544 // stack shouldn't contain anymore activities, so nothing to resume.
2545 r = null;
2546 } else {
2547 // Docked stacks occupy a dedicated region on screen so the size of all other
2548 // static stacks need to be adjusted so they don't overlap with the docked stack.
2549 // We get the bounds to use from window manager which has been adjusted for any
2550 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002551 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002552 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002553 final ActivityStack current = getStack(i);
2554 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002555 current.getStackDockedModeBounds(
2556 tempOtherTaskBounds /* currentTempTaskBounds */,
2557 tempRect /* outStackBounds */,
2558 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2559
Andrii Kuliane48b7d82017-04-28 14:51:05 -07002560 resizeStackLocked(i, !tempRect.isEmpty() ? tempRect : null,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002561 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2562 tempOtherTaskInsetBounds, preserveWindows,
2563 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002564 }
2565 }
2566 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002567 if (!deferResume) {
2568 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2569 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002570 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002571 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002572 mWindowManager.continueSurfaceLayout();
2573 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2574 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002575 }
2576
Robert Carr0d00c2e2016-02-29 17:45:02 -08002577 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002578 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002579 if (stack == null) {
2580 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2581 return;
2582 }
Winson Chung19953ca2017-04-11 11:19:23 -07002583
2584 // It is possible for the bounds animation from the WM to call this but be delayed by
2585 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2586 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2587 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2588 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002589 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002590 return;
2591 }
2592
Robert Carr0d00c2e2016-02-29 17:45:02 -08002593 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2594 mWindowManager.deferSurfaceLayout();
2595 try {
2596 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002597 Rect insetBounds = null;
2598 if (tempPinnedTaskBounds != null) {
2599 // We always use 0,0 as the position for the inset rect because
2600 // if we are getting insets at all in the pinned stack it must mean
2601 // we are headed for fullscreen.
2602 insetBounds = tempRect;
2603 insetBounds.top = 0;
2604 insetBounds.left = 0;
2605 insetBounds.right = tempPinnedTaskBounds.width();
2606 insetBounds.bottom = tempPinnedTaskBounds.height();
2607 }
2608 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002609 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002610 } finally {
2611 mWindowManager.continueSurfaceLayout();
2612 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2613 }
2614 }
2615
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002616 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002617 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002618 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002619 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002620 }
2621
Wale Ogunwale1666e312016-12-16 11:27:18 -08002622 final ActivityContainer activityContainer =
2623 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002624 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002625 }
2626
Robert Carr6914f082017-03-20 19:04:30 -07002627
Robert Carre7cc44d2017-03-20 19:04:30 -07002628 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002629 final ActivityStack stack = getStack(stackId);
2630 if (stack == null) {
2631 return;
2632 }
2633
2634 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2635 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002636 /**
2637 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2638 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2639 * that the client receives onStop() before it is reparented. We do this by detaching
2640 * the stack from the display so that it will be considered invisible when
2641 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2642 * invisible as well and added to the stopping list. After which we process the
2643 * stopping list by handling the idle.
2644 */
2645 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2646 pinnedStack.mForceHidden = true;
2647 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2648 pinnedStack.mForceHidden = false;
2649 activityIdleInternalLocked(null, false /* fromTimeout */,
2650 true /* processPausingActivites */, null /* configuration */);
2651
2652 // Move all the tasks to the bottom of the fullscreen stack
2653 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002654 } else {
2655 for (int i = tasks.size() - 1; i >= 0; i--) {
2656 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2657 REMOVE_FROM_RECENTS);
2658 }
2659 }
2660 }
2661
2662 /**
Robert Carr6914f082017-03-20 19:04:30 -07002663 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2664 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2665 * instead moved back onto the fullscreen stack.
2666 */
2667 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002668 mWindowManager.inSurfaceTransaction(
2669 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002670 }
2671
2672 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002673 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2674 */
2675 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2676 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2677 }
2678
2679 /**
Winson Chung010927a2016-12-15 16:12:35 -08002680 * Removes the task with the specified task id.
2681 *
2682 * @param taskId Identifier of the task to be removed.
2683 * @param killProcess Kill any process associated with the task if possible.
2684 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002685 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2686 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002687 * @return Returns true if the given task was found and removed.
2688 */
Winson Chung6954fc92017-03-24 16:22:12 -07002689 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2690 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002691 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2692 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002693 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002694 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002695 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2696 if (tr.isPersistable) {
2697 mService.notifyTaskPersisterLocked(null, true);
2698 }
2699 return true;
2700 }
2701 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2702 return false;
2703 }
2704
2705 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2706 if (removeFromRecents) {
2707 mRecentTasks.remove(tr);
2708 tr.removedFromRecents();
2709 }
2710 ComponentName component = tr.getBaseIntent().getComponent();
2711 if (component == null) {
2712 Slog.w(TAG, "No component for base intent of task: " + tr);
2713 return;
2714 }
2715
2716 // Find any running services associated with this app and stop if needed.
2717 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2718
2719 if (!killProcess) {
2720 return;
2721 }
2722
2723 // Determine if the process(es) for this task should be killed.
2724 final String pkg = component.getPackageName();
2725 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2726 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2727 for (int i = 0; i < pmap.size(); i++) {
2728
2729 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2730 for (int j = 0; j < uids.size(); j++) {
2731 ProcessRecord proc = uids.valueAt(j);
2732 if (proc.userId != tr.userId) {
2733 // Don't kill process for a different user.
2734 continue;
2735 }
2736 if (proc == mService.mHomeProcess) {
2737 // Don't kill the home process along with tasks from the same package.
2738 continue;
2739 }
2740 if (!proc.pkgList.containsKey(pkg)) {
2741 // Don't kill process that is not associated with this task.
2742 continue;
2743 }
2744
2745 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002746 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002747 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2748 // Don't kill process(es) that has an activity in a different task that is
2749 // also in recents.
2750 return;
2751 }
2752 }
2753
2754 if (proc.foregroundServices) {
2755 // Don't kill process(es) with foreground service.
2756 return;
2757 }
2758
2759 // Add process to kill list.
2760 procsToKill.add(proc);
2761 }
2762 }
2763
2764 // Kill the running processes.
2765 for (int i = 0; i < procsToKill.size(); i++) {
2766 ProcessRecord pr = procsToKill.get(i);
2767 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2768 && pr.curReceivers.isEmpty()) {
2769 pr.kill("remove task", true);
2770 } else {
2771 // We delay killing processes that are not in the background or running a receiver.
2772 pr.waitingToKill = "remove task";
2773 }
2774 }
2775 }
2776
Craig Mautner4a1cb222013-12-04 16:14:06 -08002777 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002778 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002779 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2780 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002781 break;
2782 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002783 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002784 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002785 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002786 }
2787
Chong Zhang5dcb2752015-08-18 13:50:26 -07002788 /**
2789 * Restores a recent task to a stack
2790 * @param task The recent task to be restored.
2791 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002792 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2793 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002794 * @return true if the task has been restored successfully.
2795 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002796 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002797 if (!StackId.isStaticStack(stackId)) {
2798 // If stack is not static (or stack id is invalid) - use the default one.
2799 // This means that tasks that were on external displays will be restored on the
2800 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002801 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002802 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002803 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2804 // Put it in the fullscreen stack.
2805 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002806 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002807
Andrii Kulian02b7a832016-10-06 23:11:56 -07002808 final ActivityStack currentStack = task.getStack();
2809 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002810 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002811 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002812 // Nothing else to do since it is already restored in the right stack.
2813 return true;
2814 }
2815 // Remove current stack association, so we can re-associate the task with the
2816 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002817 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002818 }
2819
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002820 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002821 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002822
2823 if (stack == null) {
2824 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002825 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2826 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002827 return false;
2828 }
2829
Wale Ogunwale72919d22016-12-08 18:58:50 -08002830 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002831 // TODO: move call for creation here and other place into Stack.addTask()
2832 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002833 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2834 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002835 final ArrayList<ActivityRecord> activities = task.mActivities;
2836 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002837 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002838 }
2839 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002840 }
2841
Wale Ogunwale040b4702015-08-06 18:10:50 -07002842 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002843 * Move stack with all its existing content to specified display.
2844 * @param stackId Id of stack to move.
2845 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002846 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002847 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002848 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002849 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002850 if (activityDisplay == null) {
2851 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2852 + displayId);
2853 }
2854 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2855 if (activityContainer != null) {
2856 if (activityContainer.isAttachedLocked()) {
2857 if (activityContainer.getDisplayId() == displayId) {
2858 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2859 + " to its current displayId=" + displayId);
2860 }
2861
Andrii Kulian250d6532017-02-08 23:30:45 -08002862 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002863 } else {
2864 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2865 + stackId + " is not attached to any display.");
2866 }
2867 } else {
2868 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2869 + stackId);
2870 }
Andrii Kulian839def92016-11-02 10:58:58 -07002871 // TODO(multi-display): resize stacks properly if moved from split-screen.
2872 }
2873
2874 /**
Winson Chung74666102017-02-22 17:49:24 -08002875 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2876 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002877 */
Winson Chung74666102017-02-22 17:49:24 -08002878 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002879 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002880
Winson Chung74666102017-02-22 17:49:24 -08002881 // Check that we aren't reparenting to the same stack that the task is already in
2882 if (prevStack != null && prevStack.mStackId == stackId) {
2883 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2884 + " already in stackId=" + stackId);
2885 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002886 }
2887
Winson Chung74666102017-02-22 17:49:24 -08002888 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2889 // support
2890 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2891 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2892 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002893 }
2894
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002895 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2896 // multi-display.
2897 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2898 // TODO: Check ActivityView after fixing b/35349678.
2899 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2900 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2901 + " reparent task=" + task + " to stackId=" + stackId);
2902 }
2903
Winson Chung74666102017-02-22 17:49:24 -08002904 // Ensure that we aren't trying to move into a freeform stack without freeform
2905 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002906 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002907 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2908 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002909 }
2910
Winson Chung74666102017-02-22 17:49:24 -08002911 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2912 // used for split-screen mode and will cause things like the docked divider to show up. We
2913 // instead leave the task in its current stack or move it to the fullscreen stack if it
2914 // isn't currently in a stack.
2915 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2916 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2917 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2918 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002919 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002920
Winson Chung74666102017-02-22 17:49:24 -08002921 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2922 // example, a docked stack is created which will lead to the stack we are moving from being
2923 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002924 try {
Winson Chung74666102017-02-22 17:49:24 -08002925 task.mTemporarilyUnresizable = true;
2926 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002927 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002928 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002929 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002930 }
2931
Winson Chung08f81892017-03-02 15:40:51 -08002932 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002933 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2934 if (stack == null) {
2935 throw new IllegalArgumentException(
2936 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2937 }
2938
2939 final ActivityRecord r = stack.topRunningActivityLocked();
2940 if (r == null) {
2941 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2942 + " in stack=" + stack);
2943 return false;
2944 }
2945
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002946 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002947 Slog.w(TAG,
2948 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002949 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002950 return false;
2951 }
2952
Winson Chung3a682872017-04-10 14:38:05 -07002953 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002954 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002955 return true;
2956 }
2957
Winson Chung8bca9e42017-04-16 15:59:43 -07002958 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002959 boolean moveHomeStackToFront, String reason) {
2960
Wale Ogunwale480dca02016-02-06 13:58:29 -08002961 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002962
Bryce Lee04ab3462017-04-10 15:06:33 -07002963 // This will clear the pinned stack by moving an existing task to the full screen stack,
2964 // ensuring only one task is present.
2965 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
2966
Wale Ogunwale1666e312016-12-16 11:27:18 -08002967 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002968 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002969
Wale Ogunwale480dca02016-02-06 13:58:29 -08002970 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002971 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002972
Andrii Kulian02b7a832016-10-06 23:11:56 -07002973 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002974 // An activity can't be pinned and visible behind at the same time. Go ahead and
2975 // release it from been visible behind before pinning.
2976 requestVisibleBehindLocked(r, false);
2977 }
2978
Wale Ogunwale480dca02016-02-06 13:58:29 -08002979 // Resize the pinned stack to match the current size of the task the activity we are
2980 // going to be moving is currently contained in. We do this to have the right starting
2981 // animation bounds for the pinned stack to the desired bounds the caller wants.
2982 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2983 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002984 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002985
2986 if (task.mActivities.size() == 1) {
2987 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002988 // the stack without re-parenting the activity in a different task. We don't
2989 // move the home stack forward if we are currently entering picture-in-picture
2990 // while pausing because that changes the focused stack and may prevent the new
2991 // starting activity from resuming.
2992 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002993 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002994 // Move the home stack forward if the task we just moved to the pinned stack
2995 // was launched from home so home should be visible behind it.
2996 moveHomeStackToFront(reason);
2997 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002998 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002999 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003000 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003001 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003002 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003003 // reveal/leave the other activities in their original task.
3004
3005 // Currently, we don't support reparenting activities across tasks in two different
3006 // stacks, so instead, just create a new task in the same stack, reparent the
3007 // activity into that task, and then reparent the whole task to the new stack. This
3008 // ensures that all the necessary work to migrate states in the old and new stacks
3009 // is also done.
3010 final TaskRecord newTask = task.getStack().createTaskRecord(
3011 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
3012 r.mActivityType);
3013 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3014
Winson Chung5af42fc2017-03-24 17:11:33 -07003015 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003016 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003017 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003018 }
Winson Chungc2baac02017-01-11 13:34:47 -08003019
3020 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3021 // to the pinned stack
3022 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003023 } finally {
3024 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003025 }
3026
Winson Chunge7ba6862017-05-24 12:13:33 -07003027 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3028 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3029 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003030 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3031
Winson Chunge7ba6862017-05-24 12:13:33 -07003032 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3033 true /* fromFullscreen */);
3034
3035 // Update the visibility of all activities after the they have been reparented to the new
3036 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3037 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3038 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003039 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003040 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003041
Winson Chungb5026902017-05-03 12:45:13 -07003042 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName,
3043 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003044 }
3045
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003046 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003047 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3048 if (r == null || !r.isFocusable()) {
3049 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3050 "moveActivityStackToFront: unfocusable r=" + r);
3051 return false;
3052 }
3053
Bryce Leeaf691c02017-03-20 14:20:22 -07003054 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003055 final ActivityStack stack = r.getStack();
3056 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003057 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3058 + r + " task=" + task);
3059 return false;
3060 }
3061
Chong Zhang6cda19c2016-06-14 19:07:56 -07003062 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3063 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3064 "moveActivityStackToFront: already on top, r=" + r);
3065 return false;
3066 }
3067
3068 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3069 "moveActivityStackToFront: r=" + r);
3070
3071 stack.moveToFront(reason, task);
3072 return true;
3073 }
3074
David Stevensc6b91c62017-02-08 14:23:58 -08003075 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003076 mTmpFindTaskResult.r = null;
3077 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003078 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003079 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003080 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3081 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003082 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3083 final ActivityStack stack = stacks.get(stackNdx);
Winson Chung91e5c882017-04-21 14:44:38 -07003084 if (!checkActivityBelongsInStack(r, stack)) {
3085 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3086 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003087 continue;
3088 }
3089 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003090 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3091 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003092 continue;
3093 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003094 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003095 // It is possible to have tasks in multiple stacks with the same root affinity, so
3096 // we should keep looking after finding an affinity match to see if there is a
3097 // better match in another stack. Also, task affinity isn't a good enough reason
3098 // to target a display which isn't the source of the intent, so skip any affinity
3099 // matches not on the specified display.
3100 if (mTmpFindTaskResult.r != null) {
3101 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3102 return mTmpFindTaskResult.r;
3103 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003104 // Note: since the traversing through the stacks is top down, the floating
3105 // tasks should always have lower priority than any affinity-matching tasks
3106 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003107 affinityMatch = mTmpFindTaskResult.r;
3108 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003109 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003110 }
3111 }
Winson Chung5b895b72017-05-01 13:46:25 -07003112
David Stevensc6b91c62017-02-08 14:23:58 -08003113 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3114 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003115 }
3116
Winson Chung91e5c882017-04-21 14:44:38 -07003117 /**
3118 * Checks that for the given activity {@param r}, its activity type matches the {@param stack}
3119 * type.
3120 */
3121 private boolean checkActivityBelongsInStack(ActivityRecord r, ActivityStack stack) {
3122 if (r.isHomeActivity()) {
3123 return stack.isHomeStack();
3124 } else if (r.isRecentsActivity()) {
3125 return stack.isRecentsStack();
3126 } else if (r.isAssistantActivity()) {
3127 return stack.isAssistantStack();
3128 }
3129 return true;
3130 }
3131
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003132 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3133 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003134 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3135 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003136 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003137 final ActivityRecord ar = stacks.get(stackNdx)
3138 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003139 if (ar != null) {
3140 return ar;
3141 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003142 }
3143 }
3144 return null;
3145 }
3146
Craig Mautner8d341ef2013-03-26 09:03:27 -07003147 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003148 scheduleSleepTimeout();
3149 if (!mGoingToSleep.isHeld()) {
3150 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003151 if (mLaunchingActivity.isHeld()) {
3152 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3153 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003154 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003155 mLaunchingActivity.release();
3156 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003157 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003158 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003159 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003160 }
3161
3162 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003163 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003164
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003165 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003166 final long endTime = System.currentTimeMillis() + timeout;
3167 while (true) {
3168 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003169 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3170 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003171 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3172 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3173 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003174 }
3175 if (cantShutdown) {
3176 long timeRemaining = endTime - System.currentTimeMillis();
3177 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003178 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003179 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003180 } catch (InterruptedException e) {
3181 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003182 } else {
3183 Slog.w(TAG, "Activity manager shutdown timed out");
3184 timedout = true;
3185 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003186 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003187 } else {
3188 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003189 }
3190 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003191
3192 // Force checkReadyForSleep to complete.
3193 mSleepTimeout = true;
3194 checkReadyForSleepLocked();
3195
Craig Mautner8d341ef2013-03-26 09:03:27 -07003196 return timedout;
3197 }
3198
3199 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003200 removeSleepTimeouts();
3201 if (mGoingToSleep.isHeld()) {
3202 mGoingToSleep.release();
3203 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003204 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3205 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3207 final ActivityStack stack = stacks.get(stackNdx);
3208 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003209 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003210 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003211 }
Craig Mautner5314a402013-09-26 12:40:16 -07003212 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003213 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003214 mGoingToSleepActivities.clear();
3215 }
3216
3217 void activitySleptLocked(ActivityRecord r) {
3218 mGoingToSleepActivities.remove(r);
3219 checkReadyForSleepLocked();
3220 }
3221
3222 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003223 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003224 // Do not care.
3225 return;
3226 }
3227
3228 if (!mSleepTimeout) {
3229 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003230 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3231 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003232 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3233 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3234 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003235 }
3236
3237 if (mStoppingActivities.size() > 0) {
3238 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003239 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003240 + mStoppingActivities.size() + " activities");
3241 scheduleIdleLocked();
3242 dontSleep = true;
3243 }
3244
3245 if (mGoingToSleepActivities.size() > 0) {
3246 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003247 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003248 + mGoingToSleepActivities.size() + " activities");
3249 dontSleep = true;
3250 }
3251
3252 if (dontSleep) {
3253 return;
3254 }
3255 }
3256
Wei Wang65c7a152016-06-02 18:51:22 -07003257 // Send launch end powerhint before going sleep
3258 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3259
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 stacks.get(stackNdx).goToSleep();
3264 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003265 }
3266
3267 removeSleepTimeouts();
3268
3269 if (mGoingToSleep.isHeld()) {
3270 mGoingToSleep.release();
3271 }
3272 if (mService.mShuttingDown) {
3273 mService.notifyAll();
3274 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003275 }
3276
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003277 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003278 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003279 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003280 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003281 }
3282 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003283 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003284 mWindowManager.executeAppTransition();
3285 return true;
3286 }
3287 return false;
3288 }
3289
Craig Mautner8d341ef2013-03-26 09:03:27 -07003290 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003291 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3292 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003293 int stackNdx = stacks.size() - 1;
3294 while (stackNdx >= 0) {
3295 stacks.get(stackNdx).handleAppCrashLocked(app);
3296 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003297 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003298 }
3299 }
3300
Jose Lima4b6c6692014-08-12 17:41:12 -07003301 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003302 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003303 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003304 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3305 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003306 return false;
3307 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003308
3309 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3310 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3311 + " visible=" + visible + " stackId=" + stack.mStackId
3312 + " can't contain visible behind activities");
3313 return false;
3314 }
3315
Jose Lima4b6c6692014-08-12 17:41:12 -07003316 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003317 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3318 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003319
3320 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003321 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003322 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003323 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003324 return true;
3325 }
3326
3327 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003328 if (visible && top.fullscreen) {
3329 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003330 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3331 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3332 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3333 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003334 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003335 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3336 // Only the activity set as currently visible behind should actively reset its
3337 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003338 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3339 "requestVisibleBehind: returning visible=" + visible
3340 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3341 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003342 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003343 }
3344
Jose Lima4b6c6692014-08-12 17:41:12 -07003345 stack.setVisibleBehindActivity(visible ? r : null);
3346 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003347 // If there is a translucent home activity, we need to force it stop being translucent,
3348 // because we can't depend on the application to necessarily perform that operation.
3349 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003350 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003351 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003352 mService.convertFromTranslucent(next.appToken);
3353 }
3354 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003355 if (top.app != null && top.app.thread != null) {
3356 // Notify the top app of the change.
3357 try {
3358 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3359 } catch (RemoteException e) {
3360 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003361 }
3362 return true;
3363 }
3364
Craig Mautnerbb742462014-07-07 15:28:55 -07003365 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003366 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003367 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003368 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003369
3370 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003371 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003372 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003373 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003374 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003375
3376 // When launching tasks behind, update the last active time of the top task after the new
3377 // task has been shown briefly
3378 final ActivityRecord top = stack.topActivity();
3379 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003380 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003381 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003382 }
3383
3384 void scheduleLaunchTaskBehindComplete(IBinder token) {
3385 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3386 }
3387
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003388 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3389 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003390 mKeyguardController.beginActivityVisibilityUpdate();
3391 try {
3392 // First the front stacks. In case any are not fullscreen and are in front of home.
3393 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3394 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3395 final int topStackNdx = stacks.size() - 1;
3396 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3397 final ActivityStack stack = stacks.get(stackNdx);
3398 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3399 }
Craig Mautner580ea812013-04-25 12:58:38 -07003400 }
Jorim Jaggife762342016-10-13 14:33:27 +02003401 } finally {
3402 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003403 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003404 }
3405
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003406 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3407 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3408 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3409 final int topStackNdx = stacks.size() - 1;
3410 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3411 final ActivityStack stack = stacks.get(stackNdx);
3412 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3413 }
3414 }
3415 }
3416
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003417 void invalidateTaskLayers() {
3418 mTaskLayersChanged = true;
3419 }
3420
3421 void rankTaskLayersIfNeeded() {
3422 if (!mTaskLayersChanged) {
3423 return;
3424 }
3425 mTaskLayersChanged = false;
3426 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3427 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3428 int baseLayer = 0;
3429 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3430 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3431 }
3432 }
3433 }
3434
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003435 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3436 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3437 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3438 final int topStackNdx = stacks.size() - 1;
3439 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3440 final ActivityStack stack = stacks.get(stackNdx);
3441 stack.clearOtherAppTimeTrackers(except);
3442 }
3443 }
3444 }
3445
Craig Mautner8d341ef2013-03-26 09:03:27 -07003446 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003447 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3448 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003449 final int numStacks = stacks.size();
3450 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3451 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003452 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003453 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003454 }
3455 }
3456
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003457 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3458 // Examine all activities currently running in the process.
3459 TaskRecord firstTask = null;
3460 // Tasks is non-null only if two or more tasks are found.
3461 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003462 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3463 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003464 ActivityRecord r = app.activities.get(i);
3465 // First, if we find an activity that is in the process of being destroyed,
3466 // then we just aren't going to do anything for now; we want things to settle
3467 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003468 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003469 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003470 return;
3471 }
3472 // Don't consider any activies that are currently not in a state where they
3473 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003474 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3475 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003476 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003477 continue;
3478 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003479
3480 final TaskRecord task = r.getTask();
3481 if (task != null) {
3482 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003483 + " from " + r);
3484 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003485 firstTask = task;
3486 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003487 if (tasks == null) {
3488 tasks = new ArraySet<>();
3489 tasks.add(firstTask);
3490 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003491 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003492 }
3493 }
3494 }
3495 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003496 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003497 return;
3498 }
3499 // If we have activities in multiple tasks that are in a position to be destroyed,
3500 // let's iterate through the tasks and release the oldest one.
3501 final int numDisplays = mActivityDisplays.size();
3502 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3503 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3504 // Step through all stacks starting from behind, to hit the oldest things first.
3505 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3506 final ActivityStack stack = stacks.get(stackNdx);
3507 // Try to release activities in this stack; if we manage to, we are done.
3508 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3509 return;
3510 }
3511 }
3512 }
3513 }
3514
Amith Yamasani37a40c22015-06-17 13:25:42 -07003515 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003516 final int focusStackId = mFocusedStack.getStackId();
3517 // We dismiss the docked stack whenever we switch users.
3518 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003519 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3520 // also cause all tasks to be moved to the fullscreen stack at a position that is
3521 // appropriate.
3522 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003523
3524 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003525 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003526 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003527
Craig Mautner858d8a62013-04-23 17:08:34 -07003528 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003529 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3530 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003531 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003532 final ActivityStack stack = stacks.get(stackNdx);
3533 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003534 TaskRecord task = stack.topTask();
3535 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003536 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003537 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003538 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003539 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003540
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003541 ActivityStack stack = getStack(restoreStackId);
3542 if (stack == null) {
3543 stack = mHomeStack;
3544 }
3545 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003546 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003547 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003548 } else {
3549 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003550 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003551 }
Craig Mautner93529a42013-10-04 15:03:13 -07003552 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003553 }
3554
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003555 /** Checks whether the userid is a profile of the current user. */
3556 boolean isCurrentProfileLocked(int userId) {
3557 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003558 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003559 }
3560
Bryce Leeb7c9b802017-05-02 14:20:24 -07003561 /**
3562 * Returns whether a stopping activity is present that should be stopped after visible, rather
3563 * than idle.
3564 * @return {@code true} if such activity is present. {@code false} otherwise.
3565 */
3566 boolean isStoppingNoHistoryActivity() {
3567 // Activities that are marked as nohistory should be stopped immediately after the resumed
3568 // activity has become visible.
3569 for (ActivityRecord record : mStoppingActivities) {
3570 if (record.isNoHistory()) {
3571 return true;
3572 }
3573 }
3574
3575 return false;
3576 }
3577
Winson Chung4dabf232017-01-25 13:25:22 -08003578 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3579 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003580 ArrayList<ActivityRecord> stops = null;
3581
3582 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003583 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3584 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003585 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003586 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003587 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3588 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003589 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003590 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003591 if (s.finishing) {
3592 // If this activity is finishing, it is sitting on top of
3593 // everyone else but we now know it is no longer needed...
3594 // so get rid of it. Otherwise, we need to go through the
3595 // normal flow and hide it once we determine that it is
3596 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003597 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003598 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003599 }
3600 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003601 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003602 if (!processPausingActivities && s.state == PAUSING) {
3603 // Defer processing pausing activities in this iteration and reschedule
3604 // a delayed idle to reprocess it again
3605 removeTimeoutsForActivityLocked(idleActivity);
3606 scheduleIdleTimeoutLocked(idleActivity);
3607 continue;
3608 }
3609
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003610 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003611 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003612 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003613 }
3614 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003615 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003616 }
3617 }
3618
3619 return stops;
3620 }
3621
Craig Mautnercf910b02013-04-23 11:23:27 -07003622 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003623 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3624 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3625 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3626 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003627 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003628 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003629 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003630 if (r == null) Slog.e(TAG,
3631 "validateTop...: null top activity, stack=" + stack);
3632 else {
3633 final ActivityRecord pausing = stack.mPausingActivity;
3634 if (pausing != null && pausing == r) Slog.e(TAG,
3635 "validateTop...: top stack has pausing activity r=" + r
3636 + " state=" + state);
3637 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3638 "validateTop...: activity in front not resumed r=" + r
3639 + " state=" + state);
3640 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003641 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003642 final ActivityRecord resumed = stack.mResumedActivity;
3643 if (resumed != null && resumed == r) Slog.e(TAG,
3644 "validateTop...: back stack has resumed activity r=" + r
3645 + " state=" + state);
3646 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3647 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003648 }
3649 }
3650 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003651 }
3652
Craig Mautnere0570202015-05-13 13:06:11 -07003653 private String lockTaskModeToString() {
3654 switch (mLockTaskModeState) {
3655 case LOCK_TASK_MODE_LOCKED:
3656 return "LOCKED";
3657 case LOCK_TASK_MODE_PINNED:
3658 return "PINNED";
3659 case LOCK_TASK_MODE_NONE:
3660 return "NONE";
3661 default: return "unknown=" + mLockTaskModeState;
3662 }
3663 }
3664
Craig Mautner27084302013-03-25 08:05:25 -07003665 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003666 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003667 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003668 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003669 pw.print(prefix);
3670 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003671 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003672 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003673 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003674 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3675 if (packages.size() > 0) {
3676 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3677 for (int i = 0; i < packages.size(); ++i) {
3678 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3679 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3680 }
3681 }
Bryce Lee4a194382017-04-04 14:32:48 -07003682 if (!mWaitingForActivityVisible.isEmpty()) {
3683 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3684 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3685 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3686 }
3687 }
3688
Jorim Jaggi8d786932016-10-26 19:08:36 -07003689 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3690 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003691 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003692
Winson43d1f262016-06-14 16:05:55 -07003693 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003694 * Dump all connected displays' configurations.
3695 * @param prefix Prefix to apply to each line of the dump.
3696 */
3697 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3698 pw.print(prefix); pw.println("Display override configurations:");
3699 final int displayCount = mActivityDisplays.size();
3700 for (int i = 0; i < displayCount; i++) {
3701 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3702 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3703 pw.println(activityDisplay.getOverrideConfiguration());
3704 }
3705 }
3706
3707 /**
Winson43d1f262016-06-14 16:05:55 -07003708 * Dumps the activities matching the given {@param name} in the either the focused stack
3709 * or all visible stacks if {@param dumpVisibleStacks} is true.
3710 */
Winson Chung6998bc42017-02-28 17:07:05 -08003711 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3712 boolean dumpFocusedStackOnly) {
3713 if (dumpFocusedStackOnly) {
3714 return mFocusedStack.getDumpActivitiesLocked(name);
3715 } else {
Winson43d1f262016-06-14 16:05:55 -07003716 ArrayList<ActivityRecord> activities = new ArrayList<>();
3717 int numDisplays = mActivityDisplays.size();
3718 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3719 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3720 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3721 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003722 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003723 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003724 activities.addAll(stack.getDumpActivitiesLocked(name));
3725 }
3726 }
3727 }
3728 return activities;
Winson43d1f262016-06-14 16:05:55 -07003729 }
Craig Mautner20e72272013-04-01 13:45:53 -07003730 }
3731
Dianne Hackborn390517b2013-05-30 15:03:32 -07003732 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3733 boolean needSep, String prefix) {
3734 if (activity != null) {
3735 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3736 if (needSep) {
3737 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003738 }
3739 pw.print(prefix);
3740 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003741 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003742 }
3743 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003744 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003745 }
3746
Craig Mautner8d341ef2013-03-26 09:03:27 -07003747 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3748 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003749 boolean printed = false;
3750 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003751 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3752 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003753 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003754 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003755 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003757 final ActivityStack stack = stacks.get(stackNdx);
3758 StringBuilder stackHeader = new StringBuilder(128);
3759 stackHeader.append(" Stack #");
3760 stackHeader.append(stack.mStackId);
3761 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003762 stackHeader.append("\n");
3763 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3764 stackHeader.append("\n");
3765 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003766
3767 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3768 dumpClient, dumpPackage, needSep, stackHeader.toString());
3769 printed |= printedStackHeader;
3770 if (!printedStackHeader) {
3771 // Ensure we always dump the stack header even if there are no activities
3772 pw.println();
3773 pw.println(stackHeader);
3774 }
3775
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3777 !dumpAll, false, dumpPackage, true,
3778 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003779
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 needSep = printed;
3781 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3782 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003783 if (pr) {
3784 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003786 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3788 " mResumedActivity: ");
3789 if (pr) {
3790 printed = true;
3791 needSep = false;
3792 }
3793 if (dumpAll) {
3794 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3795 " mLastPausedActivity: ");
3796 if (pr) {
3797 printed = true;
3798 needSep = true;
3799 }
3800 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3801 needSep, " mLastNoHistoryActivity: ");
3802 }
3803 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003804 }
3805 }
3806
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003807 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3808 false, dumpPackage, true, " Activities waiting to finish:", null);
3809 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3810 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003811 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3812 false, !dumpAll, false, dumpPackage, true,
3813 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003814 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3815 false, dumpPackage, true, " Activities waiting to sleep:", null);
3816 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3817 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003818
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003819 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003820 }
3821
Dianne Hackborn390517b2013-05-30 15:03:32 -07003822 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003823 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003824 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003825 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003826 String innerPrefix = null;
3827 String[] args = null;
3828 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003829 for (int i=list.size()-1; i>=0; i--) {
3830 final ActivityRecord r = list.get(i);
3831 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3832 continue;
3833 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003834 if (innerPrefix == null) {
3835 innerPrefix = prefix + " ";
3836 args = new String[0];
3837 }
3838 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003839 final boolean full = !brief && (complete || !r.isInHistory());
3840 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003841 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003842 needNL = false;
3843 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003844 if (header1 != null) {
3845 pw.println(header1);
3846 header1 = null;
3847 }
3848 if (header2 != null) {
3849 pw.println(header2);
3850 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003851 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003852 if (lastTask != r.getTask()) {
3853 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003854 pw.print(prefix);
3855 pw.print(full ? "* " : " ");
3856 pw.println(lastTask);
3857 if (full) {
3858 lastTask.dump(pw, prefix + " ");
3859 } else if (complete) {
3860 // Complete + brief == give a summary. Isn't that obvious?!?
3861 if (lastTask.intent != null) {
3862 pw.print(prefix); pw.print(" ");
3863 pw.println(lastTask.intent.toInsecureStringWithClip());
3864 }
3865 }
3866 }
3867 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3868 pw.print(" #"); pw.print(i); pw.print(": ");
3869 pw.println(r);
3870 if (full) {
3871 r.dump(pw, innerPrefix);
3872 } else if (complete) {
3873 // Complete + brief == give a summary. Isn't that obvious?!?
3874 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3875 if (r.app != null) {
3876 pw.print(innerPrefix); pw.println(r.app);
3877 }
3878 }
3879 if (client && r.app != null && r.app.thread != null) {
3880 // flush anything that is already in the PrintWriter since the thread is going
3881 // to write to the file descriptor directly
3882 pw.flush();
3883 try {
3884 TransferPipe tp = new TransferPipe();
3885 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003886 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003887 // Short timeout, since blocking here can
3888 // deadlock with the application.
3889 tp.go(fd, 2000);
3890 } finally {
3891 tp.kill();
3892 }
3893 } catch (IOException e) {
3894 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3895 } catch (RemoteException e) {
3896 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3897 }
3898 needNL = true;
3899 }
3900 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003901 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003902 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003903
Craig Mautnerf3333272013-04-22 10:55:53 -07003904 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003905 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3906 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003907 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3908 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003909 }
3910
3911 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003912 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003913 }
3914
3915 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003916 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3917 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003918 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3919 }
3920
Craig Mautner05d29032013-05-03 13:40:13 -07003921 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003922 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3923 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3924 }
Craig Mautner05d29032013-05-03 13:40:13 -07003925 }
3926
Craig Mautner0eea92c2013-05-16 13:35:39 -07003927 void removeSleepTimeouts() {
3928 mSleepTimeout = false;
3929 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3930 }
3931
3932 final void scheduleSleepTimeout() {
3933 removeSleepTimeouts();
3934 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3935 }
3936
Craig Mautner4a1cb222013-12-04 16:14:06 -08003937 @Override
3938 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003939 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003940 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3941 }
3942
3943 @Override
3944 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003945 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3947 }
3948
3949 @Override
3950 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003951 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003952 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3953 }
3954
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003955 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003956 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003957 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003958 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003959 }
3960
Andrii Kulianca007a62016-11-22 16:33:28 -08003961 /** Check if display with specified id is added to the list. */
3962 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003963 return getActivityDisplayOrCreateLocked(displayId) != null;
3964 }
3965
3966 /**
3967 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3968 * corresponding record in display manager.
3969 */
3970 private ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
3971 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3972 if (activityDisplay != null) {
3973 return activityDisplay;
3974 }
3975 if (mDisplayManager == null) {
3976 // The system isn't fully initialized yet.
3977 return null;
3978 }
3979 final Display display = mDisplayManager.getDisplay(displayId);
3980 if (display == null) {
3981 // The display is not registered in DisplayManager.
3982 return null;
3983 }
3984 // The display hasn't been added to ActivityManager yet, create a new record now.
3985 activityDisplay = new ActivityDisplay(displayId);
3986 if (activityDisplay.mDisplay == null) {
3987 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3988 return null;
3989 }
3990 mActivityDisplays.put(displayId, activityDisplay);
3991 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
3992 mWindowManager.onDisplayAdded(displayId);
3993 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08003994 }
3995
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003996 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003997 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003998 mService.mContext.getResources().getDimensionPixelSize(
3999 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004000 }
4001
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004002 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004003 if (displayId == DEFAULT_DISPLAY) {
4004 throw new IllegalArgumentException("Can't remove the primary display.");
4005 }
4006
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004008 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4009 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08004010 final boolean destroyContentOnRemoval
4011 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08004012 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
4013 while (!stacks.isEmpty()) {
4014 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08004015 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004016 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
4017 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004018 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08004019 } else {
4020 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4021 // a valid launch stack for all activities. This way the task history from
4022 // external display will be preserved on primary after move.
4023 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004024 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004025 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004026 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07004027 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004028 }
4029 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 }
4031
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004032 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004034 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4035 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004036 // TODO: Update the bounds.
4037 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004038 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004039 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004040 }
4041
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004042 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004043 final int displayId = stack.mDisplayId;
4044 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004046 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004047 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004049 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004050 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Andrii Kulian62e6f252017-05-30 22:46:53 -07004051 // A stack might be not attached to a display.
Winson529c8e42016-05-17 11:08:40 -07004052 info.position = display != null
4053 ? display.mStacks.indexOf(stack)
4054 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004055
4056 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4057 final int numTasks = tasks.size();
4058 int[] taskIds = new int[numTasks];
4059 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004060 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004061 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 for (int i = 0; i < numTasks; ++i) {
4063 final TaskRecord task = tasks.get(i);
4064 taskIds[i] = task.taskId;
4065 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4066 : task.realActivity != null ? task.realActivity.flattenToString()
4067 : task.getTopActivity() != null ? task.getTopActivity().packageName
4068 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004069 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004070 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004071 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004072 }
4073 info.taskIds = taskIds;
4074 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004075 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004076 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004077
4078 final ActivityRecord top = stack.topRunningActivityLocked();
4079 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080 return info;
4081 }
4082
4083 StackInfo getStackInfoLocked(int stackId) {
4084 ActivityStack stack = getStack(stackId);
4085 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004086 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004087 }
4088 return null;
4089 }
4090
4091 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004092 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004093 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4094 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004095 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004096 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 }
4098 }
4099 return list;
4100 }
4101
Craig Mautner15df08a2015-04-01 12:17:18 -07004102 TaskRecord getLockedTaskLocked() {
4103 final int top = mLockTaskModeTasks.size() - 1;
4104 if (top >= 0) {
4105 return mLockTaskModeTasks.get(top);
4106 }
4107 return null;
4108 }
4109
4110 boolean isLockedTask(TaskRecord task) {
4111 return mLockTaskModeTasks.contains(task);
4112 }
4113
4114 boolean isLastLockedTask(TaskRecord task) {
4115 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4116 }
4117
4118 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004119 if (!mLockTaskModeTasks.remove(task)) {
4120 return;
4121 }
4122 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4123 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004124 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004125 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4126 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004127 final Message lockTaskMsg = Message.obtain();
4128 lockTaskMsg.arg1 = task.userId;
4129 lockTaskMsg.what = LOCK_TASK_END_MSG;
4130 mHandler.sendMessage(lockTaskMsg);
4131 }
4132 }
4133
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004134 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4135 int preferredDisplayId, int actualStackId) {
4136 handleNonResizableTaskIfNeeded(task, preferredStackId, preferredDisplayId, actualStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07004137 false /* forceNonResizable */);
4138 }
4139
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004140 private void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4141 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4142 final boolean isSecondaryDisplayPreferred =
4143 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY)
4144 || StackId.isDynamicStack(preferredStackId);
4145 if (((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
4146 && !isSecondaryDisplayPreferred) || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004147 return;
4148 }
4149
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004150 // Handle incorrect launch/move to secondary display if needed.
4151 final boolean launchOnSecondaryDisplayFailed;
4152 if (isSecondaryDisplayPreferred) {
4153 final int actualDisplayId = task.getStack().mDisplayId;
4154 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4155 // The task landed on an inappropriate display somehow, move it to the default
4156 // display.
4157 // TODO(multi-display): Find proper stack for the task on the default display.
4158 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4159 true /* toTop */);
4160 launchOnSecondaryDisplayFailed = true;
4161 } else {
4162 // The task might have landed on a display different from requested.
4163 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4164 || (preferredDisplayId != INVALID_DISPLAY
4165 && preferredDisplayId != actualDisplayId);
4166 }
4167 } else {
4168 // The task wasn't requested to be on a secondary display.
4169 launchOnSecondaryDisplayFailed = false;
4170 }
4171
Jorim Jaggicd13d332016-04-27 15:40:20 -07004172 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004173 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4174 if (launchOnSecondaryDisplayFailed) {
4175 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4176 // display with config different from global config.
4177 mService.mTaskChangeNotificationController
4178 .notifyActivityLaunchOnSecondaryDisplayFailed();
4179 } else {
4180 // Display a warning toast that we tried to put a non-dockable task in the docked
4181 // stack.
4182 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4183 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004184
Andrii Kulianc27916642016-04-12 17:59:27 -07004185 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4186 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004187 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004188 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004189 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004190 final String packageName = topActivity.appInfo.packageName;
4191 final int reason = isSecondaryDisplayPreferred
4192 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4193 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004194 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004195 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004196 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004197 }
4198
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004199 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004200 if (mLockTaskNotify != null) {
4201 mLockTaskNotify.showToast(mLockTaskModeState);
4202 }
Jason Monka8f569c2014-07-07 12:15:21 -04004203 }
4204
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004205 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4206 if (mLockTaskModeTasks.contains(task)) {
4207 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4208 }
4209 }
4210
Craig Mautner432f64e2015-05-20 14:59:57 -07004211 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4212 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004213 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004214 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004215 final TaskRecord lockedTask = getLockedTaskLocked();
4216 if (lockedTask != null) {
4217 removeLockedTaskLocked(lockedTask);
4218 if (!mLockTaskModeTasks.isEmpty()) {
4219 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004220 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4221 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004222 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004223 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004224 return;
4225 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004226 }
Craig Mautnere0570202015-05-13 13:06:11 -07004227 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4228 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004229 return;
4230 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004231
4232 // Should have already been checked, but do it again.
4233 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004234 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4235 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004236 return;
4237 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004238 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004239 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004240 return;
4241 }
4242
4243 if (mLockTaskModeTasks.isEmpty()) {
4244 // First locktask.
4245 final Message lockTaskMsg = Message.obtain();
4246 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4247 lockTaskMsg.arg1 = task.userId;
4248 lockTaskMsg.what = LOCK_TASK_START_MSG;
4249 lockTaskMsg.arg2 = lockTaskModeState;
4250 mHandler.sendMessage(lockTaskMsg);
4251 }
4252 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004253 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4254 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004255 mLockTaskModeTasks.remove(task);
4256 mLockTaskModeTasks.add(task);
4257
4258 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004259 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004260 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004261
4262 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004263 findTaskToMoveToFrontLocked(task, 0, null, reason,
4264 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004265 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004266 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004267 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004268 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
4269 task.getStackId(), true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004270 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004271 }
4272
4273 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004274 return isLockTaskModeViolation(task, false);
4275 }
4276
4277 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4278 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004279 return false;
4280 }
4281 final int lockTaskAuth = task.mLockTaskAuth;
4282 switch (lockTaskAuth) {
4283 case LOCK_TASK_AUTH_DONT_LOCK:
4284 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004285 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004286 case LOCK_TASK_AUTH_LAUNCHABLE:
4287 case LOCK_TASK_AUTH_WHITELISTED:
4288 return false;
4289 case LOCK_TASK_AUTH_PINNABLE:
4290 // Pinnable tasks can't be launched on top of locktask tasks.
4291 return !mLockTaskModeTasks.isEmpty();
4292 default:
4293 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4294 return true;
4295 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004296 }
4297
Craig Mautner15df08a2015-04-01 12:17:18 -07004298 void onLockTaskPackagesUpdatedLocked() {
4299 boolean didSomething = false;
4300 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4301 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004302 final boolean wasWhitelisted =
4303 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4304 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004305 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004306 final boolean isWhitelisted =
4307 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4308 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4309 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004310 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004311 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4312 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004313 removeLockedTaskLocked(lockedTask);
4314 lockedTask.performClearTaskLocked();
4315 didSomething = true;
4316 }
4317 }
4318 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4319 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4320 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4321 final ActivityStack stack = stacks.get(stackNdx);
4322 stack.onLockTaskPackagesUpdatedLocked();
4323 }
4324 }
Craig Mautnere0570202015-05-13 13:06:11 -07004325 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004326 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004327 if (mLockTaskModeTasks.isEmpty() && task != null
4328 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4329 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004330 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4331 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4332 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4333 false);
4334 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004335 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004336 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004337 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004338 }
4339 }
4340
Benjamin Franz43261142015-02-11 15:59:44 +00004341 int getLockTaskModeState() {
4342 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004343 }
4344
Jorim Jaggife89d122015-12-22 16:28:44 +01004345 void activityRelaunchedLocked(IBinder token) {
4346 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004347 if (mService.isSleepingOrShuttingDownLocked()) {
4348 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4349 if (r != null) {
4350 r.setSleeping(true, true);
4351 }
4352 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004353 }
4354
4355 void activityRelaunchingLocked(ActivityRecord r) {
4356 mWindowManager.notifyAppRelaunching(r.appToken);
4357 }
4358
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004359 void logStackState() {
4360 mActivityMetricsLogger.logWindowState();
4361 }
4362
Winson Chung5af42fc2017-03-24 17:11:33 -07004363 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004364 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4365 // end, then ensure that we defer all in between multi-window mode changes
4366 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4367 return;
4368 }
4369
Wale Ogunwale22e25262016-02-01 10:32:02 -08004370 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 mMultiWindowModeChangedActivities.add(r);
4374 }
4375 }
4376
4377 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4378 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4379 }
4380 }
4381
Winson Chung5af42fc2017-03-24 17:11:33 -07004382 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004383 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004384 if (prevStack == null || prevStack == stack
4385 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4386 return;
4387 }
4388
Winson Chung5af42fc2017-03-24 17:11:33 -07004389 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4390 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004391
Winson Chung5af42fc2017-03-24 17:11:33 -07004392 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4393 boolean immediate) {
4394
4395 if (immediate) {
4396 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4397 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4398 final ActivityRecord r = task.mActivities.get(i);
4399 if (r.app != null && r.app.thread != null) {
4400 r.updatePictureInPictureMode(targetStackBounds);
4401 }
4402 }
4403 } else {
4404 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4405 final ActivityRecord r = task.mActivities.get(i);
4406 if (r.app != null && r.app.thread != null) {
4407 mPipModeChangedActivities.add(r);
4408 }
4409 }
4410 mPipModeChangedTargetStackBounds = targetStackBounds;
4411
4412 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4413 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4414 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004415 }
4416 }
4417
Tony Mak853304c2016-04-18 15:17:41 +01004418 void setDockedStackMinimized(boolean minimized) {
4419 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004420 }
4421
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004422 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004423
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004424 public ActivityStackSupervisorHandler(Looper looper) {
4425 super(looper);
4426 }
4427
Winson Chung4dabf232017-01-25 13:25:22 -08004428 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004429 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004430 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4431 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004432 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004433 }
4434
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004435 @Override
4436 public void handleMessage(Message msg) {
4437 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004438 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4439 synchronized (mService) {
4440 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4441 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004442 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004443 }
4444 }
4445 } break;
4446 case REPORT_PIP_MODE_CHANGED_MSG: {
4447 synchronized (mService) {
4448 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4449 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004450 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004451 }
4452 }
4453 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004454 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004455 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4456 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004457 if (mService.mDidDexOpt) {
4458 mService.mDidDexOpt = false;
4459 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4460 nmsg.obj = msg.obj;
4461 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4462 return;
4463 }
4464 // We don't at this point know if the activity is fullscreen,
4465 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004466 activityIdleInternal((ActivityRecord) msg.obj,
4467 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004468 } break;
4469 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004470 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004471 activityIdleInternal((ActivityRecord) msg.obj,
4472 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004473 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004474 case RESUME_TOP_ACTIVITY_MSG: {
4475 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004476 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004477 }
4478 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004479 case SLEEP_TIMEOUT_MSG: {
4480 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004481 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004482 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4483 mSleepTimeout = true;
4484 checkReadyForSleepLocked();
4485 }
4486 }
4487 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004488 case LAUNCH_TIMEOUT_MSG: {
4489 if (mService.mDidDexOpt) {
4490 mService.mDidDexOpt = false;
4491 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4492 return;
4493 }
4494 synchronized (mService) {
4495 if (mLaunchingActivity.isHeld()) {
4496 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4497 if (VALIDATE_WAKE_LOCK_CALLER
4498 && Binder.getCallingUid() != Process.myUid()) {
4499 throw new IllegalStateException("Calling must be system uid");
4500 }
4501 mLaunchingActivity.release();
4502 }
4503 }
4504 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004505 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004506 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004507 } break;
4508 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004509 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004510 } break;
4511 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004512 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004513 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004514 case CONTAINER_CALLBACK_VISIBILITY: {
4515 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004516 final IActivityContainerCallback callback = container.mCallback;
4517 if (callback != null) {
4518 try {
4519 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4520 } catch (RemoteException e) {
4521 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004522 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004523 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004524 case LOCK_TASK_START_MSG: {
4525 // When lock task starts, we disable the status bars.
4526 try {
Jason Monk62515be2014-05-21 16:06:19 -04004527 if (mLockTaskNotify == null) {
4528 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004529 }
Jason Monk62515be2014-05-21 16:06:19 -04004530 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004531 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004532 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004533 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004534 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004535 flags = StatusBarManager.DISABLE_MASK
4536 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004537 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004538 flags = StatusBarManager.DISABLE_MASK
4539 & (~StatusBarManager.DISABLE_BACK)
4540 & (~StatusBarManager.DISABLE_HOME)
4541 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004542 }
4543 getStatusBarService().disable(flags, mToken,
4544 mService.mContext.getPackageName());
4545 }
4546 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004547 if (getDevicePolicyManager() != null) {
4548 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004549 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004550 }
justinzhang5286d3f2014-05-12 17:06:01 -04004551 } catch (RemoteException ex) {
4552 throw new RuntimeException(ex);
4553 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004554 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004555 case LOCK_TASK_END_MSG: {
4556 // When lock task ends, we enable the status bars.
4557 try {
Jason Monk62515be2014-05-21 16:06:19 -04004558 if (getStatusBarService() != null) {
4559 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4560 mService.mContext.getPackageName());
4561 }
4562 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004563 if (getDevicePolicyManager() != null) {
4564 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4565 msg.arg1);
4566 }
Jason Monk62515be2014-05-21 16:06:19 -04004567 if (mLockTaskNotify == null) {
4568 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4569 }
4570 mLockTaskNotify.show(false);
4571 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004572 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004573 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004574 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004575 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004576 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004577 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004578 new LockPatternUtils(mService.mContext)
4579 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004580 }
4581 } catch (SettingNotFoundException e) {
4582 // No setting, don't lock.
4583 }
justinzhang5286d3f2014-05-12 17:06:01 -04004584 } catch (RemoteException ex) {
4585 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004586 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004587 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004588 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004589 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004590 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4591 if (mLockTaskNotify == null) {
4592 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4593 }
4594 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4595 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004596 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4597 final ActivityContainer container = (ActivityContainer) msg.obj;
4598 final IActivityContainerCallback callback = container.mCallback;
4599 if (callback != null) {
4600 try {
4601 callback.onAllActivitiesComplete(container.asBinder());
4602 } catch (RemoteException e) {
4603 }
4604 }
4605 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004606 case LAUNCH_TASK_BEHIND_COMPLETE: {
4607 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004608 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004609 if (r != null) {
4610 handleLaunchTaskBehindCompleteLocked(r);
4611 }
4612 }
4613 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004614
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004615 }
4616 }
4617 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004618
Ying Wangb081a592014-04-22 15:20:16 -07004619 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004620 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4621 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004622 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004623 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004624 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004625 ActivityRecord mParentActivity = null;
4626 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004627
Craig Mautnere3a00d72014-04-16 08:31:19 -07004628 boolean mVisible = true;
4629
Craig Mautner4a1cb222013-12-04 16:14:06 -08004630 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004631 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004632
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004633 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4634 final static int CONTAINER_STATE_NO_SURFACE = 1;
4635 final static int CONTAINER_STATE_FINISHING = 2;
4636 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4637
Wale Ogunwale1666e312016-12-16 11:27:18 -08004638 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004639 synchronized (mService) {
4640 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004641 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004642 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004643
4644 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004645 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004646 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004647 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004648
Bryce Leeaf691c02017-03-20 14:20:22 -07004649 protected void createStack(int stackId, boolean onTop) {
4650 switch (stackId) {
4651 case PINNED_STACK_ID:
4652 new PinnedActivityStack(this, mRecentTasks, onTop);
4653 break;
4654 default:
4655 new ActivityStack(this, mRecentTasks, onTop);
4656 break;
4657 }
4658 }
4659
Andrii Kulian839def92016-11-02 10:58:58 -07004660 /**
4661 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004662 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004663 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004664 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004665 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004666 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004667 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004668 if (mActivityDisplay != null) {
4669 throw new IllegalStateException("ActivityContainer is already attached, " +
4670 "displayId=" + mActivityDisplay.mDisplayId);
4671 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004672 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004673 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004674 }
4675
4676 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004677 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004678 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004679 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004680 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004681 return;
4682 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004683 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004684 }
4685 }
4686
4687 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004688 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004689 synchronized (mService) {
4690 if (mActivityDisplay != null) {
4691 return mActivityDisplay.mDisplayId;
4692 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004693 }
4694 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004695 }
4696
Jeff Brownca9bc702014-02-11 14:32:56 -08004697 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004698 public int getStackId() {
4699 synchronized (mService) {
4700 return mStackId;
4701 }
4702 }
4703
4704 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004705 public boolean injectEvent(InputEvent event) {
4706 final long origId = Binder.clearCallingIdentity();
4707 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004708 synchronized (mService) {
4709 if (mActivityDisplay != null) {
4710 return mInputManagerInternal.injectInputEvent(event,
4711 mActivityDisplay.mDisplayId,
4712 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4713 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004714 }
4715 return false;
4716 } finally {
4717 Binder.restoreCallingIdentity(origId);
4718 }
4719 }
4720
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004721 @Override
4722 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004723 synchronized (mService) {
4724 if (mContainerState == CONTAINER_STATE_FINISHING) {
4725 return;
4726 }
4727 mContainerState = CONTAINER_STATE_FINISHING;
4728
Craig Mautnerd163e752014-06-13 17:18:47 -07004729 long origId = Binder.clearCallingIdentity();
4730 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004731 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004732 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004733 } finally {
4734 Binder.restoreCallingIdentity(origId);
4735 }
4736 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004737 }
4738
Andrii Kulian03c403d2016-11-11 11:14:12 -08004739 /**
4740 * Remove the stack completely. Must be called only when there are no tasks left in it,
4741 * as this method does not finish running activities.
4742 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004743 void removeLocked() {
4744 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004745 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004746 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004747 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004748 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004749 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004750 }
4751
Andrii Kulian839def92016-11-02 10:58:58 -07004752 /**
4753 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4754 * completely or re-parented.
4755 */
4756 private void removeFromDisplayLocked() {
4757 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4758 + " current displayId=" + mActivityDisplay.mDisplayId);
4759
Wale Ogunwale1666e312016-12-16 11:27:18 -08004760 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004761 mActivityDisplay = null;
4762 }
4763
4764 /**
4765 * Move the stack to specified display.
4766 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004767 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004768 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004769 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004770 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4771 + mActivityDisplay + " to display=" + activityDisplay
4772 + " Callers=" + Debug.getCallers(2));
4773
4774 removeFromDisplayLocked();
4775
4776 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004777 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004778 }
4779
Craig Mautner4a1cb222013-12-04 16:14:06 -08004780 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004781 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004782 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004783 }
4784
4785 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004786 public final int startActivityIntentSender(IIntentSender intentSender)
4787 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004788 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4789
4790 if (!(intentSender instanceof PendingIntentRecord)) {
4791 throw new IllegalArgumentException("Bad PendingIntent object");
4792 }
4793
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004794 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004795 Binder.getCallingUid(), mCurrentUser, false,
4796 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004797
4798 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4799 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4800 pendingIntent.key.requestResolvedType);
4801
Dianne Hackborn98305522017-05-05 17:53:53 -07004802 return pendingIntent.sendInner(0, null, null, null, null, null, null, null, 0,
Craig Mautnerb9168362015-02-26 20:40:19 -08004803 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4804 }
4805
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004806 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004807 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004808 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004809 throw new SecurityException(
4810 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4811 }
4812 }
4813
Craig Mautnerdf88d732014-01-27 09:21:32 -08004814 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004815 public IBinder asBinder() {
4816 return this;
4817 }
4818
Craig Mautner4504de52013-12-20 09:06:56 -08004819 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004820 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004821 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004822 }
4823
Craig Mautner4a1cb222013-12-04 16:14:06 -08004824 ActivityStackSupervisor getOuter() {
4825 return ActivityStackSupervisor.this;
4826 }
4827
Craig Mautnerd163e752014-06-13 17:18:47 -07004828 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004829 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004830 }
4831
Craig Mautner6985bad2014-04-21 15:22:06 -07004832 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004833 void setVisible(boolean visible) {
4834 if (mVisible != visible) {
4835 mVisible = visible;
4836 if (mCallback != null) {
4837 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4838 0 /* unused */, this).sendToTarget();
4839 }
4840 }
4841 }
4842
Craig Mautner6985bad2014-04-21 15:22:06 -07004843 void setDrawn() {
4844 }
4845
Craig Mautner1b4bf852014-05-26 15:06:32 -07004846 // You can always start a new task on a regular ActivityStack.
4847 boolean isEligibleForNewTasks() {
4848 return true;
4849 }
4850
Craig Mautnerd163e752014-06-13 17:18:47 -07004851 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004852 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004853 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004854 }
4855
Craig Mautner34b73df2014-01-12 21:11:08 -08004856 @Override
4857 public String toString() {
4858 return mIdString + (mActivityDisplay == null ? "N" : "A");
4859 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004860 }
4861
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004862 private class VirtualActivityContainer extends ActivityContainer {
4863 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004864 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004865
4866 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004867 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4868 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004869 mParentActivity = parent;
4870 mCallback = callback;
4871 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004872 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004873 }
4874
4875 @Override
4876 public void setSurface(Surface surface, int width, int height, int density) {
4877 super.setSurface(surface, width, height, density);
4878
4879 synchronized (mService) {
4880 final long origId = Binder.clearCallingIdentity();
4881 try {
4882 setSurfaceLocked(surface, width, height, density);
4883 } finally {
4884 Binder.restoreCallingIdentity(origId);
4885 }
4886 }
4887 }
4888
4889 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4890 if (mContainerState == CONTAINER_STATE_FINISHING) {
4891 return;
4892 }
4893 VirtualActivityDisplay virtualActivityDisplay =
4894 (VirtualActivityDisplay) mActivityDisplay;
4895 if (virtualActivityDisplay == null) {
4896 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004897 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004898 mActivityDisplay = virtualActivityDisplay;
4899 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004900 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004901 }
4902
4903 if (mSurface != null) {
4904 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004905 }
4906
Craig Mautner6985bad2014-04-21 15:22:06 -07004907 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004908 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004909 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004910 } else {
4911 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004912 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004913 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004914 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004915 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004916 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004917
Craig Mautnerd163e752014-06-13 17:18:47 -07004918 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004919
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004920 if (DEBUG_STACK) Slog.d(TAG_STACK,
4921 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004922 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004923
Craig Mautner6985bad2014-04-21 15:22:06 -07004924 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004925 boolean isAttachedLocked() {
4926 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004927 }
4928
4929 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004930 void setDrawn() {
4931 synchronized (mService) {
4932 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004933 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004934 }
4935 }
4936
Craig Mautner1b4bf852014-05-26 15:06:32 -07004937 // Never start a new task on an ActivityView if it isn't explicitly specified.
4938 @Override
4939 boolean isEligibleForNewTasks() {
4940 return false;
4941 }
4942
Craig Mautnerd163e752014-06-13 17:18:47 -07004943 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004944 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004945 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4946 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4947 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4948 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4949 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004950 }
4951 }
4952
Craig Mautner4a1cb222013-12-04 16:14:06 -08004953 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4954 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004955 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004956 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004957 int mDisplayId;
4958 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004959
Craig Mautner4a1cb222013-12-04 16:14:06 -08004960 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4961 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004962 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004963
Jose Lima4b6c6692014-08-12 17:41:12 -07004964 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004965
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004966 /** Array of all UIDs that are present on the display. */
4967 private IntArray mDisplayAccessUIDs = new IntArray();
4968
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004969 ActivityDisplay() {
4970 }
Craig Mautner4504de52013-12-20 09:06:56 -08004971
Craig Mautner1a70a162014-09-13 12:09:31 -07004972 // After instantiation, check that mDisplay is not null before using this. The alternative
4973 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004974 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004975 final Display display = mDisplayManager.getDisplay(displayId);
4976 if (display == null) {
4977 return;
4978 }
4979 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004980 }
4981
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004982 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004983 mDisplay = display;
4984 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004985 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004986
Winson Chung7c3ede32017-03-14 14:18:34 -07004987 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004988 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004989 + " to displayId=" + mDisplayId + " position=" + position);
4990 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004991 }
4992
Wale Ogunwale1666e312016-12-16 11:27:18 -08004993 void detachStack(ActivityStack stack) {
4994 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004995 + " from displayId=" + mDisplayId);
4996 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004997 }
4998
Jose Lima4b6c6692014-08-12 17:41:12 -07004999 void setVisibleBehindActivity(ActivityRecord r) {
5000 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005001 }
5002
Jose Lima4b6c6692014-08-12 17:41:12 -07005003 boolean hasVisibleBehindActivity() {
5004 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005005 }
5006
Craig Mautner34b73df2014-01-12 21:11:08 -08005007 @Override
5008 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005009 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
5010 }
Andrii Kulian1779e612016-10-12 21:58:25 -07005011
5012 @Override
5013 protected int getChildCount() {
5014 return mStacks.size();
5015 }
5016
5017 @Override
5018 protected ConfigurationContainer getChildAt(int index) {
5019 return mStacks.get(index);
5020 }
5021
5022 @Override
5023 protected ConfigurationContainer getParent() {
5024 return ActivityStackSupervisor.this;
5025 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005026
5027 boolean isPrivate() {
5028 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
5029 }
5030
5031 boolean isUidPresent(int uid) {
5032 for (ActivityStack stack : mStacks) {
5033 if (stack.isUidPresent(uid)) {
5034 return true;
5035 }
5036 }
5037 return false;
5038 }
5039
5040 /** Update and get all UIDs that are present on the display and have access to it. */
5041 private IntArray getPresentUIDs() {
5042 mDisplayAccessUIDs.clear();
5043 for (ActivityStack stack : mStacks) {
5044 stack.getPresentUIDs(mDisplayAccessUIDs);
5045 }
5046 return mDisplayAccessUIDs;
5047 }
Andrii Kulian250d6532017-02-08 23:30:45 -08005048
5049 boolean shouldDestroyContentOnRemove() {
5050 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
5051 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005052 }
5053
5054 class VirtualActivityDisplay extends ActivityDisplay {
5055 VirtualDisplay mVirtualDisplay;
5056
Craig Mautner6985bad2014-04-21 15:22:06 -07005057 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005058 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Santos Cordonb0608632017-04-05 10:31:15 -07005059 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null /* projection */,
5060 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null /* surface */,
Michael Wrightc39d47a2014-07-08 18:07:36 -07005061 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Santos Cordonb0608632017-04-05 10:31:15 -07005062 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null /* callback */,
5063 null /* handler */, null /* uniqueId */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005064
5065 init(mVirtualDisplay.getDisplay());
5066
Bryce Leeaf3a4002017-03-20 10:37:12 -07005067 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005068 }
5069
5070 void setSurface(Surface surface) {
5071 if (mVirtualDisplay != null) {
5072 mVirtualDisplay.setSurface(surface);
5073 }
5074 }
5075
5076 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08005077 void detachStack(ActivityStack stack) {
5078 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005079 if (mVirtualDisplay != null) {
5080 mVirtualDisplay.release();
5081 mVirtualDisplay = null;
5082 }
5083 }
5084
5085 @Override
5086 public String toString() {
5087 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005088 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005089 }
Jose Lima58e66d62014-05-27 20:00:27 -07005090
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005091 ActivityStack findStackBehind(ActivityStack stack) {
5092 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02005093 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005094 if (display == null) {
5095 return null;
5096 }
5097 final ArrayList<ActivityStack> stacks = display.mStacks;
5098 for (int i = stacks.size() - 1; i >= 0; i--) {
5099 if (stacks.get(i) == stack && i > 0) {
5100 return stacks.get(i - 1);
5101 }
5102 }
5103 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5104 + " in=" + stacks);
5105 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005106
Jorim Jaggic69bd222016-03-15 14:38:37 +01005107 /**
5108 * Puts a task into resizing mode during the next app transition.
5109 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005110 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01005111 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005112 private void setResizingDuringAnimation(TaskRecord task) {
5113 mResizingTasksDuringAnimation.add(task.taskId);
5114 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01005115 }
5116
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005117 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
5118 final TaskRecord task;
5119 final int callingUid;
5120 final String callingPackage;
5121 final Intent intent;
5122 final int userId;
5123 final ActivityOptions activityOptions = (bOptions != null)
5124 ? new ActivityOptions(bOptions) : null;
5125 final int launchStackId = (activityOptions != null)
5126 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005127 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005128 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005129 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005130 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005131
Matthew Ng606dd802017-06-05 14:06:32 -07005132 mWindowManager.deferSurfaceLayout();
5133 try {
5134 if (launchStackId == DOCKED_STACK_ID) {
5135 mWindowManager.setDockedStackCreateState(
5136 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005137
Matthew Ng606dd802017-06-05 14:06:32 -07005138 // Defer updating the stack in which recents is until the app transition is done, to
5139 // not run into issues where we still need to draw the task in recents but the
5140 // docked stack is already created.
5141 deferUpdateBounds(RECENTS_STACK_ID);
5142 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005143 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005144
Matthew Ng606dd802017-06-05 14:06:32 -07005145 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
5146 launchStackId);
5147 if (task == null) {
5148 continueUpdateBounds(RECENTS_STACK_ID);
5149 mWindowManager.executeAppTransition();
5150 throw new IllegalArgumentException(
5151 "startActivityFromRecentsInner: Task " + taskId + " not found.");
5152 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005153
Matthew Ng606dd802017-06-05 14:06:32 -07005154 // Since we don't have an actual source record here, we assume that the currently
5155 // focused activity was the source.
5156 final ActivityStack focusedStack = getFocusedStack();
5157 final ActivityRecord sourceRecord =
5158 focusedStack != null ? focusedStack.topActivity() : null;
5159
5160 if (launchStackId != INVALID_STACK_ID) {
5161 if (task.getStackId() != launchStackId) {
5162 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
5163 DEFER_RESUME, "startActivityFromRecents");
5164 }
5165 }
5166
5167 // If the user must confirm credentials (e.g. when first launching a work app and the
5168 // Work Challenge is present) let startActivityInPackage handle the intercepting.
5169 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
5170 && task.getRootActivity() != null) {
5171 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
5172 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07005173 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07005174 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
5175 task.getTopActivity());
5176
5177 // If we are launching the task in the docked stack, put it into resizing mode so
5178 // the window renders full-screen with the background filling the void. Also only
5179 // call this at the end to make sure that tasks exists on the window manager side.
5180 if (launchStackId == DOCKED_STACK_ID) {
5181 setResizingDuringAnimation(task);
5182 }
5183
5184 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
5185 ActivityManager.START_TASK_TO_FRONT,
5186 sourceRecord != null
5187 ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
5188 sourceRecord, task.getStack());
5189 return ActivityManager.START_TASK_TO_FRONT;
5190 }
5191 callingUid = task.mCallingUid;
5192 callingPackage = task.mCallingPackage;
5193 intent = task.intent;
5194 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5195 userId = task.userId;
5196 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Wale Ogunwale692dcd62017-06-20 13:38:14 -07005197 null, null, 0, 0, bOptions, userId, null, task, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005198 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005199 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005200 }
Matthew Ng606dd802017-06-05 14:06:32 -07005201 return result;
5202 } finally {
5203 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005204 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005205 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005206
5207 /**
5208 * @return a list of activities which are the top ones in each visible stack. The first
5209 * entry will be the focused activity.
5210 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005211 List<IBinder> getTopVisibleActivities() {
5212 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5213 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005214 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005215 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5216 // Traverse all stacks on a display.
5217 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5218 final ActivityStack stack = display.mStacks.get(j);
5219 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005220 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005221 == ActivityStack.STACK_VISIBLE) {
5222 final ActivityRecord top = stack.topActivity();
5223 if (top != null) {
5224 if (stack == mFocusedStack) {
5225 topActivityTokens.add(0, top.appToken);
5226 } else {
5227 topActivityTokens.add(top.appToken);
5228 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005229 }
5230 }
5231 }
5232 }
5233 return topActivityTokens;
5234 }
Bryce Lee4a194382017-04-04 14:32:48 -07005235
5236 /**
5237 * Internal container to store a match qualifier alongside a WaitResult.
5238 */
5239 static class WaitInfo {
5240 private final ComponentName mTargetComponent;
5241 private final WaitResult mResult;
5242
5243 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5244 this.mTargetComponent = targetComponent;
5245 this.mResult = result;
5246 }
5247
Wale Ogunwale3270f172017-04-26 07:29:42 -07005248 public boolean matches(ComponentName targetComponent) {
5249 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07005250 }
5251
5252 public WaitResult getResult() {
5253 return mResult;
5254 }
5255
5256 public ComponentName getComponent() {
5257 return mTargetComponent;
5258 }
5259
5260 public void dump(PrintWriter pw, String prefix) {
5261 pw.println(prefix + "WaitInfo:");
5262 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5263 pw.println(prefix + " mResult=");
5264 mResult.dump(pw, prefix);
5265 }
5266 }
Craig Mautner27084302013-03-25 08:05:25 -07005267}