blob: e0f2a751604a95d069857405e61f0462fe6ebc78 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Andrii Kulian3a95edc2017-06-28 16:21:07 -070019import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Andrii Kuliane6ac20e2017-03-17 10:30:50 -070020import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020021import static android.Manifest.permission.START_ANY_ACTIVITY;
22import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
23import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
24import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
25import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020026import static android.app.ActivityManager.START_TASK_TO_FRONT;
27import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
30import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
32import static android.app.ActivityManager.StackId.HOME_STACK_ID;
33import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
34import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
35import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080036import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070037import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
38import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020039import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
40import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
41import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070042import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020043import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
44import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070045import static android.view.Display.FLAG_PRIVATE;
46import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080047import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070048import static android.view.Display.TYPE_VIRTUAL;
Jorim Jaggife762342016-10-13 14:33:27 +020049
Winson Chung47900652017-04-06 18:44:25 -070050import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020051import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
74import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
75import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
76import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
77import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
78import static com.android.server.am.ActivityManagerService.ANIMATE;
79import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
80import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
81import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020082import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
83import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
84import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
85import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
86import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
87import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
88import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
89import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
90import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
91import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
92import static com.android.server.am.ActivityStack.STACK_VISIBLE;
93import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
94import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
95import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
96import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
97import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080098import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
99import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
100import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +0200101import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800102import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200103
Svetoslav7008b512015-06-24 18:47:07 -0700104import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800105import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700106import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100107import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800110import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700111import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800112import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700114import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800115import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700116import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400118import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700119import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400120import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700122import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700124import android.content.Intent;
125import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700127import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.content.pm.PackageManager;
129import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100130import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800132import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800133import android.hardware.display.DisplayManager;
134import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800135import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800136import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800137import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800138import android.hardware.input.InputManager;
139import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700140import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100141import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700142import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700143import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700144import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700145import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700146import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700147import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700148import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700149import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700150import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400151import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700152import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700153import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700154import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700155import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100156import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700157import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700158import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400159import android.provider.Settings;
160import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700161import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700162import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700163import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700164import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800165import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700166import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700167import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800168import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800170import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800171import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800172import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800173
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800174import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800175import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700176import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400177import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700178import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400179import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800180import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700181import com.android.server.am.ActivityStack.ActivityState;
Winson Chung19953ca2017-04-11 11:19:23 -0700182import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700183import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700184
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185import java.io.FileDescriptor;
186import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700187import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800188import java.lang.annotation.Retention;
189import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700190import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700191import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700192import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700193import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700194
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800195public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800196 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700198 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700200 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700202 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700204 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700206 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800207 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700208 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800209
Craig Mautnerf3333272013-04-22 10:55:53 -0700210 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700211 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700212
Craig Mautner0eea92c2013-05-16 13:35:39 -0700213 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700214 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700217 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700218
Craig Mautner05d29032013-05-03 13:40:13 -0700219 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
220 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
221 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700222 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700223 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800224 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
225 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
226 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700227 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400228 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
229 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700230 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700231 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700232 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800233 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
234 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700237
Jason Monk62515be2014-05-21 16:06:19 -0400238 private static final String LOCK_TASK_TAG = "Lock-to-App";
239
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240 // Used to indicate if an object (e.g. stack) that we are trying to get
241 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800242 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700243
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700244 // Used to indicate that windows of activities should be preserved during the resize.
245 static final boolean PRESERVE_WINDOWS = true;
246
Wale Ogunwale040b4702015-08-06 18:10:50 -0700247 // Used to indicate if an object (e.g. task) should be moved/created
248 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700249 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700250
251 // Used to indicate that an objects (e.g. task) removal from its container
252 // (e.g. stack) is due to it moving to another container.
253 static final boolean MOVING = true;
254
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700255 // Force the focus to change to the stack we are moving a task to..
256 static final boolean FORCE_FOCUS = true;
257
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700258 // Don't execute any calls to resume.
259 static final boolean DEFER_RESUME = true;
260
Winson Chung010927a2016-12-15 16:12:35 -0800261 // Used to indicate that a task is removed it should also be removed from recents.
262 static final boolean REMOVE_FROM_RECENTS = true;
263
Winson Chung6954fc92017-03-24 16:22:12 -0700264 // Used to indicate that pausing an activity should occur immediately without waiting for
265 // the activity callback indicating that it has completed pausing
266 static final boolean PAUSE_IMMEDIATELY = true;
267
Winson Chung7900bee2017-03-10 08:59:25 -0800268 /**
269 * The modes which affect which tasks are returned when calling
270 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
271 */
272 @Retention(RetentionPolicy.SOURCE)
273 @IntDef({
274 MATCH_TASK_IN_STACKS_ONLY,
275 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
276 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
277 })
278 public @interface AnyTaskForIdMatchTaskMode {}
279 // Match only tasks in the current stacks
280 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
281 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
282 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
283 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
284 // provided stack id
285 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
286
Svetoslav7008b512015-06-24 18:47:07 -0700287 // Activity actions an app cannot start if it uses a permission which is not granted.
288 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
289 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700290
Svetoslav7008b512015-06-24 18:47:07 -0700291 static {
292 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
293 Manifest.permission.CAMERA);
294 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
295 Manifest.permission.CAMERA);
296 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
297 Manifest.permission.CALL_PHONE);
298 }
299
Svet Ganov99b60432015-06-27 13:15:22 -0700300 /** Action restriction: launching the activity is not restricted. */
301 private static final int ACTIVITY_RESTRICTION_NONE = 0;
302 /** Action restriction: launching the activity is restricted by a permission. */
303 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
304 /** Action restriction: launching the activity is restricted by an app op. */
305 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700306
justinzhang5286d3f2014-05-12 17:06:01 -0400307 /** Status Bar Service **/
308 private IBinder mToken = new Binder();
309 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400310 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400311
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700312 // For debugging to make sure the caller when acquiring/releasing our
313 // wake lock is the system process.
314 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800315 /** The number of distinct task ids that can be assigned to the tasks of a single user */
316 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700317
Craig Mautner27084302013-03-25 08:05:25 -0700318 final ActivityManagerService mService;
319
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800320 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800321
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322 final ActivityStackSupervisorHandler mHandler;
323
324 /** Short cut */
325 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700328 /** Counter for next free stack ID to use for dynamic activity stacks. */
329 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700330
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800331 /**
332 * Maps the task identifier that activities are currently being started in to the userId of the
333 * task. Each time a new task is created, the entry for the userId of the task is incremented
334 */
335 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700336
Craig Mautner2420ead2013-04-01 17:13:20 -0700337 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800338 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700339
Craig Mautnere0a38842013-12-16 16:14:02 -0800340 /** The stack containing the launcher app. Assumed to always be attached to
341 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800342 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700343
Craig Mautnere0a38842013-12-16 16:14:02 -0800344 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800345 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700346
Craig Mautner4a1cb222013-12-04 16:14:06 -0800347 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
348 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800349 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800350 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351
352 /** List of activities that are waiting for a new activity to become visible before completing
353 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700354 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
355 // mStoppingActivities when something else comes up.
356 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700357
Bryce Lee4a194382017-04-04 14:32:48 -0700358 /** List of processes waiting to find out when a specific activity becomes visible. */
359 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700360
361 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700362 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700363
Craig Mautnerde4ef022013-04-07 19:01:33 -0700364 /** List of activities that are ready to be stopped, but waiting for the next activity to
365 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800366 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700367
Craig Mautnerf3333272013-04-22 10:55:53 -0700368 /** List of activities that are ready to be finished, but waiting for the previous activity to
369 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800370 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700371
Craig Mautner0eea92c2013-05-16 13:35:39 -0700372 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800373 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700374
Wale Ogunwale22e25262016-02-01 10:32:02 -0800375 /** List of activities whose multi-window mode changed that we need to report to the
376 * application */
377 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
378
379 /** List of activities whose picture-in-picture mode changed that we need to report to the
380 * application */
381 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
382
Winson Chung5af42fc2017-03-24 17:11:33 -0700383 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
384 * the application */
385 Rect mPipModeChangedTargetStackBounds;
386
Craig Mautnerf3333272013-04-22 10:55:53 -0700387 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700388 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700389
Craig Mautnerde4ef022013-04-07 19:01:33 -0700390 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
391 * is being brought in front of us. */
392 boolean mUserLeaving = false;
393
Craig Mautner0eea92c2013-05-16 13:35:39 -0700394 /** Set when we have taken too long waiting to go to sleep. */
395 boolean mSleepTimeout = false;
396
397 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700398 * We don't want to allow the device to go to sleep while in the process
399 * of launching an activity. This is primarily to allow alarm intent
400 * receivers to launch an activity and get that to run before the device
401 * goes back to sleep.
402 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700403 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700404
405 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700406 * Set when the system is going to sleep, until we have
407 * successfully paused the current activity and released our wake lock.
408 * At that point the system is allowed to actually sleep.
409 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700410 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700411
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700412 /** Stack id of the front stack when user switched, indexed by userId. */
413 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700414
Craig Mautner4504de52013-12-20 09:06:56 -0800415 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800417 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418
Bryce Leeaf3a4002017-03-20 10:37:12 -0700419 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700421 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800422
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800423 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
424
425 private DisplayManagerInternal mDisplayManagerInternal;
426 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800427
Craig Mautner15df08a2015-04-01 12:17:18 -0700428 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
429 * may be finished until there is only one entry left. If this is empty the system is not
430 * in lockTask mode. */
431 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000432 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700433 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
434 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000435 */
436 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400437 /**
438 * Notifies the user when entering/exiting lock-task.
439 */
440 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800441
Wale Ogunwaled046a012015-12-24 13:05:59 -0800442 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800443 boolean inResumeTopActivity;
444
Andrii Kulian1e32e022016-09-16 15:29:34 -0700445 /**
446 * Temporary rect used during docked stack resize calculation so we don't need to create a new
447 * object each time.
448 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700449 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700450
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700451 // The default minimal size that will be used if the activity doesn't specify its minimal size.
452 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700453 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700454
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700455 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
456 private boolean mTaskLayersChanged = true;
457
Jorim Jaggi275561a2016-02-23 10:11:02 -0500458 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800459
Andrii Kulian1779e612016-10-12 21:58:25 -0700460 @Override
461 protected int getChildCount() {
462 return mActivityDisplays.size();
463 }
464
465 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700466 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700467 return mActivityDisplays.valueAt(index);
468 }
469
470 @Override
471 protected ConfigurationContainer getParent() {
472 return null;
473 }
474
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700475 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700476 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700477 if (activityDisplay == null) {
478 throw new IllegalArgumentException("No display found with id: " + displayId);
479 }
480
481 return activityDisplay.getOverrideConfiguration();
482 }
483
484 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700485 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700486 if (activityDisplay == null) {
487 throw new IllegalArgumentException("No display found with id: " + displayId);
488 }
489
490 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
491 }
492
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700493 /** Check if placing task or activity on specified display is allowed. */
494 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable) {
495 return displayId == DEFAULT_DISPLAY || (mService.mSupportsMultiDisplay
496 && (resizeable || displayConfigMatchesGlobal(displayId)));
497 }
498
499 /**
500 * Check if configuration of specified display matches current global config.
501 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
502 * the same config as on the default display.
503 * @param displayId Id of the display to check.
504 * @return {@code true} if configuration matches.
505 */
506 private boolean displayConfigMatchesGlobal(int displayId) {
507 if (displayId == DEFAULT_DISPLAY) {
508 return true;
509 }
510 if (displayId == INVALID_DISPLAY) {
511 return false;
512 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700513 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700514 if (targetDisplay == null) {
515 throw new IllegalArgumentException("No display found with id: " + displayId);
516 }
517 return getConfiguration().equals(targetDisplay.getConfiguration());
518 }
519
Wale Ogunwale39381972015-12-17 17:15:29 -0800520 static class FindTaskResult {
521 ActivityRecord r;
522 boolean matchedByRootAffinity;
523 }
524 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
525
Craig Mautneree36c772014-07-16 14:56:05 -0700526 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800527 * Temp storage for display ids sorted in focus order.
528 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
529 * it's more efficient, as the number of displays is usually small.
530 */
531 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
532
533 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100534 * Used to keep track whether app visibilities got changed since the last pause. Useful to
535 * determine whether to invoke the task stack change listener after pausing.
536 */
537 boolean mAppVisibilitiesChangedSinceLastPause;
538
539 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100540 * Set of tasks that are in resizing mode during an app transition to fill the "void".
541 */
542 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
543
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700544
545 /**
546 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
547 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
548 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
549 * like the docked stack going empty.
550 */
551 private boolean mAllowDockedStackResize = true;
552
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100553 /**
Tony Mak853304c2016-04-18 15:17:41 +0100554 * Is dock currently minimized.
555 */
556 boolean mIsDockMinimized;
557
Jorim Jaggife762342016-10-13 14:33:27 +0200558 final KeyguardController mKeyguardController;
559
Tony Mak853304c2016-04-18 15:17:41 +0100560 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700561 * Description of a request to start a new activity, which has been held
562 * due to app switches being disabled.
563 */
564 static class PendingActivityLaunch {
565 final ActivityRecord r;
566 final ActivityRecord sourceRecord;
567 final int startFlags;
568 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700569 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700570
571 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700572 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700573 r = _r;
574 sourceRecord = _sourceRecord;
575 startFlags = _startFlags;
576 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700577 callerApp = _callerApp;
578 }
579
580 void sendErrorResult(String message) {
581 try {
582 if (callerApp.thread != null) {
583 callerApp.thread.scheduleCrash(message);
584 }
585 } catch (RemoteException e) {
586 Slog.e(TAG, "Exception scheduling crash of failed "
587 + "activity launcher sourceRecord=" + sourceRecord, e);
588 }
Craig Mautneree36c772014-07-16 14:56:05 -0700589 }
590 }
591
Bryce Leeaf691c02017-03-20 14:20:22 -0700592 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700593 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700594 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800595 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200596 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700597 }
598
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800599 void setRecentTasks(RecentTasks recentTasks) {
600 mRecentTasks = recentTasks;
601 }
602
Jeff Brown2c43c332014-06-12 22:38:59 -0700603 /**
604 * At the time when the constructor runs, the power manager has not yet been
605 * initialized. So we initialize our wakelocks afterwards.
606 */
607 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800608 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700609 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700610 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700611 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700612 }
613
justinzhang5286d3f2014-05-12 17:06:01 -0400614 // This function returns a IStatusBarService. The value is from ServiceManager.
615 // getService and is cached.
616 private IStatusBarService getStatusBarService() {
617 synchronized (mService) {
618 if (mStatusBarService == null) {
619 mStatusBarService = IStatusBarService.Stub.asInterface(
620 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
621 if (mStatusBarService == null) {
622 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
623 }
624 }
625 return mStatusBarService;
626 }
627 }
628
Jason Monk35c62a42014-06-17 10:24:47 -0400629 private IDevicePolicyManager getDevicePolicyManager() {
630 synchronized (mService) {
631 if (mDevicePolicyManager == null) {
632 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
633 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
634 if (mDevicePolicyManager == null) {
635 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
636 }
637 }
638 return mDevicePolicyManager;
639 }
640 }
641
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700642 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643 synchronized (mService) {
644 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200645 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646
647 mDisplayManager =
648 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
649 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800650 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651
652 Display[] displays = mDisplayManager.getDisplays();
653 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
654 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800655 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700656 if (activityDisplay.mDisplay == null) {
657 throw new IllegalStateException("Default Display does not exist");
658 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800659 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700660 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800661 }
662
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800663 mHomeStack = mFocusedStack = mLastFocusedStack =
664 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800665
666 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800667 }
Craig Mautner27084302013-03-25 08:05:25 -0700668 }
669
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700670 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700672 }
673
Craig Mautnerde4ef022013-04-07 19:01:33 -0700674 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700676 }
677
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700678 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700679 if (stack == null) {
680 return false;
681 }
682
Craig Mautnerdf88d732014-01-27 09:21:32 -0800683 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
684 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700685 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800686 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800687 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700688 }
689
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800690 /** The top most stack on its display. */
691 boolean isFrontStackOnDisplay(ActivityStack stack) {
692 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
693 }
694
695 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700696 if (stack == null) {
697 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700699
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700700 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
701 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700702 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800703 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800704 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700705 }
706
Wale Ogunwaled046a012015-12-24 13:05:59 -0800707 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800708 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
709 if (!focusCandidate.isFocusable()) {
710 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800711 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800712 }
713
714 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800715 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800716 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800717
Wale Ogunwaled046a012015-12-24 13:05:59 -0800718 EventLogTags.writeAmFocusedStack(
719 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
720 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
721 }
722
723 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800724 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800725 if (r != null && r.idle) {
726 checkFinishBootingLocked();
727 }
728 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700729 }
730
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700731 void moveHomeStackToFront(String reason) {
732 mHomeStack.moveToFront(reason);
733 }
734
Matthew Ng330757d2017-02-28 14:19:17 -0800735 void moveRecentsStackToFront(String reason) {
736 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
737 if (recentsStack != null) {
738 recentsStack.moveToFront(reason);
739 }
740 }
741
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700742 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800743 boolean moveHomeStackTaskToTop(String reason) {
744 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700745
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700746 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700747 if (top == null) {
748 return false;
749 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700750 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700751 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700752 }
753
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800754 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700755 if (!mService.mBooting && !mService.mBooted) {
756 // Not ready yet!
757 return false;
758 }
759
Craig Mautner84984fa2014-06-19 11:19:20 -0700760 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700761 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700762 }
763
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800764 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700765 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800766 final String myReason = reason + " resumeHomeStackTask";
767
Mark Lua56ea122015-10-08 13:31:01 +0800768 // Only resume home activity if isn't finishing.
769 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700770 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800771 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700772 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800773 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700774 }
775
Craig Mautner8d341ef2013-03-26 09:03:27 -0700776 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800777 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
778 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700779 }
780
781 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700782 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700783 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800784 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700785 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800786 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
787 * valid if the matchMode is
788 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700789 */
Winson Chung7900bee2017-03-10 08:59:25 -0800790 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
791 // If there is a stack id set, ensure that we are attempting to actually restore a task
792 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
793 stackId != INVALID_STACK_ID) {
794 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
795 }
796
Craig Mautnere0a38842013-12-16 16:14:02 -0800797 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800798 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800799 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800800 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
801 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700802 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800803 if (task != null) {
804 return task;
805 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700806 }
807 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800808
Winson Chung7900bee2017-03-10 08:59:25 -0800809 // If we are matching stack tasks only, return now
810 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800811 return null;
812 }
813
Winson Chung7900bee2017-03-10 08:59:25 -0800814 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
815 // the task from recents
816 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700817 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
818
819 if (task == null) {
820 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800821 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
822 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700823
824 return null;
825 }
826
827 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700828 return task;
829 }
830
Winson Chung7900bee2017-03-10 08:59:25 -0800831 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700832 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700833 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
834 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800835 return null;
836 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700837 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800838 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700839 }
840
Craig Mautner6170f732013-04-02 13:05:23 -0700841 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800842 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800843 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800844 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800845 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
846 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
847 if (r != null) {
848 return r;
849 }
Craig Mautner6170f732013-04-02 13:05:23 -0700850 }
851 }
852 return null;
853 }
854
Tony Mak853304c2016-04-18 15:17:41 +0100855 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000856 * Detects whether we should show a lock screen in front of this task for a locked user.
857 * <p>
858 * We'll do this if either of the following holds:
859 * <ul>
860 * <li>The top activity explicitly belongs to {@param userId}.</li>
861 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
862 * </ul>
863 *
864 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100865 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000866 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
867 // To handle the case that work app is in the task but just is not the top one.
868 final ActivityRecord activityRecord = task.getTopActivity();
869 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
870
871 return (activityRecord != null && activityRecord.userId == userId)
872 || (resultTo != null && resultTo.userId == userId);
873 }
874
875 /**
876 * Find all visible task stacks containing {@param userId} and intercept them with an activity
877 * to block out the contents and possibly start a credential-confirming intent.
878 *
879 * @param userId user handle for the locked managed profile.
880 */
881 void lockAllProfileTasks(@UserIdInt int userId) {
882 mWindowManager.deferSurfaceLayout();
883 try {
884 final List<ActivityStack> stacks = getStacks();
885 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
886 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
887 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
888 final TaskRecord task = tasks.get(taskNdx);
889
890 // Check the task for a top activity belonging to userId, or returning a result
891 // to an activity belonging to userId. Example case: a document picker for
892 // personal files, opened by a work app, should still get locked.
893 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000894 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
895 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000896 }
Tony Mak853304c2016-04-18 15:17:41 +0100897 }
898 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000899 } finally {
900 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100901 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000902 }
903
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800904 void setNextTaskIdForUserLocked(int taskId, int userId) {
905 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
906 if (taskId > currentTaskId) {
907 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700908 }
909 }
910
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700911 static int nextTaskIdForUser(int taskId, int userId) {
912 int nextTaskId = taskId + 1;
913 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
914 // Wrap around as there will be smaller task ids that are available now.
915 nextTaskId -= MAX_TASK_IDS_PER_USER;
916 }
917 return nextTaskId;
918 }
919
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800920 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800921 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
922 // for a userId u, a taskId can only be in the range
923 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
924 // 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 -0700925 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800926 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800927 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800928 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700929 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800930 if (candidateTaskId == currentTaskId) {
931 // Something wrong!
932 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
933 throw new IllegalStateException("Cannot get an available task id."
934 + " Reached limit of " + MAX_TASK_IDS_PER_USER
935 + " running tasks per user.");
936 }
937 }
938 mCurTaskIdForUser.put(userId, candidateTaskId);
939 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700940 }
941
Chong Zhang6cda19c2016-06-14 19:07:56 -0700942 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800943 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700944 if (stack == null) {
945 return null;
946 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700947 ActivityRecord resumedActivity = stack.mResumedActivity;
948 if (resumedActivity == null || resumedActivity.app == null) {
949 resumedActivity = stack.mPausingActivity;
950 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700951 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700952 }
953 }
954 return resumedActivity;
955 }
956
Dianne Hackbornff072722014-09-24 10:56:28 -0700957 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700958 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800960 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
961 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
963 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700964 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800965 continue;
966 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700967 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800968 if (hr != null) {
969 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
970 && processName.equals(hr.processName)) {
971 try {
George Mount2c92c972014-03-20 09:38:23 -0700972 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 didSomething = true;
974 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700975 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 Slog.w(TAG, "Exception in new application when starting activity "
977 + hr.intent.getComponent().flattenToShortString(), e);
978 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700979 }
Craig Mautner20e72272013-04-01 13:45:53 -0700980 }
Craig Mautner20e72272013-04-01 13:45:53 -0700981 }
982 }
983 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700984 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700985 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700986 }
Craig Mautner20e72272013-04-01 13:45:53 -0700987 return didSomething;
988 }
989
990 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800991 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
992 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800993 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
994 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700995 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 continue;
997 }
998 final ActivityRecord resumedActivity = stack.mResumedActivity;
999 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001000 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -08001001 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001002 return false;
1003 }
Craig Mautner20e72272013-04-01 13:45:53 -07001004 }
1005 }
Wei Wang65c7a152016-06-02 18:51:22 -07001006 // Send launch end powerhint when idle
1007 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001008 return true;
1009 }
1010
Craig Mautnerde4ef022013-04-07 19:01:33 -07001011 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1013 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1015 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001016 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001017 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001018 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001019 return false;
1020 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001021 }
1022 }
1023 }
1024 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001025 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001026 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1027 mLastFocusedStack + " to=" + mFocusedStack);
1028 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 return true;
1030 }
1031
1032 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001033 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001034 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1035 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001036 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1037 final ActivityStack stack = stacks.get(stackNdx);
1038 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001039 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001040 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001041 return false;
1042 }
1043 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001044 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001045 }
1046 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001047 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001048 }
1049
Craig Mautner2acc3892013-09-23 10:28:14 -07001050 /**
1051 * Pause all activities in either all of the stacks or just the back stacks.
1052 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001053 * @param resuming The resuming activity.
1054 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1055 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001056 * @return true if any activity was paused as a result of this call.
1057 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001058 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001059 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001060 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1061 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001062 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1063 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001064 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001065 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001066 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001067 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1068 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001069 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001070 }
1071 }
1072 return someActivityPaused;
1073 }
1074
Craig Mautnerde4ef022013-04-07 19:01:33 -07001075 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001076 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001077 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1078 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001079 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1080 final ActivityStack stack = stacks.get(stackNdx);
1081 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001082 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001083 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001084 Slog.d(TAG_STATES,
1085 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001086 pausing = false;
1087 } else {
1088 return false;
1089 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001090 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001091 }
1092 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001093 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001094 }
1095
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001096 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001097 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001098 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001099 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1100 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1101 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1102 final ActivityStack stack = stacks.get(stackNdx);
1103 if (stack.mResumedActivity != null &&
1104 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001105 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001106 }
1107 }
1108 }
1109 }
1110
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001111 void cancelInitializingActivities() {
1112 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1113 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1114 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1115 stacks.get(stackNdx).cancelInitializingActivities();
1116 }
1117 }
1118 }
1119
Bryce Lee4a194382017-04-04 14:32:48 -07001120 void waitActivityVisible(ComponentName name, WaitResult result) {
1121 final WaitInfo waitInfo = new WaitInfo(name, result);
1122 mWaitingForActivityVisible.add(waitInfo);
1123 }
1124
1125 void cleanupActivity(ActivityRecord r) {
1126 // Make sure this record is no longer in the pending finishes list.
1127 // This could happen, for example, if we are trimming activities
1128 // down to the max limit while they are still waiting to finish.
1129 mFinishingActivities.remove(r);
1130 mActivitiesWaitingForVisibleActivity.remove(r);
1131
1132 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001133 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001134 mWaitingForActivityVisible.remove(i);
1135 }
1136 }
1137 }
1138
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001139 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001140 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001141 }
1142
1143 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1144 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001145 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1146 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001147 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001148 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001149 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001150 result.timeout = false;
1151 result.who = w.getComponent();
1152 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1153 result.thisTime = result.totalTime;
1154 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001155 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001156 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001157 if (changed) {
1158 mService.notifyAll();
1159 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001160 }
1161
Chong Zhang5022da32016-06-21 16:31:37 -07001162 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1163 boolean changed = false;
1164 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1165 WaitResult w = mWaitingActivityLaunched.remove(i);
1166 if (w.who == null) {
1167 changed = true;
1168 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1169 // the starting activity ends up moving another activity to front, and it should
1170 // wait for this new activity to become visible instead.
1171 // Do not modify other fields.
1172 w.result = START_TASK_TO_FRONT;
1173 }
1174 }
1175 if (changed) {
1176 mService.notifyAll();
1177 }
1178 }
1179
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001180 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1181 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001182 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001183 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001184 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001185 if (w.who == null) {
1186 changed = true;
1187 w.timeout = timeout;
1188 if (r != null) {
1189 w.who = new ComponentName(r.info.packageName, r.info.name);
1190 }
1191 w.thisTime = thisTime;
1192 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001193 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001194 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001195 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001196 if (changed) {
1197 mService.notifyAll();
1198 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001199 }
1200
Craig Mautner29219d92013-04-16 20:19:12 -07001201 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001202 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001203 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001204 if (r != null) {
1205 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001206 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001207
Andrii Kulian7318d632016-07-20 18:59:28 -07001208 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001209 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1210
1211 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1212 final int displayId = mTmpOrderedDisplayIds.get(i);
1213 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1214 if (stacks == null) {
1215 continue;
1216 }
1217 for (int j = stacks.size() - 1; j >= 0; --j) {
1218 final ActivityStack stack = stacks.get(j);
1219 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1220 r = stack.topRunningActivityLocked();
1221 if (r != null) {
1222 return r;
1223 }
Craig Mautner29219d92013-04-16 20:19:12 -07001224 }
1225 }
1226 }
1227 return null;
1228 }
1229
Dianne Hackborn09233282014-04-30 11:33:59 -07001230 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001231 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001232 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1233 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001234 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001235 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001236 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001237 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1238 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001239 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001240 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001241 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001242 }
1243 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001244
1245 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1246 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1247 while (maxNum > 0) {
1248 long mostRecentActiveTime = Long.MIN_VALUE;
1249 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001250 final int numTaskLists = runningTaskLists.size();
1251 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1252 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001253 if (!stackTaskList.isEmpty()) {
1254 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1255 if (lastActiveTime > mostRecentActiveTime) {
1256 mostRecentActiveTime = lastActiveTime;
1257 selectedStackList = stackTaskList;
1258 }
1259 }
1260 }
1261 if (selectedStackList != null) {
1262 list.add(selectedStackList.remove(0));
1263 --maxNum;
1264 } else {
1265 break;
1266 }
1267 }
Craig Mautner20e72272013-04-01 13:45:53 -07001268 }
1269
Todd Kennedy7440f172015-12-09 14:31:22 -08001270 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1271 ProfilerInfo profilerInfo) {
1272 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001273 if (aInfo != null) {
1274 // Store the found target back into the intent, because now that
1275 // we have it we never want to do this again. For example, if the
1276 // user navigates back to this point in the history, we should
1277 // always restart the exact same activity.
1278 intent.setComponent(new ComponentName(
1279 aInfo.applicationInfo.packageName, aInfo.name));
1280
1281 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001282 if (!aInfo.processName.equals("system")) {
1283 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001284 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001285 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001286
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001287 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1288 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1289 }
1290
Man Caocfa78b22015-06-11 20:14:34 -07001291 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1292 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1293 }
1294
1295 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001296 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001297 }
1298 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001299 final String intentLaunchToken = intent.getLaunchToken();
1300 if (aInfo.launchToken == null && intentLaunchToken != null) {
1301 aInfo.launchToken = intentLaunchToken;
1302 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001303 }
1304 return aInfo;
1305 }
1306
Todd Kennedy7440f172015-12-09 14:31:22 -08001307 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001308 return resolveIntent(intent, resolvedType, userId, 0);
1309 }
1310
1311 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001312 synchronized (mService) {
1313 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001314 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001315 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001316 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001317 }
1318
1319 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1320 ProfilerInfo profilerInfo, int userId) {
1321 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1322 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1323 }
1324
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001325 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1326 boolean andResume, boolean checkConfig) throws RemoteException {
1327
1328 if (!allPausedActivitiesComplete()) {
1329 // While there are activities pausing we skipping starting any new activities until
1330 // pauses are complete. NOTE: that we also do this for activities that are starting in
1331 // the paused state because they will first be resumed then paused on the client side.
1332 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1333 "realStartActivityLocked: Skipping start of r=" + r
1334 + " some activities pausing...");
1335 return false;
1336 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001337
Andrii Kulianada10292017-02-09 23:19:29 -08001338 r.startFreezingScreenLocked(app, 0);
Wale Ogunwaled334a052017-05-12 07:17:36 -07001339 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */, true /* isTop */)) {
1340 // We only set the visibility to true if the activity is allowed to be visible based on
1341 // keyguard state. This avoids setting this into motion in window manager that is later
1342 // cancelled due to later calls to ensure visible activities that set visibility back to
1343 // false.
1344 r.setVisibility(true);
1345 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001346
Andrii Kulianada10292017-02-09 23:19:29 -08001347 // schedule launch ticks to collect information about slow apps.
1348 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001349
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001350 // Have the window manager re-evaluate the orientation of the screen based on the new
1351 // activity order. Note that as a result of this, it can call back into the activity
1352 // manager with a new orientation. We don't care about that, because the activity is not
1353 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001354 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001355 final int displayId = r.getDisplayId();
1356 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1357 getDisplayOverrideConfiguration(displayId),
1358 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001359 // Deferring resume here because we're going to launch new activity shortly.
1360 // We don't want to perform a redundant launch of the same record while ensuring
1361 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001362 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1363 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001364 }
1365
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001366 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001367 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001368 }
Makoto Onuki7a622782017-05-10 16:32:39 -07001369 final int applicationInfoUid =
1370 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1371 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1372 Slog.wtf(TAG,
1373 "User ID for activity changing for " + r
1374 + " appInfo.uid=" + r.appInfo.uid
1375 + " info.ai.uid=" + applicationInfoUid
1376 + " old=" + r.app + " new=" + app);
1377 }
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001378
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 r.app = app;
1380 app.waitingToKill = null;
1381 r.launchCount++;
1382 r.lastLaunchTime = SystemClock.uptimeMillis();
1383
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001384 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001385
1386 int idx = app.activities.indexOf(r);
1387 if (idx < 0) {
1388 app.activities.add(r);
1389 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001390 mService.updateLruProcessLocked(app, true, null);
1391 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001392
Bryce Leeaf691c02017-03-20 14:20:22 -07001393 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001394 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1395 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001396 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001397 }
1398
Andrii Kulian02b7a832016-10-06 23:11:56 -07001399 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001400 try {
1401 if (app.thread == null) {
1402 throw new RemoteException();
1403 }
1404 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001405 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001406 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001407 // We don't need to deliver new intents and/or set results if activity is going
1408 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001409 results = r.results;
1410 newIntents = r.newIntents;
1411 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001412 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1413 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1414 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001415 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1416 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001417 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001418 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001419 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001420 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001421 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1422 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001423 r.sleeping = false;
1424 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001425 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001426 mService.showAskCompatModeDialogLocked(r);
1427 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001428 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001429 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1430 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1431 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001432 final String profileFile = mService.mProfileFile;
1433 if (profileFile != null) {
1434 ParcelFileDescriptor profileFd = mService.mProfileFd;
1435 if (profileFd != null) {
1436 try {
1437 profileFd = profileFd.dup();
1438 } catch (IOException e) {
1439 if (profileFd != null) {
1440 try {
1441 profileFd.close();
1442 } catch (IOException o) {
1443 }
1444 profileFd = null;
1445 }
1446 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001447 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001448
1449 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001450 mService.mSamplingInterval, mService.mAutoStopProfiler,
1451 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001452 }
1453 }
1454 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001455
Andrii Kulianada10292017-02-09 23:19:29 -08001456 app.hasShownUi = true;
1457 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001458 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001459 // Because we could be starting an Activity in the system process this may not go across
1460 // a Binder interface which would create a new Configuration. Consequently we have to
1461 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001462
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07001463 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1464 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1465 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001466
Sudheer Shankafab200f2017-05-17 20:41:53 -07001467 logIfTransactionTooLarge(r.intent, r.icicle);
Craig Mautner2420ead2013-04-01 17:13:20 -07001468 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001469 System.identityHashCode(r), r.info,
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07001470 // TODO: Have this take the merged configuration instead of separate global and
1471 // override configs.
1472 mergedConfiguration.getGlobalConfiguration(),
1473 mergedConfiguration.getOverrideConfiguration(), r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001474 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1475 r.persistentState, results, newIntents, !andResume,
1476 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001477
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001478 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001479 // This may be a heavy-weight process! Note that the package
1480 // manager will ensure that only activity can run in the main
1481 // process of the .apk, which is the only thing that will be
1482 // considered heavy-weight.
1483 if (app.processName.equals(app.info.packageName)) {
1484 if (mService.mHeavyWeightProcess != null
1485 && mService.mHeavyWeightProcess != app) {
1486 Slog.w(TAG, "Starting new heavy weight process " + app
1487 + " when already running "
1488 + mService.mHeavyWeightProcess);
1489 }
1490 mService.mHeavyWeightProcess = app;
1491 Message msg = mService.mHandler.obtainMessage(
1492 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1493 msg.obj = r;
1494 mService.mHandler.sendMessage(msg);
1495 }
1496 }
1497
1498 } catch (RemoteException e) {
1499 if (r.launchFailed) {
1500 // This is the second time we failed -- finish activity
1501 // and give up.
1502 Slog.e(TAG, "Second failure launching "
1503 + r.intent.getComponent().flattenToShortString()
1504 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001505 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001506 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1507 "2nd-crash", false);
1508 return false;
1509 }
1510
1511 // This is the first time we failed -- restart process and
1512 // retry.
Sudheer Shankae49e7b82017-05-05 16:39:32 -07001513 r.launchFailed = true;
Craig Mautner2420ead2013-04-01 17:13:20 -07001514 app.activities.remove(r);
1515 throw e;
1516 }
1517
1518 r.launchFailed = false;
1519 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001520 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001521 }
1522
1523 if (andResume) {
1524 // As part of the process of launching, ActivityThread also performs
1525 // a resume.
1526 stack.minimalResumeActivityLocked(r);
1527 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001528 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001529 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001530 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001531 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001532 "Moving to PAUSED: " + r + " (starting in paused state)");
1533 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001534 }
1535
1536 // Launch the new version setup screen if needed. We do this -after-
1537 // launching the initial activity (that is, home), so that it can have
1538 // a chance to initialize itself while in the background, making the
1539 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001540 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001541 mService.startSetupActivityLocked();
1542 }
1543
Dianne Hackborn465fa392014-09-14 14:21:18 -07001544 // Update any services we are bound to that might care about whether
1545 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001546 if (r.app != null) {
1547 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1548 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001549
Craig Mautner2420ead2013-04-01 17:13:20 -07001550 return true;
1551 }
1552
Sudheer Shankafab200f2017-05-17 20:41:53 -07001553 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1554 int extrasSize = 0;
1555 if (intent != null) {
1556 final Bundle extras = intent.getExtras();
1557 if (extras != null) {
1558 extrasSize = extras.getSize();
1559 }
1560 }
1561 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1562 if (extrasSize + icicleSize > 200000) {
1563 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1564 + ", icicle size: " + icicleSize);
1565 }
1566 }
1567
Craig Mautnere79d42682013-04-01 19:01:53 -07001568 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001569 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001570 // Is this activity's application already running?
1571 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001572 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001573
Andrii Kulian02b7a832016-10-06 23:11:56 -07001574 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001575
1576 if (app != null && app.thread != null) {
1577 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001578 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1579 || !"android".equals(r.info.packageName)) {
1580 // Don't add this if it is a platform component that is marked
1581 // to run in multiple processes, because this is actually
1582 // part of the framework so doesn't make sense to track as a
1583 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001584 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1585 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001586 }
George Mount2c92c972014-03-20 09:38:23 -07001587 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001588 return;
1589 } catch (RemoteException e) {
1590 Slog.w(TAG, "Exception when starting activity "
1591 + r.intent.getComponent().flattenToShortString(), e);
1592 }
1593
1594 // If a dead object exception was thrown -- fall through to
1595 // restart the application.
1596 }
1597
1598 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001599 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001600 }
1601
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001602 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001603 String resultWho, int requestCode, int callingPid, int callingUid,
1604 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001605 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001606 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1607 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001608 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001609 return true;
1610 }
1611 final int componentRestriction = getComponentRestrictionForCallingPackage(
1612 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1613 final int actionRestriction = getActionRestrictionForCallingPackage(
1614 intent.getAction(), callingPackage, callingPid, callingUid);
1615 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1616 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1617 if (resultRecord != null) {
1618 resultStack.sendActivityResultLocked(-1,
1619 resultRecord, resultWho, requestCode,
1620 Activity.RESULT_CANCELED, null);
1621 }
1622 final String msg;
1623 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1624 msg = "Permission Denial: starting " + intent.toString()
1625 + " from " + callerApp + " (pid=" + callingPid
1626 + ", uid=" + callingUid + ")" + " with revoked permission "
1627 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1628 } else if (!aInfo.exported) {
1629 msg = "Permission Denial: starting " + intent.toString()
1630 + " from " + callerApp + " (pid=" + callingPid
1631 + ", uid=" + callingUid + ")"
1632 + " not exported from uid " + aInfo.applicationInfo.uid;
1633 } else {
1634 msg = "Permission Denial: starting " + intent.toString()
1635 + " from " + callerApp + " (pid=" + callingPid
1636 + ", uid=" + callingUid + ")"
1637 + " requires " + aInfo.permission;
1638 }
1639 Slog.w(TAG, msg);
1640 throw new SecurityException(msg);
1641 }
1642
1643 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1644 final String message = "Appop Denial: starting " + intent.toString()
1645 + " from " + callerApp + " (pid=" + callingPid
1646 + ", uid=" + callingUid + ")"
1647 + " requires " + AppOpsManager.permissionToOp(
1648 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1649 Slog.w(TAG, message);
1650 return false;
1651 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1652 final String message = "Appop Denial: starting " + intent.toString()
1653 + " from " + callerApp + " (pid=" + callingPid
1654 + ", uid=" + callingUid + ")"
1655 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1656 Slog.w(TAG, message);
1657 return false;
1658 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001659 if (options != null) {
1660 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1661 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1662 callingPid, callingUid);
1663 if (startInTaskPerm != PERMISSION_GRANTED) {
1664 final String msg = "Permission Denial: starting " + intent.toString()
1665 + " from " + callerApp + " (pid=" + callingPid
1666 + ", uid=" + callingUid + ") with launchTaskId="
1667 + options.getLaunchTaskId();
1668 Slog.w(TAG, msg);
1669 throw new SecurityException(msg);
1670 }
1671 }
1672 // Check if someone tries to launch an activity on a private display with a different
1673 // owner.
1674 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001675 if (launchDisplayId != INVALID_DISPLAY
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001676 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001677 final String msg = "Permission Denial: starting " + intent.toString()
1678 + " from " + callerApp + " (pid=" + callingPid
1679 + ", uid=" + callingUid + ") with launchDisplayId="
1680 + launchDisplayId;
1681 Slog.w(TAG, msg);
1682 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001683 }
1684 }
1685
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001686 return true;
1687 }
1688
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001689 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001690 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001691 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1692 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1693
Andrii Kulian62e6f252017-05-30 22:46:53 -07001694 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001695 if (activityDisplay == null) {
1696 Slog.w(TAG, "Launch on display check: display not found");
1697 return false;
1698 }
1699
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001700 // Check if the caller can manage activity stacks.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001701 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001702 callingUid);
1703 if (startAnyPerm == PERMISSION_GRANTED) {
1704 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1705 + " allow launch any on display");
1706 return true;
1707 }
1708
1709 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001710 && activityDisplay.mDisplay.getOwnerUid() != SYSTEM_UID) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001711 // Limit launching on virtual displays, because their contents can be read from Surface
1712 // by apps that created them.
1713 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1714 + " disallow launch on virtual display for not-embedded activity");
1715 return false;
1716 }
1717
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001718 if (!activityDisplay.isPrivate()) {
1719 // Anyone can launch on a public display.
1720 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1721 + " allow launch on public display");
1722 return true;
1723 }
1724
1725 // Check if the caller is the owner of the display.
1726 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1727 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1728 + " allow launch for owner of the display");
1729 return true;
1730 }
1731
1732 // Check if caller is present on display
1733 if (activityDisplay.isUidPresent(callingUid)) {
1734 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1735 + " allow launch for caller present on the display");
1736 return true;
1737 }
1738
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001739 Slog.w(TAG, "Launch on display check: denied");
1740 return false;
1741 }
1742
1743 /** Update lists of UIDs that are present on displays and have access to them. */
1744 void updateUIDsPresentOnDisplay() {
1745 mDisplayAccessUIDs.clear();
1746 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1747 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001748 // Only bother calculating the whitelist for private displays
1749 if (activityDisplay.isPrivate()) {
1750 mDisplayAccessUIDs.append(
1751 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1752 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001753 }
1754 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1755 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1756 }
1757
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001758 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001759 final long identity = Binder.clearCallingIdentity();
1760 try {
1761 return UserManager.get(mService.mContext).getUserInfo(userId);
1762 } finally {
1763 Binder.restoreCallingIdentity(identity);
1764 }
1765 }
1766
Svet Ganov99b60432015-06-27 13:15:22 -07001767 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001768 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001769 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1770 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001771 == PackageManager.PERMISSION_DENIED) {
1772 return ACTIVITY_RESTRICTION_PERMISSION;
1773 }
1774
Christopher Tateff7add02015-08-17 10:23:22 -07001775 if (activityInfo.permission == null) {
1776 return ACTIVITY_RESTRICTION_NONE;
1777 }
1778
Svet Ganov99b60432015-06-27 13:15:22 -07001779 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1780 if (opCode == AppOpsManager.OP_NONE) {
1781 return ACTIVITY_RESTRICTION_NONE;
1782 }
1783
1784 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1785 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001786 if (!ignoreTargetSecurity) {
1787 return ACTIVITY_RESTRICTION_APPOP;
1788 }
Svet Ganov99b60432015-06-27 13:15:22 -07001789 }
1790
1791 return ACTIVITY_RESTRICTION_NONE;
1792 }
1793
Svetoslav7008b512015-06-24 18:47:07 -07001794 private int getActionRestrictionForCallingPackage(String action,
1795 String callingPackage, int callingPid, int callingUid) {
1796 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001797 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001798 }
1799
1800 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1801 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001802 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001803 }
1804
1805 final PackageInfo packageInfo;
1806 try {
1807 packageInfo = mService.mContext.getPackageManager()
1808 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1809 } catch (PackageManager.NameNotFoundException e) {
1810 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001811 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001812 }
1813
1814 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001815 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001816 }
1817
1818 if (mService.checkPermission(permission, callingPid, callingUid) ==
1819 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001820 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001821 }
1822
1823 final int opCode = AppOpsManager.permissionToOpCode(permission);
1824 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001825 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001826 }
1827
1828 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1829 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001830 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001831 }
1832
Svet Ganov99b60432015-06-27 13:15:22 -07001833 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001834 }
1835
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001836 void setLaunchSource(int uid) {
1837 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1838 }
1839
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001840 void acquireLaunchWakelock() {
1841 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1842 throw new IllegalStateException("Calling must be system uid");
1843 }
1844 mLaunchingActivity.acquire();
1845 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1846 // To be safe, don't allow the wake lock to be held for too long.
1847 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1848 }
1849 }
1850
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001851 /**
1852 * Called when the frontmost task is idle.
1853 * @return the state of mService.mBooting before this was called.
1854 */
1855 private boolean checkFinishBootingLocked() {
1856 final boolean booting = mService.mBooting;
1857 boolean enableScreen = false;
1858 mService.mBooting = false;
1859 if (!mService.mBooted) {
1860 mService.mBooted = true;
1861 enableScreen = true;
1862 }
1863 if (booting || enableScreen) {
1864 mService.postFinishBooting(booting, enableScreen);
1865 }
1866 return booting;
1867 }
1868
Craig Mautnerf3333272013-04-22 10:55:53 -07001869 // Checked.
1870 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001871 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001872 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001873
Craig Mautnerf3333272013-04-22 10:55:53 -07001874 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001875 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001876 int NS = 0;
1877 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001878 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 boolean activityRemoved = false;
1880
Wale Ogunwale7d701172015-03-11 15:36:30 -07001881 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001882 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001883 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1884 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001885 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1886 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001887 if (fromTimeout) {
1888 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001889 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001890
1891 // This is a hack to semi-deal with a race condition
1892 // in the client where it can be constructed with a
1893 // newer configuration from when we asked it to launch.
1894 // We'll update with whatever configuration it now says
1895 // it used to launch.
1896 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001897 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001898 }
1899
1900 // We are now idle. If someone is waiting for a thumbnail from
1901 // us, we can now deliver.
1902 r.idle = true;
1903
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001904 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001905 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001906 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001907 }
1908 }
1909
1910 if (allResumedActivitiesIdle()) {
1911 if (r != null) {
1912 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001913 }
1914
1915 if (mLaunchingActivity.isHeld()) {
1916 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1917 if (VALIDATE_WAKE_LOCK_CALLER &&
1918 Binder.getCallingUid() != Process.myUid()) {
1919 throw new IllegalStateException("Calling must be system uid");
1920 }
1921 mLaunchingActivity.release();
1922 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001923 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001924 }
1925
1926 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001927 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1928 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001929 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001930 if ((NF = mFinishingActivities.size()) > 0) {
1931 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001932 mFinishingActivities.clear();
1933 }
1934
Craig Mautnerf3333272013-04-22 10:55:53 -07001935 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001936 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001937 mStartingUsers.clear();
1938 }
1939
Craig Mautnerf3333272013-04-22 10:55:53 -07001940 // Stop 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 < NS; i++) {
1943 r = stops.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 if (r.finishing) {
1947 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1948 } else {
1949 stack.stopActivityLocked(r);
1950 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001951 }
1952 }
1953
1954 // Finish any activities that are scheduled to do so but have been
1955 // waiting for the next one to start.
1956 for (int i = 0; i < NF; i++) {
1957 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001958 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001959 if (stack != null) {
1960 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1961 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001962 }
1963
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001964 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001965 // Complete user switch
1966 if (startingUsers != null) {
1967 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001968 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001969 }
1970 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001971 }
1972
1973 mService.trimApplications();
1974 //dump();
1975 //mWindowManager.dump();
1976
Craig Mautnerf3333272013-04-22 10:55:53 -07001977 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001978 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001979 }
1980
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001981 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001982 }
1983
Craig Mautner8e569572013-10-11 17:36:59 -07001984 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001985 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001986 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1987 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001988 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1989 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1990 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001991 }
Craig Mautner19091252013-10-05 00:03:53 -07001992 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001993 }
1994
1995 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001996 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1997 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001998 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1999 stacks.get(stackNdx).closeSystemDialogsLocked();
2000 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002001 }
2002 }
2003
Craig Mautner93529a42013-10-04 15:03:13 -07002004 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002005 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002006 }
2007
Craig Mautner8d341ef2013-03-26 09:03:27 -07002008 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002009 * Update the last used stack id for non-current user (current user's last
2010 * used stack is the focused stack)
2011 */
2012 void updateUserStackLocked(int userId, ActivityStack stack) {
2013 if (userId != mCurrentUser) {
2014 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2015 }
2016 }
2017
2018 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002019 * @return true if some activity was finished (or would have finished if doit were true).
2020 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002021 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2022 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002023 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2025 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002026 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002027 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002028 if (stack.finishDisabledPackageActivitiesLocked(
2029 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002030 didSomething = true;
2031 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002032 }
2033 }
2034 return didSomething;
2035 }
2036
Dianne Hackborna413dc02013-07-12 12:02:55 -07002037 void updatePreviousProcessLocked(ActivityRecord r) {
2038 // Now that this process has stopped, we may want to consider
2039 // it to be the previous app to try to keep around in case
2040 // the user wants to return to it.
2041
2042 // First, found out what is currently the foreground app, so that
2043 // we don't blow away the previous app if this activity is being
2044 // hosted by the process that is actually still the foreground.
2045 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002046 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2047 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002048 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2049 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002050 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002051 if (stack.mResumedActivity != null) {
2052 fgApp = stack.mResumedActivity.app;
2053 } else if (stack.mPausingActivity != null) {
2054 fgApp = stack.mPausingActivity.app;
2055 }
2056 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002057 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002058 }
2059 }
2060
2061 // Now set this one as the previous process, only if that really
2062 // makes sense to.
2063 if (r.app != null && fgApp != null && r.app != fgApp
2064 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002065 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002066 mService.mPreviousProcess = r.app;
2067 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2068 }
2069 }
2070
Wale Ogunwaled046a012015-12-24 13:05:59 -08002071 boolean resumeFocusedStackTopActivityLocked() {
2072 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002073 }
2074
Wale Ogunwaled046a012015-12-24 13:05:59 -08002075 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002076 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002077 if (targetStack != null && isFocusedStack(targetStack)) {
2078 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002079 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002080 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2081 if (r == null || r.state != RESUMED) {
2082 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002083 } else if (r.state == RESUMED) {
2084 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2085 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002086 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002087 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002088 }
2089
Todd Kennedy39bfee52016-02-24 10:28:21 -08002090 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2091 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2092 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2093 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2094 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2095 }
2096 }
2097 }
2098
Adrian Roos20d7df32016-01-12 18:59:43 +01002099 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2100 TaskRecord finishedTask = null;
2101 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002102 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2103 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002104 final int numStacks = stacks.size();
2105 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2106 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002107 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2108 if (stack == focusedStack || finishedTask == null) {
2109 finishedTask = t;
2110 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002111 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002112 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002113 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002114 }
2115
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002116 void finishVoiceTask(IVoiceInteractionSession session) {
2117 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2118 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2119 final int numStacks = stacks.size();
2120 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2121 final ActivityStack stack = stacks.get(stackNdx);
2122 stack.finishVoiceTask(session);
2123 }
2124 }
2125 }
2126
Andrii Kulianc27916642016-04-12 17:59:27 -07002127 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2128 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002129 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2130 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002131 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002132 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2133 // Caller wants the home activity moved with it. To accomplish this,
2134 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002135 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002136 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002137 ActivityStack currentStack = task.getStack();
2138 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002139 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2140 + task + " to front. Stack is null");
2141 return;
2142 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002143
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002144 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002145 int stackId = options.getLaunchStackId();
2146 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002147 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002148 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002149 if (stackId == INVALID_STACK_ID) {
2150 stackId = task.getLaunchStackId();
2151 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002152 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002153 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2154 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002155 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002156 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2157 // still need moveTaskToFrontLocked() below for any transition settings.
2158 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002159 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2160 resizeStackLocked(stackId, bounds,
2161 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002162 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002163 } else {
2164 // WM resizeTask must be done after the task is moved to the correct stack,
2165 // because Task's setBounds() also updates dim layer's bounds, but that has
2166 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002167 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002168 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002169 }
2170 }
2171
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002172 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002173 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002174 r == null ? null : r.appTimeTracker, reason);
2175
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002176 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002177 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002178
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002179 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
2180 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002181 }
2182
Wale Ogunwale854809c2015-12-27 16:18:19 -08002183 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002184 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002185 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002186 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002187 return false;
2188 }
2189 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2190 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002191 }
2192
Winson Chung55893332017-02-17 17:13:10 -08002193 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002194 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002195 }
2196
Winson Chung55893332017-02-17 17:13:10 -08002197 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2198 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002199 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002200 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002201 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002202 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002203 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002204 return null;
2205 }
Matthew Ng330757d2017-02-28 14:19:17 -08002206 if (stackId == DOCKED_STACK_ID) {
2207 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2208 // the other side of the docked stack and we make visibility decisions based on that.
2209 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2210 }
Winson Chung55893332017-02-17 17:13:10 -08002211 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002212 }
2213
Andrii Kulian16802aa2016-11-02 12:21:33 -07002214 /**
2215 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2216 * If there is no such stack, new dynamic stack can be created.
2217 * @param displayId Target display.
2218 * @param r Activity that should be launched there.
2219 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2220 */
2221 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002222 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002223 if (activityDisplay == null) {
2224 throw new IllegalArgumentException(
2225 "Display with displayId=" + displayId + " not found.");
2226 }
2227
2228 // Return the topmost valid stack on the display.
2229 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2230 final ActivityStack stack = activityDisplay.mStacks.get(i);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002231 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002232 return stack;
2233 }
2234 }
2235
2236 // If there is no valid stack on the external display - check if new dynamic stack will do.
2237 if (displayId != Display.DEFAULT_DISPLAY) {
2238 final int newDynamicStackId = getNextStackId();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002239 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002240 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2241 }
2242 }
2243
2244 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2245 return null;
2246 }
2247
Craig Mautner967212c2013-04-13 21:10:58 -07002248 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002249 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002250 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2251 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002252 }
2253 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002254 }
2255
Jorim Jaggife762342016-10-13 14:33:27 +02002256 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2257 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2258 }
2259
Andrii Kulian7fc22812016-12-28 13:04:11 -08002260 /**
2261 * Get next focusable stack in the system. This will search across displays and stacks
2262 * in last-focused order for a focusable and visible stack, different from the target stack.
2263 *
2264 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2265 * searching for next candidate.
2266 * @return Next focusable {@link ActivityStack}, null if not found.
2267 */
2268 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2269 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2270
2271 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2272 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002273 // If a display is registered in WM, it must also be available in AM.
2274 @SuppressWarnings("ConstantConditions")
2275 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002276 for (int j = stacks.size() - 1; j >= 0; --j) {
2277 final ActivityStack stack = stacks.get(j);
2278 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002279 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002280 return stack;
2281 }
2282 }
2283 }
2284
2285 return null;
2286 }
2287
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002288 /**
2289 * Get next valid stack for launching provided activity in the system. This will search across
2290 * displays and stacks in last-focused order for a focusable and visible stack, except those
2291 * that are on a currently focused display.
2292 *
2293 * @param r The activity that is being launched.
2294 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2295 * searching for the next candidate.
2296 * @return Next valid {@link ActivityStack}, null if not found.
2297 */
2298 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2299 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2300 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2301 final int displayId = mTmpOrderedDisplayIds.get(i);
2302 if (displayId == currentFocus) {
2303 continue;
2304 }
2305 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2306 if (stack != null) {
2307 return stack;
2308 }
2309 }
2310 return null;
2311 }
2312
Craig Mautneree2e45a2014-06-27 12:10:03 -07002313 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002314 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002315 }
2316
2317 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002318 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2319 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2320 final TaskRecord task = tasks.get(taskNdx);
2321 if (task.isHomeTask()) {
2322 final ArrayList<ActivityRecord> activities = task.mActivities;
2323 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2324 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002325 if (r.isHomeActivity()
2326 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002327 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002328 }
2329 }
2330 }
2331 }
2332 return null;
2333 }
2334
Chong Zhangb15758a2015-11-17 12:12:03 -08002335 /**
2336 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2337 * the docked stack itself, or if it's side-by-side to the docked stack.
2338 */
2339 boolean isStackDockedInEffect(int stackId) {
2340 return stackId == DOCKED_STACK_ID ||
2341 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2342 }
2343
Todd Kennedyca4d8422015-01-15 15:19:22 -08002344 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002345 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002346 ActivityContainer activityContainer =
2347 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002348 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002349 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2350 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002351 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002352 return activityContainer;
2353 }
2354
Craig Mautner34b73df2014-01-12 21:11:08 -08002355 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002356 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2357 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2358 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002359 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2360 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002361 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002362 }
2363 }
2364
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002365 void deleteActivityContainerRecord(int stackId) {
2366 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2367 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2368 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002369 }
2370
Jorim Jaggidc249c42015-12-15 14:57:31 -08002371 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002372 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002373 if (stackId == DOCKED_STACK_ID) {
2374 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002375 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002376 return;
2377 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002378 final ActivityStack stack = getStack(stackId);
2379 if (stack == null) {
2380 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2381 return;
2382 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002383
Winson Chunga2249ac2017-03-30 18:15:30 -07002384 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2385 getStack(DOCKED_STACK_ID) != null) {
2386 // If the docked stack exists, don't resize non-floating stacks independently of the
2387 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002388 return;
2389 }
2390
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002391 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002392 mWindowManager.deferSurfaceLayout();
2393 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002394 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002395 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002396 stack.ensureVisibleActivitiesConfigurationLocked(
2397 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002398 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002399 } finally {
2400 mWindowManager.continueSurfaceLayout();
2401 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002402 }
2403 }
2404
Jorim Jaggi192086e2016-03-11 17:17:03 +01002405 void deferUpdateBounds(int stackId) {
2406 final ActivityStack stack = getStack(stackId);
2407 if (stack != null) {
2408 stack.deferUpdateBounds();
2409 }
2410 }
2411
2412 void continueUpdateBounds(int stackId) {
2413 final ActivityStack stack = getStack(stackId);
2414 if (stack != null) {
2415 stack.continueUpdateBounds();
2416 }
2417 }
2418
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002419 void notifyAppTransitionDone() {
Matthew Ng606dd802017-06-05 14:06:32 -07002420 continueUpdateBounds(RECENTS_STACK_ID);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002421 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2422 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002423 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002424 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002425 if (task != null) {
2426 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002427 }
2428 }
2429 mResizingTasksDuringAnimation.clear();
2430 }
2431
Robert Carre7cc44d2017-03-20 19:04:30 -07002432 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2433 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002434
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002435 final ActivityStack stack = getStack(fromStackId);
2436 if (stack == null) {
2437 return;
2438 }
2439
2440 mWindowManager.deferSurfaceLayout();
2441 try {
2442 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002443 // We are moving all tasks from the docked stack to the fullscreen stack,
2444 // which is dismissing the docked stack, so resize all other stacks to
2445 // fullscreen here already so we don't end up with resize trashing.
2446 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2447 if (StackId.isResizeableByDockedStack(i)) {
2448 ActivityStack otherStack = getStack(i);
2449 if (otherStack != null) {
2450 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2451 true /* allowResizeInDockedMode */, DEFER_RESUME);
2452 }
2453 }
2454 }
2455
2456 // Also disable docked stack resizing since we have manually adjusted the
2457 // size of other stacks above and we don't want to trigger a docked stack
2458 // resize when we remove task from it below and it is detached from the
2459 // display because it no longer contains any tasks.
2460 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002461 } else if (fromStackId == PINNED_STACK_ID) {
2462 if (onTop) {
2463 // Log if we are expanding the PiP to fullscreen
2464 MetricsLogger.action(mService.mContext,
2465 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2466 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002467 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002468 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002469 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002470 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002471 // If we are moving from the pinned stack, then the animation takes care of updating
2472 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002473 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002474 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2475 final int size = tasks.size();
2476 if (onTop) {
2477 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002478 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002479 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002480 if (fromStackId == PINNED_STACK_ID) {
2481 // Update the return-to to reflect where the pinned stack task was moved
2482 // from so that we retain the stack that was previously visible if the
2483 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002484 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2485 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002486 }
Winson Chung74666102017-02-22 17:49:24 -08002487 // Defer resume until all the tasks have been moved to the fullscreen stack
2488 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2489 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002490 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002491 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002492 }
2493 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002494 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002495 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002496 // Position the tasks in the fullscreen stack in order at the bottom of the
2497 // stack. Also defer resume until all the tasks have been moved to the
2498 // fullscreen stack.
2499 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002500 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2501 schedulePictureInPictureModeChange,
2502 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002503 }
2504 }
Winson Chung74666102017-02-22 17:49:24 -08002505
2506 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2507 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002508 } finally {
2509 mAllowDockedStackResize = true;
2510 mWindowManager.continueSurfaceLayout();
2511 }
2512 }
2513
Robert Carr6914f082017-03-20 19:04:30 -07002514 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002515 mWindowManager.inSurfaceTransaction(
2516 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002517 }
2518
Jorim Jaggidc249c42015-12-15 14:57:31 -08002519 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002520 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2521 boolean preserveWindows) {
2522 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2523 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2524 false /* deferResume */);
2525 }
2526
2527 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2528 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2529 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002530
2531 if (!mAllowDockedStackResize) {
2532 // Docked stack resize currently disabled.
2533 return;
2534 }
2535
Jorim Jaggidc249c42015-12-15 14:57:31 -08002536 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2537 if (stack == null) {
2538 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2539 return;
2540 }
2541
2542 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2543 mWindowManager.deferSurfaceLayout();
2544 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002545 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2546 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002547 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002548 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002549
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002550 // TODO: Checking for isAttached might not be needed as if the user passes in null
2551 // dockedBounds then they want the docked stack to be dismissed.
2552 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2553 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002554 // In this case we make all other static stacks fullscreen and move all
2555 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002556 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002557
2558 // stack shouldn't contain anymore activities, so nothing to resume.
2559 r = null;
2560 } else {
2561 // Docked stacks occupy a dedicated region on screen so the size of all other
2562 // static stacks need to be adjusted so they don't overlap with the docked stack.
2563 // We get the bounds to use from window manager which has been adjusted for any
2564 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002565 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002566 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002567 final ActivityStack current = getStack(i);
2568 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002569 current.getStackDockedModeBounds(
2570 tempOtherTaskBounds /* currentTempTaskBounds */,
2571 tempRect /* outStackBounds */,
2572 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2573
Andrii Kuliane48b7d82017-04-28 14:51:05 -07002574 resizeStackLocked(i, !tempRect.isEmpty() ? tempRect : null,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002575 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2576 tempOtherTaskInsetBounds, preserveWindows,
2577 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002578 }
2579 }
2580 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002581 if (!deferResume) {
2582 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2583 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002584 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002585 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002586 mWindowManager.continueSurfaceLayout();
2587 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2588 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002589 }
2590
Robert Carr0d00c2e2016-02-29 17:45:02 -08002591 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002592 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002593 if (stack == null) {
2594 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2595 return;
2596 }
Winson Chung19953ca2017-04-11 11:19:23 -07002597
2598 // It is possible for the bounds animation from the WM to call this but be delayed by
2599 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2600 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2601 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2602 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002603 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002604 return;
2605 }
2606
Robert Carr0d00c2e2016-02-29 17:45:02 -08002607 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2608 mWindowManager.deferSurfaceLayout();
2609 try {
2610 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002611 Rect insetBounds = null;
2612 if (tempPinnedTaskBounds != null) {
2613 // We always use 0,0 as the position for the inset rect because
2614 // if we are getting insets at all in the pinned stack it must mean
2615 // we are headed for fullscreen.
2616 insetBounds = tempRect;
2617 insetBounds.top = 0;
2618 insetBounds.left = 0;
2619 insetBounds.right = tempPinnedTaskBounds.width();
2620 insetBounds.bottom = tempPinnedTaskBounds.height();
2621 }
2622 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002623 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002624 } finally {
2625 mWindowManager.continueSurfaceLayout();
2626 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2627 }
2628 }
2629
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002630 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002631 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002632 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002633 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002634 }
2635
Wale Ogunwale1666e312016-12-16 11:27:18 -08002636 final ActivityContainer activityContainer =
2637 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002638 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002639 }
2640
Robert Carr6914f082017-03-20 19:04:30 -07002641
Robert Carre7cc44d2017-03-20 19:04:30 -07002642 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002643 final ActivityStack stack = getStack(stackId);
2644 if (stack == null) {
2645 return;
2646 }
2647
2648 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2649 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002650 /**
2651 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2652 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2653 * that the client receives onStop() before it is reparented. We do this by detaching
2654 * the stack from the display so that it will be considered invisible when
2655 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2656 * invisible as well and added to the stopping list. After which we process the
2657 * stopping list by handling the idle.
2658 */
2659 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2660 pinnedStack.mForceHidden = true;
2661 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2662 pinnedStack.mForceHidden = false;
2663 activityIdleInternalLocked(null, false /* fromTimeout */,
2664 true /* processPausingActivites */, null /* configuration */);
2665
2666 // Move all the tasks to the bottom of the fullscreen stack
2667 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002668 } else {
2669 for (int i = tasks.size() - 1; i >= 0; i--) {
2670 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2671 REMOVE_FROM_RECENTS);
2672 }
2673 }
2674 }
2675
2676 /**
Robert Carr6914f082017-03-20 19:04:30 -07002677 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2678 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2679 * instead moved back onto the fullscreen stack.
2680 */
2681 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002682 mWindowManager.inSurfaceTransaction(
2683 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002684 }
2685
2686 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002687 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2688 */
2689 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2690 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2691 }
2692
2693 /**
Winson Chung010927a2016-12-15 16:12:35 -08002694 * Removes the task with the specified task id.
2695 *
2696 * @param taskId Identifier of the task to be removed.
2697 * @param killProcess Kill any process associated with the task if possible.
2698 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002699 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2700 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002701 * @return Returns true if the given task was found and removed.
2702 */
Winson Chung6954fc92017-03-24 16:22:12 -07002703 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2704 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002705 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2706 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002707 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002708 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002709 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2710 if (tr.isPersistable) {
2711 mService.notifyTaskPersisterLocked(null, true);
2712 }
2713 return true;
2714 }
2715 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2716 return false;
2717 }
2718
2719 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2720 if (removeFromRecents) {
2721 mRecentTasks.remove(tr);
2722 tr.removedFromRecents();
2723 }
2724 ComponentName component = tr.getBaseIntent().getComponent();
2725 if (component == null) {
2726 Slog.w(TAG, "No component for base intent of task: " + tr);
2727 return;
2728 }
2729
2730 // Find any running services associated with this app and stop if needed.
2731 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2732
2733 if (!killProcess) {
2734 return;
2735 }
2736
2737 // Determine if the process(es) for this task should be killed.
2738 final String pkg = component.getPackageName();
2739 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2740 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2741 for (int i = 0; i < pmap.size(); i++) {
2742
2743 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2744 for (int j = 0; j < uids.size(); j++) {
2745 ProcessRecord proc = uids.valueAt(j);
2746 if (proc.userId != tr.userId) {
2747 // Don't kill process for a different user.
2748 continue;
2749 }
2750 if (proc == mService.mHomeProcess) {
2751 // Don't kill the home process along with tasks from the same package.
2752 continue;
2753 }
2754 if (!proc.pkgList.containsKey(pkg)) {
2755 // Don't kill process that is not associated with this task.
2756 continue;
2757 }
2758
2759 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002760 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002761 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2762 // Don't kill process(es) that has an activity in a different task that is
2763 // also in recents.
2764 return;
2765 }
2766 }
2767
2768 if (proc.foregroundServices) {
2769 // Don't kill process(es) with foreground service.
2770 return;
2771 }
2772
2773 // Add process to kill list.
2774 procsToKill.add(proc);
2775 }
2776 }
2777
2778 // Kill the running processes.
2779 for (int i = 0; i < procsToKill.size(); i++) {
2780 ProcessRecord pr = procsToKill.get(i);
2781 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2782 && pr.curReceivers.isEmpty()) {
2783 pr.kill("remove task", true);
2784 } else {
2785 // We delay killing processes that are not in the background or running a receiver.
2786 pr.waitingToKill = "remove task";
2787 }
2788 }
2789 }
2790
Craig Mautner4a1cb222013-12-04 16:14:06 -08002791 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002792 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002793 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2794 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002795 break;
2796 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002797 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002798 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002799 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002800 }
2801
Chong Zhang5dcb2752015-08-18 13:50:26 -07002802 /**
2803 * Restores a recent task to a stack
2804 * @param task The recent task to be restored.
2805 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002806 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2807 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002808 * @return true if the task has been restored successfully.
2809 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002810 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002811 if (!StackId.isStaticStack(stackId)) {
2812 // If stack is not static (or stack id is invalid) - use the default one.
2813 // This means that tasks that were on external displays will be restored on the
2814 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002815 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002816 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002817 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2818 // Put it in the fullscreen stack.
2819 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002820 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002821
Andrii Kulian02b7a832016-10-06 23:11:56 -07002822 final ActivityStack currentStack = task.getStack();
2823 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002824 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002825 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002826 // Nothing else to do since it is already restored in the right stack.
2827 return true;
2828 }
2829 // Remove current stack association, so we can re-associate the task with the
2830 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002831 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002832 }
2833
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002834 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002835 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002836
2837 if (stack == null) {
2838 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002839 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2840 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002841 return false;
2842 }
2843
Wale Ogunwale72919d22016-12-08 18:58:50 -08002844 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002845 // TODO: move call for creation here and other place into Stack.addTask()
2846 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002847 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2848 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002849 final ArrayList<ActivityRecord> activities = task.mActivities;
2850 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002851 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002852 }
2853 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002854 }
2855
Wale Ogunwale040b4702015-08-06 18:10:50 -07002856 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002857 * Move stack with all its existing content to specified display.
2858 * @param stackId Id of stack to move.
2859 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002860 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002861 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002862 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002863 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002864 if (activityDisplay == null) {
2865 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2866 + displayId);
2867 }
2868 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2869 if (activityContainer != null) {
2870 if (activityContainer.isAttachedLocked()) {
2871 if (activityContainer.getDisplayId() == displayId) {
2872 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2873 + " to its current displayId=" + displayId);
2874 }
2875
Andrii Kulian250d6532017-02-08 23:30:45 -08002876 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002877 } else {
2878 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2879 + stackId + " is not attached to any display.");
2880 }
2881 } else {
2882 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2883 + stackId);
2884 }
Andrii Kulian839def92016-11-02 10:58:58 -07002885 // TODO(multi-display): resize stacks properly if moved from split-screen.
2886 }
2887
2888 /**
Winson Chung74666102017-02-22 17:49:24 -08002889 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2890 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002891 */
Winson Chung74666102017-02-22 17:49:24 -08002892 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002893 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002894
Winson Chung74666102017-02-22 17:49:24 -08002895 // Check that we aren't reparenting to the same stack that the task is already in
2896 if (prevStack != null && prevStack.mStackId == stackId) {
2897 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2898 + " already in stackId=" + stackId);
2899 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002900 }
2901
Winson Chung74666102017-02-22 17:49:24 -08002902 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2903 // support
2904 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2905 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2906 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002907 }
2908
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002909 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2910 // multi-display.
2911 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2912 // TODO: Check ActivityView after fixing b/35349678.
2913 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2914 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2915 + " reparent task=" + task + " to stackId=" + stackId);
2916 }
2917
Winson Chung74666102017-02-22 17:49:24 -08002918 // Ensure that we aren't trying to move into a freeform stack without freeform
2919 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002920 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002921 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2922 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002923 }
2924
Winson Chung74666102017-02-22 17:49:24 -08002925 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2926 // used for split-screen mode and will cause things like the docked divider to show up. We
2927 // instead leave the task in its current stack or move it to the fullscreen stack if it
2928 // isn't currently in a stack.
2929 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2930 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2931 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2932 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002933 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002934
Winson Chung74666102017-02-22 17:49:24 -08002935 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2936 // example, a docked stack is created which will lead to the stack we are moving from being
2937 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002938 try {
Winson Chung74666102017-02-22 17:49:24 -08002939 task.mTemporarilyUnresizable = true;
2940 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002941 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002942 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002943 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002944 }
2945
Winson Chung08f81892017-03-02 15:40:51 -08002946 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002947 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2948 if (stack == null) {
2949 throw new IllegalArgumentException(
2950 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2951 }
2952
2953 final ActivityRecord r = stack.topRunningActivityLocked();
2954 if (r == null) {
2955 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2956 + " in stack=" + stack);
2957 return false;
2958 }
2959
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002960 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002961 Slog.w(TAG,
2962 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002963 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002964 return false;
2965 }
2966
Winson Chung3a682872017-04-10 14:38:05 -07002967 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002968 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002969 return true;
2970 }
2971
Winson Chung8bca9e42017-04-16 15:59:43 -07002972 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002973 boolean moveHomeStackToFront, String reason) {
2974
Wale Ogunwale480dca02016-02-06 13:58:29 -08002975 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002976
Bryce Lee04ab3462017-04-10 15:06:33 -07002977 // This will clear the pinned stack by moving an existing task to the full screen stack,
2978 // ensuring only one task is present.
2979 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
2980
Wale Ogunwale1666e312016-12-16 11:27:18 -08002981 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002982 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002983
Wale Ogunwale480dca02016-02-06 13:58:29 -08002984 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002985 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002986
Andrii Kulian02b7a832016-10-06 23:11:56 -07002987 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002988 // An activity can't be pinned and visible behind at the same time. Go ahead and
2989 // release it from been visible behind before pinning.
2990 requestVisibleBehindLocked(r, false);
2991 }
2992
Wale Ogunwale480dca02016-02-06 13:58:29 -08002993 // Resize the pinned stack to match the current size of the task the activity we are
2994 // going to be moving is currently contained in. We do this to have the right starting
2995 // animation bounds for the pinned stack to the desired bounds the caller wants.
2996 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2997 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002998 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002999
3000 if (task.mActivities.size() == 1) {
3001 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003002 // the stack without re-parenting the activity in a different task. We don't
3003 // move the home stack forward if we are currently entering picture-in-picture
3004 // while pausing because that changes the focused stack and may prevent the new
3005 // starting activity from resuming.
3006 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08003007 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003008 // Move the home stack forward if the task we just moved to the pinned stack
3009 // was launched from home so home should be visible behind it.
3010 moveHomeStackToFront(reason);
3011 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003012 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003013 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003014 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003015 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003016 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003017 // reveal/leave the other activities in their original task.
3018
3019 // Currently, we don't support reparenting activities across tasks in two different
3020 // stacks, so instead, just create a new task in the same stack, reparent the
3021 // activity into that task, and then reparent the whole task to the new stack. This
3022 // ensures that all the necessary work to migrate states in the old and new stacks
3023 // is also done.
3024 final TaskRecord newTask = task.getStack().createTaskRecord(
3025 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
3026 r.mActivityType);
3027 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3028
Winson Chung5af42fc2017-03-24 17:11:33 -07003029 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003030 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003031 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003032 }
Winson Chungc2baac02017-01-11 13:34:47 -08003033
3034 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3035 // to the pinned stack
3036 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003037 } finally {
3038 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003039 }
3040
Winson Chunge7ba6862017-05-24 12:13:33 -07003041 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3042 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3043 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003044 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3045
Winson Chunge7ba6862017-05-24 12:13:33 -07003046 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3047 true /* fromFullscreen */);
3048
3049 // Update the visibility of all activities after the they have been reparented to the new
3050 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3051 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3052 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003053 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003054 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003055
Winson Chungb5026902017-05-03 12:45:13 -07003056 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName,
3057 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003058 }
3059
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003060 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003061 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3062 if (r == null || !r.isFocusable()) {
3063 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3064 "moveActivityStackToFront: unfocusable r=" + r);
3065 return false;
3066 }
3067
Bryce Leeaf691c02017-03-20 14:20:22 -07003068 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003069 final ActivityStack stack = r.getStack();
3070 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003071 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3072 + r + " task=" + task);
3073 return false;
3074 }
3075
Chong Zhang6cda19c2016-06-14 19:07:56 -07003076 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3077 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3078 "moveActivityStackToFront: already on top, r=" + r);
3079 return false;
3080 }
3081
3082 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3083 "moveActivityStackToFront: r=" + r);
3084
3085 stack.moveToFront(reason, task);
3086 return true;
3087 }
3088
David Stevensc6b91c62017-02-08 14:23:58 -08003089 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003090 mTmpFindTaskResult.r = null;
3091 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003092 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003093 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003094 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3095 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003096 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3097 final ActivityStack stack = stacks.get(stackNdx);
Winson Chung91e5c882017-04-21 14:44:38 -07003098 if (!checkActivityBelongsInStack(r, stack)) {
3099 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3100 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003101 continue;
3102 }
3103 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003104 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3105 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003106 continue;
3107 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003108 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003109 // It is possible to have tasks in multiple stacks with the same root affinity, so
3110 // we should keep looking after finding an affinity match to see if there is a
3111 // better match in another stack. Also, task affinity isn't a good enough reason
3112 // to target a display which isn't the source of the intent, so skip any affinity
3113 // matches not on the specified display.
3114 if (mTmpFindTaskResult.r != null) {
3115 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3116 return mTmpFindTaskResult.r;
3117 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003118 // Note: since the traversing through the stacks is top down, the floating
3119 // tasks should always have lower priority than any affinity-matching tasks
3120 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003121 affinityMatch = mTmpFindTaskResult.r;
3122 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003124 }
3125 }
Winson Chung5b895b72017-05-01 13:46:25 -07003126
David Stevensc6b91c62017-02-08 14:23:58 -08003127 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3128 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003129 }
3130
Winson Chung91e5c882017-04-21 14:44:38 -07003131 /**
3132 * Checks that for the given activity {@param r}, its activity type matches the {@param stack}
3133 * type.
3134 */
3135 private boolean checkActivityBelongsInStack(ActivityRecord r, ActivityStack stack) {
3136 if (r.isHomeActivity()) {
3137 return stack.isHomeStack();
3138 } else if (r.isRecentsActivity()) {
3139 return stack.isRecentsStack();
3140 } else if (r.isAssistantActivity()) {
3141 return stack.isAssistantStack();
3142 }
3143 return true;
3144 }
3145
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003146 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3147 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003148 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3149 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003150 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003151 final ActivityRecord ar = stacks.get(stackNdx)
3152 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003153 if (ar != null) {
3154 return ar;
3155 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003156 }
3157 }
3158 return null;
3159 }
3160
Craig Mautner8d341ef2013-03-26 09:03:27 -07003161 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003162 scheduleSleepTimeout();
3163 if (!mGoingToSleep.isHeld()) {
3164 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003165 if (mLaunchingActivity.isHeld()) {
3166 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3167 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003168 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003169 mLaunchingActivity.release();
3170 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003171 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003172 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003173 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003174 }
3175
3176 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003177 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003178
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003179 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003180 final long endTime = System.currentTimeMillis() + timeout;
3181 while (true) {
3182 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003183 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3184 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003185 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3186 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3187 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003188 }
3189 if (cantShutdown) {
3190 long timeRemaining = endTime - System.currentTimeMillis();
3191 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003192 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003193 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003194 } catch (InterruptedException e) {
3195 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003196 } else {
3197 Slog.w(TAG, "Activity manager shutdown timed out");
3198 timedout = true;
3199 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003200 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003201 } else {
3202 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003203 }
3204 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003205
3206 // Force checkReadyForSleep to complete.
3207 mSleepTimeout = true;
3208 checkReadyForSleepLocked();
3209
Craig Mautner8d341ef2013-03-26 09:03:27 -07003210 return timedout;
3211 }
3212
3213 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003214 removeSleepTimeouts();
3215 if (mGoingToSleep.isHeld()) {
3216 mGoingToSleep.release();
3217 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003218 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3219 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003220 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3221 final ActivityStack stack = stacks.get(stackNdx);
3222 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003223 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003224 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003225 }
Craig Mautner5314a402013-09-26 12:40:16 -07003226 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003227 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003228 mGoingToSleepActivities.clear();
3229 }
3230
3231 void activitySleptLocked(ActivityRecord r) {
3232 mGoingToSleepActivities.remove(r);
3233 checkReadyForSleepLocked();
3234 }
3235
3236 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003237 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003238 // Do not care.
3239 return;
3240 }
3241
3242 if (!mSleepTimeout) {
3243 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003244 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3245 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003246 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3247 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3248 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003249 }
3250
3251 if (mStoppingActivities.size() > 0) {
3252 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003253 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003254 + mStoppingActivities.size() + " activities");
3255 scheduleIdleLocked();
3256 dontSleep = true;
3257 }
3258
3259 if (mGoingToSleepActivities.size() > 0) {
3260 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003261 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003262 + mGoingToSleepActivities.size() + " activities");
3263 dontSleep = true;
3264 }
3265
3266 if (dontSleep) {
3267 return;
3268 }
3269 }
3270
Wei Wang65c7a152016-06-02 18:51:22 -07003271 // Send launch end powerhint before going sleep
3272 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3273
Craig Mautnere0a38842013-12-16 16:14:02 -08003274 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3275 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003276 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3277 stacks.get(stackNdx).goToSleep();
3278 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003279 }
3280
3281 removeSleepTimeouts();
3282
3283 if (mGoingToSleep.isHeld()) {
3284 mGoingToSleep.release();
3285 }
3286 if (mService.mShuttingDown) {
3287 mService.notifyAll();
3288 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003289 }
3290
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003291 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003292 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003293 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003294 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003295 }
3296 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003297 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003298 mWindowManager.executeAppTransition();
3299 return true;
3300 }
3301 return false;
3302 }
3303
Craig Mautner8d341ef2013-03-26 09:03:27 -07003304 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003305 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3306 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003307 int stackNdx = stacks.size() - 1;
3308 while (stackNdx >= 0) {
3309 stacks.get(stackNdx).handleAppCrashLocked(app);
3310 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003311 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003312 }
3313 }
3314
Jose Lima4b6c6692014-08-12 17:41:12 -07003315 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003316 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003317 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003318 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3319 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003320 return false;
3321 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003322
3323 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3324 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3325 + " visible=" + visible + " stackId=" + stack.mStackId
3326 + " can't contain visible behind activities");
3327 return false;
3328 }
3329
Jose Lima4b6c6692014-08-12 17:41:12 -07003330 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003331 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3332 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003333
3334 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003335 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003336 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003337 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003338 return true;
3339 }
3340
3341 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003342 if (visible && top.fullscreen) {
3343 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003344 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3345 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3346 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3347 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003348 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003349 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3350 // Only the activity set as currently visible behind should actively reset its
3351 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003352 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3353 "requestVisibleBehind: returning visible=" + visible
3354 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3355 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003356 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003357 }
3358
Jose Lima4b6c6692014-08-12 17:41:12 -07003359 stack.setVisibleBehindActivity(visible ? r : null);
3360 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003361 // If there is a translucent home activity, we need to force it stop being translucent,
3362 // because we can't depend on the application to necessarily perform that operation.
3363 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003364 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003365 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003366 mService.convertFromTranslucent(next.appToken);
3367 }
3368 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003369 if (top.app != null && top.app.thread != null) {
3370 // Notify the top app of the change.
3371 try {
3372 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3373 } catch (RemoteException e) {
3374 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003375 }
3376 return true;
3377 }
3378
Craig Mautnerbb742462014-07-07 15:28:55 -07003379 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003380 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003381 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003382 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003383
3384 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003385 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003386 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003387 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003388 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003389
3390 // When launching tasks behind, update the last active time of the top task after the new
3391 // task has been shown briefly
3392 final ActivityRecord top = stack.topActivity();
3393 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003394 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003395 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003396 }
3397
3398 void scheduleLaunchTaskBehindComplete(IBinder token) {
3399 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3400 }
3401
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003402 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3403 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003404 mKeyguardController.beginActivityVisibilityUpdate();
3405 try {
3406 // First the front stacks. In case any are not fullscreen and are in front of home.
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.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3413 }
Craig Mautner580ea812013-04-25 12:58:38 -07003414 }
Jorim Jaggife762342016-10-13 14:33:27 +02003415 } finally {
3416 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003417 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003418 }
3419
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003420 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3421 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3422 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3423 final int topStackNdx = stacks.size() - 1;
3424 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3425 final ActivityStack stack = stacks.get(stackNdx);
3426 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3427 }
3428 }
3429 }
3430
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003431 void invalidateTaskLayers() {
3432 mTaskLayersChanged = true;
3433 }
3434
3435 void rankTaskLayersIfNeeded() {
3436 if (!mTaskLayersChanged) {
3437 return;
3438 }
3439 mTaskLayersChanged = false;
3440 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3441 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3442 int baseLayer = 0;
3443 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3444 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3445 }
3446 }
3447 }
3448
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003449 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3450 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3451 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3452 final int topStackNdx = stacks.size() - 1;
3453 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3454 final ActivityStack stack = stacks.get(stackNdx);
3455 stack.clearOtherAppTimeTrackers(except);
3456 }
3457 }
3458 }
3459
Craig Mautner8d341ef2013-03-26 09:03:27 -07003460 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003461 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3462 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003463 final int numStacks = stacks.size();
3464 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3465 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003466 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003468 }
3469 }
3470
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003471 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3472 // Examine all activities currently running in the process.
3473 TaskRecord firstTask = null;
3474 // Tasks is non-null only if two or more tasks are found.
3475 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003476 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3477 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003478 ActivityRecord r = app.activities.get(i);
3479 // First, if we find an activity that is in the process of being destroyed,
3480 // then we just aren't going to do anything for now; we want things to settle
3481 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003482 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003483 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003484 return;
3485 }
3486 // Don't consider any activies that are currently not in a state where they
3487 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003488 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3489 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003490 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003491 continue;
3492 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003493
3494 final TaskRecord task = r.getTask();
3495 if (task != null) {
3496 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003497 + " from " + r);
3498 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003499 firstTask = task;
3500 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003501 if (tasks == null) {
3502 tasks = new ArraySet<>();
3503 tasks.add(firstTask);
3504 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003505 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003506 }
3507 }
3508 }
3509 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003510 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003511 return;
3512 }
3513 // If we have activities in multiple tasks that are in a position to be destroyed,
3514 // let's iterate through the tasks and release the oldest one.
3515 final int numDisplays = mActivityDisplays.size();
3516 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3517 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3518 // Step through all stacks starting from behind, to hit the oldest things first.
3519 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3520 final ActivityStack stack = stacks.get(stackNdx);
3521 // Try to release activities in this stack; if we manage to, we are done.
3522 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3523 return;
3524 }
3525 }
3526 }
3527 }
3528
Amith Yamasani37a40c22015-06-17 13:25:42 -07003529 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003530 final int focusStackId = mFocusedStack.getStackId();
3531 // We dismiss the docked stack whenever we switch users.
3532 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003533 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3534 // also cause all tasks to be moved to the fullscreen stack at a position that is
3535 // appropriate.
3536 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003537
3538 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003539 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003540 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003541
Craig Mautner858d8a62013-04-23 17:08:34 -07003542 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3544 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003546 final ActivityStack stack = stacks.get(stackNdx);
3547 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003548 TaskRecord task = stack.topTask();
3549 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003550 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003551 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003552 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003553 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003554
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003555 ActivityStack stack = getStack(restoreStackId);
3556 if (stack == null) {
3557 stack = mHomeStack;
3558 }
3559 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003560 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003561 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003562 } else {
3563 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003564 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003565 }
Craig Mautner93529a42013-10-04 15:03:13 -07003566 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003567 }
3568
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003569 /** Checks whether the userid is a profile of the current user. */
3570 boolean isCurrentProfileLocked(int userId) {
3571 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003572 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003573 }
3574
Bryce Leeb7c9b802017-05-02 14:20:24 -07003575 /**
3576 * Returns whether a stopping activity is present that should be stopped after visible, rather
3577 * than idle.
3578 * @return {@code true} if such activity is present. {@code false} otherwise.
3579 */
3580 boolean isStoppingNoHistoryActivity() {
3581 // Activities that are marked as nohistory should be stopped immediately after the resumed
3582 // activity has become visible.
3583 for (ActivityRecord record : mStoppingActivities) {
3584 if (record.isNoHistory()) {
3585 return true;
3586 }
3587 }
3588
3589 return false;
3590 }
3591
Winson Chung4dabf232017-01-25 13:25:22 -08003592 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3593 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003594 ArrayList<ActivityRecord> stops = null;
3595
3596 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003597 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3598 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003599 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003600 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003601 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3602 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003603 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003604 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003605 if (s.finishing) {
3606 // If this activity is finishing, it is sitting on top of
3607 // everyone else but we now know it is no longer needed...
3608 // so get rid of it. Otherwise, we need to go through the
3609 // normal flow and hide it once we determine that it is
3610 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003611 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003612 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003613 }
3614 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003615 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003616 if (!processPausingActivities && s.state == PAUSING) {
3617 // Defer processing pausing activities in this iteration and reschedule
3618 // a delayed idle to reprocess it again
3619 removeTimeoutsForActivityLocked(idleActivity);
3620 scheduleIdleTimeoutLocked(idleActivity);
3621 continue;
3622 }
3623
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003624 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003625 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003626 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003627 }
3628 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003629 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003630 }
3631 }
3632
3633 return stops;
3634 }
3635
Craig Mautnercf910b02013-04-23 11:23:27 -07003636 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003637 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3638 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3640 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003641 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003642 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003643 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003644 if (r == null) Slog.e(TAG,
3645 "validateTop...: null top activity, stack=" + stack);
3646 else {
3647 final ActivityRecord pausing = stack.mPausingActivity;
3648 if (pausing != null && pausing == r) Slog.e(TAG,
3649 "validateTop...: top stack has pausing activity r=" + r
3650 + " state=" + state);
3651 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3652 "validateTop...: activity in front not resumed r=" + r
3653 + " state=" + state);
3654 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003655 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003656 final ActivityRecord resumed = stack.mResumedActivity;
3657 if (resumed != null && resumed == r) Slog.e(TAG,
3658 "validateTop...: back stack has resumed activity r=" + r
3659 + " state=" + state);
3660 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3661 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003662 }
3663 }
3664 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003665 }
3666
Craig Mautnere0570202015-05-13 13:06:11 -07003667 private String lockTaskModeToString() {
3668 switch (mLockTaskModeState) {
3669 case LOCK_TASK_MODE_LOCKED:
3670 return "LOCKED";
3671 case LOCK_TASK_MODE_PINNED:
3672 return "PINNED";
3673 case LOCK_TASK_MODE_NONE:
3674 return "NONE";
3675 default: return "unknown=" + mLockTaskModeState;
3676 }
3677 }
3678
Craig Mautner27084302013-03-25 08:05:25 -07003679 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003680 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003682 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003683 pw.print(prefix);
3684 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003685 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003686 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003687 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003688 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3689 if (packages.size() > 0) {
3690 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3691 for (int i = 0; i < packages.size(); ++i) {
3692 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3693 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3694 }
3695 }
Bryce Lee4a194382017-04-04 14:32:48 -07003696 if (!mWaitingForActivityVisible.isEmpty()) {
3697 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3698 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3699 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3700 }
3701 }
3702
Jorim Jaggi8d786932016-10-26 19:08:36 -07003703 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3704 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003705 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003706
Winson43d1f262016-06-14 16:05:55 -07003707 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003708 * Dump all connected displays' configurations.
3709 * @param prefix Prefix to apply to each line of the dump.
3710 */
3711 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3712 pw.print(prefix); pw.println("Display override configurations:");
3713 final int displayCount = mActivityDisplays.size();
3714 for (int i = 0; i < displayCount; i++) {
3715 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3716 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3717 pw.println(activityDisplay.getOverrideConfiguration());
3718 }
3719 }
3720
3721 /**
Winson43d1f262016-06-14 16:05:55 -07003722 * Dumps the activities matching the given {@param name} in the either the focused stack
3723 * or all visible stacks if {@param dumpVisibleStacks} is true.
3724 */
Winson Chung6998bc42017-02-28 17:07:05 -08003725 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3726 boolean dumpFocusedStackOnly) {
3727 if (dumpFocusedStackOnly) {
3728 return mFocusedStack.getDumpActivitiesLocked(name);
3729 } else {
Winson43d1f262016-06-14 16:05:55 -07003730 ArrayList<ActivityRecord> activities = new ArrayList<>();
3731 int numDisplays = mActivityDisplays.size();
3732 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3733 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3734 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3735 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003736 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003737 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003738 activities.addAll(stack.getDumpActivitiesLocked(name));
3739 }
3740 }
3741 }
3742 return activities;
Winson43d1f262016-06-14 16:05:55 -07003743 }
Craig Mautner20e72272013-04-01 13:45:53 -07003744 }
3745
Dianne Hackborn390517b2013-05-30 15:03:32 -07003746 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3747 boolean needSep, String prefix) {
3748 if (activity != null) {
3749 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3750 if (needSep) {
3751 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003752 }
3753 pw.print(prefix);
3754 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003755 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003756 }
3757 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003758 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003759 }
3760
Craig Mautner8d341ef2013-03-26 09:03:27 -07003761 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3762 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003763 boolean printed = false;
3764 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003765 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3766 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003767 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003768 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003769 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003770 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 final ActivityStack stack = stacks.get(stackNdx);
3772 StringBuilder stackHeader = new StringBuilder(128);
3773 stackHeader.append(" Stack #");
3774 stackHeader.append(stack.mStackId);
3775 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003776 stackHeader.append("\n");
3777 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3778 stackHeader.append("\n");
3779 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003780
3781 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3782 dumpClient, dumpPackage, needSep, stackHeader.toString());
3783 printed |= printedStackHeader;
3784 if (!printedStackHeader) {
3785 // Ensure we always dump the stack header even if there are no activities
3786 pw.println();
3787 pw.println(stackHeader);
3788 }
3789
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3791 !dumpAll, false, dumpPackage, true,
3792 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003793
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 needSep = printed;
3795 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3796 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003797 if (pr) {
3798 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003799 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003800 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3802 " mResumedActivity: ");
3803 if (pr) {
3804 printed = true;
3805 needSep = false;
3806 }
3807 if (dumpAll) {
3808 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3809 " mLastPausedActivity: ");
3810 if (pr) {
3811 printed = true;
3812 needSep = true;
3813 }
3814 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3815 needSep, " mLastNoHistoryActivity: ");
3816 }
3817 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003818 }
3819 }
3820
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003821 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3822 false, dumpPackage, true, " Activities waiting to finish:", null);
3823 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3824 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003825 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3826 false, !dumpAll, false, dumpPackage, true,
3827 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003828 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3829 false, dumpPackage, true, " Activities waiting to sleep:", null);
3830 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3831 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003832
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003833 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003834 }
3835
Dianne Hackborn390517b2013-05-30 15:03:32 -07003836 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003837 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003838 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003839 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003840 String innerPrefix = null;
3841 String[] args = null;
3842 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003843 for (int i=list.size()-1; i>=0; i--) {
3844 final ActivityRecord r = list.get(i);
3845 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3846 continue;
3847 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003848 if (innerPrefix == null) {
3849 innerPrefix = prefix + " ";
3850 args = new String[0];
3851 }
3852 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003853 final boolean full = !brief && (complete || !r.isInHistory());
3854 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003855 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003856 needNL = false;
3857 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003858 if (header1 != null) {
3859 pw.println(header1);
3860 header1 = null;
3861 }
3862 if (header2 != null) {
3863 pw.println(header2);
3864 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003865 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003866 if (lastTask != r.getTask()) {
3867 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003868 pw.print(prefix);
3869 pw.print(full ? "* " : " ");
3870 pw.println(lastTask);
3871 if (full) {
3872 lastTask.dump(pw, prefix + " ");
3873 } else if (complete) {
3874 // Complete + brief == give a summary. Isn't that obvious?!?
3875 if (lastTask.intent != null) {
3876 pw.print(prefix); pw.print(" ");
3877 pw.println(lastTask.intent.toInsecureStringWithClip());
3878 }
3879 }
3880 }
3881 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3882 pw.print(" #"); pw.print(i); pw.print(": ");
3883 pw.println(r);
3884 if (full) {
3885 r.dump(pw, innerPrefix);
3886 } else if (complete) {
3887 // Complete + brief == give a summary. Isn't that obvious?!?
3888 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3889 if (r.app != null) {
3890 pw.print(innerPrefix); pw.println(r.app);
3891 }
3892 }
3893 if (client && r.app != null && r.app.thread != null) {
3894 // flush anything that is already in the PrintWriter since the thread is going
3895 // to write to the file descriptor directly
3896 pw.flush();
3897 try {
3898 TransferPipe tp = new TransferPipe();
3899 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003900 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003901 // Short timeout, since blocking here can
3902 // deadlock with the application.
3903 tp.go(fd, 2000);
3904 } finally {
3905 tp.kill();
3906 }
3907 } catch (IOException e) {
3908 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3909 } catch (RemoteException e) {
3910 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3911 }
3912 needNL = true;
3913 }
3914 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003915 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003916 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003917
Craig Mautnerf3333272013-04-22 10:55:53 -07003918 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003919 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3920 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003921 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3922 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003923 }
3924
3925 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003926 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003927 }
3928
3929 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003930 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3931 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003932 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3933 }
3934
Craig Mautner05d29032013-05-03 13:40:13 -07003935 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003936 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3937 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3938 }
Craig Mautner05d29032013-05-03 13:40:13 -07003939 }
3940
Craig Mautner0eea92c2013-05-16 13:35:39 -07003941 void removeSleepTimeouts() {
3942 mSleepTimeout = false;
3943 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3944 }
3945
3946 final void scheduleSleepTimeout() {
3947 removeSleepTimeouts();
3948 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3949 }
3950
Craig Mautner4a1cb222013-12-04 16:14:06 -08003951 @Override
3952 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003953 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003954 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3955 }
3956
3957 @Override
3958 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003959 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003960 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3961 }
3962
3963 @Override
3964 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003965 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003966 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3967 }
3968
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003969 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003970 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003971 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003972 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003973 }
3974
Andrii Kulianca007a62016-11-22 16:33:28 -08003975 /** Check if display with specified id is added to the list. */
3976 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003977 return getActivityDisplayOrCreateLocked(displayId) != null;
3978 }
3979
3980 /**
3981 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3982 * corresponding record in display manager.
3983 */
3984 private ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
3985 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3986 if (activityDisplay != null) {
3987 return activityDisplay;
3988 }
3989 if (mDisplayManager == null) {
3990 // The system isn't fully initialized yet.
3991 return null;
3992 }
3993 final Display display = mDisplayManager.getDisplay(displayId);
3994 if (display == null) {
3995 // The display is not registered in DisplayManager.
3996 return null;
3997 }
3998 // The display hasn't been added to ActivityManager yet, create a new record now.
3999 activityDisplay = new ActivityDisplay(displayId);
4000 if (activityDisplay.mDisplay == null) {
4001 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4002 return null;
4003 }
4004 mActivityDisplays.put(displayId, activityDisplay);
4005 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4006 mWindowManager.onDisplayAdded(displayId);
4007 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004008 }
4009
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004010 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004011 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004012 mService.mContext.getResources().getDimensionPixelSize(
4013 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004014 }
4015
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004016 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004017 if (displayId == DEFAULT_DISPLAY) {
4018 throw new IllegalArgumentException("Can't remove the primary display.");
4019 }
4020
Craig Mautner4a1cb222013-12-04 16:14:06 -08004021 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004022 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4023 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08004024 final boolean destroyContentOnRemoval
4025 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08004026 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
4027 while (!stacks.isEmpty()) {
4028 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08004029 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004030 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
4031 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004032 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08004033 } else {
4034 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4035 // a valid launch stack for all activities. This way the task history from
4036 // external display will be preserved on primary after move.
4037 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004038 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004039 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004040 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07004041 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004042 }
4043 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004044 }
4045
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004046 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004048 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4049 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 // TODO: Update the bounds.
4051 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004052 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004054 }
4055
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004056 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004057 final int displayId = stack.mDisplayId;
4058 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004060 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004061 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004063 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004064 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Andrii Kulian62e6f252017-05-30 22:46:53 -07004065 // A stack might be not attached to a display.
Winson529c8e42016-05-17 11:08:40 -07004066 info.position = display != null
4067 ? display.mStacks.indexOf(stack)
4068 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004069
4070 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4071 final int numTasks = tasks.size();
4072 int[] taskIds = new int[numTasks];
4073 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004074 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004075 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004076 for (int i = 0; i < numTasks; ++i) {
4077 final TaskRecord task = tasks.get(i);
4078 taskIds[i] = task.taskId;
4079 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4080 : task.realActivity != null ? task.realActivity.flattenToString()
4081 : task.getTopActivity() != null ? task.getTopActivity().packageName
4082 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004083 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004084 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004085 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004086 }
4087 info.taskIds = taskIds;
4088 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004089 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004090 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004091
4092 final ActivityRecord top = stack.topRunningActivityLocked();
4093 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004094 return info;
4095 }
4096
4097 StackInfo getStackInfoLocked(int stackId) {
4098 ActivityStack stack = getStack(stackId);
4099 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004100 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004101 }
4102 return null;
4103 }
4104
4105 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004106 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004107 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4108 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004109 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004110 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004111 }
4112 }
4113 return list;
4114 }
4115
Craig Mautner15df08a2015-04-01 12:17:18 -07004116 TaskRecord getLockedTaskLocked() {
4117 final int top = mLockTaskModeTasks.size() - 1;
4118 if (top >= 0) {
4119 return mLockTaskModeTasks.get(top);
4120 }
4121 return null;
4122 }
4123
4124 boolean isLockedTask(TaskRecord task) {
4125 return mLockTaskModeTasks.contains(task);
4126 }
4127
4128 boolean isLastLockedTask(TaskRecord task) {
4129 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4130 }
4131
4132 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004133 if (!mLockTaskModeTasks.remove(task)) {
4134 return;
4135 }
4136 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4137 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004138 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004139 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4140 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004141 final Message lockTaskMsg = Message.obtain();
4142 lockTaskMsg.arg1 = task.userId;
4143 lockTaskMsg.what = LOCK_TASK_END_MSG;
4144 mHandler.sendMessage(lockTaskMsg);
4145 }
4146 }
4147
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004148 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4149 int preferredDisplayId, int actualStackId) {
4150 handleNonResizableTaskIfNeeded(task, preferredStackId, preferredDisplayId, actualStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07004151 false /* forceNonResizable */);
4152 }
4153
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004154 private void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4155 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4156 final boolean isSecondaryDisplayPreferred =
4157 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY)
4158 || StackId.isDynamicStack(preferredStackId);
4159 if (((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
4160 && !isSecondaryDisplayPreferred) || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004161 return;
4162 }
4163
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004164 // Handle incorrect launch/move to secondary display if needed.
4165 final boolean launchOnSecondaryDisplayFailed;
4166 if (isSecondaryDisplayPreferred) {
4167 final int actualDisplayId = task.getStack().mDisplayId;
4168 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4169 // The task landed on an inappropriate display somehow, move it to the default
4170 // display.
4171 // TODO(multi-display): Find proper stack for the task on the default display.
4172 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4173 true /* toTop */);
4174 launchOnSecondaryDisplayFailed = true;
4175 } else {
4176 // The task might have landed on a display different from requested.
4177 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4178 || (preferredDisplayId != INVALID_DISPLAY
4179 && preferredDisplayId != actualDisplayId);
4180 }
4181 } else {
4182 // The task wasn't requested to be on a secondary display.
4183 launchOnSecondaryDisplayFailed = false;
4184 }
4185
Jorim Jaggicd13d332016-04-27 15:40:20 -07004186 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004187 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4188 if (launchOnSecondaryDisplayFailed) {
4189 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4190 // display with config different from global config.
4191 mService.mTaskChangeNotificationController
4192 .notifyActivityLaunchOnSecondaryDisplayFailed();
4193 } else {
4194 // Display a warning toast that we tried to put a non-dockable task in the docked
4195 // stack.
4196 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4197 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004198
Andrii Kulianc27916642016-04-12 17:59:27 -07004199 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4200 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004201 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004202 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004203 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004204 final String packageName = topActivity.appInfo.packageName;
4205 final int reason = isSecondaryDisplayPreferred
4206 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4207 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004208 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004209 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004210 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004211 }
4212
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004213 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004214 if (mLockTaskNotify != null) {
4215 mLockTaskNotify.showToast(mLockTaskModeState);
4216 }
Jason Monka8f569c2014-07-07 12:15:21 -04004217 }
4218
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004219 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4220 if (mLockTaskModeTasks.contains(task)) {
4221 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4222 }
4223 }
4224
Craig Mautner432f64e2015-05-20 14:59:57 -07004225 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4226 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004227 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004228 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004229 final TaskRecord lockedTask = getLockedTaskLocked();
4230 if (lockedTask != null) {
4231 removeLockedTaskLocked(lockedTask);
4232 if (!mLockTaskModeTasks.isEmpty()) {
4233 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004234 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4235 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004236 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004237 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004238 return;
4239 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004240 }
Craig Mautnere0570202015-05-13 13:06:11 -07004241 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4242 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004243 return;
4244 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004245
4246 // Should have already been checked, but do it again.
4247 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004248 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4249 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004250 return;
4251 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004252 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004253 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004254 return;
4255 }
4256
4257 if (mLockTaskModeTasks.isEmpty()) {
4258 // First locktask.
4259 final Message lockTaskMsg = Message.obtain();
4260 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4261 lockTaskMsg.arg1 = task.userId;
4262 lockTaskMsg.what = LOCK_TASK_START_MSG;
4263 lockTaskMsg.arg2 = lockTaskModeState;
4264 mHandler.sendMessage(lockTaskMsg);
4265 }
4266 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004267 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4268 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004269 mLockTaskModeTasks.remove(task);
4270 mLockTaskModeTasks.add(task);
4271
4272 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004273 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004274 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004275
4276 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004277 findTaskToMoveToFrontLocked(task, 0, null, reason,
4278 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004279 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004280 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004281 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004282 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
4283 task.getStackId(), true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004284 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004285 }
4286
4287 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004288 return isLockTaskModeViolation(task, false);
4289 }
4290
4291 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4292 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004293 return false;
4294 }
4295 final int lockTaskAuth = task.mLockTaskAuth;
4296 switch (lockTaskAuth) {
4297 case LOCK_TASK_AUTH_DONT_LOCK:
4298 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004299 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004300 case LOCK_TASK_AUTH_LAUNCHABLE:
4301 case LOCK_TASK_AUTH_WHITELISTED:
4302 return false;
4303 case LOCK_TASK_AUTH_PINNABLE:
4304 // Pinnable tasks can't be launched on top of locktask tasks.
4305 return !mLockTaskModeTasks.isEmpty();
4306 default:
4307 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4308 return true;
4309 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004310 }
4311
Craig Mautner15df08a2015-04-01 12:17:18 -07004312 void onLockTaskPackagesUpdatedLocked() {
4313 boolean didSomething = false;
4314 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4315 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004316 final boolean wasWhitelisted =
4317 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4318 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004319 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004320 final boolean isWhitelisted =
4321 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4322 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4323 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004324 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004325 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4326 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004327 removeLockedTaskLocked(lockedTask);
4328 lockedTask.performClearTaskLocked();
4329 didSomething = true;
4330 }
4331 }
4332 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4333 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4334 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4335 final ActivityStack stack = stacks.get(stackNdx);
4336 stack.onLockTaskPackagesUpdatedLocked();
4337 }
4338 }
Craig Mautnere0570202015-05-13 13:06:11 -07004339 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004340 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004341 if (mLockTaskModeTasks.isEmpty() && task != null
4342 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4343 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004344 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4345 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4346 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4347 false);
4348 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004349 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004350 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004351 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004352 }
4353 }
4354
Benjamin Franz43261142015-02-11 15:59:44 +00004355 int getLockTaskModeState() {
4356 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004357 }
4358
Jorim Jaggife89d122015-12-22 16:28:44 +01004359 void activityRelaunchedLocked(IBinder token) {
4360 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004361 if (mService.isSleepingOrShuttingDownLocked()) {
4362 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4363 if (r != null) {
4364 r.setSleeping(true, true);
4365 }
4366 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004367 }
4368
4369 void activityRelaunchingLocked(ActivityRecord r) {
4370 mWindowManager.notifyAppRelaunching(r.appToken);
4371 }
4372
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004373 void logStackState() {
4374 mActivityMetricsLogger.logWindowState();
4375 }
4376
Winson Chung5af42fc2017-03-24 17:11:33 -07004377 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004378 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4379 // end, then ensure that we defer all in between multi-window mode changes
4380 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4381 return;
4382 }
4383
Wale Ogunwale22e25262016-02-01 10:32:02 -08004384 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4385 final ActivityRecord r = task.mActivities.get(i);
4386 if (r.app != null && r.app.thread != null) {
4387 mMultiWindowModeChangedActivities.add(r);
4388 }
4389 }
4390
4391 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4392 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4393 }
4394 }
4395
Winson Chung5af42fc2017-03-24 17:11:33 -07004396 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004397 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004398 if (prevStack == null || prevStack == stack
4399 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4400 return;
4401 }
4402
Winson Chung5af42fc2017-03-24 17:11:33 -07004403 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4404 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004405
Winson Chung5af42fc2017-03-24 17:11:33 -07004406 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4407 boolean immediate) {
4408
4409 if (immediate) {
4410 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4411 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4412 final ActivityRecord r = task.mActivities.get(i);
4413 if (r.app != null && r.app.thread != null) {
4414 r.updatePictureInPictureMode(targetStackBounds);
4415 }
4416 }
4417 } else {
4418 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4419 final ActivityRecord r = task.mActivities.get(i);
4420 if (r.app != null && r.app.thread != null) {
4421 mPipModeChangedActivities.add(r);
4422 }
4423 }
4424 mPipModeChangedTargetStackBounds = targetStackBounds;
4425
4426 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4427 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4428 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004429 }
4430 }
4431
Tony Mak853304c2016-04-18 15:17:41 +01004432 void setDockedStackMinimized(boolean minimized) {
4433 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004434 }
4435
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004436 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004437
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004438 public ActivityStackSupervisorHandler(Looper looper) {
4439 super(looper);
4440 }
4441
Winson Chung4dabf232017-01-25 13:25:22 -08004442 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004443 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004444 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4445 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004446 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004447 }
4448
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004449 @Override
4450 public void handleMessage(Message msg) {
4451 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004452 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4453 synchronized (mService) {
4454 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4455 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004456 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004457 }
4458 }
4459 } break;
4460 case REPORT_PIP_MODE_CHANGED_MSG: {
4461 synchronized (mService) {
4462 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4463 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004464 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004465 }
4466 }
4467 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004468 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004469 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4470 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004471 // We don't at this point know if the activity is fullscreen,
4472 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004473 activityIdleInternal((ActivityRecord) msg.obj,
4474 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004475 } break;
4476 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004477 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004478 activityIdleInternal((ActivityRecord) msg.obj,
4479 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004480 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004481 case RESUME_TOP_ACTIVITY_MSG: {
4482 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004483 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004484 }
4485 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004486 case SLEEP_TIMEOUT_MSG: {
4487 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004488 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004489 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4490 mSleepTimeout = true;
4491 checkReadyForSleepLocked();
4492 }
4493 }
4494 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004495 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004496 synchronized (mService) {
4497 if (mLaunchingActivity.isHeld()) {
4498 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4499 if (VALIDATE_WAKE_LOCK_CALLER
4500 && Binder.getCallingUid() != Process.myUid()) {
4501 throw new IllegalStateException("Calling must be system uid");
4502 }
4503 mLaunchingActivity.release();
4504 }
4505 }
4506 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004507 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004508 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004509 } break;
4510 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004511 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004512 } break;
4513 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004514 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004515 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004516 case CONTAINER_CALLBACK_VISIBILITY: {
4517 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004518 final IActivityContainerCallback callback = container.mCallback;
4519 if (callback != null) {
4520 try {
4521 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4522 } catch (RemoteException e) {
4523 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004524 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004525 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004526 case LOCK_TASK_START_MSG: {
4527 // When lock task starts, we disable the status bars.
4528 try {
Jason Monk62515be2014-05-21 16:06:19 -04004529 if (mLockTaskNotify == null) {
4530 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004531 }
Jason Monk62515be2014-05-21 16:06:19 -04004532 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004533 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004534 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004535 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004536 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004537 flags = StatusBarManager.DISABLE_MASK
4538 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004539 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004540 flags = StatusBarManager.DISABLE_MASK
4541 & (~StatusBarManager.DISABLE_BACK)
4542 & (~StatusBarManager.DISABLE_HOME)
4543 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004544 }
4545 getStatusBarService().disable(flags, mToken,
4546 mService.mContext.getPackageName());
4547 }
4548 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004549 if (getDevicePolicyManager() != null) {
4550 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004551 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004552 }
justinzhang5286d3f2014-05-12 17:06:01 -04004553 } catch (RemoteException ex) {
4554 throw new RuntimeException(ex);
4555 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004556 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004557 case LOCK_TASK_END_MSG: {
4558 // When lock task ends, we enable the status bars.
4559 try {
Jason Monk62515be2014-05-21 16:06:19 -04004560 if (getStatusBarService() != null) {
4561 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4562 mService.mContext.getPackageName());
4563 }
4564 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004565 if (getDevicePolicyManager() != null) {
4566 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4567 msg.arg1);
4568 }
Jason Monk62515be2014-05-21 16:06:19 -04004569 if (mLockTaskNotify == null) {
4570 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4571 }
4572 mLockTaskNotify.show(false);
4573 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004574 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004575 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004576 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004577 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004578 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004579 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004580 new LockPatternUtils(mService.mContext)
4581 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004582 }
4583 } catch (SettingNotFoundException e) {
4584 // No setting, don't lock.
4585 }
justinzhang5286d3f2014-05-12 17:06:01 -04004586 } catch (RemoteException ex) {
4587 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004588 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004589 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004590 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004591 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004592 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4593 if (mLockTaskNotify == null) {
4594 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4595 }
4596 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4597 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004598 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4599 final ActivityContainer container = (ActivityContainer) msg.obj;
4600 final IActivityContainerCallback callback = container.mCallback;
4601 if (callback != null) {
4602 try {
4603 callback.onAllActivitiesComplete(container.asBinder());
4604 } catch (RemoteException e) {
4605 }
4606 }
4607 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004608 case LAUNCH_TASK_BEHIND_COMPLETE: {
4609 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004610 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004611 if (r != null) {
4612 handleLaunchTaskBehindCompleteLocked(r);
4613 }
4614 }
4615 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004616
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004617 }
4618 }
4619 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004620
Ying Wangb081a592014-04-22 15:20:16 -07004621 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004622 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4623 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004624 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004625 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004626 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004627 ActivityRecord mParentActivity = null;
4628 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004629
Craig Mautnere3a00d72014-04-16 08:31:19 -07004630 boolean mVisible = true;
4631
Craig Mautner4a1cb222013-12-04 16:14:06 -08004632 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004633 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004634
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004635 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4636 final static int CONTAINER_STATE_NO_SURFACE = 1;
4637 final static int CONTAINER_STATE_FINISHING = 2;
4638 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4639
Wale Ogunwale1666e312016-12-16 11:27:18 -08004640 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004641 synchronized (mService) {
4642 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004643 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004644 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004645
4646 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004647 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004648 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004649 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004650
Bryce Leeaf691c02017-03-20 14:20:22 -07004651 protected void createStack(int stackId, boolean onTop) {
4652 switch (stackId) {
4653 case PINNED_STACK_ID:
4654 new PinnedActivityStack(this, mRecentTasks, onTop);
4655 break;
4656 default:
4657 new ActivityStack(this, mRecentTasks, onTop);
4658 break;
4659 }
4660 }
4661
Andrii Kulian839def92016-11-02 10:58:58 -07004662 /**
4663 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004664 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004665 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004666 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004667 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004668 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004669 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004670 if (mActivityDisplay != null) {
4671 throw new IllegalStateException("ActivityContainer is already attached, " +
4672 "displayId=" + mActivityDisplay.mDisplayId);
4673 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004674 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004675 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004676 }
4677
4678 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004679 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004680 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004681 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004682 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004683 return;
4684 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004685 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004686 }
4687 }
4688
4689 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004690 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004691 synchronized (mService) {
4692 if (mActivityDisplay != null) {
4693 return mActivityDisplay.mDisplayId;
4694 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004695 }
4696 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004697 }
4698
Jeff Brownca9bc702014-02-11 14:32:56 -08004699 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004700 public int getStackId() {
4701 synchronized (mService) {
4702 return mStackId;
4703 }
4704 }
4705
4706 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004707 public boolean injectEvent(InputEvent event) {
4708 final long origId = Binder.clearCallingIdentity();
4709 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004710 synchronized (mService) {
4711 if (mActivityDisplay != null) {
4712 return mInputManagerInternal.injectInputEvent(event,
4713 mActivityDisplay.mDisplayId,
4714 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4715 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004716 }
4717 return false;
4718 } finally {
4719 Binder.restoreCallingIdentity(origId);
4720 }
4721 }
4722
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004723 @Override
4724 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004725 synchronized (mService) {
4726 if (mContainerState == CONTAINER_STATE_FINISHING) {
4727 return;
4728 }
4729 mContainerState = CONTAINER_STATE_FINISHING;
4730
Craig Mautnerd163e752014-06-13 17:18:47 -07004731 long origId = Binder.clearCallingIdentity();
4732 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004733 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004734 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004735 } finally {
4736 Binder.restoreCallingIdentity(origId);
4737 }
4738 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004739 }
4740
Andrii Kulian03c403d2016-11-11 11:14:12 -08004741 /**
4742 * Remove the stack completely. Must be called only when there are no tasks left in it,
4743 * as this method does not finish running activities.
4744 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004745 void removeLocked() {
4746 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004747 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004748 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004749 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004750 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004751 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004752 }
4753
Andrii Kulian839def92016-11-02 10:58:58 -07004754 /**
4755 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4756 * completely or re-parented.
4757 */
4758 private void removeFromDisplayLocked() {
4759 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4760 + " current displayId=" + mActivityDisplay.mDisplayId);
4761
Wale Ogunwale1666e312016-12-16 11:27:18 -08004762 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004763 mActivityDisplay = null;
4764 }
4765
4766 /**
4767 * Move the stack to specified display.
4768 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004769 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004770 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004771 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004772 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4773 + mActivityDisplay + " to display=" + activityDisplay
4774 + " Callers=" + Debug.getCallers(2));
4775
4776 removeFromDisplayLocked();
4777
4778 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004779 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004780 }
4781
Craig Mautner4a1cb222013-12-04 16:14:06 -08004782 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004783 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004784 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004785 }
4786
4787 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004788 public final int startActivityIntentSender(IIntentSender intentSender)
4789 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004790 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4791
4792 if (!(intentSender instanceof PendingIntentRecord)) {
4793 throw new IllegalArgumentException("Bad PendingIntent object");
4794 }
4795
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004796 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004797 Binder.getCallingUid(), mCurrentUser, false,
4798 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004799
4800 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4801 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4802 pendingIntent.key.requestResolvedType);
4803
Dianne Hackborn98305522017-05-05 17:53:53 -07004804 return pendingIntent.sendInner(0, null, null, null, null, null, null, null, 0,
Craig Mautnerb9168362015-02-26 20:40:19 -08004805 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4806 }
4807
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004808 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004809 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004810 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004811 throw new SecurityException(
4812 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4813 }
4814 }
4815
Craig Mautnerdf88d732014-01-27 09:21:32 -08004816 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004817 public IBinder asBinder() {
4818 return this;
4819 }
4820
Craig Mautner4504de52013-12-20 09:06:56 -08004821 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004822 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004823 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004824 }
4825
Craig Mautner4a1cb222013-12-04 16:14:06 -08004826 ActivityStackSupervisor getOuter() {
4827 return ActivityStackSupervisor.this;
4828 }
4829
Craig Mautnerd163e752014-06-13 17:18:47 -07004830 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004831 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004832 }
4833
Craig Mautner6985bad2014-04-21 15:22:06 -07004834 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004835 void setVisible(boolean visible) {
4836 if (mVisible != visible) {
4837 mVisible = visible;
4838 if (mCallback != null) {
4839 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4840 0 /* unused */, this).sendToTarget();
4841 }
4842 }
4843 }
4844
Craig Mautner6985bad2014-04-21 15:22:06 -07004845 void setDrawn() {
4846 }
4847
Craig Mautner1b4bf852014-05-26 15:06:32 -07004848 // You can always start a new task on a regular ActivityStack.
4849 boolean isEligibleForNewTasks() {
4850 return true;
4851 }
4852
Craig Mautnerd163e752014-06-13 17:18:47 -07004853 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004854 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004855 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004856 }
4857
Craig Mautner34b73df2014-01-12 21:11:08 -08004858 @Override
4859 public String toString() {
4860 return mIdString + (mActivityDisplay == null ? "N" : "A");
4861 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004862 }
4863
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004864 private class VirtualActivityContainer extends ActivityContainer {
4865 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004866 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004867
4868 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004869 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4870 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004871 mParentActivity = parent;
4872 mCallback = callback;
4873 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004874 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004875 }
4876
4877 @Override
4878 public void setSurface(Surface surface, int width, int height, int density) {
4879 super.setSurface(surface, width, height, density);
4880
4881 synchronized (mService) {
4882 final long origId = Binder.clearCallingIdentity();
4883 try {
4884 setSurfaceLocked(surface, width, height, density);
4885 } finally {
4886 Binder.restoreCallingIdentity(origId);
4887 }
4888 }
4889 }
4890
4891 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4892 if (mContainerState == CONTAINER_STATE_FINISHING) {
4893 return;
4894 }
4895 VirtualActivityDisplay virtualActivityDisplay =
4896 (VirtualActivityDisplay) mActivityDisplay;
4897 if (virtualActivityDisplay == null) {
4898 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004899 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004900 mActivityDisplay = virtualActivityDisplay;
4901 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004902 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004903 }
4904
4905 if (mSurface != null) {
4906 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004907 }
4908
Craig Mautner6985bad2014-04-21 15:22:06 -07004909 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004910 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004911 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004912 } else {
4913 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004914 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004915 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004916 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004917 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004918 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004919
Craig Mautnerd163e752014-06-13 17:18:47 -07004920 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004921
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004922 if (DEBUG_STACK) Slog.d(TAG_STACK,
4923 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004924 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004925
Craig Mautner6985bad2014-04-21 15:22:06 -07004926 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004927 boolean isAttachedLocked() {
4928 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004929 }
4930
4931 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004932 void setDrawn() {
4933 synchronized (mService) {
4934 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004935 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004936 }
4937 }
4938
Craig Mautner1b4bf852014-05-26 15:06:32 -07004939 // Never start a new task on an ActivityView if it isn't explicitly specified.
4940 @Override
4941 boolean isEligibleForNewTasks() {
4942 return false;
4943 }
4944
Craig Mautnerd163e752014-06-13 17:18:47 -07004945 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004946 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004947 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4948 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4949 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4950 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4951 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004952 }
4953 }
4954
Craig Mautner4a1cb222013-12-04 16:14:06 -08004955 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4956 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004957 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004958 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004959 int mDisplayId;
4960 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004961
Craig Mautner4a1cb222013-12-04 16:14:06 -08004962 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4963 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004964 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004965
Jose Lima4b6c6692014-08-12 17:41:12 -07004966 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004967
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004968 /** Array of all UIDs that are present on the display. */
4969 private IntArray mDisplayAccessUIDs = new IntArray();
4970
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004971 ActivityDisplay() {
4972 }
Craig Mautner4504de52013-12-20 09:06:56 -08004973
Craig Mautner1a70a162014-09-13 12:09:31 -07004974 // After instantiation, check that mDisplay is not null before using this. The alternative
4975 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004976 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004977 final Display display = mDisplayManager.getDisplay(displayId);
4978 if (display == null) {
4979 return;
4980 }
4981 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004982 }
4983
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004984 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004985 mDisplay = display;
4986 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004987 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004988
Winson Chung7c3ede32017-03-14 14:18:34 -07004989 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004990 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004991 + " to displayId=" + mDisplayId + " position=" + position);
4992 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004993 }
4994
Wale Ogunwale1666e312016-12-16 11:27:18 -08004995 void detachStack(ActivityStack stack) {
4996 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004997 + " from displayId=" + mDisplayId);
4998 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004999 }
5000
Jose Lima4b6c6692014-08-12 17:41:12 -07005001 void setVisibleBehindActivity(ActivityRecord r) {
5002 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005003 }
5004
Jose Lima4b6c6692014-08-12 17:41:12 -07005005 boolean hasVisibleBehindActivity() {
5006 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005007 }
5008
Craig Mautner34b73df2014-01-12 21:11:08 -08005009 @Override
5010 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005011 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
5012 }
Andrii Kulian1779e612016-10-12 21:58:25 -07005013
5014 @Override
5015 protected int getChildCount() {
5016 return mStacks.size();
5017 }
5018
5019 @Override
5020 protected ConfigurationContainer getChildAt(int index) {
5021 return mStacks.get(index);
5022 }
5023
5024 @Override
5025 protected ConfigurationContainer getParent() {
5026 return ActivityStackSupervisor.this;
5027 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005028
5029 boolean isPrivate() {
5030 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
5031 }
5032
5033 boolean isUidPresent(int uid) {
5034 for (ActivityStack stack : mStacks) {
5035 if (stack.isUidPresent(uid)) {
5036 return true;
5037 }
5038 }
5039 return false;
5040 }
5041
5042 /** Update and get all UIDs that are present on the display and have access to it. */
5043 private IntArray getPresentUIDs() {
5044 mDisplayAccessUIDs.clear();
5045 for (ActivityStack stack : mStacks) {
5046 stack.getPresentUIDs(mDisplayAccessUIDs);
5047 }
5048 return mDisplayAccessUIDs;
5049 }
Andrii Kulian250d6532017-02-08 23:30:45 -08005050
5051 boolean shouldDestroyContentOnRemove() {
5052 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
5053 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005054 }
5055
5056 class VirtualActivityDisplay extends ActivityDisplay {
5057 VirtualDisplay mVirtualDisplay;
5058
Craig Mautner6985bad2014-04-21 15:22:06 -07005059 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005060 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Santos Cordonee8931e2017-04-05 10:31:15 -07005061 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null /* projection */,
5062 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null /* surface */,
Michael Wrightc39d47a2014-07-08 18:07:36 -07005063 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Santos Cordonee8931e2017-04-05 10:31:15 -07005064 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null /* callback */,
5065 null /* handler */, null /* uniqueId */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005066
5067 init(mVirtualDisplay.getDisplay());
5068
Bryce Leeaf3a4002017-03-20 10:37:12 -07005069 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005070 }
5071
5072 void setSurface(Surface surface) {
5073 if (mVirtualDisplay != null) {
5074 mVirtualDisplay.setSurface(surface);
5075 }
5076 }
5077
5078 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08005079 void detachStack(ActivityStack stack) {
5080 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005081 if (mVirtualDisplay != null) {
5082 mVirtualDisplay.release();
5083 mVirtualDisplay = null;
5084 }
5085 }
5086
5087 @Override
5088 public String toString() {
5089 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005090 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005091 }
Jose Lima58e66d62014-05-27 20:00:27 -07005092
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005093 ActivityStack findStackBehind(ActivityStack stack) {
5094 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02005095 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005096 if (display == null) {
5097 return null;
5098 }
5099 final ArrayList<ActivityStack> stacks = display.mStacks;
5100 for (int i = stacks.size() - 1; i >= 0; i--) {
5101 if (stacks.get(i) == stack && i > 0) {
5102 return stacks.get(i - 1);
5103 }
5104 }
5105 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5106 + " in=" + stacks);
5107 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005108
Jorim Jaggic69bd222016-03-15 14:38:37 +01005109 /**
5110 * Puts a task into resizing mode during the next app transition.
5111 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005112 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01005113 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005114 private void setResizingDuringAnimation(TaskRecord task) {
5115 mResizingTasksDuringAnimation.add(task.taskId);
5116 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01005117 }
5118
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005119 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
5120 final TaskRecord task;
5121 final int callingUid;
5122 final String callingPackage;
5123 final Intent intent;
5124 final int userId;
5125 final ActivityOptions activityOptions = (bOptions != null)
5126 ? new ActivityOptions(bOptions) : null;
5127 final int launchStackId = (activityOptions != null)
5128 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005129 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005130 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005131 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005132 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005133
Matthew Ng606dd802017-06-05 14:06:32 -07005134 mWindowManager.deferSurfaceLayout();
5135 try {
5136 if (launchStackId == DOCKED_STACK_ID) {
5137 mWindowManager.setDockedStackCreateState(
5138 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005139
Matthew Ng606dd802017-06-05 14:06:32 -07005140 // Defer updating the stack in which recents is until the app transition is done, to
5141 // not run into issues where we still need to draw the task in recents but the
5142 // docked stack is already created.
5143 deferUpdateBounds(RECENTS_STACK_ID);
5144 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005145 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005146
Matthew Ng606dd802017-06-05 14:06:32 -07005147 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
5148 launchStackId);
5149 if (task == null) {
5150 continueUpdateBounds(RECENTS_STACK_ID);
5151 mWindowManager.executeAppTransition();
5152 throw new IllegalArgumentException(
5153 "startActivityFromRecentsInner: Task " + taskId + " not found.");
5154 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005155
Matthew Ng606dd802017-06-05 14:06:32 -07005156 // Since we don't have an actual source record here, we assume that the currently
5157 // focused activity was the source.
5158 final ActivityStack focusedStack = getFocusedStack();
5159 final ActivityRecord sourceRecord =
5160 focusedStack != null ? focusedStack.topActivity() : null;
5161
5162 if (launchStackId != INVALID_STACK_ID) {
5163 if (task.getStackId() != launchStackId) {
5164 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
5165 DEFER_RESUME, "startActivityFromRecents");
5166 }
5167 }
5168
5169 // If the user must confirm credentials (e.g. when first launching a work app and the
5170 // Work Challenge is present) let startActivityInPackage handle the intercepting.
5171 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
5172 && task.getRootActivity() != null) {
5173 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
5174 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07005175 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07005176 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
5177 task.getTopActivity());
5178
5179 // If we are launching the task in the docked stack, put it into resizing mode so
5180 // the window renders full-screen with the background filling the void. Also only
5181 // call this at the end to make sure that tasks exists on the window manager side.
5182 if (launchStackId == DOCKED_STACK_ID) {
5183 setResizingDuringAnimation(task);
5184 }
5185
5186 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
5187 ActivityManager.START_TASK_TO_FRONT,
5188 sourceRecord != null
5189 ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
5190 sourceRecord, task.getStack());
5191 return ActivityManager.START_TASK_TO_FRONT;
5192 }
5193 callingUid = task.mCallingUid;
5194 callingPackage = task.mCallingPackage;
5195 intent = task.intent;
5196 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5197 userId = task.userId;
5198 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Wale Ogunwale692dcd62017-06-20 13:38:14 -07005199 null, null, 0, 0, bOptions, userId, null, task, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005200 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005201 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005202 }
Matthew Ng606dd802017-06-05 14:06:32 -07005203 return result;
5204 } finally {
5205 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005206 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005207 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005208
5209 /**
5210 * @return a list of activities which are the top ones in each visible stack. The first
5211 * entry will be the focused activity.
5212 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005213 List<IBinder> getTopVisibleActivities() {
5214 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5215 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005216 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005217 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5218 // Traverse all stacks on a display.
5219 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5220 final ActivityStack stack = display.mStacks.get(j);
5221 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005222 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005223 == ActivityStack.STACK_VISIBLE) {
5224 final ActivityRecord top = stack.topActivity();
5225 if (top != null) {
5226 if (stack == mFocusedStack) {
5227 topActivityTokens.add(0, top.appToken);
5228 } else {
5229 topActivityTokens.add(top.appToken);
5230 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005231 }
5232 }
5233 }
5234 }
5235 return topActivityTokens;
5236 }
Bryce Lee4a194382017-04-04 14:32:48 -07005237
5238 /**
5239 * Internal container to store a match qualifier alongside a WaitResult.
5240 */
5241 static class WaitInfo {
5242 private final ComponentName mTargetComponent;
5243 private final WaitResult mResult;
5244
5245 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5246 this.mTargetComponent = targetComponent;
5247 this.mResult = result;
5248 }
5249
Wale Ogunwale3270f172017-04-26 07:29:42 -07005250 public boolean matches(ComponentName targetComponent) {
5251 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07005252 }
5253
5254 public WaitResult getResult() {
5255 return mResult;
5256 }
5257
5258 public ComponentName getComponent() {
5259 return mTargetComponent;
5260 }
5261
5262 public void dump(PrintWriter pw, String prefix) {
5263 pw.println(prefix + "WaitInfo:");
5264 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5265 pw.println(prefix + " mResult=");
5266 mResult.dump(pw, prefix);
5267 }
5268 }
Craig Mautner27084302013-03-25 08:05:25 -07005269}