blob: 3e3fee54bdd0753db3f53c9ccd0e5248b041a132 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Andrii Kuliane6ac20e2017-03-17 10:30:50 -070019import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020020import static android.Manifest.permission.START_ANY_ACTIVITY;
21import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
22import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
23import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
24import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.app.ActivityManager.START_TASK_TO_FRONT;
26import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.HOME_STACK_ID;
32import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
33import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
34import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080035import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020036import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
37import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
38import static android.content.pm.PackageManager.PERMISSION_GRANTED;
39import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
40import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070041import static android.view.Display.FLAG_PRIVATE;
42import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080043import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Jorim Jaggife762342016-10-13 14:33:27 +020044
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
72import static com.android.server.am.ActivityManagerService.ANIMATE;
73import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
74import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
75import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
77import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
78import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
79import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
81import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
84import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
85import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
86import static com.android.server.am.ActivityStack.STACK_VISIBLE;
87import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080092import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
93import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
94import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020095import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080096import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020097
Svetoslav7008b512015-06-24 18:47:07 -070098import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -080099import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700100import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100101import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700102import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800104import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700105import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800109import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700110import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400112import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700113import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400114import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700116import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import android.content.Intent;
119import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700121import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700122import android.content.pm.PackageManager;
123import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100124import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800126import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800127import android.hardware.display.DisplayManager;
128import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800129import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800130import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800131import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800132import android.hardware.input.InputManager;
133import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100135import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700136import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700137import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700139import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700140import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700141import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700142import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700143import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700144import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400145import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700146import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700147import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700148import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700149import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100150import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700151import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700152import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400153import android.provider.Settings;
154import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700155import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700157import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700158import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800159import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700160import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800163import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800164import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800165import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800166
Bryce Leeaf691c02017-03-20 14:20:22 -0700167import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800168import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800169import com.android.internal.logging.MetricsLogger;
170import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700171import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400172import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700173import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400174import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800175import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700176import com.android.server.am.ActivityStack.ActivityState;
Bryce Leeaf691c02017-03-20 14:20:22 -0700177import com.android.server.wm.StackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700178import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700179
Craig Mautner8d341ef2013-03-26 09:03:27 -0700180import java.io.FileDescriptor;
181import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700182import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800183import java.lang.annotation.Retention;
184import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700185import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700186import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700187import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700188import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700189
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800190public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800191 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700193 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700195 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700196 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800202 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800204
Craig Mautnerf3333272013-04-22 10:55:53 -0700205 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700206 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700207
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700209 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700210
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700212 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700213
Craig Mautner05d29032013-05-03 13:40:13 -0700214 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
215 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
216 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700217 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700218 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800219 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
220 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
221 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700222 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400223 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
224 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700225 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700226 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700227 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800228 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
229 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800230
Wale Ogunwale040b4702015-08-06 18:10:50 -0700231 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700232
Jason Monk62515be2014-05-21 16:06:19 -0400233 private static final String LOCK_TASK_TAG = "Lock-to-App";
234
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235 // Used to indicate if an object (e.g. stack) that we are trying to get
236 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800237 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700238
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700239 // Used to indicate that windows of activities should be preserved during the resize.
240 static final boolean PRESERVE_WINDOWS = true;
241
Wale Ogunwale040b4702015-08-06 18:10:50 -0700242 // Used to indicate if an object (e.g. task) should be moved/created
243 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700244 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700245
246 // Used to indicate that an objects (e.g. task) removal from its container
247 // (e.g. stack) is due to it moving to another container.
248 static final boolean MOVING = true;
249
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700250 // Force the focus to change to the stack we are moving a task to..
251 static final boolean FORCE_FOCUS = true;
252
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700253 // Don't execute any calls to resume.
254 static final boolean DEFER_RESUME = true;
255
Winson Chung010927a2016-12-15 16:12:35 -0800256 // Used to indicate that a task is removed it should also be removed from recents.
257 static final boolean REMOVE_FROM_RECENTS = true;
258
Winson Chung6954fc92017-03-24 16:22:12 -0700259 // Used to indicate that pausing an activity should occur immediately without waiting for
260 // the activity callback indicating that it has completed pausing
261 static final boolean PAUSE_IMMEDIATELY = true;
262
Winson Chung7900bee2017-03-10 08:59:25 -0800263 /**
264 * The modes which affect which tasks are returned when calling
265 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
266 */
267 @Retention(RetentionPolicy.SOURCE)
268 @IntDef({
269 MATCH_TASK_IN_STACKS_ONLY,
270 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
271 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
272 })
273 public @interface AnyTaskForIdMatchTaskMode {}
274 // Match only tasks in the current stacks
275 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
276 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
277 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
278 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
279 // provided stack id
280 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
281
Svetoslav7008b512015-06-24 18:47:07 -0700282 // Activity actions an app cannot start if it uses a permission which is not granted.
283 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
284 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700285
Svetoslav7008b512015-06-24 18:47:07 -0700286 static {
287 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
288 Manifest.permission.CAMERA);
289 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
290 Manifest.permission.CAMERA);
291 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
292 Manifest.permission.CALL_PHONE);
293 }
294
Svet Ganov99b60432015-06-27 13:15:22 -0700295 /** Action restriction: launching the activity is not restricted. */
296 private static final int ACTIVITY_RESTRICTION_NONE = 0;
297 /** Action restriction: launching the activity is restricted by a permission. */
298 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
299 /** Action restriction: launching the activity is restricted by an app op. */
300 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700301
justinzhang5286d3f2014-05-12 17:06:01 -0400302 /** Status Bar Service **/
303 private IBinder mToken = new Binder();
304 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400305 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400306
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700307 // For debugging to make sure the caller when acquiring/releasing our
308 // wake lock is the system process.
309 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800310 /** The number of distinct task ids that can be assigned to the tasks of a single user */
311 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700312
Craig Mautner27084302013-03-25 08:05:25 -0700313 final ActivityManagerService mService;
314
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800315 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800316
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700317 final ActivityStackSupervisorHandler mHandler;
318
319 /** Short cut */
320 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700323 /** Counter for next free stack ID to use for dynamic activity stacks. */
324 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700325
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800326 /**
327 * Maps the task identifier that activities are currently being started in to the userId of the
328 * task. Each time a new task is created, the entry for the userId of the task is incremented
329 */
330 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700331
Craig Mautner2420ead2013-04-01 17:13:20 -0700332 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800333 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700334
Craig Mautnere0a38842013-12-16 16:14:02 -0800335 /** The stack containing the launcher app. Assumed to always be attached to
336 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800337 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700338
Craig Mautnere0a38842013-12-16 16:14:02 -0800339 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800340 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700341
Craig Mautner4a1cb222013-12-04 16:14:06 -0800342 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
343 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800344 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800345 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700346
347 /** List of activities that are waiting for a new activity to become visible before completing
348 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800349 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700350
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700351 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700352 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700353
354 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700355 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700356
Craig Mautnerde4ef022013-04-07 19:01:33 -0700357 /** List of activities that are ready to be stopped, but waiting for the next activity to
358 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800359 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700360
Craig Mautnerf3333272013-04-22 10:55:53 -0700361 /** List of activities that are ready to be finished, but waiting for the previous activity to
362 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800363 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700364
Craig Mautner0eea92c2013-05-16 13:35:39 -0700365 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800366 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700367
Wale Ogunwale22e25262016-02-01 10:32:02 -0800368 /** List of activities whose multi-window mode changed that we need to report to the
369 * application */
370 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
371
372 /** List of activities whose picture-in-picture mode changed that we need to report to the
373 * application */
374 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
375
Winson Chung5af42fc2017-03-24 17:11:33 -0700376 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
377 * the application */
378 Rect mPipModeChangedTargetStackBounds;
379
Craig Mautnerf3333272013-04-22 10:55:53 -0700380 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700381 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700382
Craig Mautnerde4ef022013-04-07 19:01:33 -0700383 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
384 * is being brought in front of us. */
385 boolean mUserLeaving = false;
386
Craig Mautner0eea92c2013-05-16 13:35:39 -0700387 /** Set when we have taken too long waiting to go to sleep. */
388 boolean mSleepTimeout = false;
389
390 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700391 * We don't want to allow the device to go to sleep while in the process
392 * of launching an activity. This is primarily to allow alarm intent
393 * receivers to launch an activity and get that to run before the device
394 * goes back to sleep.
395 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700396 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700397
398 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700399 * Set when the system is going to sleep, until we have
400 * successfully paused the current activity and released our wake lock.
401 * At that point the system is allowed to actually sleep.
402 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700403 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700404
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700405 /** Stack id of the front stack when user switched, indexed by userId. */
406 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700407
Craig Mautner4504de52013-12-20 09:06:56 -0800408 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800409 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800410 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800411
Bryce Leeaf3a4002017-03-20 10:37:12 -0700412 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800413 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700414 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800416 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
417
418 private DisplayManagerInternal mDisplayManagerInternal;
419 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800420
Craig Mautner15df08a2015-04-01 12:17:18 -0700421 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
422 * may be finished until there is only one entry left. If this is empty the system is not
423 * in lockTask mode. */
424 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000425 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700426 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
427 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000428 */
429 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400430 /**
431 * Notifies the user when entering/exiting lock-task.
432 */
433 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800434
Wale Ogunwaled046a012015-12-24 13:05:59 -0800435 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800436 boolean inResumeTopActivity;
437
Andrii Kulian1e32e022016-09-16 15:29:34 -0700438 /**
439 * Temporary rect used during docked stack resize calculation so we don't need to create a new
440 * object each time.
441 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700442 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700443
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700444 // The default minimal size that will be used if the activity doesn't specify its minimal size.
445 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700446 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700447
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700448 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
449 private boolean mTaskLayersChanged = true;
450
Jorim Jaggi275561a2016-02-23 10:11:02 -0500451 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800452
Andrii Kulian1779e612016-10-12 21:58:25 -0700453 @Override
454 protected int getChildCount() {
455 return mActivityDisplays.size();
456 }
457
458 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700459 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700460 return mActivityDisplays.valueAt(index);
461 }
462
463 @Override
464 protected ConfigurationContainer getParent() {
465 return null;
466 }
467
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700468 Configuration getDisplayOverrideConfiguration(int displayId) {
469 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
470 if (activityDisplay == null) {
471 throw new IllegalArgumentException("No display found with id: " + displayId);
472 }
473
474 return activityDisplay.getOverrideConfiguration();
475 }
476
477 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
478 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
479 if (activityDisplay == null) {
480 throw new IllegalArgumentException("No display found with id: " + displayId);
481 }
482
483 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
484 }
485
Wale Ogunwale39381972015-12-17 17:15:29 -0800486 static class FindTaskResult {
487 ActivityRecord r;
488 boolean matchedByRootAffinity;
489 }
490 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
491
Craig Mautneree36c772014-07-16 14:56:05 -0700492 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800493 * Temp storage for display ids sorted in focus order.
494 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
495 * it's more efficient, as the number of displays is usually small.
496 */
497 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
498
499 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100500 * Used to keep track whether app visibilities got changed since the last pause. Useful to
501 * determine whether to invoke the task stack change listener after pausing.
502 */
503 boolean mAppVisibilitiesChangedSinceLastPause;
504
505 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100506 * Set of tasks that are in resizing mode during an app transition to fill the "void".
507 */
508 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
509
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700510
511 /**
512 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
513 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
514 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
515 * like the docked stack going empty.
516 */
517 private boolean mAllowDockedStackResize = true;
518
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100519 /**
Tony Mak853304c2016-04-18 15:17:41 +0100520 * Is dock currently minimized.
521 */
522 boolean mIsDockMinimized;
523
Jorim Jaggife762342016-10-13 14:33:27 +0200524 final KeyguardController mKeyguardController;
525
Tony Mak853304c2016-04-18 15:17:41 +0100526 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700527 * Description of a request to start a new activity, which has been held
528 * due to app switches being disabled.
529 */
530 static class PendingActivityLaunch {
531 final ActivityRecord r;
532 final ActivityRecord sourceRecord;
533 final int startFlags;
534 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700535 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700536
537 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700538 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700539 r = _r;
540 sourceRecord = _sourceRecord;
541 startFlags = _startFlags;
542 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700543 callerApp = _callerApp;
544 }
545
546 void sendErrorResult(String message) {
547 try {
548 if (callerApp.thread != null) {
549 callerApp.thread.scheduleCrash(message);
550 }
551 } catch (RemoteException e) {
552 Slog.e(TAG, "Exception scheduling crash of failed "
553 + "activity launcher sourceRecord=" + sourceRecord, e);
554 }
Craig Mautneree36c772014-07-16 14:56:05 -0700555 }
556 }
557
Bryce Leeaf691c02017-03-20 14:20:22 -0700558 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700559 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700560 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800561 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200562 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700563 }
564
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800565 void setRecentTasks(RecentTasks recentTasks) {
566 mRecentTasks = recentTasks;
567 }
568
Jeff Brown2c43c332014-06-12 22:38:59 -0700569 /**
570 * At the time when the constructor runs, the power manager has not yet been
571 * initialized. So we initialize our wakelocks afterwards.
572 */
573 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800574 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700575 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700576 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700577 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700578 }
579
justinzhang5286d3f2014-05-12 17:06:01 -0400580 // This function returns a IStatusBarService. The value is from ServiceManager.
581 // getService and is cached.
582 private IStatusBarService getStatusBarService() {
583 synchronized (mService) {
584 if (mStatusBarService == null) {
585 mStatusBarService = IStatusBarService.Stub.asInterface(
586 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
587 if (mStatusBarService == null) {
588 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
589 }
590 }
591 return mStatusBarService;
592 }
593 }
594
Jason Monk35c62a42014-06-17 10:24:47 -0400595 private IDevicePolicyManager getDevicePolicyManager() {
596 synchronized (mService) {
597 if (mDevicePolicyManager == null) {
598 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
599 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
600 if (mDevicePolicyManager == null) {
601 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
602 }
603 }
604 return mDevicePolicyManager;
605 }
606 }
607
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700608 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 synchronized (mService) {
610 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200611 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800612
613 mDisplayManager =
614 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
615 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800616 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800617
618 Display[] displays = mDisplayManager.getDisplays();
619 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
620 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800621 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700622 if (activityDisplay.mDisplay == null) {
623 throw new IllegalStateException("Default Display does not exist");
624 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800625 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700626 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 }
628
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800629 mHomeStack = mFocusedStack = mLastFocusedStack =
630 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800631
632 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800633 }
Craig Mautner27084302013-03-25 08:05:25 -0700634 }
635
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700636 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700638 }
639
Craig Mautnerde4ef022013-04-07 19:01:33 -0700640 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700642 }
643
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700644 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700645 if (stack == null) {
646 return false;
647 }
648
Craig Mautnerdf88d732014-01-27 09:21:32 -0800649 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
650 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700651 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800652 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800653 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700654 }
655
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800656 /** The top most stack on its display. */
657 boolean isFrontStackOnDisplay(ActivityStack stack) {
658 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
659 }
660
661 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700662 if (stack == null) {
663 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700665
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700666 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
667 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700668 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800669 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800670 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700671 }
672
Wale Ogunwaled046a012015-12-24 13:05:59 -0800673 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800674 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
675 if (!focusCandidate.isFocusable()) {
676 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800677 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800678 }
679
680 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800681 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800682 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800683
Wale Ogunwaled046a012015-12-24 13:05:59 -0800684 EventLogTags.writeAmFocusedStack(
685 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
686 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
687 }
688
689 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800690 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800691 if (r != null && r.idle) {
692 checkFinishBootingLocked();
693 }
694 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700695 }
696
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700697 void moveHomeStackToFront(String reason) {
698 mHomeStack.moveToFront(reason);
699 }
700
Matthew Ng330757d2017-02-28 14:19:17 -0800701 void moveRecentsStackToFront(String reason) {
702 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
703 if (recentsStack != null) {
704 recentsStack.moveToFront(reason);
705 }
706 }
707
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700708 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800709 boolean moveHomeStackTaskToTop(String reason) {
710 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700711
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700712 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700713 if (top == null) {
714 return false;
715 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700716 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700717 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700718 }
719
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800720 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700721 if (!mService.mBooting && !mService.mBooted) {
722 // Not ready yet!
723 return false;
724 }
725
Craig Mautner84984fa2014-06-19 11:19:20 -0700726 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700727 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700728 }
729
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800730 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700731 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800732 final String myReason = reason + " resumeHomeStackTask";
733
Mark Lua56ea122015-10-08 13:31:01 +0800734 // Only resume home activity if isn't finishing.
735 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700736 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800737 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700738 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800739 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700740 }
741
Craig Mautner8d341ef2013-03-26 09:03:27 -0700742 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800743 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
744 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700745 }
746
747 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700748 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700749 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800750 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700751 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800752 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
753 * valid if the matchMode is
754 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700755 */
Winson Chung7900bee2017-03-10 08:59:25 -0800756 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
757 // If there is a stack id set, ensure that we are attempting to actually restore a task
758 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
759 stackId != INVALID_STACK_ID) {
760 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
761 }
762
Craig Mautnere0a38842013-12-16 16:14:02 -0800763 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800765 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800766 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
767 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700768 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 if (task != null) {
770 return task;
771 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700772 }
773 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800774
Winson Chung7900bee2017-03-10 08:59:25 -0800775 // If we are matching stack tasks only, return now
776 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800777 return null;
778 }
779
Winson Chung7900bee2017-03-10 08:59:25 -0800780 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
781 // the task from recents
782 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700783 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
784
785 if (task == null) {
786 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800787 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
788 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700789
790 return null;
791 }
792
793 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700794 return task;
795 }
796
Winson Chung7900bee2017-03-10 08:59:25 -0800797 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700798 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700799 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
800 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800801 return null;
802 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700803 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800804 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700805 }
806
Craig Mautner6170f732013-04-02 13:05:23 -0700807 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800808 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800810 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
812 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
813 if (r != null) {
814 return r;
815 }
Craig Mautner6170f732013-04-02 13:05:23 -0700816 }
817 }
818 return null;
819 }
820
Tony Mak853304c2016-04-18 15:17:41 +0100821 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000822 * Detects whether we should show a lock screen in front of this task for a locked user.
823 * <p>
824 * We'll do this if either of the following holds:
825 * <ul>
826 * <li>The top activity explicitly belongs to {@param userId}.</li>
827 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
828 * </ul>
829 *
830 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100831 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000832 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
833 // To handle the case that work app is in the task but just is not the top one.
834 final ActivityRecord activityRecord = task.getTopActivity();
835 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
836
837 return (activityRecord != null && activityRecord.userId == userId)
838 || (resultTo != null && resultTo.userId == userId);
839 }
840
841 /**
842 * Find all visible task stacks containing {@param userId} and intercept them with an activity
843 * to block out the contents and possibly start a credential-confirming intent.
844 *
845 * @param userId user handle for the locked managed profile.
846 */
847 void lockAllProfileTasks(@UserIdInt int userId) {
848 mWindowManager.deferSurfaceLayout();
849 try {
850 final List<ActivityStack> stacks = getStacks();
851 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
852 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
853 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
854 final TaskRecord task = tasks.get(taskNdx);
855
856 // Check the task for a top activity belonging to userId, or returning a result
857 // to an activity belonging to userId. Example case: a document picker for
858 // personal files, opened by a work app, should still get locked.
859 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000860 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
861 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000862 }
Tony Mak853304c2016-04-18 15:17:41 +0100863 }
864 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000865 } finally {
866 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100867 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000868 }
869
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800870 void setNextTaskIdForUserLocked(int taskId, int userId) {
871 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
872 if (taskId > currentTaskId) {
873 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700874 }
875 }
876
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700877 static int nextTaskIdForUser(int taskId, int userId) {
878 int nextTaskId = taskId + 1;
879 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
880 // Wrap around as there will be smaller task ids that are available now.
881 nextTaskId -= MAX_TASK_IDS_PER_USER;
882 }
883 return nextTaskId;
884 }
885
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800886 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800887 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
888 // for a userId u, a taskId can only be in the range
889 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
890 // 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 -0700891 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800892 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800893 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800894 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700895 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800896 if (candidateTaskId == currentTaskId) {
897 // Something wrong!
898 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
899 throw new IllegalStateException("Cannot get an available task id."
900 + " Reached limit of " + MAX_TASK_IDS_PER_USER
901 + " running tasks per user.");
902 }
903 }
904 mCurTaskIdForUser.put(userId, candidateTaskId);
905 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700906 }
907
Chong Zhang6cda19c2016-06-14 19:07:56 -0700908 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800909 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700910 if (stack == null) {
911 return null;
912 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700913 ActivityRecord resumedActivity = stack.mResumedActivity;
914 if (resumedActivity == null || resumedActivity.app == null) {
915 resumedActivity = stack.mPausingActivity;
916 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700917 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700918 }
919 }
920 return resumedActivity;
921 }
922
Dianne Hackbornff072722014-09-24 10:56:28 -0700923 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700924 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800925 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800926 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
927 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800928 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
929 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700930 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 continue;
932 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700933 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 if (hr != null) {
935 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
936 && processName.equals(hr.processName)) {
937 try {
George Mount2c92c972014-03-20 09:38:23 -0700938 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 didSomething = true;
940 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700941 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 Slog.w(TAG, "Exception in new application when starting activity "
943 + hr.intent.getComponent().flattenToShortString(), e);
944 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700945 }
Craig Mautner20e72272013-04-01 13:45:53 -0700946 }
Craig Mautner20e72272013-04-01 13:45:53 -0700947 }
948 }
949 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700950 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700951 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700952 }
Craig Mautner20e72272013-04-01 13:45:53 -0700953 return didSomething;
954 }
955
956 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800957 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
958 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
960 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700961 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 continue;
963 }
964 final ActivityRecord resumedActivity = stack.mResumedActivity;
965 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700966 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800967 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800968 return false;
969 }
Craig Mautner20e72272013-04-01 13:45:53 -0700970 }
971 }
Wei Wang65c7a152016-06-02 18:51:22 -0700972 // Send launch end powerhint when idle
973 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700974 return true;
975 }
976
Craig Mautnerde4ef022013-04-07 19:01:33 -0700977 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800978 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
979 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800980 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
981 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700982 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800983 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700984 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800985 return false;
986 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 }
988 }
989 }
990 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700991 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
993 mLastFocusedStack + " to=" + mFocusedStack);
994 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700995 return true;
996 }
997
998 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800999 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001000 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1001 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001002 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1003 final ActivityStack stack = stacks.get(stackNdx);
1004 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001005 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -07001006 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001007 return false;
1008 }
1009 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001010 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001011 }
1012 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001013 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001014 }
1015
Craig Mautner2acc3892013-09-23 10:28:14 -07001016 /**
1017 * Pause all activities in either all of the stacks or just the back stacks.
1018 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001019 * @param resuming The resuming activity.
1020 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1021 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001022 * @return true if any activity was paused as a result of this call.
1023 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001024 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001025 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001026 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1027 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001028 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1029 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001030 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001031 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001032 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001033 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1034 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001035 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001036 }
1037 }
1038 return someActivityPaused;
1039 }
1040
Craig Mautnerde4ef022013-04-07 19:01:33 -07001041 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001042 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001043 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1044 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001045 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1046 final ActivityStack stack = stacks.get(stackNdx);
1047 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001048 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001049 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001050 Slog.d(TAG_STATES,
1051 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001052 pausing = false;
1053 } else {
1054 return false;
1055 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001056 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001057 }
1058 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001059 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001060 }
1061
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001062 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001063 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001064 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001065 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1066 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1067 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1068 final ActivityStack stack = stacks.get(stackNdx);
1069 if (stack.mResumedActivity != null &&
1070 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001071 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001072 }
1073 }
1074 }
1075 }
1076
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001077 void cancelInitializingActivities() {
1078 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1079 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1080 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1081 stacks.get(stackNdx).cancelInitializingActivities();
1082 }
1083 }
1084 }
1085
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001086 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001087 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001088 }
1089
1090 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1091 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001092 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001094 if (w.who == null) {
1095 changed = true;
1096 w.timeout = false;
1097 if (r != null) {
1098 w.who = new ComponentName(r.info.packageName, r.info.name);
1099 }
1100 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1101 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001102 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001103 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001104 if (changed) {
1105 mService.notifyAll();
1106 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001107 }
1108
Chong Zhang5022da32016-06-21 16:31:37 -07001109 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1110 boolean changed = false;
1111 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1112 WaitResult w = mWaitingActivityLaunched.remove(i);
1113 if (w.who == null) {
1114 changed = true;
1115 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1116 // the starting activity ends up moving another activity to front, and it should
1117 // wait for this new activity to become visible instead.
1118 // Do not modify other fields.
1119 w.result = START_TASK_TO_FRONT;
1120 }
1121 }
1122 if (changed) {
1123 mService.notifyAll();
1124 }
1125 }
1126
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001127 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1128 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001129 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001130 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001131 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001132 if (w.who == null) {
1133 changed = true;
1134 w.timeout = timeout;
1135 if (r != null) {
1136 w.who = new ComponentName(r.info.packageName, r.info.name);
1137 }
1138 w.thisTime = thisTime;
1139 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001140 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001141 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001142 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001143 if (changed) {
1144 mService.notifyAll();
1145 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001146 }
1147
Craig Mautner29219d92013-04-16 20:19:12 -07001148 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001149 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001150 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001151 if (r != null) {
1152 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001153 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001154
Andrii Kulian7318d632016-07-20 18:59:28 -07001155 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001156 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1157
1158 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1159 final int displayId = mTmpOrderedDisplayIds.get(i);
1160 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1161 if (stacks == null) {
1162 continue;
1163 }
1164 for (int j = stacks.size() - 1; j >= 0; --j) {
1165 final ActivityStack stack = stacks.get(j);
1166 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1167 r = stack.topRunningActivityLocked();
1168 if (r != null) {
1169 return r;
1170 }
Craig Mautner29219d92013-04-16 20:19:12 -07001171 }
1172 }
1173 }
1174 return null;
1175 }
1176
Dianne Hackborn09233282014-04-30 11:33:59 -07001177 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001178 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001179 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1180 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001181 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001182 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001183 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001184 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1185 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001186 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001187 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001188 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001189 }
1190 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001191
1192 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1193 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1194 while (maxNum > 0) {
1195 long mostRecentActiveTime = Long.MIN_VALUE;
1196 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001197 final int numTaskLists = runningTaskLists.size();
1198 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1199 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001200 if (!stackTaskList.isEmpty()) {
1201 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1202 if (lastActiveTime > mostRecentActiveTime) {
1203 mostRecentActiveTime = lastActiveTime;
1204 selectedStackList = stackTaskList;
1205 }
1206 }
1207 }
1208 if (selectedStackList != null) {
1209 list.add(selectedStackList.remove(0));
1210 --maxNum;
1211 } else {
1212 break;
1213 }
1214 }
Craig Mautner20e72272013-04-01 13:45:53 -07001215 }
1216
Todd Kennedy7440f172015-12-09 14:31:22 -08001217 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1218 ProfilerInfo profilerInfo) {
1219 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001220 if (aInfo != null) {
1221 // Store the found target back into the intent, because now that
1222 // we have it we never want to do this again. For example, if the
1223 // user navigates back to this point in the history, we should
1224 // always restart the exact same activity.
1225 intent.setComponent(new ComponentName(
1226 aInfo.applicationInfo.packageName, aInfo.name));
1227
1228 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001229 if (!aInfo.processName.equals("system")) {
1230 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001231 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001232 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001233
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001234 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1235 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1236 }
1237
Man Caocfa78b22015-06-11 20:14:34 -07001238 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1239 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1240 }
1241
1242 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001243 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001244 }
1245 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001246 final String intentLaunchToken = intent.getLaunchToken();
1247 if (aInfo.launchToken == null && intentLaunchToken != null) {
1248 aInfo.launchToken = intentLaunchToken;
1249 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001250 }
1251 return aInfo;
1252 }
1253
Todd Kennedy7440f172015-12-09 14:31:22 -08001254 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001255 return resolveIntent(intent, resolvedType, userId, 0);
1256 }
1257
1258 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001259 synchronized (mService) {
1260 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001261 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001262 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001263 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001264 }
1265
1266 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1267 ProfilerInfo profilerInfo, int userId) {
1268 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1269 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1270 }
1271
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001272 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1273 boolean andResume, boolean checkConfig) throws RemoteException {
1274
1275 if (!allPausedActivitiesComplete()) {
1276 // While there are activities pausing we skipping starting any new activities until
1277 // pauses are complete. NOTE: that we also do this for activities that are starting in
1278 // the paused state because they will first be resumed then paused on the client side.
1279 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1280 "realStartActivityLocked: Skipping start of r=" + r
1281 + " some activities pausing...");
1282 return false;
1283 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001284
Andrii Kulianada10292017-02-09 23:19:29 -08001285 r.startFreezingScreenLocked(app, 0);
1286 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001287
Andrii Kulianada10292017-02-09 23:19:29 -08001288 // schedule launch ticks to collect information about slow apps.
1289 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001290
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001291 // Have the window manager re-evaluate the orientation of the screen based on the new
1292 // activity order. Note that as a result of this, it can call back into the activity
1293 // manager with a new orientation. We don't care about that, because the activity is not
1294 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001296 final int displayId = r.getDisplayId();
1297 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1298 getDisplayOverrideConfiguration(displayId),
1299 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001300 // Deferring resume here because we're going to launch new activity shortly.
1301 // We don't want to perform a redundant launch of the same record while ensuring
1302 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001303 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1304 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001305 }
1306
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001307 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001308 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001309 }
1310
Craig Mautner2420ead2013-04-01 17:13:20 -07001311 r.app = app;
1312 app.waitingToKill = null;
1313 r.launchCount++;
1314 r.lastLaunchTime = SystemClock.uptimeMillis();
1315
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001316 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001317
1318 int idx = app.activities.indexOf(r);
1319 if (idx < 0) {
1320 app.activities.add(r);
1321 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001322 mService.updateLruProcessLocked(app, true, null);
1323 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001324
Bryce Leeaf691c02017-03-20 14:20:22 -07001325 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001326 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1327 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001328 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001329 }
1330
Andrii Kulian02b7a832016-10-06 23:11:56 -07001331 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001332 try {
1333 if (app.thread == null) {
1334 throw new RemoteException();
1335 }
1336 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001337 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001338 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001339 // We don't need to deliver new intents and/or set results if activity is going
1340 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001341 results = r.results;
1342 newIntents = r.newIntents;
1343 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001344 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1345 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1346 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001347 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1348 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001349 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001350 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001351 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001353 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1354 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001355 r.sleeping = false;
1356 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001357 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001358 mService.showAskCompatModeDialogLocked(r);
1359 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001360 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001361 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1362 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1363 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001364 final String profileFile = mService.mProfileFile;
1365 if (profileFile != null) {
1366 ParcelFileDescriptor profileFd = mService.mProfileFd;
1367 if (profileFd != null) {
1368 try {
1369 profileFd = profileFd.dup();
1370 } catch (IOException e) {
1371 if (profileFd != null) {
1372 try {
1373 profileFd.close();
1374 } catch (IOException o) {
1375 }
1376 profileFd = null;
1377 }
1378 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001380
1381 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001382 mService.mSamplingInterval, mService.mAutoStopProfiler,
1383 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001384 }
1385 }
1386 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001387
Andrii Kulianada10292017-02-09 23:19:29 -08001388 app.hasShownUi = true;
1389 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001390 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001391 // Because we could be starting an Activity in the system process this may not go across
1392 // a Binder interface which would create a new Configuration. Consequently we have to
1393 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001394
1395 final Configuration globalConfiguration =
1396 new Configuration(mService.getGlobalConfiguration());
1397 r.setLastReportedGlobalConfiguration(globalConfiguration);
1398 final Configuration mergedOverrideConfiguration =
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001399 new Configuration(r.getMergedOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08001400 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1401
Craig Mautner2420ead2013-04-01 17:13:20 -07001402 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001403 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001404 globalConfiguration,
1405 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001406 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1407 r.persistentState, results, newIntents, !andResume,
1408 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001409
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001410 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001411 // This may be a heavy-weight process! Note that the package
1412 // manager will ensure that only activity can run in the main
1413 // process of the .apk, which is the only thing that will be
1414 // considered heavy-weight.
1415 if (app.processName.equals(app.info.packageName)) {
1416 if (mService.mHeavyWeightProcess != null
1417 && mService.mHeavyWeightProcess != app) {
1418 Slog.w(TAG, "Starting new heavy weight process " + app
1419 + " when already running "
1420 + mService.mHeavyWeightProcess);
1421 }
1422 mService.mHeavyWeightProcess = app;
1423 Message msg = mService.mHandler.obtainMessage(
1424 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1425 msg.obj = r;
1426 mService.mHandler.sendMessage(msg);
1427 }
1428 }
1429
1430 } catch (RemoteException e) {
1431 if (r.launchFailed) {
1432 // This is the second time we failed -- finish activity
1433 // and give up.
1434 Slog.e(TAG, "Second failure launching "
1435 + r.intent.getComponent().flattenToShortString()
1436 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001437 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001438 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1439 "2nd-crash", false);
1440 return false;
1441 }
1442
1443 // This is the first time we failed -- restart process and
1444 // retry.
1445 app.activities.remove(r);
1446 throw e;
1447 }
1448
1449 r.launchFailed = false;
1450 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001451 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001452 }
1453
1454 if (andResume) {
1455 // As part of the process of launching, ActivityThread also performs
1456 // a resume.
1457 stack.minimalResumeActivityLocked(r);
1458 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001459 // This activity is not starting in the resumed state... which should look like we asked
1460 // it to pause+stop (but remain visible), and it has done so and reported back the
1461 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001462 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001463 "Moving to PAUSED: " + r + " (starting in paused state)");
1464 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001465 }
1466
1467 // Launch the new version setup screen if needed. We do this -after-
1468 // launching the initial activity (that is, home), so that it can have
1469 // a chance to initialize itself while in the background, making the
1470 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001471 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001472 mService.startSetupActivityLocked();
1473 }
1474
Dianne Hackborn465fa392014-09-14 14:21:18 -07001475 // Update any services we are bound to that might care about whether
1476 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001477 if (r.app != null) {
1478 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1479 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001480
Craig Mautner2420ead2013-04-01 17:13:20 -07001481 return true;
1482 }
1483
Craig Mautnere79d42682013-04-01 19:01:53 -07001484 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001485 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001486 // Is this activity's application already running?
1487 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001488 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001489
Andrii Kulian02b7a832016-10-06 23:11:56 -07001490 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001491
1492 if (app != null && app.thread != null) {
1493 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001494 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1495 || !"android".equals(r.info.packageName)) {
1496 // Don't add this if it is a platform component that is marked
1497 // to run in multiple processes, because this is actually
1498 // part of the framework so doesn't make sense to track as a
1499 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001500 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1501 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001502 }
George Mount2c92c972014-03-20 09:38:23 -07001503 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001504 return;
1505 } catch (RemoteException e) {
1506 Slog.w(TAG, "Exception when starting activity "
1507 + r.intent.getComponent().flattenToShortString(), e);
1508 }
1509
1510 // If a dead object exception was thrown -- fall through to
1511 // restart the application.
1512 }
1513
1514 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001515 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001516 }
1517
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001518 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001519 String resultWho, int requestCode, int callingPid, int callingUid,
1520 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001521 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001522 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1523 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001524 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001525 return true;
1526 }
1527 final int componentRestriction = getComponentRestrictionForCallingPackage(
1528 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1529 final int actionRestriction = getActionRestrictionForCallingPackage(
1530 intent.getAction(), callingPackage, callingPid, callingUid);
1531 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1532 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1533 if (resultRecord != null) {
1534 resultStack.sendActivityResultLocked(-1,
1535 resultRecord, resultWho, requestCode,
1536 Activity.RESULT_CANCELED, null);
1537 }
1538 final String msg;
1539 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1540 msg = "Permission Denial: starting " + intent.toString()
1541 + " from " + callerApp + " (pid=" + callingPid
1542 + ", uid=" + callingUid + ")" + " with revoked permission "
1543 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1544 } else if (!aInfo.exported) {
1545 msg = "Permission Denial: starting " + intent.toString()
1546 + " from " + callerApp + " (pid=" + callingPid
1547 + ", uid=" + callingUid + ")"
1548 + " not exported from uid " + aInfo.applicationInfo.uid;
1549 } else {
1550 msg = "Permission Denial: starting " + intent.toString()
1551 + " from " + callerApp + " (pid=" + callingPid
1552 + ", uid=" + callingUid + ")"
1553 + " requires " + aInfo.permission;
1554 }
1555 Slog.w(TAG, msg);
1556 throw new SecurityException(msg);
1557 }
1558
1559 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1560 final String message = "Appop Denial: starting " + intent.toString()
1561 + " from " + callerApp + " (pid=" + callingPid
1562 + ", uid=" + callingUid + ")"
1563 + " requires " + AppOpsManager.permissionToOp(
1564 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1565 Slog.w(TAG, message);
1566 return false;
1567 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1568 final String message = "Appop Denial: starting " + intent.toString()
1569 + " from " + callerApp + " (pid=" + callingPid
1570 + ", uid=" + callingUid + ")"
1571 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1572 Slog.w(TAG, message);
1573 return false;
1574 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001575 if (options != null) {
1576 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1577 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1578 callingPid, callingUid);
1579 if (startInTaskPerm != PERMISSION_GRANTED) {
1580 final String msg = "Permission Denial: starting " + intent.toString()
1581 + " from " + callerApp + " (pid=" + callingPid
1582 + ", uid=" + callingUid + ") with launchTaskId="
1583 + options.getLaunchTaskId();
1584 Slog.w(TAG, msg);
1585 throw new SecurityException(msg);
1586 }
1587 }
1588 // Check if someone tries to launch an activity on a private display with a different
1589 // owner.
1590 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001591 if (launchDisplayId != INVALID_DISPLAY
1592 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1593 final String msg = "Permission Denial: starting " + intent.toString()
1594 + " from " + callerApp + " (pid=" + callingPid
1595 + ", uid=" + callingUid + ") with launchDisplayId="
1596 + launchDisplayId;
1597 Slog.w(TAG, msg);
1598 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001599 }
1600 }
1601
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001602 return true;
1603 }
1604
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001605 /** Check if caller is allowed to launch activities on specified display. */
1606 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1607 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1608 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1609
1610 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1611 if (activityDisplay == null) {
1612 Slog.w(TAG, "Launch on display check: display not found");
1613 return false;
1614 }
1615
1616 if (!activityDisplay.isPrivate()) {
1617 // Anyone can launch on a public display.
1618 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1619 + " allow launch on public display");
1620 return true;
1621 }
1622
1623 // Check if the caller is the owner of the display.
1624 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1625 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1626 + " allow launch for owner of the display");
1627 return true;
1628 }
1629
1630 // Check if caller is present on display
1631 if (activityDisplay.isUidPresent(callingUid)) {
1632 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1633 + " allow launch for caller present on the display");
1634 return true;
1635 }
1636
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001637 // Check if the caller can manage activity stacks.
1638 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001639 callingUid);
1640 if (startAnyPerm == PERMISSION_GRANTED) {
1641 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1642 + " allow launch any on display");
1643 return true;
1644 }
1645
1646 Slog.w(TAG, "Launch on display check: denied");
1647 return false;
1648 }
1649
1650 /** Update lists of UIDs that are present on displays and have access to them. */
1651 void updateUIDsPresentOnDisplay() {
1652 mDisplayAccessUIDs.clear();
1653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1654 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001655 // Only bother calculating the whitelist for private displays
1656 if (activityDisplay.isPrivate()) {
1657 mDisplayAccessUIDs.append(
1658 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1659 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001660 }
1661 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1662 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1663 }
1664
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001665 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001666 final long identity = Binder.clearCallingIdentity();
1667 try {
1668 return UserManager.get(mService.mContext).getUserInfo(userId);
1669 } finally {
1670 Binder.restoreCallingIdentity(identity);
1671 }
1672 }
1673
Svet Ganov99b60432015-06-27 13:15:22 -07001674 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001675 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001676 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1677 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001678 == PackageManager.PERMISSION_DENIED) {
1679 return ACTIVITY_RESTRICTION_PERMISSION;
1680 }
1681
Christopher Tateff7add02015-08-17 10:23:22 -07001682 if (activityInfo.permission == null) {
1683 return ACTIVITY_RESTRICTION_NONE;
1684 }
1685
Svet Ganov99b60432015-06-27 13:15:22 -07001686 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1687 if (opCode == AppOpsManager.OP_NONE) {
1688 return ACTIVITY_RESTRICTION_NONE;
1689 }
1690
1691 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1692 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001693 if (!ignoreTargetSecurity) {
1694 return ACTIVITY_RESTRICTION_APPOP;
1695 }
Svet Ganov99b60432015-06-27 13:15:22 -07001696 }
1697
1698 return ACTIVITY_RESTRICTION_NONE;
1699 }
1700
Svetoslav7008b512015-06-24 18:47:07 -07001701 private int getActionRestrictionForCallingPackage(String action,
1702 String callingPackage, int callingPid, int callingUid) {
1703 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001704 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001705 }
1706
1707 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1708 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001709 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001710 }
1711
1712 final PackageInfo packageInfo;
1713 try {
1714 packageInfo = mService.mContext.getPackageManager()
1715 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1716 } catch (PackageManager.NameNotFoundException e) {
1717 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001718 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001719 }
1720
1721 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001722 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001723 }
1724
1725 if (mService.checkPermission(permission, callingPid, callingUid) ==
1726 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001727 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001728 }
1729
1730 final int opCode = AppOpsManager.permissionToOpCode(permission);
1731 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001732 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001733 }
1734
1735 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1736 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001737 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001738 }
1739
Svet Ganov99b60432015-06-27 13:15:22 -07001740 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001741 }
1742
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001743 void setLaunchSource(int uid) {
1744 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1745 }
1746
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001747 void acquireLaunchWakelock() {
1748 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1749 throw new IllegalStateException("Calling must be system uid");
1750 }
1751 mLaunchingActivity.acquire();
1752 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1753 // To be safe, don't allow the wake lock to be held for too long.
1754 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1755 }
1756 }
1757
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001758 /**
1759 * Called when the frontmost task is idle.
1760 * @return the state of mService.mBooting before this was called.
1761 */
1762 private boolean checkFinishBootingLocked() {
1763 final boolean booting = mService.mBooting;
1764 boolean enableScreen = false;
1765 mService.mBooting = false;
1766 if (!mService.mBooted) {
1767 mService.mBooted = true;
1768 enableScreen = true;
1769 }
1770 if (booting || enableScreen) {
1771 mService.postFinishBooting(booting, enableScreen);
1772 }
1773 return booting;
1774 }
1775
Craig Mautnerf3333272013-04-22 10:55:53 -07001776 // Checked.
1777 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001778 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001779 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001780
Craig Mautnerf3333272013-04-22 10:55:53 -07001781 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001782 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001783 int NS = 0;
1784 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001785 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001786 boolean activityRemoved = false;
1787
Wale Ogunwale7d701172015-03-11 15:36:30 -07001788 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001789 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001790 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1791 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001792 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1793 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001794 if (fromTimeout) {
1795 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001796 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001797
1798 // This is a hack to semi-deal with a race condition
1799 // in the client where it can be constructed with a
1800 // newer configuration from when we asked it to launch.
1801 // We'll update with whatever configuration it now says
1802 // it used to launch.
1803 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001804 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001805 }
1806
1807 // We are now idle. If someone is waiting for a thumbnail from
1808 // us, we can now deliver.
1809 r.idle = true;
1810
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001811 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001812 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001813 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001814 }
1815 }
1816
1817 if (allResumedActivitiesIdle()) {
1818 if (r != null) {
1819 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001820 }
1821
1822 if (mLaunchingActivity.isHeld()) {
1823 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1824 if (VALIDATE_WAKE_LOCK_CALLER &&
1825 Binder.getCallingUid() != Process.myUid()) {
1826 throw new IllegalStateException("Calling must be system uid");
1827 }
1828 mLaunchingActivity.release();
1829 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001830 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001831 }
1832
1833 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001834 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1835 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001836 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001837 if ((NF = mFinishingActivities.size()) > 0) {
1838 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001839 mFinishingActivities.clear();
1840 }
1841
Craig Mautnerf3333272013-04-22 10:55:53 -07001842 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001843 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001844 mStartingUsers.clear();
1845 }
1846
Craig Mautnerf3333272013-04-22 10:55:53 -07001847 // Stop any activities that are scheduled to do so but have been
1848 // waiting for the next one to start.
1849 for (int i = 0; i < NS; i++) {
1850 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001851 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001852 if (stack != null) {
1853 if (r.finishing) {
1854 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1855 } else {
1856 stack.stopActivityLocked(r);
1857 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001858 }
1859 }
1860
1861 // Finish any activities that are scheduled to do so but have been
1862 // waiting for the next one to start.
1863 for (int i = 0; i < NF; i++) {
1864 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001865 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001866 if (stack != null) {
1867 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1868 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001869 }
1870
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001871 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001872 // Complete user switch
1873 if (startingUsers != null) {
1874 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001875 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001876 }
1877 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001878 }
1879
1880 mService.trimApplications();
1881 //dump();
1882 //mWindowManager.dump();
1883
Craig Mautnerf3333272013-04-22 10:55:53 -07001884 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001885 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001886 }
1887
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001888 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001889 }
1890
Craig Mautner8e569572013-10-11 17:36:59 -07001891 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001892 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001893 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1894 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001895 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1896 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1897 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001898 }
Craig Mautner19091252013-10-05 00:03:53 -07001899 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001900 }
1901
1902 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001903 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1904 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001905 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1906 stacks.get(stackNdx).closeSystemDialogsLocked();
1907 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001908 }
1909 }
1910
Craig Mautner93529a42013-10-04 15:03:13 -07001911 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001912 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001913 }
1914
Craig Mautner8d341ef2013-03-26 09:03:27 -07001915 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001916 * Update the last used stack id for non-current user (current user's last
1917 * used stack is the focused stack)
1918 */
1919 void updateUserStackLocked(int userId, ActivityStack stack) {
1920 if (userId != mCurrentUser) {
1921 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1922 }
1923 }
1924
1925 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001926 * @return true if some activity was finished (or would have finished if doit were true).
1927 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001928 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1929 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001930 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001931 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1932 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001933 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001934 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001935 if (stack.finishDisabledPackageActivitiesLocked(
1936 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001937 didSomething = true;
1938 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001939 }
1940 }
1941 return didSomething;
1942 }
1943
Dianne Hackborna413dc02013-07-12 12:02:55 -07001944 void updatePreviousProcessLocked(ActivityRecord r) {
1945 // Now that this process has stopped, we may want to consider
1946 // it to be the previous app to try to keep around in case
1947 // the user wants to return to it.
1948
1949 // First, found out what is currently the foreground app, so that
1950 // we don't blow away the previous app if this activity is being
1951 // hosted by the process that is actually still the foreground.
1952 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001953 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1954 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001955 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1956 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001957 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001958 if (stack.mResumedActivity != null) {
1959 fgApp = stack.mResumedActivity.app;
1960 } else if (stack.mPausingActivity != null) {
1961 fgApp = stack.mPausingActivity.app;
1962 }
1963 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001964 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001965 }
1966 }
1967
1968 // Now set this one as the previous process, only if that really
1969 // makes sense to.
1970 if (r.app != null && fgApp != null && r.app != fgApp
1971 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001972 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001973 mService.mPreviousProcess = r.app;
1974 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1975 }
1976 }
1977
Wale Ogunwaled046a012015-12-24 13:05:59 -08001978 boolean resumeFocusedStackTopActivityLocked() {
1979 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001980 }
1981
Wale Ogunwaled046a012015-12-24 13:05:59 -08001982 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001983 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001984 if (targetStack != null && isFocusedStack(targetStack)) {
1985 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001986 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001987 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1988 if (r == null || r.state != RESUMED) {
1989 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001990 } else if (r.state == RESUMED) {
1991 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1992 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001993 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001994 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001995 }
1996
Todd Kennedy39bfee52016-02-24 10:28:21 -08001997 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1998 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1999 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2000 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2001 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2002 }
2003 }
2004 }
2005
Adrian Roos20d7df32016-01-12 18:59:43 +01002006 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2007 TaskRecord finishedTask = null;
2008 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002009 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2010 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002011 final int numStacks = stacks.size();
2012 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2013 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002014 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2015 if (stack == focusedStack || finishedTask == null) {
2016 finishedTask = t;
2017 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002018 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002019 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002020 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002021 }
2022
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002023 void finishVoiceTask(IVoiceInteractionSession session) {
2024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2025 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2026 final int numStacks = stacks.size();
2027 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2028 final ActivityStack stack = stacks.get(stackNdx);
2029 stack.finishVoiceTask(session);
2030 }
2031 }
2032 }
2033
Andrii Kulianc27916642016-04-12 17:59:27 -07002034 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2035 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002036 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2037 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002038 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002039 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2040 // Caller wants the home activity moved with it. To accomplish this,
2041 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002042 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002043 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002044 ActivityStack currentStack = task.getStack();
2045 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002046 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2047 + task + " to front. Stack is null");
2048 return;
2049 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002050
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002051 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002052 int stackId = options.getLaunchStackId();
2053 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002054 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002055 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002056 if (stackId == INVALID_STACK_ID) {
2057 stackId = task.getLaunchStackId();
2058 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002059 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002060 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2061 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002062 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002063 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2064 // still need moveTaskToFrontLocked() below for any transition settings.
2065 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002066 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2067 resizeStackLocked(stackId, bounds,
2068 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002069 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002070 } else {
2071 // WM resizeTask must be done after the task is moved to the correct stack,
2072 // because Task's setBounds() also updates dim layer's bounds, but that has
2073 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002074 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002075 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002076 }
2077 }
2078
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002079 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002080 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002081 r == null ? null : r.appTimeTracker, reason);
2082
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002083 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002084 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002085
Andrii Kulian02b7a832016-10-06 23:11:56 -07002086 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002087 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002088 }
2089
Wale Ogunwale854809c2015-12-27 16:18:19 -08002090 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002091 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002092 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002093 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002094 return false;
2095 }
2096 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2097 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002098 }
2099
Winson Chung55893332017-02-17 17:13:10 -08002100 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002101 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002102 }
2103
Winson Chung55893332017-02-17 17:13:10 -08002104 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2105 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002106 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002107 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002108 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002109 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002110 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002111 return null;
2112 }
Matthew Ng330757d2017-02-28 14:19:17 -08002113 if (stackId == DOCKED_STACK_ID) {
2114 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2115 // the other side of the docked stack and we make visibility decisions based on that.
2116 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2117 }
Winson Chung55893332017-02-17 17:13:10 -08002118 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002119 }
2120
Andrii Kulian16802aa2016-11-02 12:21:33 -07002121 /**
2122 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2123 * If there is no such stack, new dynamic stack can be created.
2124 * @param displayId Target display.
2125 * @param r Activity that should be launched there.
2126 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2127 */
2128 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2129 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2130 if (activityDisplay == null) {
2131 throw new IllegalArgumentException(
2132 "Display with displayId=" + displayId + " not found.");
2133 }
2134
2135 // Return the topmost valid stack on the display.
2136 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2137 final ActivityStack stack = activityDisplay.mStacks.get(i);
2138 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2139 return stack;
2140 }
2141 }
2142
2143 // If there is no valid stack on the external display - check if new dynamic stack will do.
2144 if (displayId != Display.DEFAULT_DISPLAY) {
2145 final int newDynamicStackId = getNextStackId();
2146 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2147 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2148 }
2149 }
2150
2151 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2152 return null;
2153 }
2154
Craig Mautner967212c2013-04-13 21:10:58 -07002155 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002156 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002157 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2158 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002159 }
2160 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002161 }
2162
Jorim Jaggife762342016-10-13 14:33:27 +02002163 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2164 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2165 }
2166
Andrii Kulian7fc22812016-12-28 13:04:11 -08002167 /**
2168 * Get next focusable stack in the system. This will search across displays and stacks
2169 * in last-focused order for a focusable and visible stack, different from the target stack.
2170 *
2171 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2172 * searching for next candidate.
2173 * @return Next focusable {@link ActivityStack}, null if not found.
2174 */
2175 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2176 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2177
2178 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2179 final int displayId = mTmpOrderedDisplayIds.get(i);
2180 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2181 if (stacks == null) {
2182 continue;
2183 }
2184 for (int j = stacks.size() - 1; j >= 0; --j) {
2185 final ActivityStack stack = stacks.get(j);
2186 if (stack != currentFocus && stack.isFocusable()
2187 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2188 return stack;
2189 }
2190 }
2191 }
2192
2193 return null;
2194 }
2195
Craig Mautneree2e45a2014-06-27 12:10:03 -07002196 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002197 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002198 }
2199
2200 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002201 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2202 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2203 final TaskRecord task = tasks.get(taskNdx);
2204 if (task.isHomeTask()) {
2205 final ArrayList<ActivityRecord> activities = task.mActivities;
2206 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2207 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002208 if (r.isHomeActivity()
2209 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002210 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002211 }
2212 }
2213 }
2214 }
2215 return null;
2216 }
2217
Chong Zhangb15758a2015-11-17 12:12:03 -08002218 /**
2219 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2220 * the docked stack itself, or if it's side-by-side to the docked stack.
2221 */
2222 boolean isStackDockedInEffect(int stackId) {
2223 return stackId == DOCKED_STACK_ID ||
2224 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2225 }
2226
Todd Kennedyca4d8422015-01-15 15:19:22 -08002227 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002228 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002229 ActivityContainer activityContainer =
2230 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002231 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002232 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2233 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002234 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002235 return activityContainer;
2236 }
2237
Craig Mautner34b73df2014-01-12 21:11:08 -08002238 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002239 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2240 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2241 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002242 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2243 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002244 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002245 }
2246 }
2247
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002248 void deleteActivityContainerRecord(int stackId) {
2249 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2250 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2251 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002252 }
2253
Jorim Jaggidc249c42015-12-15 14:57:31 -08002254 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002255 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002256 if (stackId == DOCKED_STACK_ID) {
2257 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002258 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002259 return;
2260 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002261 final ActivityStack stack = getStack(stackId);
2262 if (stack == null) {
2263 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2264 return;
2265 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002266
Winson Chunga2249ac2017-03-30 18:15:30 -07002267 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2268 getStack(DOCKED_STACK_ID) != null) {
2269 // If the docked stack exists, don't resize non-floating stacks independently of the
2270 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002271 return;
2272 }
2273
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002274 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002275 mWindowManager.deferSurfaceLayout();
2276 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002277 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002278 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002279 stack.ensureVisibleActivitiesConfigurationLocked(
2280 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002281 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002282 } finally {
2283 mWindowManager.continueSurfaceLayout();
2284 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002285 }
2286 }
2287
Jorim Jaggi192086e2016-03-11 17:17:03 +01002288 void deferUpdateBounds(int stackId) {
2289 final ActivityStack stack = getStack(stackId);
2290 if (stack != null) {
2291 stack.deferUpdateBounds();
2292 }
2293 }
2294
2295 void continueUpdateBounds(int stackId) {
2296 final ActivityStack stack = getStack(stackId);
2297 if (stack != null) {
2298 stack.continueUpdateBounds();
2299 }
2300 }
2301
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002302 void notifyAppTransitionDone() {
2303 continueUpdateBounds(HOME_STACK_ID);
2304 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2305 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002306 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002307 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002308 if (task != null) {
2309 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002310 }
2311 }
2312 mResizingTasksDuringAnimation.clear();
2313 }
2314
Robert Carre7cc44d2017-03-20 19:04:30 -07002315 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2316 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002317
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002318 final ActivityStack stack = getStack(fromStackId);
2319 if (stack == null) {
2320 return;
2321 }
2322
2323 mWindowManager.deferSurfaceLayout();
2324 try {
2325 if (fromStackId == DOCKED_STACK_ID) {
2326
2327 // We are moving all tasks from the docked stack to the fullscreen stack,
2328 // which is dismissing the docked stack, so resize all other stacks to
2329 // fullscreen here already so we don't end up with resize trashing.
2330 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2331 if (StackId.isResizeableByDockedStack(i)) {
2332 ActivityStack otherStack = getStack(i);
2333 if (otherStack != null) {
2334 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2335 true /* allowResizeInDockedMode */, DEFER_RESUME);
2336 }
2337 }
2338 }
2339
2340 // Also disable docked stack resizing since we have manually adjusted the
2341 // size of other stacks above and we don't want to trigger a docked stack
2342 // resize when we remove task from it below and it is detached from the
2343 // display because it no longer contains any tasks.
2344 mAllowDockedStackResize = false;
2345 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002346 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002347 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2348 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002349 // If we are moving from the pinned stack, then the animation takes care of updating
2350 // the picture-in-picture mode.
2351 final boolean schedulePictureInPictureModeChange = (fromStackId != PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002352 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2353 final int size = tasks.size();
2354 if (onTop) {
2355 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002356 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002357 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002358 if (fromStackId == PINNED_STACK_ID) {
2359 // Update the return-to to reflect where the pinned stack task was moved
2360 // from so that we retain the stack that was previously visible if the
2361 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002362 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2363 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002364 MetricsLogger.action(mService.mContext,
2365 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002366 }
Winson Chung74666102017-02-22 17:49:24 -08002367 // Defer resume until all the tasks have been moved to the fullscreen stack
2368 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2369 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002370 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002371 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002372 }
2373 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002374 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002375 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002376 final int position = fullscreenStack != null
2377 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2378 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002379 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung5af42fc2017-03-24 17:11:33 -07002380 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2381 schedulePictureInPictureModeChange,
2382 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002383 }
2384 }
Winson Chung74666102017-02-22 17:49:24 -08002385
2386 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2387 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002388 } finally {
2389 mAllowDockedStackResize = true;
2390 mWindowManager.continueSurfaceLayout();
2391 }
2392 }
2393
Robert Carr6914f082017-03-20 19:04:30 -07002394 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002395 mWindowManager.inSurfaceTransaction(
2396 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002397 }
2398
Jorim Jaggidc249c42015-12-15 14:57:31 -08002399 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002400 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2401 boolean preserveWindows) {
2402 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2403 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2404 false /* deferResume */);
2405 }
2406
2407 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2408 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2409 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002410
2411 if (!mAllowDockedStackResize) {
2412 // Docked stack resize currently disabled.
2413 return;
2414 }
2415
Jorim Jaggidc249c42015-12-15 14:57:31 -08002416 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2417 if (stack == null) {
2418 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2419 return;
2420 }
2421
2422 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2423 mWindowManager.deferSurfaceLayout();
2424 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002425 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2426 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002427 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002428 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002429
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002430 // TODO: Checking for isAttached might not be needed as if the user passes in null
2431 // dockedBounds then they want the docked stack to be dismissed.
2432 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2433 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002434 // In this case we make all other static stacks fullscreen and move all
2435 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002436 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002437
2438 // stack shouldn't contain anymore activities, so nothing to resume.
2439 r = null;
2440 } else {
2441 // Docked stacks occupy a dedicated region on screen so the size of all other
2442 // static stacks need to be adjusted so they don't overlap with the docked stack.
2443 // We get the bounds to use from window manager which has been adjusted for any
2444 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002445 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002446 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002447 final ActivityStack current = getStack(i);
2448 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002449 current.getStackDockedModeBounds(
2450 tempOtherTaskBounds /* currentTempTaskBounds */,
2451 tempRect /* outStackBounds */,
2452 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2453
Matthew Nge15352e2016-12-20 15:36:29 -08002454 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002455 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2456 tempOtherTaskInsetBounds, preserveWindows,
2457 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002458 }
2459 }
2460 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002461 if (!deferResume) {
2462 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2463 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002464 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002465 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002466 mWindowManager.continueSurfaceLayout();
2467 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2468 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002469 }
2470
Robert Carr0d00c2e2016-02-29 17:45:02 -08002471 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2472 final ActivityStack stack = getStack(PINNED_STACK_ID);
2473 if (stack == null) {
2474 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2475 return;
2476 }
2477 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2478 mWindowManager.deferSurfaceLayout();
2479 try {
2480 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002481 Rect insetBounds = null;
2482 if (tempPinnedTaskBounds != null) {
2483 // We always use 0,0 as the position for the inset rect because
2484 // if we are getting insets at all in the pinned stack it must mean
2485 // we are headed for fullscreen.
2486 insetBounds = tempRect;
2487 insetBounds.top = 0;
2488 insetBounds.left = 0;
2489 insetBounds.right = tempPinnedTaskBounds.width();
2490 insetBounds.bottom = tempPinnedTaskBounds.height();
2491 }
2492 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002493 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002494 } finally {
2495 mWindowManager.continueSurfaceLayout();
2496 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2497 }
2498 }
2499
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002500 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002501 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002502 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002503 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002504 }
2505
Wale Ogunwale1666e312016-12-16 11:27:18 -08002506 final ActivityContainer activityContainer =
2507 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002508 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002509 }
2510
Robert Carr6914f082017-03-20 19:04:30 -07002511
Robert Carre7cc44d2017-03-20 19:04:30 -07002512 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002513 final ActivityStack stack = getStack(stackId);
2514 if (stack == null) {
2515 return;
2516 }
2517
2518 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2519 if (stack.getStackId() == PINNED_STACK_ID) {
2520 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2521 if (fullscreenStack != null) {
2522 final boolean isFullscreenStackVisible =
2523 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2524 for (int i = 0; i < tasks.size(); i++) {
2525 // Insert the task either at the top of the fullscreen stack if it is hidden,
Winson Chungd62acec2017-03-31 19:42:32 -07002526 // or to the bottom if it is currently visible
2527 final int insertPosition = isFullscreenStackVisible ? 0
Winson Chung010927a2016-12-15 16:12:35 -08002528 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002529 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002530 // Defer resume until we remove all the tasks
2531 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2532 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002533 }
2534 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2535 resumeFocusedStackTopActivityLocked();
2536 } else {
2537 // If there is no fullscreen stack, then create the stack and move all the tasks
2538 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002539 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002540 }
2541 } else {
2542 for (int i = tasks.size() - 1; i >= 0; i--) {
2543 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2544 REMOVE_FROM_RECENTS);
2545 }
2546 }
2547 }
2548
2549 /**
Robert Carr6914f082017-03-20 19:04:30 -07002550 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2551 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2552 * instead moved back onto the fullscreen stack.
2553 */
2554 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002555 mWindowManager.inSurfaceTransaction(
2556 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002557 }
2558
2559 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002560 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2561 */
2562 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2563 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2564 }
2565
2566 /**
Winson Chung010927a2016-12-15 16:12:35 -08002567 * Removes the task with the specified task id.
2568 *
2569 * @param taskId Identifier of the task to be removed.
2570 * @param killProcess Kill any process associated with the task if possible.
2571 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002572 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2573 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002574 * @return Returns true if the given task was found and removed.
2575 */
Winson Chung6954fc92017-03-24 16:22:12 -07002576 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2577 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002578 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2579 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002580 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002581 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002582 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2583 if (tr.isPersistable) {
2584 mService.notifyTaskPersisterLocked(null, true);
2585 }
2586 return true;
2587 }
2588 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2589 return false;
2590 }
2591
2592 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2593 if (removeFromRecents) {
2594 mRecentTasks.remove(tr);
2595 tr.removedFromRecents();
2596 }
2597 ComponentName component = tr.getBaseIntent().getComponent();
2598 if (component == null) {
2599 Slog.w(TAG, "No component for base intent of task: " + tr);
2600 return;
2601 }
2602
2603 // Find any running services associated with this app and stop if needed.
2604 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2605
2606 if (!killProcess) {
2607 return;
2608 }
2609
2610 // Determine if the process(es) for this task should be killed.
2611 final String pkg = component.getPackageName();
2612 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2613 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2614 for (int i = 0; i < pmap.size(); i++) {
2615
2616 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2617 for (int j = 0; j < uids.size(); j++) {
2618 ProcessRecord proc = uids.valueAt(j);
2619 if (proc.userId != tr.userId) {
2620 // Don't kill process for a different user.
2621 continue;
2622 }
2623 if (proc == mService.mHomeProcess) {
2624 // Don't kill the home process along with tasks from the same package.
2625 continue;
2626 }
2627 if (!proc.pkgList.containsKey(pkg)) {
2628 // Don't kill process that is not associated with this task.
2629 continue;
2630 }
2631
2632 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002633 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002634 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2635 // Don't kill process(es) that has an activity in a different task that is
2636 // also in recents.
2637 return;
2638 }
2639 }
2640
2641 if (proc.foregroundServices) {
2642 // Don't kill process(es) with foreground service.
2643 return;
2644 }
2645
2646 // Add process to kill list.
2647 procsToKill.add(proc);
2648 }
2649 }
2650
2651 // Kill the running processes.
2652 for (int i = 0; i < procsToKill.size(); i++) {
2653 ProcessRecord pr = procsToKill.get(i);
2654 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2655 && pr.curReceivers.isEmpty()) {
2656 pr.kill("remove task", true);
2657 } else {
2658 // We delay killing processes that are not in the background or running a receiver.
2659 pr.waitingToKill = "remove task";
2660 }
2661 }
2662 }
2663
Craig Mautner4a1cb222013-12-04 16:14:06 -08002664 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002665 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002666 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2667 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002668 break;
2669 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002670 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002671 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002672 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002673 }
2674
Chong Zhang5dcb2752015-08-18 13:50:26 -07002675 /**
2676 * Restores a recent task to a stack
2677 * @param task The recent task to be restored.
2678 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002679 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2680 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002681 * @return true if the task has been restored successfully.
2682 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002683 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002684 if (!StackId.isStaticStack(stackId)) {
2685 // If stack is not static (or stack id is invalid) - use the default one.
2686 // This means that tasks that were on external displays will be restored on the
2687 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002688 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002689 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002690 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2691 // Put it in the fullscreen stack.
2692 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002693 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002694
Andrii Kulian02b7a832016-10-06 23:11:56 -07002695 final ActivityStack currentStack = task.getStack();
2696 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002697 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002698 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002699 // Nothing else to do since it is already restored in the right stack.
2700 return true;
2701 }
2702 // Remove current stack association, so we can re-associate the task with the
2703 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002704 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002705 }
2706
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002707 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002708 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002709
2710 if (stack == null) {
2711 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002712 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2713 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002714 return false;
2715 }
2716
Wale Ogunwale72919d22016-12-08 18:58:50 -08002717 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002718 // TODO: move call for creation here and other place into Stack.addTask()
2719 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002720 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2721 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002722 final ArrayList<ActivityRecord> activities = task.mActivities;
2723 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002724 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002725 }
2726 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002727 }
2728
Wale Ogunwale040b4702015-08-06 18:10:50 -07002729 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002730 * Move stack with all its existing content to specified display.
2731 * @param stackId Id of stack to move.
2732 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002733 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002734 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002735 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002736 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2737 if (activityDisplay == null) {
2738 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2739 + displayId);
2740 }
2741 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2742 if (activityContainer != null) {
2743 if (activityContainer.isAttachedLocked()) {
2744 if (activityContainer.getDisplayId() == displayId) {
2745 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2746 + " to its current displayId=" + displayId);
2747 }
2748
Andrii Kulian250d6532017-02-08 23:30:45 -08002749 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002750 } else {
2751 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2752 + stackId + " is not attached to any display.");
2753 }
2754 } else {
2755 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2756 + stackId);
2757 }
2758
2759 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2760 !PRESERVE_WINDOWS);
2761 // TODO(multi-display): resize stacks properly if moved from split-screen.
2762 }
2763
2764 /**
Winson Chung74666102017-02-22 17:49:24 -08002765 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2766 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002767 */
Winson Chung74666102017-02-22 17:49:24 -08002768 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002769 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002770
Winson Chung74666102017-02-22 17:49:24 -08002771 // Check that we aren't reparenting to the same stack that the task is already in
2772 if (prevStack != null && prevStack.mStackId == stackId) {
2773 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2774 + " already in stackId=" + stackId);
2775 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002776 }
2777
Winson Chung74666102017-02-22 17:49:24 -08002778 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2779 // support
2780 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2781 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2782 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002783 }
2784
Winson Chung74666102017-02-22 17:49:24 -08002785 // Ensure that we aren't trying to move into a freeform stack without freeform
2786 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002787 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002788 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2789 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002790 }
2791
Winson Chung74666102017-02-22 17:49:24 -08002792 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2793 // used for split-screen mode and will cause things like the docked divider to show up. We
2794 // instead leave the task in its current stack or move it to the fullscreen stack if it
2795 // isn't currently in a stack.
2796 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2797 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2798 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2799 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002800 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002801
Winson Chung74666102017-02-22 17:49:24 -08002802 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2803 // example, a docked stack is created which will lead to the stack we are moving from being
2804 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002805 try {
Winson Chung74666102017-02-22 17:49:24 -08002806 task.mTemporarilyUnresizable = true;
2807 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002808 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002809 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002810 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002811 }
2812
Winson Chung08f81892017-03-02 15:40:51 -08002813 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002814 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2815 if (stack == null) {
2816 throw new IllegalArgumentException(
2817 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2818 }
2819
2820 final ActivityRecord r = stack.topRunningActivityLocked();
2821 if (r == null) {
2822 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2823 + " in stack=" + stack);
2824 return false;
2825 }
2826
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002827 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002828 Slog.w(TAG,
2829 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002830 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002831 return false;
2832 }
2833
Winson Chung08f81892017-03-02 15:40:51 -08002834 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, destBounds,
2835 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002836 return true;
2837 }
2838
Winson Chung08f81892017-03-02 15:40:51 -08002839 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceBounds, Rect destBounds,
2840 boolean moveHomeStackToFront, String reason) {
2841
Wale Ogunwale480dca02016-02-06 13:58:29 -08002842 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002843
Wale Ogunwale1666e312016-12-16 11:27:18 -08002844 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002845 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002846
Wale Ogunwale480dca02016-02-06 13:58:29 -08002847 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002848 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002849
Andrii Kulian02b7a832016-10-06 23:11:56 -07002850 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002851 // An activity can't be pinned and visible behind at the same time. Go ahead and
2852 // release it from been visible behind before pinning.
2853 requestVisibleBehindLocked(r, false);
2854 }
2855
Wale Ogunwale480dca02016-02-06 13:58:29 -08002856 // Resize the pinned stack to match the current size of the task the activity we are
2857 // going to be moving is currently contained in. We do this to have the right starting
2858 // animation bounds for the pinned stack to the desired bounds the caller wants.
2859 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2860 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002861 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002862
2863 if (task.mActivities.size() == 1) {
2864 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002865 // the stack without re-parenting the activity in a different task. We don't
2866 // move the home stack forward if we are currently entering picture-in-picture
2867 // while pausing because that changes the focused stack and may prevent the new
2868 // starting activity from resuming.
2869 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002870 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002871 // Move the home stack forward if the task we just moved to the pinned stack
2872 // was launched from home so home should be visible behind it.
2873 moveHomeStackToFront(reason);
2874 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002875 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002876 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002877 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002878 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002879 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002880 // reveal/leave the other activities in their original task.
2881
2882 // Currently, we don't support reparenting activities across tasks in two different
2883 // stacks, so instead, just create a new task in the same stack, reparent the
2884 // activity into that task, and then reparent the whole task to the new stack. This
2885 // ensures that all the necessary work to migrate states in the old and new stacks
2886 // is also done.
2887 final TaskRecord newTask = task.getStack().createTaskRecord(
2888 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2889 r.mActivityType);
2890 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2891
Winson Chung5af42fc2017-03-24 17:11:33 -07002892 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002893 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002894 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002895 }
Winson Chungc2baac02017-01-11 13:34:47 -08002896
2897 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2898 // to the pinned stack
2899 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002900 } finally {
2901 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002902 }
2903
Wale Ogunwale480dca02016-02-06 13:58:29 -08002904 // The task might have already been running and its visibility needs to be synchronized
2905 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002906 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002907 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002908
Winson Chung08f81892017-03-02 15:40:51 -08002909 stack.animateResizePinnedStack(sourceBounds, destBounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002910 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002911 }
2912
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002913 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002914 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2915 if (r == null || !r.isFocusable()) {
2916 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2917 "moveActivityStackToFront: unfocusable r=" + r);
2918 return false;
2919 }
2920
Bryce Leeaf691c02017-03-20 14:20:22 -07002921 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002922 final ActivityStack stack = r.getStack();
2923 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002924 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2925 + r + " task=" + task);
2926 return false;
2927 }
2928
Chong Zhang6cda19c2016-06-14 19:07:56 -07002929 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2930 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2931 "moveActivityStackToFront: already on top, r=" + r);
2932 return false;
2933 }
2934
2935 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2936 "moveActivityStackToFront: r=" + r);
2937
2938 stack.moveToFront(reason, task);
2939 return true;
2940 }
2941
David Stevensc6b91c62017-02-08 14:23:58 -08002942 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002943 mTmpFindTaskResult.r = null;
2944 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002945 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002946 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002947 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2948 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002949 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2950 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002951 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002952 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002953 continue;
2954 }
2955 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002956 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2957 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002958 continue;
2959 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002960 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002961 // It is possible to have tasks in multiple stacks with the same root affinity, so
2962 // we should keep looking after finding an affinity match to see if there is a
2963 // better match in another stack. Also, task affinity isn't a good enough reason
2964 // to target a display which isn't the source of the intent, so skip any affinity
2965 // matches not on the specified display.
2966 if (mTmpFindTaskResult.r != null) {
2967 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2968 return mTmpFindTaskResult.r;
2969 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
2970 affinityMatch = mTmpFindTaskResult.r;
2971 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002972 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002973 }
2974 }
David Stevensc6b91c62017-02-08 14:23:58 -08002975 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
2976 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002977 }
2978
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002979 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2980 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002981 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2982 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002983 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002984 final ActivityRecord ar = stacks.get(stackNdx)
2985 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002986 if (ar != null) {
2987 return ar;
2988 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002989 }
2990 }
2991 return null;
2992 }
2993
Craig Mautner8d341ef2013-03-26 09:03:27 -07002994 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002995 scheduleSleepTimeout();
2996 if (!mGoingToSleep.isHeld()) {
2997 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002998 if (mLaunchingActivity.isHeld()) {
2999 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3000 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003001 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003002 mLaunchingActivity.release();
3003 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003004 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003005 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003006 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003007 }
3008
3009 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003010 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003011
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003012 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003013 final long endTime = System.currentTimeMillis() + timeout;
3014 while (true) {
3015 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3017 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003018 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3019 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3020 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003021 }
3022 if (cantShutdown) {
3023 long timeRemaining = endTime - System.currentTimeMillis();
3024 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003025 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003026 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027 } catch (InterruptedException e) {
3028 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003029 } else {
3030 Slog.w(TAG, "Activity manager shutdown timed out");
3031 timedout = true;
3032 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003033 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003034 } else {
3035 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003036 }
3037 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003038
3039 // Force checkReadyForSleep to complete.
3040 mSleepTimeout = true;
3041 checkReadyForSleepLocked();
3042
Craig Mautner8d341ef2013-03-26 09:03:27 -07003043 return timedout;
3044 }
3045
3046 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003047 removeSleepTimeouts();
3048 if (mGoingToSleep.isHeld()) {
3049 mGoingToSleep.release();
3050 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003051 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3052 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003053 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3054 final ActivityStack stack = stacks.get(stackNdx);
3055 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003056 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003057 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003058 }
Craig Mautner5314a402013-09-26 12:40:16 -07003059 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003060 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003061 mGoingToSleepActivities.clear();
3062 }
3063
3064 void activitySleptLocked(ActivityRecord r) {
3065 mGoingToSleepActivities.remove(r);
3066 checkReadyForSleepLocked();
3067 }
3068
3069 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003070 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003071 // Do not care.
3072 return;
3073 }
3074
3075 if (!mSleepTimeout) {
3076 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003077 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3078 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003079 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3080 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3081 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003082 }
3083
3084 if (mStoppingActivities.size() > 0) {
3085 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003086 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003087 + mStoppingActivities.size() + " activities");
3088 scheduleIdleLocked();
3089 dontSleep = true;
3090 }
3091
3092 if (mGoingToSleepActivities.size() > 0) {
3093 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003094 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003095 + mGoingToSleepActivities.size() + " activities");
3096 dontSleep = true;
3097 }
3098
3099 if (dontSleep) {
3100 return;
3101 }
3102 }
3103
Wei Wang65c7a152016-06-02 18:51:22 -07003104 // Send launch end powerhint before going sleep
3105 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3106
Craig Mautnere0a38842013-12-16 16:14:02 -08003107 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3108 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003109 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3110 stacks.get(stackNdx).goToSleep();
3111 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003112 }
3113
3114 removeSleepTimeouts();
3115
3116 if (mGoingToSleep.isHeld()) {
3117 mGoingToSleep.release();
3118 }
3119 if (mService.mShuttingDown) {
3120 mService.notifyAll();
3121 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003122 }
3123
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003124 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003125 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003126 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003127 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003128 }
3129 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003130 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003131 mWindowManager.executeAppTransition();
3132 return true;
3133 }
3134 return false;
3135 }
3136
Craig Mautner8d341ef2013-03-26 09:03:27 -07003137 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003138 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3139 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003140 int stackNdx = stacks.size() - 1;
3141 while (stackNdx >= 0) {
3142 stacks.get(stackNdx).handleAppCrashLocked(app);
3143 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003144 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003145 }
3146 }
3147
Jose Lima4b6c6692014-08-12 17:41:12 -07003148 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003149 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003150 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003151 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3152 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003153 return false;
3154 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003155
3156 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3157 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3158 + " visible=" + visible + " stackId=" + stack.mStackId
3159 + " can't contain visible behind activities");
3160 return false;
3161 }
3162
Jose Lima4b6c6692014-08-12 17:41:12 -07003163 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003164 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3165 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003166
3167 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003168 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003169 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003170 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003171 return true;
3172 }
3173
3174 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003175 if (visible && top.fullscreen) {
3176 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003177 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3178 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3179 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3180 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003181 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003182 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3183 // Only the activity set as currently visible behind should actively reset its
3184 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003185 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3186 "requestVisibleBehind: returning visible=" + visible
3187 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3188 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003189 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003190 }
3191
Jose Lima4b6c6692014-08-12 17:41:12 -07003192 stack.setVisibleBehindActivity(visible ? r : null);
3193 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003194 // If there is a translucent home activity, we need to force it stop being translucent,
3195 // because we can't depend on the application to necessarily perform that operation.
3196 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003197 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003198 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003199 mService.convertFromTranslucent(next.appToken);
3200 }
3201 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003202 if (top.app != null && top.app.thread != null) {
3203 // Notify the top app of the change.
3204 try {
3205 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3206 } catch (RemoteException e) {
3207 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003208 }
3209 return true;
3210 }
3211
Craig Mautnerbb742462014-07-07 15:28:55 -07003212 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003213 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003214 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003215 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003216
3217 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003218 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003219 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003220 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003221 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003222
3223 // When launching tasks behind, update the last active time of the top task after the new
3224 // task has been shown briefly
3225 final ActivityRecord top = stack.topActivity();
3226 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003227 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003228 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003229 }
3230
3231 void scheduleLaunchTaskBehindComplete(IBinder token) {
3232 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3233 }
3234
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003235 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3236 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003237 mKeyguardController.beginActivityVisibilityUpdate();
3238 try {
3239 // First the front stacks. In case any are not fullscreen and are in front of home.
3240 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3241 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3242 final int topStackNdx = stacks.size() - 1;
3243 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3244 final ActivityStack stack = stacks.get(stackNdx);
3245 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3246 }
Craig Mautner580ea812013-04-25 12:58:38 -07003247 }
Jorim Jaggife762342016-10-13 14:33:27 +02003248 } finally {
3249 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003250 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003251 }
3252
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003253 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3254 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3255 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3256 final int topStackNdx = stacks.size() - 1;
3257 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3258 final ActivityStack stack = stacks.get(stackNdx);
3259 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3260 }
3261 }
3262 }
3263
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003264 void invalidateTaskLayers() {
3265 mTaskLayersChanged = true;
3266 }
3267
3268 void rankTaskLayersIfNeeded() {
3269 if (!mTaskLayersChanged) {
3270 return;
3271 }
3272 mTaskLayersChanged = false;
3273 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3274 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3275 int baseLayer = 0;
3276 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3277 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3278 }
3279 }
3280 }
3281
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003282 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3283 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3284 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3285 final int topStackNdx = stacks.size() - 1;
3286 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3287 final ActivityStack stack = stacks.get(stackNdx);
3288 stack.clearOtherAppTimeTrackers(except);
3289 }
3290 }
3291 }
3292
Craig Mautner8d341ef2013-03-26 09:03:27 -07003293 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003294 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3295 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003296 final int numStacks = stacks.size();
3297 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3298 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003299 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003300 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003301 }
3302 }
3303
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003304 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3305 // Examine all activities currently running in the process.
3306 TaskRecord firstTask = null;
3307 // Tasks is non-null only if two or more tasks are found.
3308 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003309 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3310 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003311 ActivityRecord r = app.activities.get(i);
3312 // First, if we find an activity that is in the process of being destroyed,
3313 // then we just aren't going to do anything for now; we want things to settle
3314 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003315 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003316 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003317 return;
3318 }
3319 // Don't consider any activies that are currently not in a state where they
3320 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003321 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3322 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003323 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003324 continue;
3325 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003326
3327 final TaskRecord task = r.getTask();
3328 if (task != null) {
3329 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003330 + " from " + r);
3331 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003332 firstTask = task;
3333 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003334 if (tasks == null) {
3335 tasks = new ArraySet<>();
3336 tasks.add(firstTask);
3337 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003338 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003339 }
3340 }
3341 }
3342 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003343 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003344 return;
3345 }
3346 // If we have activities in multiple tasks that are in a position to be destroyed,
3347 // let's iterate through the tasks and release the oldest one.
3348 final int numDisplays = mActivityDisplays.size();
3349 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3350 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3351 // Step through all stacks starting from behind, to hit the oldest things first.
3352 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3353 final ActivityStack stack = stacks.get(stackNdx);
3354 // Try to release activities in this stack; if we manage to, we are done.
3355 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3356 return;
3357 }
3358 }
3359 }
3360 }
3361
Amith Yamasani37a40c22015-06-17 13:25:42 -07003362 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003363 final int focusStackId = mFocusedStack.getStackId();
3364 // We dismiss the docked stack whenever we switch users.
3365 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003366 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3367 // also cause all tasks to be moved to the fullscreen stack at a position that is
3368 // appropriate.
3369 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003370
3371 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003372 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003373 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003374
Craig Mautner858d8a62013-04-23 17:08:34 -07003375 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003376 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3377 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003378 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003379 final ActivityStack stack = stacks.get(stackNdx);
3380 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003381 TaskRecord task = stack.topTask();
3382 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003383 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003384 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003385 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003386 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003387
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003388 ActivityStack stack = getStack(restoreStackId);
3389 if (stack == null) {
3390 stack = mHomeStack;
3391 }
3392 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003393 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003394 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003395 } else {
3396 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003397 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003398 }
Craig Mautner93529a42013-10-04 15:03:13 -07003399 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003400 }
3401
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003402 /** Checks whether the userid is a profile of the current user. */
3403 boolean isCurrentProfileLocked(int userId) {
3404 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003405 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003406 }
3407
Winson Chung4dabf232017-01-25 13:25:22 -08003408 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3409 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003410 ArrayList<ActivityRecord> stops = null;
3411
3412 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003413 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3414 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003415 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3416 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003417 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003418 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003419 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3420 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003421 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003422 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003423 if (s.finishing) {
3424 // If this activity is finishing, it is sitting on top of
3425 // everyone else but we now know it is no longer needed...
3426 // so get rid of it. Otherwise, we need to go through the
3427 // normal flow and hide it once we determine that it is
3428 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003429 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003430 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003431 }
3432 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003433 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003434 if (!processPausingActivities && s.state == PAUSING) {
3435 // Defer processing pausing activities in this iteration and reschedule
3436 // a delayed idle to reprocess it again
3437 removeTimeoutsForActivityLocked(idleActivity);
3438 scheduleIdleTimeoutLocked(idleActivity);
3439 continue;
3440 }
3441
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003442 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003443 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003444 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003445 }
3446 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003447 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003448 }
3449 }
3450
3451 return stops;
3452 }
3453
Craig Mautnercf910b02013-04-23 11:23:27 -07003454 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003455 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3456 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3457 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3458 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003459 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003460 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003461 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003462 if (r == null) Slog.e(TAG,
3463 "validateTop...: null top activity, stack=" + stack);
3464 else {
3465 final ActivityRecord pausing = stack.mPausingActivity;
3466 if (pausing != null && pausing == r) Slog.e(TAG,
3467 "validateTop...: top stack has pausing activity r=" + r
3468 + " state=" + state);
3469 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3470 "validateTop...: activity in front not resumed r=" + r
3471 + " state=" + state);
3472 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003473 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003474 final ActivityRecord resumed = stack.mResumedActivity;
3475 if (resumed != null && resumed == r) Slog.e(TAG,
3476 "validateTop...: back stack has resumed activity r=" + r
3477 + " state=" + state);
3478 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3479 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003480 }
3481 }
3482 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003483 }
3484
Craig Mautnere0570202015-05-13 13:06:11 -07003485 private String lockTaskModeToString() {
3486 switch (mLockTaskModeState) {
3487 case LOCK_TASK_MODE_LOCKED:
3488 return "LOCKED";
3489 case LOCK_TASK_MODE_PINNED:
3490 return "PINNED";
3491 case LOCK_TASK_MODE_NONE:
3492 return "NONE";
3493 default: return "unknown=" + mLockTaskModeState;
3494 }
3495 }
3496
Craig Mautner27084302013-03-25 08:05:25 -07003497 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003498 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003500 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003501 pw.print(prefix);
3502 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003503 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003504 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003505 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003506 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3507 if (packages.size() > 0) {
3508 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3509 for (int i = 0; i < packages.size(); ++i) {
3510 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3511 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3512 }
3513 }
3514 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3515 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003517
Winson43d1f262016-06-14 16:05:55 -07003518 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003519 * Dump all connected displays' configurations.
3520 * @param prefix Prefix to apply to each line of the dump.
3521 */
3522 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3523 pw.print(prefix); pw.println("Display override configurations:");
3524 final int displayCount = mActivityDisplays.size();
3525 for (int i = 0; i < displayCount; i++) {
3526 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3527 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3528 pw.println(activityDisplay.getOverrideConfiguration());
3529 }
3530 }
3531
3532 /**
Winson43d1f262016-06-14 16:05:55 -07003533 * Dumps the activities matching the given {@param name} in the either the focused stack
3534 * or all visible stacks if {@param dumpVisibleStacks} is true.
3535 */
Winson Chung6998bc42017-02-28 17:07:05 -08003536 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3537 boolean dumpFocusedStackOnly) {
3538 if (dumpFocusedStackOnly) {
3539 return mFocusedStack.getDumpActivitiesLocked(name);
3540 } else {
Winson43d1f262016-06-14 16:05:55 -07003541 ArrayList<ActivityRecord> activities = new ArrayList<>();
3542 int numDisplays = mActivityDisplays.size();
3543 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3544 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3546 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003547 if (!dumpVisibleStacksOnly ||
3548 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003549 activities.addAll(stack.getDumpActivitiesLocked(name));
3550 }
3551 }
3552 }
3553 return activities;
Winson43d1f262016-06-14 16:05:55 -07003554 }
Craig Mautner20e72272013-04-01 13:45:53 -07003555 }
3556
Dianne Hackborn390517b2013-05-30 15:03:32 -07003557 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3558 boolean needSep, String prefix) {
3559 if (activity != null) {
3560 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3561 if (needSep) {
3562 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003563 }
3564 pw.print(prefix);
3565 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003566 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003567 }
3568 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003569 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003570 }
3571
Craig Mautner8d341ef2013-03-26 09:03:27 -07003572 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3573 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003574 boolean printed = false;
3575 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003576 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3577 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003578 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003579 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003580 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003581 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 final ActivityStack stack = stacks.get(stackNdx);
3583 StringBuilder stackHeader = new StringBuilder(128);
3584 stackHeader.append(" Stack #");
3585 stackHeader.append(stack.mStackId);
3586 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003587 stackHeader.append("\n");
3588 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3589 stackHeader.append("\n");
3590 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003591
3592 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3593 dumpClient, dumpPackage, needSep, stackHeader.toString());
3594 printed |= printedStackHeader;
3595 if (!printedStackHeader) {
3596 // Ensure we always dump the stack header even if there are no activities
3597 pw.println();
3598 pw.println(stackHeader);
3599 }
3600
Craig Mautner4a1cb222013-12-04 16:14:06 -08003601 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3602 !dumpAll, false, dumpPackage, true,
3603 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003604
Craig Mautner4a1cb222013-12-04 16:14:06 -08003605 needSep = printed;
3606 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3607 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003608 if (pr) {
3609 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003610 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003611 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003612 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3613 " mResumedActivity: ");
3614 if (pr) {
3615 printed = true;
3616 needSep = false;
3617 }
3618 if (dumpAll) {
3619 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3620 " mLastPausedActivity: ");
3621 if (pr) {
3622 printed = true;
3623 needSep = true;
3624 }
3625 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3626 needSep, " mLastNoHistoryActivity: ");
3627 }
3628 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003629 }
3630 }
3631
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003632 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3633 false, dumpPackage, true, " Activities waiting to finish:", null);
3634 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3635 false, dumpPackage, true, " Activities waiting to stop:", null);
3636 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3637 false, dumpPackage, true, " Activities waiting for another to become visible:",
3638 null);
3639 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3640 false, dumpPackage, true, " Activities waiting to sleep:", null);
3641 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3642 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003643
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003644 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003645 }
3646
Dianne Hackborn390517b2013-05-30 15:03:32 -07003647 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003648 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003649 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003650 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003651 String innerPrefix = null;
3652 String[] args = null;
3653 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003654 for (int i=list.size()-1; i>=0; i--) {
3655 final ActivityRecord r = list.get(i);
3656 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3657 continue;
3658 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003659 if (innerPrefix == null) {
3660 innerPrefix = prefix + " ";
3661 args = new String[0];
3662 }
3663 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003664 final boolean full = !brief && (complete || !r.isInHistory());
3665 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003666 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003667 needNL = false;
3668 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003669 if (header1 != null) {
3670 pw.println(header1);
3671 header1 = null;
3672 }
3673 if (header2 != null) {
3674 pw.println(header2);
3675 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003676 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003677 if (lastTask != r.getTask()) {
3678 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003679 pw.print(prefix);
3680 pw.print(full ? "* " : " ");
3681 pw.println(lastTask);
3682 if (full) {
3683 lastTask.dump(pw, prefix + " ");
3684 } else if (complete) {
3685 // Complete + brief == give a summary. Isn't that obvious?!?
3686 if (lastTask.intent != null) {
3687 pw.print(prefix); pw.print(" ");
3688 pw.println(lastTask.intent.toInsecureStringWithClip());
3689 }
3690 }
3691 }
3692 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3693 pw.print(" #"); pw.print(i); pw.print(": ");
3694 pw.println(r);
3695 if (full) {
3696 r.dump(pw, innerPrefix);
3697 } else if (complete) {
3698 // Complete + brief == give a summary. Isn't that obvious?!?
3699 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3700 if (r.app != null) {
3701 pw.print(innerPrefix); pw.println(r.app);
3702 }
3703 }
3704 if (client && r.app != null && r.app.thread != null) {
3705 // flush anything that is already in the PrintWriter since the thread is going
3706 // to write to the file descriptor directly
3707 pw.flush();
3708 try {
3709 TransferPipe tp = new TransferPipe();
3710 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003711 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003712 // Short timeout, since blocking here can
3713 // deadlock with the application.
3714 tp.go(fd, 2000);
3715 } finally {
3716 tp.kill();
3717 }
3718 } catch (IOException e) {
3719 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3720 } catch (RemoteException e) {
3721 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3722 }
3723 needNL = true;
3724 }
3725 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003726 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003727 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003728
Craig Mautnerf3333272013-04-22 10:55:53 -07003729 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003730 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3731 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003732 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3733 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003734 }
3735
3736 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003737 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003738 }
3739
3740 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003741 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3742 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003743 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3744 }
3745
Craig Mautner05d29032013-05-03 13:40:13 -07003746 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003747 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3748 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3749 }
Craig Mautner05d29032013-05-03 13:40:13 -07003750 }
3751
Craig Mautner0eea92c2013-05-16 13:35:39 -07003752 void removeSleepTimeouts() {
3753 mSleepTimeout = false;
3754 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3755 }
3756
3757 final void scheduleSleepTimeout() {
3758 removeSleepTimeouts();
3759 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3760 }
3761
Craig Mautner4a1cb222013-12-04 16:14:06 -08003762 @Override
3763 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003764 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003765 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3766 }
3767
3768 @Override
3769 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003770 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3772 }
3773
3774 @Override
3775 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003776 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003777 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3778 }
3779
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003780 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003781 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003782 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003783 newDisplay = mActivityDisplays.get(displayId) == null;
3784 if (newDisplay) {
3785 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003786 if (activityDisplay.mDisplay == null) {
3787 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3788 return;
3789 }
Craig Mautner4504de52013-12-20 09:06:56 -08003790 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003791 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003792 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003793 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003795 }
3796
Andrii Kulianca007a62016-11-22 16:33:28 -08003797 /** Check if display with specified id is added to the list. */
3798 boolean isDisplayAdded(int displayId) {
3799 return mActivityDisplays.get(displayId) != null;
3800 }
3801
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003802 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003803 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003804 mService.mContext.getResources().getDimensionPixelSize(
3805 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003806 }
3807
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003808 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003809 if (displayId == DEFAULT_DISPLAY) {
3810 throw new IllegalArgumentException("Can't remove the primary display.");
3811 }
3812
Craig Mautner4a1cb222013-12-04 16:14:06 -08003813 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003814 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3815 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003816 final boolean destroyContentOnRemoval
3817 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003818 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3819 while (!stacks.isEmpty()) {
3820 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003821 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003822 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3823 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003824 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003825 } else {
3826 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3827 // a valid launch stack for all activities. This way the task history from
3828 // external display will be preserved on primary after move.
3829 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003830 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003832 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003833 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003834 }
3835 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836 }
3837
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003838 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003839 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003840 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3841 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 // TODO: Update the bounds.
3843 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003844 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003845 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003846 }
3847
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003848 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003849 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003851 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003852 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003853 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003854 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003855 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003856 info.position = display != null
3857 ? display.mStacks.indexOf(stack)
3858 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003859
3860 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3861 final int numTasks = tasks.size();
3862 int[] taskIds = new int[numTasks];
3863 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003864 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003865 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 for (int i = 0; i < numTasks; ++i) {
3867 final TaskRecord task = tasks.get(i);
3868 taskIds[i] = task.taskId;
3869 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3870 : task.realActivity != null ? task.realActivity.flattenToString()
3871 : task.getTopActivity() != null ? task.getTopActivity().packageName
3872 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003873 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003874 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003875 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003876 }
3877 info.taskIds = taskIds;
3878 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003879 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003880 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003881
3882 final ActivityRecord top = stack.topRunningActivityLocked();
3883 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003884 return info;
3885 }
3886
3887 StackInfo getStackInfoLocked(int stackId) {
3888 ActivityStack stack = getStack(stackId);
3889 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003890 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003891 }
3892 return null;
3893 }
3894
3895 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003896 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003897 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3898 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003899 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003900 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901 }
3902 }
3903 return list;
3904 }
3905
Craig Mautner15df08a2015-04-01 12:17:18 -07003906 TaskRecord getLockedTaskLocked() {
3907 final int top = mLockTaskModeTasks.size() - 1;
3908 if (top >= 0) {
3909 return mLockTaskModeTasks.get(top);
3910 }
3911 return null;
3912 }
3913
3914 boolean isLockedTask(TaskRecord task) {
3915 return mLockTaskModeTasks.contains(task);
3916 }
3917
3918 boolean isLastLockedTask(TaskRecord task) {
3919 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3920 }
3921
3922 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003923 if (!mLockTaskModeTasks.remove(task)) {
3924 return;
3925 }
3926 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3927 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003928 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003929 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3930 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003931 final Message lockTaskMsg = Message.obtain();
3932 lockTaskMsg.arg1 = task.userId;
3933 lockTaskMsg.what = LOCK_TASK_END_MSG;
3934 mHandler.sendMessage(lockTaskMsg);
3935 }
3936 }
3937
Andrii Kulianc27916642016-04-12 17:59:27 -07003938 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3939 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3940 false /* forceNonResizable */);
3941 }
3942
Jorim Jaggid53f0922016-04-06 22:16:23 -07003943 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003944 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003945 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3946 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003947 return;
3948 }
3949
Jorim Jaggicd13d332016-04-27 15:40:20 -07003950 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003951 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003952 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003953 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003954
Andrii Kulianc27916642016-04-12 17:59:27 -07003955 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3956 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003957 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003958 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003959 && !topActivity.noDisplay) {
3960 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003961 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3962 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003963 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003964 }
3965
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003966 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003967 if (mLockTaskNotify != null) {
3968 mLockTaskNotify.showToast(mLockTaskModeState);
3969 }
Jason Monka8f569c2014-07-07 12:15:21 -04003970 }
3971
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003972 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3973 if (mLockTaskModeTasks.contains(task)) {
3974 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3975 }
3976 }
3977
Craig Mautner432f64e2015-05-20 14:59:57 -07003978 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3979 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003980 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003981 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003982 final TaskRecord lockedTask = getLockedTaskLocked();
3983 if (lockedTask != null) {
3984 removeLockedTaskLocked(lockedTask);
3985 if (!mLockTaskModeTasks.isEmpty()) {
3986 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003987 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3988 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003989 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003990 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003991 return;
3992 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003993 }
Craig Mautnere0570202015-05-13 13:06:11 -07003994 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3995 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003996 return;
3997 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003998
3999 // Should have already been checked, but do it again.
4000 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004001 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4002 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004003 return;
4004 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004005 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004006 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004007 return;
4008 }
4009
4010 if (mLockTaskModeTasks.isEmpty()) {
4011 // First locktask.
4012 final Message lockTaskMsg = Message.obtain();
4013 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4014 lockTaskMsg.arg1 = task.userId;
4015 lockTaskMsg.what = LOCK_TASK_START_MSG;
4016 lockTaskMsg.arg2 = lockTaskModeState;
4017 mHandler.sendMessage(lockTaskMsg);
4018 }
4019 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004020 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4021 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004022 mLockTaskModeTasks.remove(task);
4023 mLockTaskModeTasks.add(task);
4024
4025 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004026 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004027 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004028
4029 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004030 findTaskToMoveToFrontLocked(task, 0, null, reason,
4031 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004032 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004033 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004034 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004035 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004036 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004037 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004038 }
4039
4040 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004041 return isLockTaskModeViolation(task, false);
4042 }
4043
4044 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4045 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004046 return false;
4047 }
4048 final int lockTaskAuth = task.mLockTaskAuth;
4049 switch (lockTaskAuth) {
4050 case LOCK_TASK_AUTH_DONT_LOCK:
4051 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004052 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004053 case LOCK_TASK_AUTH_LAUNCHABLE:
4054 case LOCK_TASK_AUTH_WHITELISTED:
4055 return false;
4056 case LOCK_TASK_AUTH_PINNABLE:
4057 // Pinnable tasks can't be launched on top of locktask tasks.
4058 return !mLockTaskModeTasks.isEmpty();
4059 default:
4060 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4061 return true;
4062 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004063 }
4064
Craig Mautner15df08a2015-04-01 12:17:18 -07004065 void onLockTaskPackagesUpdatedLocked() {
4066 boolean didSomething = false;
4067 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4068 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004069 final boolean wasWhitelisted =
4070 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4071 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004072 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004073 final boolean isWhitelisted =
4074 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4075 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4076 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004077 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004078 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4079 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004080 removeLockedTaskLocked(lockedTask);
4081 lockedTask.performClearTaskLocked();
4082 didSomething = true;
4083 }
4084 }
4085 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4086 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4087 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4088 final ActivityStack stack = stacks.get(stackNdx);
4089 stack.onLockTaskPackagesUpdatedLocked();
4090 }
4091 }
Craig Mautnere0570202015-05-13 13:06:11 -07004092 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004093 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004094 if (mLockTaskModeTasks.isEmpty() && task != null
4095 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4096 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004097 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4098 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4099 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4100 false);
4101 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004102 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004103 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004104 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004105 }
4106 }
4107
Benjamin Franz43261142015-02-11 15:59:44 +00004108 int getLockTaskModeState() {
4109 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004110 }
4111
Jorim Jaggife89d122015-12-22 16:28:44 +01004112 void activityRelaunchedLocked(IBinder token) {
4113 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004114 if (mService.isSleepingOrShuttingDownLocked()) {
4115 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4116 if (r != null) {
4117 r.setSleeping(true, true);
4118 }
4119 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004120 }
4121
4122 void activityRelaunchingLocked(ActivityRecord r) {
4123 mWindowManager.notifyAppRelaunching(r.appToken);
4124 }
4125
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004126 void logStackState() {
4127 mActivityMetricsLogger.logWindowState();
4128 }
4129
Winson Chung5af42fc2017-03-24 17:11:33 -07004130 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004131 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4132 final ActivityRecord r = task.mActivities.get(i);
4133 if (r.app != null && r.app.thread != null) {
4134 mMultiWindowModeChangedActivities.add(r);
4135 }
4136 }
4137
4138 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4139 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4140 }
4141 }
4142
Winson Chung5af42fc2017-03-24 17:11:33 -07004143 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004144 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004145 if (prevStack == null || prevStack == stack
4146 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4147 return;
4148 }
4149
Winson Chung5af42fc2017-03-24 17:11:33 -07004150 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4151 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004152
Winson Chung5af42fc2017-03-24 17:11:33 -07004153 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4154 boolean immediate) {
4155
4156 if (immediate) {
4157 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4158 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4159 final ActivityRecord r = task.mActivities.get(i);
4160 if (r.app != null && r.app.thread != null) {
4161 r.updatePictureInPictureMode(targetStackBounds);
4162 }
4163 }
4164 } else {
4165 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4166 final ActivityRecord r = task.mActivities.get(i);
4167 if (r.app != null && r.app.thread != null) {
4168 mPipModeChangedActivities.add(r);
4169 }
4170 }
4171 mPipModeChangedTargetStackBounds = targetStackBounds;
4172
4173 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4174 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4175 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004176 }
4177 }
4178
Tony Mak853304c2016-04-18 15:17:41 +01004179 void setDockedStackMinimized(boolean minimized) {
4180 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004181 }
4182
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004183 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004184
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004185 public ActivityStackSupervisorHandler(Looper looper) {
4186 super(looper);
4187 }
4188
Winson Chung4dabf232017-01-25 13:25:22 -08004189 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004190 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004191 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4192 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004193 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004194 }
4195
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004196 @Override
4197 public void handleMessage(Message msg) {
4198 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004199 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4200 synchronized (mService) {
4201 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4202 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004203 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004204 }
4205 }
4206 } break;
4207 case REPORT_PIP_MODE_CHANGED_MSG: {
4208 synchronized (mService) {
4209 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4210 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004211 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004212 }
4213 }
4214 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004215 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004216 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4217 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004218 if (mService.mDidDexOpt) {
4219 mService.mDidDexOpt = false;
4220 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4221 nmsg.obj = msg.obj;
4222 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4223 return;
4224 }
4225 // We don't at this point know if the activity is fullscreen,
4226 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004227 activityIdleInternal((ActivityRecord) msg.obj,
4228 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004229 } break;
4230 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004231 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004232 activityIdleInternal((ActivityRecord) msg.obj,
4233 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004234 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004235 case RESUME_TOP_ACTIVITY_MSG: {
4236 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004237 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004238 }
4239 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004240 case SLEEP_TIMEOUT_MSG: {
4241 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004242 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004243 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4244 mSleepTimeout = true;
4245 checkReadyForSleepLocked();
4246 }
4247 }
4248 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004249 case LAUNCH_TIMEOUT_MSG: {
4250 if (mService.mDidDexOpt) {
4251 mService.mDidDexOpt = false;
4252 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4253 return;
4254 }
4255 synchronized (mService) {
4256 if (mLaunchingActivity.isHeld()) {
4257 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4258 if (VALIDATE_WAKE_LOCK_CALLER
4259 && Binder.getCallingUid() != Process.myUid()) {
4260 throw new IllegalStateException("Calling must be system uid");
4261 }
4262 mLaunchingActivity.release();
4263 }
4264 }
4265 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004266 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004267 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004268 } break;
4269 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004270 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004271 } break;
4272 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004273 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004274 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004275 case CONTAINER_CALLBACK_VISIBILITY: {
4276 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004277 final IActivityContainerCallback callback = container.mCallback;
4278 if (callback != null) {
4279 try {
4280 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4281 } catch (RemoteException e) {
4282 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004283 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004284 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004285 case LOCK_TASK_START_MSG: {
4286 // When lock task starts, we disable the status bars.
4287 try {
Jason Monk62515be2014-05-21 16:06:19 -04004288 if (mLockTaskNotify == null) {
4289 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004290 }
Jason Monk62515be2014-05-21 16:06:19 -04004291 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004292 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004293 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004294 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004295 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004296 flags = StatusBarManager.DISABLE_MASK
4297 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004298 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004299 flags = StatusBarManager.DISABLE_MASK
4300 & (~StatusBarManager.DISABLE_BACK)
4301 & (~StatusBarManager.DISABLE_HOME)
4302 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004303 }
4304 getStatusBarService().disable(flags, mToken,
4305 mService.mContext.getPackageName());
4306 }
4307 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004308 if (getDevicePolicyManager() != null) {
4309 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004310 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004311 }
justinzhang5286d3f2014-05-12 17:06:01 -04004312 } catch (RemoteException ex) {
4313 throw new RuntimeException(ex);
4314 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004315 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004316 case LOCK_TASK_END_MSG: {
4317 // When lock task ends, we enable the status bars.
4318 try {
Jason Monk62515be2014-05-21 16:06:19 -04004319 if (getStatusBarService() != null) {
4320 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4321 mService.mContext.getPackageName());
4322 }
4323 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004324 if (getDevicePolicyManager() != null) {
4325 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4326 msg.arg1);
4327 }
Jason Monk62515be2014-05-21 16:06:19 -04004328 if (mLockTaskNotify == null) {
4329 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4330 }
4331 mLockTaskNotify.show(false);
4332 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004333 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004334 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004335 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004336 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004337 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004338 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004339 new LockPatternUtils(mService.mContext)
4340 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004341 }
4342 } catch (SettingNotFoundException e) {
4343 // No setting, don't lock.
4344 }
justinzhang5286d3f2014-05-12 17:06:01 -04004345 } catch (RemoteException ex) {
4346 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004347 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004348 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004349 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004350 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004351 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4352 if (mLockTaskNotify == null) {
4353 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4354 }
4355 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4356 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004357 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4358 final ActivityContainer container = (ActivityContainer) msg.obj;
4359 final IActivityContainerCallback callback = container.mCallback;
4360 if (callback != null) {
4361 try {
4362 callback.onAllActivitiesComplete(container.asBinder());
4363 } catch (RemoteException e) {
4364 }
4365 }
4366 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004367 case LAUNCH_TASK_BEHIND_COMPLETE: {
4368 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004369 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004370 if (r != null) {
4371 handleLaunchTaskBehindCompleteLocked(r);
4372 }
4373 }
4374 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004375
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004376 }
4377 }
4378 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004379
Ying Wangb081a592014-04-22 15:20:16 -07004380 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004381 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4382 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004383 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004384 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004385 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004386 ActivityRecord mParentActivity = null;
4387 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004388
Craig Mautnere3a00d72014-04-16 08:31:19 -07004389 boolean mVisible = true;
4390
Craig Mautner4a1cb222013-12-04 16:14:06 -08004391 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004392 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004393
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004394 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4395 final static int CONTAINER_STATE_NO_SURFACE = 1;
4396 final static int CONTAINER_STATE_FINISHING = 2;
4397 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4398
Wale Ogunwale1666e312016-12-16 11:27:18 -08004399 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004400 synchronized (mService) {
4401 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004402 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004403 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004404
4405 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004406 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004407 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004408 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004409
Bryce Leeaf691c02017-03-20 14:20:22 -07004410 protected void createStack(int stackId, boolean onTop) {
4411 switch (stackId) {
4412 case PINNED_STACK_ID:
4413 new PinnedActivityStack(this, mRecentTasks, onTop);
4414 break;
4415 default:
4416 new ActivityStack(this, mRecentTasks, onTop);
4417 break;
4418 }
4419 }
4420
Andrii Kulian839def92016-11-02 10:58:58 -07004421 /**
4422 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004423 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004424 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004425 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004426 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004427 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004428 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004429 if (mActivityDisplay != null) {
4430 throw new IllegalStateException("ActivityContainer is already attached, " +
4431 "displayId=" + mActivityDisplay.mDisplayId);
4432 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004433 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004434 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004435 }
4436
4437 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004438 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004439 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004440 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4441 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004442 return;
4443 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004444 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004445 }
4446 }
4447
4448 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004449 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004450 synchronized (mService) {
4451 if (mActivityDisplay != null) {
4452 return mActivityDisplay.mDisplayId;
4453 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004454 }
4455 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004456 }
4457
Jeff Brownca9bc702014-02-11 14:32:56 -08004458 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004459 public int getStackId() {
4460 synchronized (mService) {
4461 return mStackId;
4462 }
4463 }
4464
4465 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004466 public boolean injectEvent(InputEvent event) {
4467 final long origId = Binder.clearCallingIdentity();
4468 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004469 synchronized (mService) {
4470 if (mActivityDisplay != null) {
4471 return mInputManagerInternal.injectInputEvent(event,
4472 mActivityDisplay.mDisplayId,
4473 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4474 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004475 }
4476 return false;
4477 } finally {
4478 Binder.restoreCallingIdentity(origId);
4479 }
4480 }
4481
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004482 @Override
4483 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004484 synchronized (mService) {
4485 if (mContainerState == CONTAINER_STATE_FINISHING) {
4486 return;
4487 }
4488 mContainerState = CONTAINER_STATE_FINISHING;
4489
Craig Mautnerd163e752014-06-13 17:18:47 -07004490 long origId = Binder.clearCallingIdentity();
4491 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004492 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004493 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004494 } finally {
4495 Binder.restoreCallingIdentity(origId);
4496 }
4497 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004498 }
4499
Andrii Kulian03c403d2016-11-11 11:14:12 -08004500 /**
4501 * Remove the stack completely. Must be called only when there are no tasks left in it,
4502 * as this method does not finish running activities.
4503 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004504 void removeLocked() {
4505 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004506 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004507 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004508 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004509 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004510 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004511 }
4512
Andrii Kulian839def92016-11-02 10:58:58 -07004513 /**
4514 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4515 * completely or re-parented.
4516 */
4517 private void removeFromDisplayLocked() {
4518 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4519 + " current displayId=" + mActivityDisplay.mDisplayId);
4520
Wale Ogunwale1666e312016-12-16 11:27:18 -08004521 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004522 mActivityDisplay = null;
4523 }
4524
4525 /**
4526 * Move the stack to specified display.
4527 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004528 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004529 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004530 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004531 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4532 + mActivityDisplay + " to display=" + activityDisplay
4533 + " Callers=" + Debug.getCallers(2));
4534
4535 removeFromDisplayLocked();
4536
4537 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004538 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004539 }
4540
Craig Mautner4a1cb222013-12-04 16:14:06 -08004541 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004542 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004543 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004544 }
4545
4546 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004547 public final int startActivityIntentSender(IIntentSender intentSender)
4548 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004549 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4550
4551 if (!(intentSender instanceof PendingIntentRecord)) {
4552 throw new IllegalArgumentException("Bad PendingIntent object");
4553 }
4554
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004555 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004556 Binder.getCallingUid(), mCurrentUser, false,
4557 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004558
4559 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4560 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4561 pendingIntent.key.requestResolvedType);
4562
4563 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4564 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4565 }
4566
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004567 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004568 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004569 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004570 throw new SecurityException(
4571 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4572 }
4573 }
4574
Craig Mautnerdf88d732014-01-27 09:21:32 -08004575 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004576 public IBinder asBinder() {
4577 return this;
4578 }
4579
Craig Mautner4504de52013-12-20 09:06:56 -08004580 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004581 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004582 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004583 }
4584
Craig Mautner4a1cb222013-12-04 16:14:06 -08004585 ActivityStackSupervisor getOuter() {
4586 return ActivityStackSupervisor.this;
4587 }
4588
Craig Mautnerd163e752014-06-13 17:18:47 -07004589 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004590 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004591 }
4592
Craig Mautner6985bad2014-04-21 15:22:06 -07004593 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004594 void setVisible(boolean visible) {
4595 if (mVisible != visible) {
4596 mVisible = visible;
4597 if (mCallback != null) {
4598 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4599 0 /* unused */, this).sendToTarget();
4600 }
4601 }
4602 }
4603
Craig Mautner6985bad2014-04-21 15:22:06 -07004604 void setDrawn() {
4605 }
4606
Craig Mautner1b4bf852014-05-26 15:06:32 -07004607 // You can always start a new task on a regular ActivityStack.
4608 boolean isEligibleForNewTasks() {
4609 return true;
4610 }
4611
Craig Mautnerd163e752014-06-13 17:18:47 -07004612 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004613 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004614 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004615 }
4616
Craig Mautner34b73df2014-01-12 21:11:08 -08004617 @Override
4618 public String toString() {
4619 return mIdString + (mActivityDisplay == null ? "N" : "A");
4620 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004621 }
4622
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004623 private class VirtualActivityContainer extends ActivityContainer {
4624 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004625 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004626
4627 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004628 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4629 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004630 mParentActivity = parent;
4631 mCallback = callback;
4632 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004633 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004634 }
4635
4636 @Override
4637 public void setSurface(Surface surface, int width, int height, int density) {
4638 super.setSurface(surface, width, height, density);
4639
4640 synchronized (mService) {
4641 final long origId = Binder.clearCallingIdentity();
4642 try {
4643 setSurfaceLocked(surface, width, height, density);
4644 } finally {
4645 Binder.restoreCallingIdentity(origId);
4646 }
4647 }
4648 }
4649
4650 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4651 if (mContainerState == CONTAINER_STATE_FINISHING) {
4652 return;
4653 }
4654 VirtualActivityDisplay virtualActivityDisplay =
4655 (VirtualActivityDisplay) mActivityDisplay;
4656 if (virtualActivityDisplay == null) {
4657 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004658 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004659 mActivityDisplay = virtualActivityDisplay;
4660 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004661 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004662 }
4663
4664 if (mSurface != null) {
4665 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004666 }
4667
Craig Mautner6985bad2014-04-21 15:22:06 -07004668 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004669 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004670 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004671 } else {
4672 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004673 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004674 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004675 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004676 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004677 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004678
Craig Mautnerd163e752014-06-13 17:18:47 -07004679 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004680
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004681 if (DEBUG_STACK) Slog.d(TAG_STACK,
4682 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004683 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004684
Craig Mautner6985bad2014-04-21 15:22:06 -07004685 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004686 boolean isAttachedLocked() {
4687 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004688 }
4689
4690 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004691 void setDrawn() {
4692 synchronized (mService) {
4693 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004694 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004695 }
4696 }
4697
Craig Mautner1b4bf852014-05-26 15:06:32 -07004698 // Never start a new task on an ActivityView if it isn't explicitly specified.
4699 @Override
4700 boolean isEligibleForNewTasks() {
4701 return false;
4702 }
4703
Craig Mautnerd163e752014-06-13 17:18:47 -07004704 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004705 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004706 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4707 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4708 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4709 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4710 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004711 }
4712 }
4713
Craig Mautner4a1cb222013-12-04 16:14:06 -08004714 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4715 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004716 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004717 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004718 int mDisplayId;
4719 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004720
Craig Mautner4a1cb222013-12-04 16:14:06 -08004721 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4722 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004723 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004724
Jose Lima4b6c6692014-08-12 17:41:12 -07004725 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004726
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004727 /** Array of all UIDs that are present on the display. */
4728 private IntArray mDisplayAccessUIDs = new IntArray();
4729
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004730 ActivityDisplay() {
4731 }
Craig Mautner4504de52013-12-20 09:06:56 -08004732
Craig Mautner1a70a162014-09-13 12:09:31 -07004733 // After instantiation, check that mDisplay is not null before using this. The alternative
4734 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004735 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004736 final Display display = mDisplayManager.getDisplay(displayId);
4737 if (display == null) {
4738 return;
4739 }
4740 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004741 }
4742
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004743 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004744 mDisplay = display;
4745 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004746 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004747
Winson Chung7c3ede32017-03-14 14:18:34 -07004748 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004749 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004750 + " to displayId=" + mDisplayId + " position=" + position);
4751 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004752 }
4753
Wale Ogunwale1666e312016-12-16 11:27:18 -08004754 void detachStack(ActivityStack stack) {
4755 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004756 + " from displayId=" + mDisplayId);
4757 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004758 }
4759
Jose Lima4b6c6692014-08-12 17:41:12 -07004760 void setVisibleBehindActivity(ActivityRecord r) {
4761 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004762 }
4763
Jose Lima4b6c6692014-08-12 17:41:12 -07004764 boolean hasVisibleBehindActivity() {
4765 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004766 }
4767
Craig Mautner34b73df2014-01-12 21:11:08 -08004768 @Override
4769 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004770 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4771 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004772
4773 @Override
4774 protected int getChildCount() {
4775 return mStacks.size();
4776 }
4777
4778 @Override
4779 protected ConfigurationContainer getChildAt(int index) {
4780 return mStacks.get(index);
4781 }
4782
4783 @Override
4784 protected ConfigurationContainer getParent() {
4785 return ActivityStackSupervisor.this;
4786 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004787
4788 boolean isPrivate() {
4789 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4790 }
4791
4792 boolean isUidPresent(int uid) {
4793 for (ActivityStack stack : mStacks) {
4794 if (stack.isUidPresent(uid)) {
4795 return true;
4796 }
4797 }
4798 return false;
4799 }
4800
4801 /** Update and get all UIDs that are present on the display and have access to it. */
4802 private IntArray getPresentUIDs() {
4803 mDisplayAccessUIDs.clear();
4804 for (ActivityStack stack : mStacks) {
4805 stack.getPresentUIDs(mDisplayAccessUIDs);
4806 }
4807 return mDisplayAccessUIDs;
4808 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004809
4810 boolean shouldDestroyContentOnRemove() {
4811 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4812 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004813 }
4814
4815 class VirtualActivityDisplay extends ActivityDisplay {
4816 VirtualDisplay mVirtualDisplay;
4817
Craig Mautner6985bad2014-04-21 15:22:06 -07004818 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004819 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004820 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4821 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4822 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4823 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004824
4825 init(mVirtualDisplay.getDisplay());
4826
Bryce Leeaf3a4002017-03-20 10:37:12 -07004827 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004828 }
4829
4830 void setSurface(Surface surface) {
4831 if (mVirtualDisplay != null) {
4832 mVirtualDisplay.setSurface(surface);
4833 }
4834 }
4835
4836 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004837 void detachStack(ActivityStack stack) {
4838 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004839 if (mVirtualDisplay != null) {
4840 mVirtualDisplay.release();
4841 mVirtualDisplay = null;
4842 }
4843 }
4844
4845 @Override
4846 public String toString() {
4847 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004848 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004849 }
Jose Lima58e66d62014-05-27 20:00:27 -07004850
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004851 ActivityStack findStackBehind(ActivityStack stack) {
4852 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004853 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004854 if (display == null) {
4855 return null;
4856 }
4857 final ArrayList<ActivityStack> stacks = display.mStacks;
4858 for (int i = stacks.size() - 1; i >= 0; i--) {
4859 if (stacks.get(i) == stack && i > 0) {
4860 return stacks.get(i - 1);
4861 }
4862 }
4863 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4864 + " in=" + stacks);
4865 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004866
Jorim Jaggic69bd222016-03-15 14:38:37 +01004867 /**
4868 * Puts a task into resizing mode during the next app transition.
4869 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004870 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004871 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004872 private void setResizingDuringAnimation(TaskRecord task) {
4873 mResizingTasksDuringAnimation.add(task.taskId);
4874 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004875 }
4876
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004877 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4878 final TaskRecord task;
4879 final int callingUid;
4880 final String callingPackage;
4881 final Intent intent;
4882 final int userId;
4883 final ActivityOptions activityOptions = (bOptions != null)
4884 ? new ActivityOptions(bOptions) : null;
4885 final int launchStackId = (activityOptions != null)
4886 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004887 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004888 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004889 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004890 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004891
4892 if (launchStackId == DOCKED_STACK_ID) {
4893 mWindowManager.setDockedStackCreateState(
4894 activityOptions.getDockCreateMode(), null /* initialBounds */);
4895
4896 // Defer updating the stack in which recents is until the app transition is done, to
4897 // not run into issues where we still need to draw the task in recents but the
4898 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004899 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004900 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4901 }
4902
Winson Chung7900bee2017-03-10 08:59:25 -08004903 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4904 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004905 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004906 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004907 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004908 throw new IllegalArgumentException(
4909 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4910 }
4911
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004912 // Since we don't have an actual source record here, we assume that the currently focused
4913 // activity was the source.
4914 final ActivityStack focusedStack = getFocusedStack();
4915 final ActivityRecord sourceRecord =
4916 focusedStack != null ? focusedStack.topActivity() : null;
4917
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004918 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004919 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004920 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4921 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004922 }
4923 }
4924
4925 // If the user must confirm credentials (e.g. when first launching a work app and the
4926 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4927 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4928 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004929 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004930 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004931 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004932 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4933 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004934
4935 // If we are launching the task in the docked stack, put it into resizing mode so
4936 // the window renders full-screen with the background filling the void. Also only
4937 // call this at the end to make sure that tasks exists on the window manager side.
4938 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004939 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004940 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004941
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004942 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004943 ActivityManager.START_TASK_TO_FRONT,
Bryce Leeaf691c02017-03-20 14:20:22 -07004944 sourceRecord != null ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004945 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004946 return ActivityManager.START_TASK_TO_FRONT;
4947 }
4948 callingUid = task.mCallingUid;
4949 callingPackage = task.mCallingPackage;
4950 intent = task.intent;
4951 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4952 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004953 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4954 null, null, 0, 0, bOptions, userId, null, task);
4955 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004956 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004957 }
4958 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004959 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004960
4961 /**
4962 * @return a list of activities which are the top ones in each visible stack. The first
4963 * entry will be the focused activity.
4964 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004965 List<IBinder> getTopVisibleActivities() {
4966 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4967 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004968 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004969 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4970 // Traverse all stacks on a display.
4971 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4972 final ActivityStack stack = display.mStacks.get(j);
4973 // Get top activity from a visible stack and add it to the list.
4974 if (stack.getStackVisibilityLocked(null /* starting */)
4975 == ActivityStack.STACK_VISIBLE) {
4976 final ActivityRecord top = stack.topActivity();
4977 if (top != null) {
4978 if (stack == mFocusedStack) {
4979 topActivityTokens.add(0, top.appToken);
4980 } else {
4981 topActivityTokens.add(top.appToken);
4982 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004983 }
4984 }
4985 }
4986 }
4987 return topActivityTokens;
4988 }
Craig Mautner27084302013-03-25 08:05:25 -07004989}