blob: 43ae4b22ba63cb3d8a1fb2d9ac4b30fe4c0ecaa7 [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
Winson Chung47900652017-04-06 18:44:25 -070045import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020046import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
72import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
73import static com.android.server.am.ActivityManagerService.ANIMATE;
74import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
75import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
76import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020077import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
78import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
79import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
82import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
85import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
86import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
87import static com.android.server.am.ActivityStack.STACK_VISIBLE;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
92import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080093import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
94import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
95import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020096import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080097import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020098
Svetoslav7008b512015-06-24 18:47:07 -070099import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800100import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700101import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100102import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700103import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800105import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700106import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800107import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800110import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700111import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700112import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700114import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400115import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700117import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import android.content.Intent;
120import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700122import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.PackageManager;
124import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100125import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800127import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128import android.hardware.display.DisplayManager;
129import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800130import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800131import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800132import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800133import android.hardware.input.InputManager;
134import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100136import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700137import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700138import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700140import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700141import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700143import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700144import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700145import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400146import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700147import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700148import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700149import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700150import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100151import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700152import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700153import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400154import android.provider.Settings;
155import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700156import android.service.voice.IVoiceInteractionSession;
Bryce Lee4a194382017-04-04 14:32:48 -0700157import android.text.TextUtils;
Svetoslav7008b512015-06-24 18:47:07 -0700158import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700159import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700160import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800161import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700162import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800163import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800165import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800166import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800167import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800168
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800169import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800170import com.android.internal.logging.MetricsLogger;
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;
Winson Chung19953ca2017-04-11 11:19:23 -0700177import com.android.server.wm.PinnedStackWindowController;
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. */
Bryce Lee4a194382017-04-04 14:32:48 -0700349 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
350 // mStoppingActivities when something else comes up.
351 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700352
Bryce Lee4a194382017-04-04 14:32:48 -0700353 /** List of processes waiting to find out when a specific activity becomes visible. */
354 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700355
356 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700357 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700358
Craig Mautnerde4ef022013-04-07 19:01:33 -0700359 /** List of activities that are ready to be stopped, but waiting for the next activity to
360 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800361 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700362
Craig Mautnerf3333272013-04-22 10:55:53 -0700363 /** List of activities that are ready to be finished, but waiting for the previous activity to
364 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800365 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700366
Craig Mautner0eea92c2013-05-16 13:35:39 -0700367 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800368 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700369
Wale Ogunwale22e25262016-02-01 10:32:02 -0800370 /** List of activities whose multi-window mode changed that we need to report to the
371 * application */
372 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
373
374 /** List of activities whose picture-in-picture mode changed that we need to report to the
375 * application */
376 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
377
Winson Chung5af42fc2017-03-24 17:11:33 -0700378 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
379 * the application */
380 Rect mPipModeChangedTargetStackBounds;
381
Craig Mautnerf3333272013-04-22 10:55:53 -0700382 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700383 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700384
Craig Mautnerde4ef022013-04-07 19:01:33 -0700385 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
386 * is being brought in front of us. */
387 boolean mUserLeaving = false;
388
Craig Mautner0eea92c2013-05-16 13:35:39 -0700389 /** Set when we have taken too long waiting to go to sleep. */
390 boolean mSleepTimeout = false;
391
392 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700393 * We don't want to allow the device to go to sleep while in the process
394 * of launching an activity. This is primarily to allow alarm intent
395 * receivers to launch an activity and get that to run before the device
396 * goes back to sleep.
397 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700398 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700399
400 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700401 * Set when the system is going to sleep, until we have
402 * successfully paused the current activity and released our wake lock.
403 * At that point the system is allowed to actually sleep.
404 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700405 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700406
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700407 /** Stack id of the front stack when user switched, indexed by userId. */
408 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700409
Craig Mautner4504de52013-12-20 09:06:56 -0800410 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800411 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800412 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800413
Bryce Leeaf3a4002017-03-20 10:37:12 -0700414 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700416 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800418 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
419
420 private DisplayManagerInternal mDisplayManagerInternal;
421 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800422
Craig Mautner15df08a2015-04-01 12:17:18 -0700423 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
424 * may be finished until there is only one entry left. If this is empty the system is not
425 * in lockTask mode. */
426 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000427 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700428 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
429 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000430 */
431 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400432 /**
433 * Notifies the user when entering/exiting lock-task.
434 */
435 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800436
Wale Ogunwaled046a012015-12-24 13:05:59 -0800437 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800438 boolean inResumeTopActivity;
439
Andrii Kulian1e32e022016-09-16 15:29:34 -0700440 /**
441 * Temporary rect used during docked stack resize calculation so we don't need to create a new
442 * object each time.
443 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700444 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700445
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700446 // The default minimal size that will be used if the activity doesn't specify its minimal size.
447 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700448 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700449
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700450 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
451 private boolean mTaskLayersChanged = true;
452
Jorim Jaggi275561a2016-02-23 10:11:02 -0500453 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800454
Andrii Kulian1779e612016-10-12 21:58:25 -0700455 @Override
456 protected int getChildCount() {
457 return mActivityDisplays.size();
458 }
459
460 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700461 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700462 return mActivityDisplays.valueAt(index);
463 }
464
465 @Override
466 protected ConfigurationContainer getParent() {
467 return null;
468 }
469
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700470 Configuration getDisplayOverrideConfiguration(int displayId) {
471 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
472 if (activityDisplay == null) {
473 throw new IllegalArgumentException("No display found with id: " + displayId);
474 }
475
476 return activityDisplay.getOverrideConfiguration();
477 }
478
479 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
480 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
481 if (activityDisplay == null) {
482 throw new IllegalArgumentException("No display found with id: " + displayId);
483 }
484
485 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
486 }
487
Wale Ogunwale39381972015-12-17 17:15:29 -0800488 static class FindTaskResult {
489 ActivityRecord r;
490 boolean matchedByRootAffinity;
491 }
492 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
493
Craig Mautneree36c772014-07-16 14:56:05 -0700494 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800495 * Temp storage for display ids sorted in focus order.
496 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
497 * it's more efficient, as the number of displays is usually small.
498 */
499 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
500
501 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100502 * Used to keep track whether app visibilities got changed since the last pause. Useful to
503 * determine whether to invoke the task stack change listener after pausing.
504 */
505 boolean mAppVisibilitiesChangedSinceLastPause;
506
507 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100508 * Set of tasks that are in resizing mode during an app transition to fill the "void".
509 */
510 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
511
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700512
513 /**
514 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
515 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
516 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
517 * like the docked stack going empty.
518 */
519 private boolean mAllowDockedStackResize = true;
520
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100521 /**
Tony Mak853304c2016-04-18 15:17:41 +0100522 * Is dock currently minimized.
523 */
524 boolean mIsDockMinimized;
525
Jorim Jaggife762342016-10-13 14:33:27 +0200526 final KeyguardController mKeyguardController;
527
Tony Mak853304c2016-04-18 15:17:41 +0100528 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700529 * Description of a request to start a new activity, which has been held
530 * due to app switches being disabled.
531 */
532 static class PendingActivityLaunch {
533 final ActivityRecord r;
534 final ActivityRecord sourceRecord;
535 final int startFlags;
536 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700537 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700538
539 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700540 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700541 r = _r;
542 sourceRecord = _sourceRecord;
543 startFlags = _startFlags;
544 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700545 callerApp = _callerApp;
546 }
547
548 void sendErrorResult(String message) {
549 try {
550 if (callerApp.thread != null) {
551 callerApp.thread.scheduleCrash(message);
552 }
553 } catch (RemoteException e) {
554 Slog.e(TAG, "Exception scheduling crash of failed "
555 + "activity launcher sourceRecord=" + sourceRecord, e);
556 }
Craig Mautneree36c772014-07-16 14:56:05 -0700557 }
558 }
559
Bryce Leeaf691c02017-03-20 14:20:22 -0700560 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700561 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700562 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800563 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200564 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700565 }
566
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800567 void setRecentTasks(RecentTasks recentTasks) {
568 mRecentTasks = recentTasks;
569 }
570
Jeff Brown2c43c332014-06-12 22:38:59 -0700571 /**
572 * At the time when the constructor runs, the power manager has not yet been
573 * initialized. So we initialize our wakelocks afterwards.
574 */
575 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800576 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700577 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700578 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700579 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700580 }
581
justinzhang5286d3f2014-05-12 17:06:01 -0400582 // This function returns a IStatusBarService. The value is from ServiceManager.
583 // getService and is cached.
584 private IStatusBarService getStatusBarService() {
585 synchronized (mService) {
586 if (mStatusBarService == null) {
587 mStatusBarService = IStatusBarService.Stub.asInterface(
588 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
589 if (mStatusBarService == null) {
590 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
591 }
592 }
593 return mStatusBarService;
594 }
595 }
596
Jason Monk35c62a42014-06-17 10:24:47 -0400597 private IDevicePolicyManager getDevicePolicyManager() {
598 synchronized (mService) {
599 if (mDevicePolicyManager == null) {
600 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
601 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
602 if (mDevicePolicyManager == null) {
603 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
604 }
605 }
606 return mDevicePolicyManager;
607 }
608 }
609
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700610 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800611 synchronized (mService) {
612 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200613 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614
615 mDisplayManager =
616 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
617 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800618 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800619
620 Display[] displays = mDisplayManager.getDisplays();
621 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
622 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800623 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700624 if (activityDisplay.mDisplay == null) {
625 throw new IllegalStateException("Default Display does not exist");
626 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800627 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700628 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 }
630
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800631 mHomeStack = mFocusedStack = mLastFocusedStack =
632 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800633
634 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800635 }
Craig Mautner27084302013-03-25 08:05:25 -0700636 }
637
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700638 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800639 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700640 }
641
Craig Mautnerde4ef022013-04-07 19:01:33 -0700642 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700644 }
645
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700646 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700647 if (stack == null) {
648 return false;
649 }
650
Craig Mautnerdf88d732014-01-27 09:21:32 -0800651 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
652 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700653 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800654 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800655 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700656 }
657
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800658 /** The top most stack on its display. */
659 boolean isFrontStackOnDisplay(ActivityStack stack) {
660 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
661 }
662
663 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700664 if (stack == null) {
665 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700667
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700668 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
669 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700670 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800671 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800672 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700673 }
674
Wale Ogunwaled046a012015-12-24 13:05:59 -0800675 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800676 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
677 if (!focusCandidate.isFocusable()) {
678 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800679 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800680 }
681
682 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800683 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800684 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800685
Wale Ogunwaled046a012015-12-24 13:05:59 -0800686 EventLogTags.writeAmFocusedStack(
687 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
688 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
689 }
690
691 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800692 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800693 if (r != null && r.idle) {
694 checkFinishBootingLocked();
695 }
696 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700697 }
698
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700699 void moveHomeStackToFront(String reason) {
700 mHomeStack.moveToFront(reason);
701 }
702
Matthew Ng330757d2017-02-28 14:19:17 -0800703 void moveRecentsStackToFront(String reason) {
704 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
705 if (recentsStack != null) {
706 recentsStack.moveToFront(reason);
707 }
708 }
709
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700710 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800711 boolean moveHomeStackTaskToTop(String reason) {
712 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700713
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700714 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700715 if (top == null) {
716 return false;
717 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700718 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700719 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700720 }
721
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800722 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700723 if (!mService.mBooting && !mService.mBooted) {
724 // Not ready yet!
725 return false;
726 }
727
Craig Mautner84984fa2014-06-19 11:19:20 -0700728 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700729 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700730 }
731
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800732 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700733 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800734 final String myReason = reason + " resumeHomeStackTask";
735
Mark Lua56ea122015-10-08 13:31:01 +0800736 // Only resume home activity if isn't finishing.
737 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700738 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800739 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700740 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800741 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700742 }
743
Craig Mautner8d341ef2013-03-26 09:03:27 -0700744 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800745 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
746 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700747 }
748
749 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700750 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700751 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800752 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700753 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800754 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
755 * valid if the matchMode is
756 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700757 */
Winson Chung7900bee2017-03-10 08:59:25 -0800758 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
759 // If there is a stack id set, ensure that we are attempting to actually restore a task
760 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
761 stackId != INVALID_STACK_ID) {
762 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
763 }
764
Craig Mautnere0a38842013-12-16 16:14:02 -0800765 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800766 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800767 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800768 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
769 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700770 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 if (task != null) {
772 return task;
773 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700774 }
775 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800776
Winson Chung7900bee2017-03-10 08:59:25 -0800777 // If we are matching stack tasks only, return now
778 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800779 return null;
780 }
781
Winson Chung7900bee2017-03-10 08:59:25 -0800782 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
783 // the task from recents
784 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700785 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
786
787 if (task == null) {
788 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800789 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
790 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700791
792 return null;
793 }
794
795 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700796 return task;
797 }
798
Winson Chung7900bee2017-03-10 08:59:25 -0800799 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700800 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700801 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
802 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800803 return null;
804 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700805 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800806 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700807 }
808
Craig Mautner6170f732013-04-02 13:05:23 -0700809 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800810 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800812 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800813 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
814 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
815 if (r != null) {
816 return r;
817 }
Craig Mautner6170f732013-04-02 13:05:23 -0700818 }
819 }
820 return null;
821 }
822
Tony Mak853304c2016-04-18 15:17:41 +0100823 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000824 * Detects whether we should show a lock screen in front of this task for a locked user.
825 * <p>
826 * We'll do this if either of the following holds:
827 * <ul>
828 * <li>The top activity explicitly belongs to {@param userId}.</li>
829 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
830 * </ul>
831 *
832 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100833 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000834 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
835 // To handle the case that work app is in the task but just is not the top one.
836 final ActivityRecord activityRecord = task.getTopActivity();
837 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
838
839 return (activityRecord != null && activityRecord.userId == userId)
840 || (resultTo != null && resultTo.userId == userId);
841 }
842
843 /**
844 * Find all visible task stacks containing {@param userId} and intercept them with an activity
845 * to block out the contents and possibly start a credential-confirming intent.
846 *
847 * @param userId user handle for the locked managed profile.
848 */
849 void lockAllProfileTasks(@UserIdInt int userId) {
850 mWindowManager.deferSurfaceLayout();
851 try {
852 final List<ActivityStack> stacks = getStacks();
853 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
854 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
855 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
856 final TaskRecord task = tasks.get(taskNdx);
857
858 // Check the task for a top activity belonging to userId, or returning a result
859 // to an activity belonging to userId. Example case: a document picker for
860 // personal files, opened by a work app, should still get locked.
861 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000862 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
863 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000864 }
Tony Mak853304c2016-04-18 15:17:41 +0100865 }
866 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000867 } finally {
868 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100869 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000870 }
871
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800872 void setNextTaskIdForUserLocked(int taskId, int userId) {
873 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
874 if (taskId > currentTaskId) {
875 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700876 }
877 }
878
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700879 static int nextTaskIdForUser(int taskId, int userId) {
880 int nextTaskId = taskId + 1;
881 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
882 // Wrap around as there will be smaller task ids that are available now.
883 nextTaskId -= MAX_TASK_IDS_PER_USER;
884 }
885 return nextTaskId;
886 }
887
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800888 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800889 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
890 // for a userId u, a taskId can only be in the range
891 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
892 // 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 -0700893 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800894 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800895 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800896 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700897 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800898 if (candidateTaskId == currentTaskId) {
899 // Something wrong!
900 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
901 throw new IllegalStateException("Cannot get an available task id."
902 + " Reached limit of " + MAX_TASK_IDS_PER_USER
903 + " running tasks per user.");
904 }
905 }
906 mCurTaskIdForUser.put(userId, candidateTaskId);
907 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700908 }
909
Chong Zhang6cda19c2016-06-14 19:07:56 -0700910 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800911 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700912 if (stack == null) {
913 return null;
914 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700915 ActivityRecord resumedActivity = stack.mResumedActivity;
916 if (resumedActivity == null || resumedActivity.app == null) {
917 resumedActivity = stack.mPausingActivity;
918 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700919 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700920 }
921 }
922 return resumedActivity;
923 }
924
Dianne Hackbornff072722014-09-24 10:56:28 -0700925 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700926 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800928 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
929 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800930 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
931 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700932 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800933 continue;
934 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700935 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 if (hr != null) {
937 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
938 && processName.equals(hr.processName)) {
939 try {
George Mount2c92c972014-03-20 09:38:23 -0700940 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800941 didSomething = true;
942 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700943 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800944 Slog.w(TAG, "Exception in new application when starting activity "
945 + hr.intent.getComponent().flattenToShortString(), e);
946 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700947 }
Craig Mautner20e72272013-04-01 13:45:53 -0700948 }
Craig Mautner20e72272013-04-01 13:45:53 -0700949 }
950 }
951 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700952 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700953 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700954 }
Craig Mautner20e72272013-04-01 13:45:53 -0700955 return didSomething;
956 }
957
958 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800959 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
960 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800961 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
962 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700963 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800964 continue;
965 }
966 final ActivityRecord resumedActivity = stack.mResumedActivity;
967 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700968 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800969 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800970 return false;
971 }
Craig Mautner20e72272013-04-01 13:45:53 -0700972 }
973 }
Wei Wang65c7a152016-06-02 18:51:22 -0700974 // Send launch end powerhint when idle
975 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700976 return true;
977 }
978
Craig Mautnerde4ef022013-04-07 19:01:33 -0700979 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800980 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
981 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
983 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700984 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800985 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700986 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800987 return false;
988 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700989 }
990 }
991 }
992 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700993 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
995 mLastFocusedStack + " to=" + mFocusedStack);
996 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700997 return true;
998 }
999
1000 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001001 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001002 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1003 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001004 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1005 final ActivityStack stack = stacks.get(stackNdx);
1006 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001007 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001008 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001009 return false;
1010 }
1011 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001012 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001013 }
1014 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001015 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001016 }
1017
Craig Mautner2acc3892013-09-23 10:28:14 -07001018 /**
1019 * Pause all activities in either all of the stacks or just the back stacks.
1020 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001021 * @param resuming The resuming activity.
1022 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1023 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001024 * @return true if any activity was paused as a result of this call.
1025 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001026 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001027 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001028 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1029 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001030 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1031 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001032 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001033 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001035 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1036 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001037 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001038 }
1039 }
1040 return someActivityPaused;
1041 }
1042
Craig Mautnerde4ef022013-04-07 19:01:33 -07001043 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001044 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001045 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1046 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001047 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1048 final ActivityStack stack = stacks.get(stackNdx);
1049 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001050 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001051 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001052 Slog.d(TAG_STATES,
1053 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001054 pausing = false;
1055 } else {
1056 return false;
1057 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001058 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001059 }
1060 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001061 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001062 }
1063
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001064 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001065 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001066 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001067 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1068 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1069 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1070 final ActivityStack stack = stacks.get(stackNdx);
1071 if (stack.mResumedActivity != null &&
1072 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001073 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001074 }
1075 }
1076 }
1077 }
1078
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001079 void cancelInitializingActivities() {
1080 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1081 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1082 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1083 stacks.get(stackNdx).cancelInitializingActivities();
1084 }
1085 }
1086 }
1087
Bryce Lee4a194382017-04-04 14:32:48 -07001088 void waitActivityVisible(ComponentName name, WaitResult result) {
1089 final WaitInfo waitInfo = new WaitInfo(name, result);
1090 mWaitingForActivityVisible.add(waitInfo);
1091 }
1092
1093 void cleanupActivity(ActivityRecord r) {
1094 // Make sure this record is no longer in the pending finishes list.
1095 // This could happen, for example, if we are trimming activities
1096 // down to the max limit while they are still waiting to finish.
1097 mFinishingActivities.remove(r);
1098 mActivitiesWaitingForVisibleActivity.remove(r);
1099
1100 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1101 if (mWaitingForActivityVisible.get(i).matches(r)) {
1102 mWaitingForActivityVisible.remove(i);
1103 }
1104 }
1105 }
1106
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001107 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001108 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001109 }
1110
1111 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1112 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001113 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1114 final WaitInfo w = mWaitingForActivityVisible.get(i);
1115 if (w.matches(r)) {
1116 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001117 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001118 result.timeout = false;
1119 result.who = w.getComponent();
1120 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1121 result.thisTime = result.totalTime;
1122 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001124 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001125 if (changed) {
1126 mService.notifyAll();
1127 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001128 }
1129
Chong Zhang5022da32016-06-21 16:31:37 -07001130 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1131 boolean changed = false;
1132 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1133 WaitResult w = mWaitingActivityLaunched.remove(i);
1134 if (w.who == null) {
1135 changed = true;
1136 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1137 // the starting activity ends up moving another activity to front, and it should
1138 // wait for this new activity to become visible instead.
1139 // Do not modify other fields.
1140 w.result = START_TASK_TO_FRONT;
1141 }
1142 }
1143 if (changed) {
1144 mService.notifyAll();
1145 }
1146 }
1147
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001148 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1149 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001150 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001151 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001152 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001153 if (w.who == null) {
1154 changed = true;
1155 w.timeout = timeout;
1156 if (r != null) {
1157 w.who = new ComponentName(r.info.packageName, r.info.name);
1158 }
1159 w.thisTime = thisTime;
1160 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001161 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001162 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001163 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001164 if (changed) {
1165 mService.notifyAll();
1166 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001167 }
1168
Craig Mautner29219d92013-04-16 20:19:12 -07001169 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001170 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001171 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001172 if (r != null) {
1173 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001174 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001175
Andrii Kulian7318d632016-07-20 18:59:28 -07001176 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001177 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1178
1179 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1180 final int displayId = mTmpOrderedDisplayIds.get(i);
1181 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1182 if (stacks == null) {
1183 continue;
1184 }
1185 for (int j = stacks.size() - 1; j >= 0; --j) {
1186 final ActivityStack stack = stacks.get(j);
1187 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1188 r = stack.topRunningActivityLocked();
1189 if (r != null) {
1190 return r;
1191 }
Craig Mautner29219d92013-04-16 20:19:12 -07001192 }
1193 }
1194 }
1195 return null;
1196 }
1197
Dianne Hackborn09233282014-04-30 11:33:59 -07001198 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001199 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001200 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1201 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001202 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001203 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001204 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001205 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1206 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001207 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001208 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001209 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001210 }
1211 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001212
1213 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1214 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1215 while (maxNum > 0) {
1216 long mostRecentActiveTime = Long.MIN_VALUE;
1217 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001218 final int numTaskLists = runningTaskLists.size();
1219 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1220 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001221 if (!stackTaskList.isEmpty()) {
1222 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1223 if (lastActiveTime > mostRecentActiveTime) {
1224 mostRecentActiveTime = lastActiveTime;
1225 selectedStackList = stackTaskList;
1226 }
1227 }
1228 }
1229 if (selectedStackList != null) {
1230 list.add(selectedStackList.remove(0));
1231 --maxNum;
1232 } else {
1233 break;
1234 }
1235 }
Craig Mautner20e72272013-04-01 13:45:53 -07001236 }
1237
Todd Kennedy7440f172015-12-09 14:31:22 -08001238 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1239 ProfilerInfo profilerInfo) {
1240 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001241 if (aInfo != null) {
1242 // Store the found target back into the intent, because now that
1243 // we have it we never want to do this again. For example, if the
1244 // user navigates back to this point in the history, we should
1245 // always restart the exact same activity.
1246 intent.setComponent(new ComponentName(
1247 aInfo.applicationInfo.packageName, aInfo.name));
1248
1249 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001250 if (!aInfo.processName.equals("system")) {
1251 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001252 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001253 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001254
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001255 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1256 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1257 }
1258
Man Caocfa78b22015-06-11 20:14:34 -07001259 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1260 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1261 }
1262
1263 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001264 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001265 }
1266 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001267 final String intentLaunchToken = intent.getLaunchToken();
1268 if (aInfo.launchToken == null && intentLaunchToken != null) {
1269 aInfo.launchToken = intentLaunchToken;
1270 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001271 }
1272 return aInfo;
1273 }
1274
Todd Kennedy7440f172015-12-09 14:31:22 -08001275 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001276 return resolveIntent(intent, resolvedType, userId, 0);
1277 }
1278
1279 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001280 synchronized (mService) {
1281 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001282 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001283 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001284 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001285 }
1286
1287 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1288 ProfilerInfo profilerInfo, int userId) {
1289 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1290 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1291 }
1292
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001293 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1294 boolean andResume, boolean checkConfig) throws RemoteException {
1295
1296 if (!allPausedActivitiesComplete()) {
1297 // While there are activities pausing we skipping starting any new activities until
1298 // pauses are complete. NOTE: that we also do this for activities that are starting in
1299 // the paused state because they will first be resumed then paused on the client side.
1300 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1301 "realStartActivityLocked: Skipping start of r=" + r
1302 + " some activities pausing...");
1303 return false;
1304 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001305
Andrii Kulianada10292017-02-09 23:19:29 -08001306 r.startFreezingScreenLocked(app, 0);
1307 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001308
Andrii Kulianada10292017-02-09 23:19:29 -08001309 // schedule launch ticks to collect information about slow apps.
1310 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001311
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001312 // Have the window manager re-evaluate the orientation of the screen based on the new
1313 // activity order. Note that as a result of this, it can call back into the activity
1314 // manager with a new orientation. We don't care about that, because the activity is not
1315 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001317 final int displayId = r.getDisplayId();
1318 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1319 getDisplayOverrideConfiguration(displayId),
1320 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001321 // Deferring resume here because we're going to launch new activity shortly.
1322 // We don't want to perform a redundant launch of the same record while ensuring
1323 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001324 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1325 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001326 }
1327
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001328 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001329 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001330 }
1331
Craig Mautner2420ead2013-04-01 17:13:20 -07001332 r.app = app;
1333 app.waitingToKill = null;
1334 r.launchCount++;
1335 r.lastLaunchTime = SystemClock.uptimeMillis();
1336
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001337 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001338
1339 int idx = app.activities.indexOf(r);
1340 if (idx < 0) {
1341 app.activities.add(r);
1342 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001343 mService.updateLruProcessLocked(app, true, null);
1344 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001345
Bryce Leeaf691c02017-03-20 14:20:22 -07001346 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001347 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1348 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001349 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001350 }
1351
Andrii Kulian02b7a832016-10-06 23:11:56 -07001352 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 try {
1354 if (app.thread == null) {
1355 throw new RemoteException();
1356 }
1357 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001358 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001359 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001360 // We don't need to deliver new intents and/or set results if activity is going
1361 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001362 results = r.results;
1363 newIntents = r.newIntents;
1364 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001365 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1366 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1367 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001368 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1369 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001370 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001371 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001372 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001373 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001374 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1375 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 r.sleeping = false;
1377 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001378 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 mService.showAskCompatModeDialogLocked(r);
1380 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001381 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001382 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1383 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1384 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001385 final String profileFile = mService.mProfileFile;
1386 if (profileFile != null) {
1387 ParcelFileDescriptor profileFd = mService.mProfileFd;
1388 if (profileFd != null) {
1389 try {
1390 profileFd = profileFd.dup();
1391 } catch (IOException e) {
1392 if (profileFd != null) {
1393 try {
1394 profileFd.close();
1395 } catch (IOException o) {
1396 }
1397 profileFd = null;
1398 }
1399 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001400 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001401
1402 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001403 mService.mSamplingInterval, mService.mAutoStopProfiler,
1404 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001405 }
1406 }
1407 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001408
Andrii Kulianada10292017-02-09 23:19:29 -08001409 app.hasShownUi = true;
1410 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001411 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001412 // Because we could be starting an Activity in the system process this may not go across
1413 // a Binder interface which would create a new Configuration. Consequently we have to
1414 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001415
1416 final Configuration globalConfiguration =
1417 new Configuration(mService.getGlobalConfiguration());
1418 r.setLastReportedGlobalConfiguration(globalConfiguration);
1419 final Configuration mergedOverrideConfiguration =
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001420 new Configuration(r.getMergedOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08001421 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1422
Craig Mautner2420ead2013-04-01 17:13:20 -07001423 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001424 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001425 globalConfiguration,
1426 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001427 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1428 r.persistentState, results, newIntents, !andResume,
1429 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001430
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001431 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001432 // This may be a heavy-weight process! Note that the package
1433 // manager will ensure that only activity can run in the main
1434 // process of the .apk, which is the only thing that will be
1435 // considered heavy-weight.
1436 if (app.processName.equals(app.info.packageName)) {
1437 if (mService.mHeavyWeightProcess != null
1438 && mService.mHeavyWeightProcess != app) {
1439 Slog.w(TAG, "Starting new heavy weight process " + app
1440 + " when already running "
1441 + mService.mHeavyWeightProcess);
1442 }
1443 mService.mHeavyWeightProcess = app;
1444 Message msg = mService.mHandler.obtainMessage(
1445 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1446 msg.obj = r;
1447 mService.mHandler.sendMessage(msg);
1448 }
1449 }
1450
1451 } catch (RemoteException e) {
1452 if (r.launchFailed) {
1453 // This is the second time we failed -- finish activity
1454 // and give up.
1455 Slog.e(TAG, "Second failure launching "
1456 + r.intent.getComponent().flattenToShortString()
1457 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001458 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001459 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1460 "2nd-crash", false);
1461 return false;
1462 }
1463
1464 // This is the first time we failed -- restart process and
1465 // retry.
1466 app.activities.remove(r);
1467 throw e;
1468 }
1469
1470 r.launchFailed = false;
1471 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001472 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001473 }
1474
1475 if (andResume) {
1476 // As part of the process of launching, ActivityThread also performs
1477 // a resume.
1478 stack.minimalResumeActivityLocked(r);
1479 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001480 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001481 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001482 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001483 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001484 "Moving to PAUSED: " + r + " (starting in paused state)");
1485 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001486 }
1487
1488 // Launch the new version setup screen if needed. We do this -after-
1489 // launching the initial activity (that is, home), so that it can have
1490 // a chance to initialize itself while in the background, making the
1491 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001492 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001493 mService.startSetupActivityLocked();
1494 }
1495
Dianne Hackborn465fa392014-09-14 14:21:18 -07001496 // Update any services we are bound to that might care about whether
1497 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001498 if (r.app != null) {
1499 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1500 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001501
Craig Mautner2420ead2013-04-01 17:13:20 -07001502 return true;
1503 }
1504
Craig Mautnere79d42682013-04-01 19:01:53 -07001505 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001506 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001507 // Is this activity's application already running?
1508 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001509 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001510
Andrii Kulian02b7a832016-10-06 23:11:56 -07001511 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001512
1513 if (app != null && app.thread != null) {
1514 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001515 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1516 || !"android".equals(r.info.packageName)) {
1517 // Don't add this if it is a platform component that is marked
1518 // to run in multiple processes, because this is actually
1519 // part of the framework so doesn't make sense to track as a
1520 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001521 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1522 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001523 }
George Mount2c92c972014-03-20 09:38:23 -07001524 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001525 return;
1526 } catch (RemoteException e) {
1527 Slog.w(TAG, "Exception when starting activity "
1528 + r.intent.getComponent().flattenToShortString(), e);
1529 }
1530
1531 // If a dead object exception was thrown -- fall through to
1532 // restart the application.
1533 }
1534
1535 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001536 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001537 }
1538
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001539 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001540 String resultWho, int requestCode, int callingPid, int callingUid,
1541 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001542 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001543 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1544 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001545 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001546 return true;
1547 }
1548 final int componentRestriction = getComponentRestrictionForCallingPackage(
1549 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1550 final int actionRestriction = getActionRestrictionForCallingPackage(
1551 intent.getAction(), callingPackage, callingPid, callingUid);
1552 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1553 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1554 if (resultRecord != null) {
1555 resultStack.sendActivityResultLocked(-1,
1556 resultRecord, resultWho, requestCode,
1557 Activity.RESULT_CANCELED, null);
1558 }
1559 final String msg;
1560 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1561 msg = "Permission Denial: starting " + intent.toString()
1562 + " from " + callerApp + " (pid=" + callingPid
1563 + ", uid=" + callingUid + ")" + " with revoked permission "
1564 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1565 } else if (!aInfo.exported) {
1566 msg = "Permission Denial: starting " + intent.toString()
1567 + " from " + callerApp + " (pid=" + callingPid
1568 + ", uid=" + callingUid + ")"
1569 + " not exported from uid " + aInfo.applicationInfo.uid;
1570 } else {
1571 msg = "Permission Denial: starting " + intent.toString()
1572 + " from " + callerApp + " (pid=" + callingPid
1573 + ", uid=" + callingUid + ")"
1574 + " requires " + aInfo.permission;
1575 }
1576 Slog.w(TAG, msg);
1577 throw new SecurityException(msg);
1578 }
1579
1580 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1581 final String message = "Appop Denial: starting " + intent.toString()
1582 + " from " + callerApp + " (pid=" + callingPid
1583 + ", uid=" + callingUid + ")"
1584 + " requires " + AppOpsManager.permissionToOp(
1585 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1586 Slog.w(TAG, message);
1587 return false;
1588 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1589 final String message = "Appop Denial: starting " + intent.toString()
1590 + " from " + callerApp + " (pid=" + callingPid
1591 + ", uid=" + callingUid + ")"
1592 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1593 Slog.w(TAG, message);
1594 return false;
1595 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001596 if (options != null) {
1597 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1598 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1599 callingPid, callingUid);
1600 if (startInTaskPerm != PERMISSION_GRANTED) {
1601 final String msg = "Permission Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ") with launchTaskId="
1604 + options.getLaunchTaskId();
1605 Slog.w(TAG, msg);
1606 throw new SecurityException(msg);
1607 }
1608 }
1609 // Check if someone tries to launch an activity on a private display with a different
1610 // owner.
1611 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001612 if (launchDisplayId != INVALID_DISPLAY
1613 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1614 final String msg = "Permission Denial: starting " + intent.toString()
1615 + " from " + callerApp + " (pid=" + callingPid
1616 + ", uid=" + callingUid + ") with launchDisplayId="
1617 + launchDisplayId;
1618 Slog.w(TAG, msg);
1619 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001620 }
1621 }
1622
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001623 return true;
1624 }
1625
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001626 /** Check if caller is allowed to launch activities on specified display. */
1627 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1628 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1629 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1630
1631 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1632 if (activityDisplay == null) {
1633 Slog.w(TAG, "Launch on display check: display not found");
1634 return false;
1635 }
1636
1637 if (!activityDisplay.isPrivate()) {
1638 // Anyone can launch on a public display.
1639 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1640 + " allow launch on public display");
1641 return true;
1642 }
1643
1644 // Check if the caller is the owner of the display.
1645 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1646 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1647 + " allow launch for owner of the display");
1648 return true;
1649 }
1650
1651 // Check if caller is present on display
1652 if (activityDisplay.isUidPresent(callingUid)) {
1653 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1654 + " allow launch for caller present on the display");
1655 return true;
1656 }
1657
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001658 // Check if the caller can manage activity stacks.
1659 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001660 callingUid);
1661 if (startAnyPerm == PERMISSION_GRANTED) {
1662 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1663 + " allow launch any on display");
1664 return true;
1665 }
1666
1667 Slog.w(TAG, "Launch on display check: denied");
1668 return false;
1669 }
1670
1671 /** Update lists of UIDs that are present on displays and have access to them. */
1672 void updateUIDsPresentOnDisplay() {
1673 mDisplayAccessUIDs.clear();
1674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1675 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001676 // Only bother calculating the whitelist for private displays
1677 if (activityDisplay.isPrivate()) {
1678 mDisplayAccessUIDs.append(
1679 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1680 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001681 }
1682 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1683 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1684 }
1685
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001686 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001687 final long identity = Binder.clearCallingIdentity();
1688 try {
1689 return UserManager.get(mService.mContext).getUserInfo(userId);
1690 } finally {
1691 Binder.restoreCallingIdentity(identity);
1692 }
1693 }
1694
Svet Ganov99b60432015-06-27 13:15:22 -07001695 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001696 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001697 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1698 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001699 == PackageManager.PERMISSION_DENIED) {
1700 return ACTIVITY_RESTRICTION_PERMISSION;
1701 }
1702
Christopher Tateff7add02015-08-17 10:23:22 -07001703 if (activityInfo.permission == null) {
1704 return ACTIVITY_RESTRICTION_NONE;
1705 }
1706
Svet Ganov99b60432015-06-27 13:15:22 -07001707 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1708 if (opCode == AppOpsManager.OP_NONE) {
1709 return ACTIVITY_RESTRICTION_NONE;
1710 }
1711
1712 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1713 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001714 if (!ignoreTargetSecurity) {
1715 return ACTIVITY_RESTRICTION_APPOP;
1716 }
Svet Ganov99b60432015-06-27 13:15:22 -07001717 }
1718
1719 return ACTIVITY_RESTRICTION_NONE;
1720 }
1721
Svetoslav7008b512015-06-24 18:47:07 -07001722 private int getActionRestrictionForCallingPackage(String action,
1723 String callingPackage, int callingPid, int callingUid) {
1724 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001725 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001726 }
1727
1728 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1729 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001730 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001731 }
1732
1733 final PackageInfo packageInfo;
1734 try {
1735 packageInfo = mService.mContext.getPackageManager()
1736 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1737 } catch (PackageManager.NameNotFoundException e) {
1738 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001739 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001740 }
1741
1742 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001743 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001744 }
1745
1746 if (mService.checkPermission(permission, callingPid, callingUid) ==
1747 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
1751 final int opCode = AppOpsManager.permissionToOpCode(permission);
1752 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001753 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001754 }
1755
1756 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1757 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001758 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001759 }
1760
Svet Ganov99b60432015-06-27 13:15:22 -07001761 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001762 }
1763
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001764 void setLaunchSource(int uid) {
1765 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1766 }
1767
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001768 void acquireLaunchWakelock() {
1769 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1770 throw new IllegalStateException("Calling must be system uid");
1771 }
1772 mLaunchingActivity.acquire();
1773 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1774 // To be safe, don't allow the wake lock to be held for too long.
1775 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1776 }
1777 }
1778
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001779 /**
1780 * Called when the frontmost task is idle.
1781 * @return the state of mService.mBooting before this was called.
1782 */
1783 private boolean checkFinishBootingLocked() {
1784 final boolean booting = mService.mBooting;
1785 boolean enableScreen = false;
1786 mService.mBooting = false;
1787 if (!mService.mBooted) {
1788 mService.mBooted = true;
1789 enableScreen = true;
1790 }
1791 if (booting || enableScreen) {
1792 mService.postFinishBooting(booting, enableScreen);
1793 }
1794 return booting;
1795 }
1796
Craig Mautnerf3333272013-04-22 10:55:53 -07001797 // Checked.
1798 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001799 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001800 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001801
Craig Mautnerf3333272013-04-22 10:55:53 -07001802 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001803 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001804 int NS = 0;
1805 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001806 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001807 boolean activityRemoved = false;
1808
Wale Ogunwale7d701172015-03-11 15:36:30 -07001809 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001810 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001811 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1812 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001813 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1814 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001815 if (fromTimeout) {
1816 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001817 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001818
1819 // This is a hack to semi-deal with a race condition
1820 // in the client where it can be constructed with a
1821 // newer configuration from when we asked it to launch.
1822 // We'll update with whatever configuration it now says
1823 // it used to launch.
1824 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001825 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001826 }
1827
1828 // We are now idle. If someone is waiting for a thumbnail from
1829 // us, we can now deliver.
1830 r.idle = true;
1831
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001832 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001833 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001834 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001835 }
1836 }
1837
1838 if (allResumedActivitiesIdle()) {
1839 if (r != null) {
1840 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001841 }
1842
1843 if (mLaunchingActivity.isHeld()) {
1844 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1845 if (VALIDATE_WAKE_LOCK_CALLER &&
1846 Binder.getCallingUid() != Process.myUid()) {
1847 throw new IllegalStateException("Calling must be system uid");
1848 }
1849 mLaunchingActivity.release();
1850 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001851 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001852 }
1853
1854 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001855 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1856 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001857 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001858 if ((NF = mFinishingActivities.size()) > 0) {
1859 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001860 mFinishingActivities.clear();
1861 }
1862
Craig Mautnerf3333272013-04-22 10:55:53 -07001863 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001864 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001865 mStartingUsers.clear();
1866 }
1867
Craig Mautnerf3333272013-04-22 10:55:53 -07001868 // Stop any activities that are scheduled to do so but have been
1869 // waiting for the next one to start.
1870 for (int i = 0; i < NS; i++) {
1871 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001872 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001873 if (stack != null) {
1874 if (r.finishing) {
1875 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1876 } else {
1877 stack.stopActivityLocked(r);
1878 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 }
1880 }
1881
1882 // Finish any activities that are scheduled to do so but have been
1883 // waiting for the next one to start.
1884 for (int i = 0; i < NF; i++) {
1885 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001886 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001887 if (stack != null) {
1888 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1889 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 }
1891
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001892 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001893 // Complete user switch
1894 if (startingUsers != null) {
1895 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001896 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001897 }
1898 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001899 }
1900
1901 mService.trimApplications();
1902 //dump();
1903 //mWindowManager.dump();
1904
Craig Mautnerf3333272013-04-22 10:55:53 -07001905 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001906 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001907 }
1908
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001909 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001910 }
1911
Craig Mautner8e569572013-10-11 17:36:59 -07001912 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001913 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001914 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1915 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001916 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1917 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1918 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001919 }
Craig Mautner19091252013-10-05 00:03:53 -07001920 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001921 }
1922
1923 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1925 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1927 stacks.get(stackNdx).closeSystemDialogsLocked();
1928 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001929 }
1930 }
1931
Craig Mautner93529a42013-10-04 15:03:13 -07001932 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001933 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001934 }
1935
Craig Mautner8d341ef2013-03-26 09:03:27 -07001936 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001937 * Update the last used stack id for non-current user (current user's last
1938 * used stack is the focused stack)
1939 */
1940 void updateUserStackLocked(int userId, ActivityStack stack) {
1941 if (userId != mCurrentUser) {
1942 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1943 }
1944 }
1945
1946 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001947 * @return true if some activity was finished (or would have finished if doit were true).
1948 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001949 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1950 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001951 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1953 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001954 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001955 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001956 if (stack.finishDisabledPackageActivitiesLocked(
1957 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001958 didSomething = true;
1959 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001960 }
1961 }
1962 return didSomething;
1963 }
1964
Dianne Hackborna413dc02013-07-12 12:02:55 -07001965 void updatePreviousProcessLocked(ActivityRecord r) {
1966 // Now that this process has stopped, we may want to consider
1967 // it to be the previous app to try to keep around in case
1968 // the user wants to return to it.
1969
1970 // First, found out what is currently the foreground app, so that
1971 // we don't blow away the previous app if this activity is being
1972 // hosted by the process that is actually still the foreground.
1973 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001974 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1975 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001976 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1977 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001978 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001979 if (stack.mResumedActivity != null) {
1980 fgApp = stack.mResumedActivity.app;
1981 } else if (stack.mPausingActivity != null) {
1982 fgApp = stack.mPausingActivity.app;
1983 }
1984 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001985 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001986 }
1987 }
1988
1989 // Now set this one as the previous process, only if that really
1990 // makes sense to.
1991 if (r.app != null && fgApp != null && r.app != fgApp
1992 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001993 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001994 mService.mPreviousProcess = r.app;
1995 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1996 }
1997 }
1998
Wale Ogunwaled046a012015-12-24 13:05:59 -08001999 boolean resumeFocusedStackTopActivityLocked() {
2000 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002001 }
2002
Wale Ogunwaled046a012015-12-24 13:05:59 -08002003 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002004 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002005 if (targetStack != null && isFocusedStack(targetStack)) {
2006 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002007 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002008 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2009 if (r == null || r.state != RESUMED) {
2010 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002011 } else if (r.state == RESUMED) {
2012 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2013 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002014 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002015 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002016 }
2017
Todd Kennedy39bfee52016-02-24 10:28:21 -08002018 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2019 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2020 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2021 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2022 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2023 }
2024 }
2025 }
2026
Adrian Roos20d7df32016-01-12 18:59:43 +01002027 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2028 TaskRecord finishedTask = null;
2029 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2031 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002032 final int numStacks = stacks.size();
2033 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2034 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002035 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2036 if (stack == focusedStack || finishedTask == null) {
2037 finishedTask = t;
2038 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002039 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002040 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002041 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002042 }
2043
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002044 void finishVoiceTask(IVoiceInteractionSession session) {
2045 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2046 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2047 final int numStacks = stacks.size();
2048 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2049 final ActivityStack stack = stacks.get(stackNdx);
2050 stack.finishVoiceTask(session);
2051 }
2052 }
2053 }
2054
Andrii Kulianc27916642016-04-12 17:59:27 -07002055 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2056 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002057 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2058 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002059 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002060 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2061 // Caller wants the home activity moved with it. To accomplish this,
2062 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002063 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002064 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002065 ActivityStack currentStack = task.getStack();
2066 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002067 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2068 + task + " to front. Stack is null");
2069 return;
2070 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002071
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002072 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002073 int stackId = options.getLaunchStackId();
2074 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002075 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002076 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002077 if (stackId == INVALID_STACK_ID) {
2078 stackId = task.getLaunchStackId();
2079 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002080 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002081 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2082 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002083 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002084 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2085 // still need moveTaskToFrontLocked() below for any transition settings.
2086 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002087 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2088 resizeStackLocked(stackId, bounds,
2089 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002090 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002091 } else {
2092 // WM resizeTask must be done after the task is moved to the correct stack,
2093 // because Task's setBounds() also updates dim layer's bounds, but that has
2094 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002095 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002096 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002097 }
2098 }
2099
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002100 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002101 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002102 r == null ? null : r.appTimeTracker, reason);
2103
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002104 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002105 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002106
Andrii Kulian02b7a832016-10-06 23:11:56 -07002107 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002108 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002109 }
2110
Wale Ogunwale854809c2015-12-27 16:18:19 -08002111 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002112 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002113 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002114 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002115 return false;
2116 }
2117 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2118 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002119 }
2120
Winson Chung55893332017-02-17 17:13:10 -08002121 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002122 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002123 }
2124
Winson Chung55893332017-02-17 17:13:10 -08002125 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2126 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002127 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002128 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002129 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002130 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002131 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002132 return null;
2133 }
Matthew Ng330757d2017-02-28 14:19:17 -08002134 if (stackId == DOCKED_STACK_ID) {
2135 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2136 // the other side of the docked stack and we make visibility decisions based on that.
2137 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2138 }
Winson Chung55893332017-02-17 17:13:10 -08002139 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002140 }
2141
Andrii Kulian16802aa2016-11-02 12:21:33 -07002142 /**
2143 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2144 * If there is no such stack, new dynamic stack can be created.
2145 * @param displayId Target display.
2146 * @param r Activity that should be launched there.
2147 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2148 */
2149 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2150 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2151 if (activityDisplay == null) {
2152 throw new IllegalArgumentException(
2153 "Display with displayId=" + displayId + " not found.");
2154 }
2155
2156 // Return the topmost valid stack on the display.
2157 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2158 final ActivityStack stack = activityDisplay.mStacks.get(i);
2159 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2160 return stack;
2161 }
2162 }
2163
2164 // If there is no valid stack on the external display - check if new dynamic stack will do.
2165 if (displayId != Display.DEFAULT_DISPLAY) {
2166 final int newDynamicStackId = getNextStackId();
2167 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2168 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2169 }
2170 }
2171
2172 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2173 return null;
2174 }
2175
Craig Mautner967212c2013-04-13 21:10:58 -07002176 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002177 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002178 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2179 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002180 }
2181 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002182 }
2183
Jorim Jaggife762342016-10-13 14:33:27 +02002184 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2185 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2186 }
2187
Andrii Kulian7fc22812016-12-28 13:04:11 -08002188 /**
2189 * Get next focusable stack in the system. This will search across displays and stacks
2190 * in last-focused order for a focusable and visible stack, different from the target stack.
2191 *
2192 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2193 * searching for next candidate.
2194 * @return Next focusable {@link ActivityStack}, null if not found.
2195 */
2196 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2197 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2198
2199 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2200 final int displayId = mTmpOrderedDisplayIds.get(i);
2201 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2202 if (stacks == null) {
2203 continue;
2204 }
2205 for (int j = stacks.size() - 1; j >= 0; --j) {
2206 final ActivityStack stack = stacks.get(j);
2207 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002208 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002209 return stack;
2210 }
2211 }
2212 }
2213
2214 return null;
2215 }
2216
Craig Mautneree2e45a2014-06-27 12:10:03 -07002217 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002218 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002219 }
2220
2221 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002222 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2223 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2224 final TaskRecord task = tasks.get(taskNdx);
2225 if (task.isHomeTask()) {
2226 final ArrayList<ActivityRecord> activities = task.mActivities;
2227 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2228 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002229 if (r.isHomeActivity()
2230 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002231 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002232 }
2233 }
2234 }
2235 }
2236 return null;
2237 }
2238
Chong Zhangb15758a2015-11-17 12:12:03 -08002239 /**
2240 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2241 * the docked stack itself, or if it's side-by-side to the docked stack.
2242 */
2243 boolean isStackDockedInEffect(int stackId) {
2244 return stackId == DOCKED_STACK_ID ||
2245 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2246 }
2247
Todd Kennedyca4d8422015-01-15 15:19:22 -08002248 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002249 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002250 ActivityContainer activityContainer =
2251 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002252 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002253 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2254 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002255 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002256 return activityContainer;
2257 }
2258
Craig Mautner34b73df2014-01-12 21:11:08 -08002259 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002260 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2261 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2262 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002263 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2264 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002265 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002266 }
2267 }
2268
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002269 void deleteActivityContainerRecord(int stackId) {
2270 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2271 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2272 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002273 }
2274
Jorim Jaggidc249c42015-12-15 14:57:31 -08002275 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002276 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002277 if (stackId == DOCKED_STACK_ID) {
2278 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002279 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002280 return;
2281 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002282 final ActivityStack stack = getStack(stackId);
2283 if (stack == null) {
2284 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2285 return;
2286 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002287
Winson Chunga2249ac2017-03-30 18:15:30 -07002288 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2289 getStack(DOCKED_STACK_ID) != null) {
2290 // If the docked stack exists, don't resize non-floating stacks independently of the
2291 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002292 return;
2293 }
2294
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002295 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002296 mWindowManager.deferSurfaceLayout();
2297 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002298 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002299 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002300 stack.ensureVisibleActivitiesConfigurationLocked(
2301 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002302 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002303 } finally {
2304 mWindowManager.continueSurfaceLayout();
2305 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002306 }
2307 }
2308
Jorim Jaggi192086e2016-03-11 17:17:03 +01002309 void deferUpdateBounds(int stackId) {
2310 final ActivityStack stack = getStack(stackId);
2311 if (stack != null) {
2312 stack.deferUpdateBounds();
2313 }
2314 }
2315
2316 void continueUpdateBounds(int stackId) {
2317 final ActivityStack stack = getStack(stackId);
2318 if (stack != null) {
2319 stack.continueUpdateBounds();
2320 }
2321 }
2322
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002323 void notifyAppTransitionDone() {
2324 continueUpdateBounds(HOME_STACK_ID);
2325 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2326 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002327 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002328 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002329 if (task != null) {
2330 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002331 }
2332 }
2333 mResizingTasksDuringAnimation.clear();
2334 }
2335
Robert Carre7cc44d2017-03-20 19:04:30 -07002336 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2337 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002338
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002339 final ActivityStack stack = getStack(fromStackId);
2340 if (stack == null) {
2341 return;
2342 }
2343
2344 mWindowManager.deferSurfaceLayout();
2345 try {
2346 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002347 // We are moving all tasks from the docked stack to the fullscreen stack,
2348 // which is dismissing the docked stack, so resize all other stacks to
2349 // fullscreen here already so we don't end up with resize trashing.
2350 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2351 if (StackId.isResizeableByDockedStack(i)) {
2352 ActivityStack otherStack = getStack(i);
2353 if (otherStack != null) {
2354 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2355 true /* allowResizeInDockedMode */, DEFER_RESUME);
2356 }
2357 }
2358 }
2359
2360 // Also disable docked stack resizing since we have manually adjusted the
2361 // size of other stacks above and we don't want to trigger a docked stack
2362 // resize when we remove task from it below and it is detached from the
2363 // display because it no longer contains any tasks.
2364 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002365 } else if (fromStackId == PINNED_STACK_ID) {
2366 if (onTop) {
2367 // Log if we are expanding the PiP to fullscreen
2368 MetricsLogger.action(mService.mContext,
2369 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2370 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002371 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002372 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002373 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002374 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002375 // If we are moving from the pinned stack, then the animation takes care of updating
2376 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002377 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002378 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2379 final int size = tasks.size();
2380 if (onTop) {
2381 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002382 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002383 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002384 if (fromStackId == PINNED_STACK_ID) {
2385 // Update the return-to to reflect where the pinned stack task was moved
2386 // from so that we retain the stack that was previously visible if the
2387 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002388 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2389 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002390 }
Winson Chung74666102017-02-22 17:49:24 -08002391 // Defer resume until all the tasks have been moved to the fullscreen stack
2392 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2393 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002394 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002395 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002396 }
2397 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002398 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002399 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002400 final int position = fullscreenStack != null
2401 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2402 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002403 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung5af42fc2017-03-24 17:11:33 -07002404 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2405 schedulePictureInPictureModeChange,
2406 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002407 }
2408 }
Winson Chung74666102017-02-22 17:49:24 -08002409
2410 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2411 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002412 } finally {
2413 mAllowDockedStackResize = true;
2414 mWindowManager.continueSurfaceLayout();
2415 }
2416 }
2417
Robert Carr6914f082017-03-20 19:04:30 -07002418 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002419 mWindowManager.inSurfaceTransaction(
2420 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002421 }
2422
Jorim Jaggidc249c42015-12-15 14:57:31 -08002423 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002424 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2425 boolean preserveWindows) {
2426 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2427 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2428 false /* deferResume */);
2429 }
2430
2431 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2432 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2433 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002434
2435 if (!mAllowDockedStackResize) {
2436 // Docked stack resize currently disabled.
2437 return;
2438 }
2439
Jorim Jaggidc249c42015-12-15 14:57:31 -08002440 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2441 if (stack == null) {
2442 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2443 return;
2444 }
2445
2446 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2447 mWindowManager.deferSurfaceLayout();
2448 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002449 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2450 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002451 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002452 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002453
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002454 // TODO: Checking for isAttached might not be needed as if the user passes in null
2455 // dockedBounds then they want the docked stack to be dismissed.
2456 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2457 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002458 // In this case we make all other static stacks fullscreen and move all
2459 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002460 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002461
2462 // stack shouldn't contain anymore activities, so nothing to resume.
2463 r = null;
2464 } else {
2465 // Docked stacks occupy a dedicated region on screen so the size of all other
2466 // static stacks need to be adjusted so they don't overlap with the docked stack.
2467 // We get the bounds to use from window manager which has been adjusted for any
2468 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002469 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002470 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002471 final ActivityStack current = getStack(i);
2472 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002473 current.getStackDockedModeBounds(
2474 tempOtherTaskBounds /* currentTempTaskBounds */,
2475 tempRect /* outStackBounds */,
2476 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2477
Matthew Nge15352e2016-12-20 15:36:29 -08002478 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002479 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2480 tempOtherTaskInsetBounds, preserveWindows,
2481 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002482 }
2483 }
2484 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002485 if (!deferResume) {
2486 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2487 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002488 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002489 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002490 mWindowManager.continueSurfaceLayout();
2491 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2492 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002493 }
2494
Robert Carr0d00c2e2016-02-29 17:45:02 -08002495 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002496 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002497 if (stack == null) {
2498 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2499 return;
2500 }
Winson Chung19953ca2017-04-11 11:19:23 -07002501
2502 // It is possible for the bounds animation from the WM to call this but be delayed by
2503 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2504 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2505 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2506 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002507 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002508 return;
2509 }
2510
Robert Carr0d00c2e2016-02-29 17:45:02 -08002511 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2512 mWindowManager.deferSurfaceLayout();
2513 try {
2514 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002515 Rect insetBounds = null;
2516 if (tempPinnedTaskBounds != null) {
2517 // We always use 0,0 as the position for the inset rect because
2518 // if we are getting insets at all in the pinned stack it must mean
2519 // we are headed for fullscreen.
2520 insetBounds = tempRect;
2521 insetBounds.top = 0;
2522 insetBounds.left = 0;
2523 insetBounds.right = tempPinnedTaskBounds.width();
2524 insetBounds.bottom = tempPinnedTaskBounds.height();
2525 }
2526 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002527 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002528 } finally {
2529 mWindowManager.continueSurfaceLayout();
2530 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2531 }
2532 }
2533
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002534 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002535 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002536 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002537 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002538 }
2539
Wale Ogunwale1666e312016-12-16 11:27:18 -08002540 final ActivityContainer activityContainer =
2541 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002542 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002543 }
2544
Robert Carr6914f082017-03-20 19:04:30 -07002545
Robert Carre7cc44d2017-03-20 19:04:30 -07002546 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002547 final ActivityStack stack = getStack(stackId);
2548 if (stack == null) {
2549 return;
2550 }
2551
2552 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2553 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002554 /**
2555 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2556 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2557 * that the client receives onStop() before it is reparented. We do this by detaching
2558 * the stack from the display so that it will be considered invisible when
2559 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2560 * invisible as well and added to the stopping list. After which we process the
2561 * stopping list by handling the idle.
2562 */
2563 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2564 pinnedStack.mForceHidden = true;
2565 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2566 pinnedStack.mForceHidden = false;
2567 activityIdleInternalLocked(null, false /* fromTimeout */,
2568 true /* processPausingActivites */, null /* configuration */);
2569
2570 // Move all the tasks to the bottom of the fullscreen stack
2571 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002572 } else {
2573 for (int i = tasks.size() - 1; i >= 0; i--) {
2574 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2575 REMOVE_FROM_RECENTS);
2576 }
2577 }
2578 }
2579
2580 /**
Robert Carr6914f082017-03-20 19:04:30 -07002581 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2582 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2583 * instead moved back onto the fullscreen stack.
2584 */
2585 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002586 mWindowManager.inSurfaceTransaction(
2587 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002588 }
2589
2590 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002591 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2592 */
2593 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2594 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2595 }
2596
2597 /**
Winson Chung010927a2016-12-15 16:12:35 -08002598 * Removes the task with the specified task id.
2599 *
2600 * @param taskId Identifier of the task to be removed.
2601 * @param killProcess Kill any process associated with the task if possible.
2602 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002603 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2604 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002605 * @return Returns true if the given task was found and removed.
2606 */
Winson Chung6954fc92017-03-24 16:22:12 -07002607 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2608 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002609 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2610 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002611 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002612 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002613 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2614 if (tr.isPersistable) {
2615 mService.notifyTaskPersisterLocked(null, true);
2616 }
2617 return true;
2618 }
2619 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2620 return false;
2621 }
2622
2623 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2624 if (removeFromRecents) {
2625 mRecentTasks.remove(tr);
2626 tr.removedFromRecents();
2627 }
2628 ComponentName component = tr.getBaseIntent().getComponent();
2629 if (component == null) {
2630 Slog.w(TAG, "No component for base intent of task: " + tr);
2631 return;
2632 }
2633
2634 // Find any running services associated with this app and stop if needed.
2635 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2636
2637 if (!killProcess) {
2638 return;
2639 }
2640
2641 // Determine if the process(es) for this task should be killed.
2642 final String pkg = component.getPackageName();
2643 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2644 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2645 for (int i = 0; i < pmap.size(); i++) {
2646
2647 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2648 for (int j = 0; j < uids.size(); j++) {
2649 ProcessRecord proc = uids.valueAt(j);
2650 if (proc.userId != tr.userId) {
2651 // Don't kill process for a different user.
2652 continue;
2653 }
2654 if (proc == mService.mHomeProcess) {
2655 // Don't kill the home process along with tasks from the same package.
2656 continue;
2657 }
2658 if (!proc.pkgList.containsKey(pkg)) {
2659 // Don't kill process that is not associated with this task.
2660 continue;
2661 }
2662
2663 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002664 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002665 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2666 // Don't kill process(es) that has an activity in a different task that is
2667 // also in recents.
2668 return;
2669 }
2670 }
2671
2672 if (proc.foregroundServices) {
2673 // Don't kill process(es) with foreground service.
2674 return;
2675 }
2676
2677 // Add process to kill list.
2678 procsToKill.add(proc);
2679 }
2680 }
2681
2682 // Kill the running processes.
2683 for (int i = 0; i < procsToKill.size(); i++) {
2684 ProcessRecord pr = procsToKill.get(i);
2685 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2686 && pr.curReceivers.isEmpty()) {
2687 pr.kill("remove task", true);
2688 } else {
2689 // We delay killing processes that are not in the background or running a receiver.
2690 pr.waitingToKill = "remove task";
2691 }
2692 }
2693 }
2694
Craig Mautner4a1cb222013-12-04 16:14:06 -08002695 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002696 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002697 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2698 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002699 break;
2700 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002701 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002702 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002703 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002704 }
2705
Chong Zhang5dcb2752015-08-18 13:50:26 -07002706 /**
2707 * Restores a recent task to a stack
2708 * @param task The recent task to be restored.
2709 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002710 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2711 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002712 * @return true if the task has been restored successfully.
2713 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002714 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002715 if (!StackId.isStaticStack(stackId)) {
2716 // If stack is not static (or stack id is invalid) - use the default one.
2717 // This means that tasks that were on external displays will be restored on the
2718 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002719 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002720 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002721 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2722 // Put it in the fullscreen stack.
2723 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002724 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002725
Andrii Kulian02b7a832016-10-06 23:11:56 -07002726 final ActivityStack currentStack = task.getStack();
2727 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002728 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002729 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002730 // Nothing else to do since it is already restored in the right stack.
2731 return true;
2732 }
2733 // Remove current stack association, so we can re-associate the task with the
2734 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002735 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002736 }
2737
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002738 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002739 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002740
2741 if (stack == null) {
2742 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002743 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2744 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002745 return false;
2746 }
2747
Wale Ogunwale72919d22016-12-08 18:58:50 -08002748 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002749 // TODO: move call for creation here and other place into Stack.addTask()
2750 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002751 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2752 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002753 final ArrayList<ActivityRecord> activities = task.mActivities;
2754 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002755 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002756 }
2757 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002758 }
2759
Wale Ogunwale040b4702015-08-06 18:10:50 -07002760 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002761 * Move stack with all its existing content to specified display.
2762 * @param stackId Id of stack to move.
2763 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002764 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002765 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002766 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002767 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2768 if (activityDisplay == null) {
2769 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2770 + displayId);
2771 }
2772 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2773 if (activityContainer != null) {
2774 if (activityContainer.isAttachedLocked()) {
2775 if (activityContainer.getDisplayId() == displayId) {
2776 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2777 + " to its current displayId=" + displayId);
2778 }
2779
Andrii Kulian250d6532017-02-08 23:30:45 -08002780 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002781 } else {
2782 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2783 + stackId + " is not attached to any display.");
2784 }
2785 } else {
2786 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2787 + stackId);
2788 }
Andrii Kulian839def92016-11-02 10:58:58 -07002789 // TODO(multi-display): resize stacks properly if moved from split-screen.
2790 }
2791
2792 /**
Winson Chung74666102017-02-22 17:49:24 -08002793 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2794 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002795 */
Winson Chung74666102017-02-22 17:49:24 -08002796 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002797 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002798
Winson Chung74666102017-02-22 17:49:24 -08002799 // Check that we aren't reparenting to the same stack that the task is already in
2800 if (prevStack != null && prevStack.mStackId == stackId) {
2801 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2802 + " already in stackId=" + stackId);
2803 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002804 }
2805
Winson Chung74666102017-02-22 17:49:24 -08002806 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2807 // support
2808 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2809 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2810 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002811 }
2812
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002813 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2814 // multi-display.
2815 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2816 // TODO: Check ActivityView after fixing b/35349678.
2817 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2818 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2819 + " reparent task=" + task + " to stackId=" + stackId);
2820 }
2821
Winson Chung74666102017-02-22 17:49:24 -08002822 // Ensure that we aren't trying to move into a freeform stack without freeform
2823 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002824 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002825 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2826 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002827 }
2828
Winson Chung74666102017-02-22 17:49:24 -08002829 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2830 // used for split-screen mode and will cause things like the docked divider to show up. We
2831 // instead leave the task in its current stack or move it to the fullscreen stack if it
2832 // isn't currently in a stack.
2833 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2834 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2835 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2836 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002837 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002838
Winson Chung74666102017-02-22 17:49:24 -08002839 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2840 // example, a docked stack is created which will lead to the stack we are moving from being
2841 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002842 try {
Winson Chung74666102017-02-22 17:49:24 -08002843 task.mTemporarilyUnresizable = true;
2844 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002845 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002846 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002847 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002848 }
2849
Winson Chung08f81892017-03-02 15:40:51 -08002850 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002851 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2852 if (stack == null) {
2853 throw new IllegalArgumentException(
2854 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2855 }
2856
2857 final ActivityRecord r = stack.topRunningActivityLocked();
2858 if (r == null) {
2859 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2860 + " in stack=" + stack);
2861 return false;
2862 }
2863
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002864 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002865 Slog.w(TAG,
2866 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002867 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002868 return false;
2869 }
2870
Winson Chung3a682872017-04-10 14:38:05 -07002871 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002872 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002873 return true;
2874 }
2875
Winson Chung8bca9e42017-04-16 15:59:43 -07002876 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002877 boolean moveHomeStackToFront, String reason) {
2878
Wale Ogunwale480dca02016-02-06 13:58:29 -08002879 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002880
Wale Ogunwale1666e312016-12-16 11:27:18 -08002881 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002882 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002883
Wale Ogunwale480dca02016-02-06 13:58:29 -08002884 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002885 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002886
Andrii Kulian02b7a832016-10-06 23:11:56 -07002887 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002888 // An activity can't be pinned and visible behind at the same time. Go ahead and
2889 // release it from been visible behind before pinning.
2890 requestVisibleBehindLocked(r, false);
2891 }
2892
Wale Ogunwale480dca02016-02-06 13:58:29 -08002893 // Resize the pinned stack to match the current size of the task the activity we are
2894 // going to be moving is currently contained in. We do this to have the right starting
2895 // animation bounds for the pinned stack to the desired bounds the caller wants.
2896 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2897 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002898 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002899
2900 if (task.mActivities.size() == 1) {
2901 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002902 // the stack without re-parenting the activity in a different task. We don't
2903 // move the home stack forward if we are currently entering picture-in-picture
2904 // while pausing because that changes the focused stack and may prevent the new
2905 // starting activity from resuming.
2906 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002907 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002908 // Move the home stack forward if the task we just moved to the pinned stack
2909 // was launched from home so home should be visible behind it.
2910 moveHomeStackToFront(reason);
2911 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002912 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002913 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002914 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002915 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002916 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002917 // reveal/leave the other activities in their original task.
2918
2919 // Currently, we don't support reparenting activities across tasks in two different
2920 // stacks, so instead, just create a new task in the same stack, reparent the
2921 // activity into that task, and then reparent the whole task to the new stack. This
2922 // ensures that all the necessary work to migrate states in the old and new stacks
2923 // is also done.
2924 final TaskRecord newTask = task.getStack().createTaskRecord(
2925 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2926 r.mActivityType);
2927 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2928
Winson Chung5af42fc2017-03-24 17:11:33 -07002929 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002930 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002931 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002932 }
Winson Chungc2baac02017-01-11 13:34:47 -08002933
2934 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2935 // to the pinned stack
2936 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002937 } finally {
2938 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002939 }
2940
Wale Ogunwale480dca02016-02-06 13:58:29 -08002941 // The task might have already been running and its visibility needs to be synchronized
2942 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002943 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002944 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002945
Winson Chung3a682872017-04-10 14:38:05 -07002946 // Calculate the default bounds (don't use existing stack bounds as we may have just created
2947 // the stack
2948 final Rect destBounds = mWindowManager.getPictureInPictureBounds(DEFAULT_DISPLAY,
2949 aspectRatio, false /* useExistingStackBounds */);
2950
Winson Chung8bca9e42017-04-16 15:59:43 -07002951 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
2952 true /* schedulePipModeChangedOnAnimationEnd */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002953 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002954 }
2955
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002956 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002957 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2958 if (r == null || !r.isFocusable()) {
2959 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2960 "moveActivityStackToFront: unfocusable r=" + r);
2961 return false;
2962 }
2963
Bryce Leeaf691c02017-03-20 14:20:22 -07002964 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002965 final ActivityStack stack = r.getStack();
2966 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002967 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2968 + r + " task=" + task);
2969 return false;
2970 }
2971
Chong Zhang6cda19c2016-06-14 19:07:56 -07002972 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2973 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2974 "moveActivityStackToFront: already on top, r=" + r);
2975 return false;
2976 }
2977
2978 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2979 "moveActivityStackToFront: r=" + r);
2980
2981 stack.moveToFront(reason, task);
2982 return true;
2983 }
2984
David Stevensc6b91c62017-02-08 14:23:58 -08002985 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002986 mTmpFindTaskResult.r = null;
2987 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002988 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002989 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2991 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2993 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002994 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002995 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002996 continue;
2997 }
2998 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002999 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3000 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003001 continue;
3002 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003003 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003004 // It is possible to have tasks in multiple stacks with the same root affinity, so
3005 // we should keep looking after finding an affinity match to see if there is a
3006 // better match in another stack. Also, task affinity isn't a good enough reason
3007 // to target a display which isn't the source of the intent, so skip any affinity
3008 // matches not on the specified display.
3009 if (mTmpFindTaskResult.r != null) {
3010 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3011 return mTmpFindTaskResult.r;
3012 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
3013 affinityMatch = mTmpFindTaskResult.r;
3014 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003015 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003016 }
3017 }
David Stevensc6b91c62017-02-08 14:23:58 -08003018 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3019 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003020 }
3021
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003022 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3023 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3025 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003026 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003027 final ActivityRecord ar = stacks.get(stackNdx)
3028 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003029 if (ar != null) {
3030 return ar;
3031 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003032 }
3033 }
3034 return null;
3035 }
3036
Craig Mautner8d341ef2013-03-26 09:03:27 -07003037 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003038 scheduleSleepTimeout();
3039 if (!mGoingToSleep.isHeld()) {
3040 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003041 if (mLaunchingActivity.isHeld()) {
3042 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3043 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003044 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003045 mLaunchingActivity.release();
3046 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003047 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003048 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003049 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003050 }
3051
3052 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003053 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003054
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003055 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003056 final long endTime = System.currentTimeMillis() + timeout;
3057 while (true) {
3058 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003059 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3060 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003061 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3062 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3063 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003064 }
3065 if (cantShutdown) {
3066 long timeRemaining = endTime - System.currentTimeMillis();
3067 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003068 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003069 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003070 } catch (InterruptedException e) {
3071 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003072 } else {
3073 Slog.w(TAG, "Activity manager shutdown timed out");
3074 timedout = true;
3075 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003076 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003077 } else {
3078 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003079 }
3080 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003081
3082 // Force checkReadyForSleep to complete.
3083 mSleepTimeout = true;
3084 checkReadyForSleepLocked();
3085
Craig Mautner8d341ef2013-03-26 09:03:27 -07003086 return timedout;
3087 }
3088
3089 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003090 removeSleepTimeouts();
3091 if (mGoingToSleep.isHeld()) {
3092 mGoingToSleep.release();
3093 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003094 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3095 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003096 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3097 final ActivityStack stack = stacks.get(stackNdx);
3098 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003099 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003100 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 }
Craig Mautner5314a402013-09-26 12:40:16 -07003102 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003103 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003104 mGoingToSleepActivities.clear();
3105 }
3106
3107 void activitySleptLocked(ActivityRecord r) {
3108 mGoingToSleepActivities.remove(r);
3109 checkReadyForSleepLocked();
3110 }
3111
3112 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003113 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003114 // Do not care.
3115 return;
3116 }
3117
3118 if (!mSleepTimeout) {
3119 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003120 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3121 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003122 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3123 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3124 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003125 }
3126
3127 if (mStoppingActivities.size() > 0) {
3128 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003129 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003130 + mStoppingActivities.size() + " activities");
3131 scheduleIdleLocked();
3132 dontSleep = true;
3133 }
3134
3135 if (mGoingToSleepActivities.size() > 0) {
3136 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003137 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003138 + mGoingToSleepActivities.size() + " activities");
3139 dontSleep = true;
3140 }
3141
3142 if (dontSleep) {
3143 return;
3144 }
3145 }
3146
Wei Wang65c7a152016-06-02 18:51:22 -07003147 // Send launch end powerhint before going sleep
3148 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3149
Craig Mautnere0a38842013-12-16 16:14:02 -08003150 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3151 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003152 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3153 stacks.get(stackNdx).goToSleep();
3154 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003155 }
3156
3157 removeSleepTimeouts();
3158
3159 if (mGoingToSleep.isHeld()) {
3160 mGoingToSleep.release();
3161 }
3162 if (mService.mShuttingDown) {
3163 mService.notifyAll();
3164 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003165 }
3166
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003167 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003168 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003169 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003170 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003171 }
3172 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003173 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003174 mWindowManager.executeAppTransition();
3175 return true;
3176 }
3177 return false;
3178 }
3179
Craig Mautner8d341ef2013-03-26 09:03:27 -07003180 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003181 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3182 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003183 int stackNdx = stacks.size() - 1;
3184 while (stackNdx >= 0) {
3185 stacks.get(stackNdx).handleAppCrashLocked(app);
3186 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003188 }
3189 }
3190
Jose Lima4b6c6692014-08-12 17:41:12 -07003191 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003192 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003193 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003194 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3195 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003196 return false;
3197 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003198
3199 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3200 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3201 + " visible=" + visible + " stackId=" + stack.mStackId
3202 + " can't contain visible behind activities");
3203 return false;
3204 }
3205
Jose Lima4b6c6692014-08-12 17:41:12 -07003206 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003207 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3208 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003209
3210 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003211 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003212 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003213 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003214 return true;
3215 }
3216
3217 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003218 if (visible && top.fullscreen) {
3219 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003220 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3221 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3222 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3223 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003224 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003225 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3226 // Only the activity set as currently visible behind should actively reset its
3227 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003228 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3229 "requestVisibleBehind: returning visible=" + visible
3230 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3231 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003232 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003233 }
3234
Jose Lima4b6c6692014-08-12 17:41:12 -07003235 stack.setVisibleBehindActivity(visible ? r : null);
3236 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003237 // If there is a translucent home activity, we need to force it stop being translucent,
3238 // because we can't depend on the application to necessarily perform that operation.
3239 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003240 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003241 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003242 mService.convertFromTranslucent(next.appToken);
3243 }
3244 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003245 if (top.app != null && top.app.thread != null) {
3246 // Notify the top app of the change.
3247 try {
3248 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3249 } catch (RemoteException e) {
3250 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003251 }
3252 return true;
3253 }
3254
Craig Mautnerbb742462014-07-07 15:28:55 -07003255 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003256 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003257 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003258 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003259
3260 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003261 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003262 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003263 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003264 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003265
3266 // When launching tasks behind, update the last active time of the top task after the new
3267 // task has been shown briefly
3268 final ActivityRecord top = stack.topActivity();
3269 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003270 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003271 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003272 }
3273
3274 void scheduleLaunchTaskBehindComplete(IBinder token) {
3275 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3276 }
3277
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003278 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3279 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003280 mKeyguardController.beginActivityVisibilityUpdate();
3281 try {
3282 // First the front stacks. In case any are not fullscreen and are in front of home.
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.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3289 }
Craig Mautner580ea812013-04-25 12:58:38 -07003290 }
Jorim Jaggife762342016-10-13 14:33:27 +02003291 } finally {
3292 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003293 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003294 }
3295
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003296 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3297 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3298 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3299 final int topStackNdx = stacks.size() - 1;
3300 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3301 final ActivityStack stack = stacks.get(stackNdx);
3302 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3303 }
3304 }
3305 }
3306
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003307 void invalidateTaskLayers() {
3308 mTaskLayersChanged = true;
3309 }
3310
3311 void rankTaskLayersIfNeeded() {
3312 if (!mTaskLayersChanged) {
3313 return;
3314 }
3315 mTaskLayersChanged = false;
3316 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3317 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3318 int baseLayer = 0;
3319 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3320 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3321 }
3322 }
3323 }
3324
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003325 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3326 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3327 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3328 final int topStackNdx = stacks.size() - 1;
3329 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3330 final ActivityStack stack = stacks.get(stackNdx);
3331 stack.clearOtherAppTimeTrackers(except);
3332 }
3333 }
3334 }
3335
Craig Mautner8d341ef2013-03-26 09:03:27 -07003336 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003337 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3338 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003339 final int numStacks = stacks.size();
3340 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3341 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003342 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003343 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003344 }
3345 }
3346
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003347 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3348 // Examine all activities currently running in the process.
3349 TaskRecord firstTask = null;
3350 // Tasks is non-null only if two or more tasks are found.
3351 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003352 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3353 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003354 ActivityRecord r = app.activities.get(i);
3355 // First, if we find an activity that is in the process of being destroyed,
3356 // then we just aren't going to do anything for now; we want things to settle
3357 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003358 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003359 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003360 return;
3361 }
3362 // Don't consider any activies that are currently not in a state where they
3363 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003364 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3365 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003366 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003367 continue;
3368 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003369
3370 final TaskRecord task = r.getTask();
3371 if (task != null) {
3372 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003373 + " from " + r);
3374 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003375 firstTask = task;
3376 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003377 if (tasks == null) {
3378 tasks = new ArraySet<>();
3379 tasks.add(firstTask);
3380 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003381 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003382 }
3383 }
3384 }
3385 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003386 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003387 return;
3388 }
3389 // If we have activities in multiple tasks that are in a position to be destroyed,
3390 // let's iterate through the tasks and release the oldest one.
3391 final int numDisplays = mActivityDisplays.size();
3392 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3393 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3394 // Step through all stacks starting from behind, to hit the oldest things first.
3395 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3396 final ActivityStack stack = stacks.get(stackNdx);
3397 // Try to release activities in this stack; if we manage to, we are done.
3398 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3399 return;
3400 }
3401 }
3402 }
3403 }
3404
Amith Yamasani37a40c22015-06-17 13:25:42 -07003405 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003406 final int focusStackId = mFocusedStack.getStackId();
3407 // We dismiss the docked stack whenever we switch users.
3408 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003409 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3410 // also cause all tasks to be moved to the fullscreen stack at a position that is
3411 // appropriate.
3412 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003413
3414 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003415 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003416 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003417
Craig Mautner858d8a62013-04-23 17:08:34 -07003418 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003419 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3420 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003422 final ActivityStack stack = stacks.get(stackNdx);
3423 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003424 TaskRecord task = stack.topTask();
3425 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003426 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003427 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003428 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003429 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003430
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003431 ActivityStack stack = getStack(restoreStackId);
3432 if (stack == null) {
3433 stack = mHomeStack;
3434 }
3435 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003436 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003437 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003438 } else {
3439 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003440 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003441 }
Craig Mautner93529a42013-10-04 15:03:13 -07003442 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003443 }
3444
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003445 /** Checks whether the userid is a profile of the current user. */
3446 boolean isCurrentProfileLocked(int userId) {
3447 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003448 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003449 }
3450
Winson Chung4dabf232017-01-25 13:25:22 -08003451 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3452 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003453 ArrayList<ActivityRecord> stops = null;
3454
3455 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003456 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3457 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003458 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003459 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003460 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3461 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003462 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003463 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003464 if (s.finishing) {
3465 // If this activity is finishing, it is sitting on top of
3466 // everyone else but we now know it is no longer needed...
3467 // so get rid of it. Otherwise, we need to go through the
3468 // normal flow and hide it once we determine that it is
3469 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003470 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003471 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003472 }
3473 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003474 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003475 if (!processPausingActivities && s.state == PAUSING) {
3476 // Defer processing pausing activities in this iteration and reschedule
3477 // a delayed idle to reprocess it again
3478 removeTimeoutsForActivityLocked(idleActivity);
3479 scheduleIdleTimeoutLocked(idleActivity);
3480 continue;
3481 }
3482
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003483 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003484 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003485 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003486 }
3487 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003488 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003489 }
3490 }
3491
3492 return stops;
3493 }
3494
Craig Mautnercf910b02013-04-23 11:23:27 -07003495 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003496 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3497 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3498 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3499 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003500 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003501 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003502 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003503 if (r == null) Slog.e(TAG,
3504 "validateTop...: null top activity, stack=" + stack);
3505 else {
3506 final ActivityRecord pausing = stack.mPausingActivity;
3507 if (pausing != null && pausing == r) Slog.e(TAG,
3508 "validateTop...: top stack has pausing activity r=" + r
3509 + " state=" + state);
3510 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3511 "validateTop...: activity in front not resumed r=" + r
3512 + " state=" + state);
3513 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003514 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003515 final ActivityRecord resumed = stack.mResumedActivity;
3516 if (resumed != null && resumed == r) Slog.e(TAG,
3517 "validateTop...: back stack has resumed activity r=" + r
3518 + " state=" + state);
3519 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3520 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003521 }
3522 }
3523 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003524 }
3525
Craig Mautnere0570202015-05-13 13:06:11 -07003526 private String lockTaskModeToString() {
3527 switch (mLockTaskModeState) {
3528 case LOCK_TASK_MODE_LOCKED:
3529 return "LOCKED";
3530 case LOCK_TASK_MODE_PINNED:
3531 return "PINNED";
3532 case LOCK_TASK_MODE_NONE:
3533 return "NONE";
3534 default: return "unknown=" + mLockTaskModeState;
3535 }
3536 }
3537
Craig Mautner27084302013-03-25 08:05:25 -07003538 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003539 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003541 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003542 pw.print(prefix);
3543 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003544 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003545 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003546 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003547 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3548 if (packages.size() > 0) {
3549 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3550 for (int i = 0; i < packages.size(); ++i) {
3551 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3552 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3553 }
3554 }
Bryce Lee4a194382017-04-04 14:32:48 -07003555 if (!mWaitingForActivityVisible.isEmpty()) {
3556 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3557 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3558 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3559 }
3560 }
3561
Jorim Jaggi8d786932016-10-26 19:08:36 -07003562 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3563 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003564 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003565
Winson43d1f262016-06-14 16:05:55 -07003566 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003567 * Dump all connected displays' configurations.
3568 * @param prefix Prefix to apply to each line of the dump.
3569 */
3570 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3571 pw.print(prefix); pw.println("Display override configurations:");
3572 final int displayCount = mActivityDisplays.size();
3573 for (int i = 0; i < displayCount; i++) {
3574 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3575 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3576 pw.println(activityDisplay.getOverrideConfiguration());
3577 }
3578 }
3579
3580 /**
Winson43d1f262016-06-14 16:05:55 -07003581 * Dumps the activities matching the given {@param name} in the either the focused stack
3582 * or all visible stacks if {@param dumpVisibleStacks} is true.
3583 */
Winson Chung6998bc42017-02-28 17:07:05 -08003584 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3585 boolean dumpFocusedStackOnly) {
3586 if (dumpFocusedStackOnly) {
3587 return mFocusedStack.getDumpActivitiesLocked(name);
3588 } else {
Winson43d1f262016-06-14 16:05:55 -07003589 ArrayList<ActivityRecord> activities = new ArrayList<>();
3590 int numDisplays = mActivityDisplays.size();
3591 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3592 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3593 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3594 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003595 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003596 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003597 activities.addAll(stack.getDumpActivitiesLocked(name));
3598 }
3599 }
3600 }
3601 return activities;
Winson43d1f262016-06-14 16:05:55 -07003602 }
Craig Mautner20e72272013-04-01 13:45:53 -07003603 }
3604
Dianne Hackborn390517b2013-05-30 15:03:32 -07003605 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3606 boolean needSep, String prefix) {
3607 if (activity != null) {
3608 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3609 if (needSep) {
3610 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003611 }
3612 pw.print(prefix);
3613 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003614 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003615 }
3616 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003617 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003618 }
3619
Craig Mautner8d341ef2013-03-26 09:03:27 -07003620 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3621 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003622 boolean printed = false;
3623 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003624 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3625 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003626 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003627 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003628 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003629 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003630 final ActivityStack stack = stacks.get(stackNdx);
3631 StringBuilder stackHeader = new StringBuilder(128);
3632 stackHeader.append(" Stack #");
3633 stackHeader.append(stack.mStackId);
3634 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003635 stackHeader.append("\n");
3636 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3637 stackHeader.append("\n");
3638 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003639
3640 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3641 dumpClient, dumpPackage, needSep, stackHeader.toString());
3642 printed |= printedStackHeader;
3643 if (!printedStackHeader) {
3644 // Ensure we always dump the stack header even if there are no activities
3645 pw.println();
3646 pw.println(stackHeader);
3647 }
3648
Craig Mautner4a1cb222013-12-04 16:14:06 -08003649 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3650 !dumpAll, false, dumpPackage, true,
3651 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003652
Craig Mautner4a1cb222013-12-04 16:14:06 -08003653 needSep = printed;
3654 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3655 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003656 if (pr) {
3657 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003658 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003659 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003660 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3661 " mResumedActivity: ");
3662 if (pr) {
3663 printed = true;
3664 needSep = false;
3665 }
3666 if (dumpAll) {
3667 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3668 " mLastPausedActivity: ");
3669 if (pr) {
3670 printed = true;
3671 needSep = true;
3672 }
3673 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3674 needSep, " mLastNoHistoryActivity: ");
3675 }
3676 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003677 }
3678 }
3679
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003680 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3681 false, dumpPackage, true, " Activities waiting to finish:", null);
3682 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3683 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003684 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3685 false, !dumpAll, false, dumpPackage, true,
3686 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003687 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3688 false, dumpPackage, true, " Activities waiting to sleep:", null);
3689 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3690 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003691
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003692 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003693 }
3694
Dianne Hackborn390517b2013-05-30 15:03:32 -07003695 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003696 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003697 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003698 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003699 String innerPrefix = null;
3700 String[] args = null;
3701 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003702 for (int i=list.size()-1; i>=0; i--) {
3703 final ActivityRecord r = list.get(i);
3704 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3705 continue;
3706 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003707 if (innerPrefix == null) {
3708 innerPrefix = prefix + " ";
3709 args = new String[0];
3710 }
3711 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003712 final boolean full = !brief && (complete || !r.isInHistory());
3713 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003714 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003715 needNL = false;
3716 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003717 if (header1 != null) {
3718 pw.println(header1);
3719 header1 = null;
3720 }
3721 if (header2 != null) {
3722 pw.println(header2);
3723 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003724 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003725 if (lastTask != r.getTask()) {
3726 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003727 pw.print(prefix);
3728 pw.print(full ? "* " : " ");
3729 pw.println(lastTask);
3730 if (full) {
3731 lastTask.dump(pw, prefix + " ");
3732 } else if (complete) {
3733 // Complete + brief == give a summary. Isn't that obvious?!?
3734 if (lastTask.intent != null) {
3735 pw.print(prefix); pw.print(" ");
3736 pw.println(lastTask.intent.toInsecureStringWithClip());
3737 }
3738 }
3739 }
3740 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3741 pw.print(" #"); pw.print(i); pw.print(": ");
3742 pw.println(r);
3743 if (full) {
3744 r.dump(pw, innerPrefix);
3745 } else if (complete) {
3746 // Complete + brief == give a summary. Isn't that obvious?!?
3747 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3748 if (r.app != null) {
3749 pw.print(innerPrefix); pw.println(r.app);
3750 }
3751 }
3752 if (client && r.app != null && r.app.thread != null) {
3753 // flush anything that is already in the PrintWriter since the thread is going
3754 // to write to the file descriptor directly
3755 pw.flush();
3756 try {
3757 TransferPipe tp = new TransferPipe();
3758 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003759 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003760 // Short timeout, since blocking here can
3761 // deadlock with the application.
3762 tp.go(fd, 2000);
3763 } finally {
3764 tp.kill();
3765 }
3766 } catch (IOException e) {
3767 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3768 } catch (RemoteException e) {
3769 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3770 }
3771 needNL = true;
3772 }
3773 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003774 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003775 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003776
Craig Mautnerf3333272013-04-22 10:55:53 -07003777 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003778 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3779 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003780 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3781 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003782 }
3783
3784 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003785 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003786 }
3787
3788 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003789 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3790 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003791 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3792 }
3793
Craig Mautner05d29032013-05-03 13:40:13 -07003794 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003795 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3796 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3797 }
Craig Mautner05d29032013-05-03 13:40:13 -07003798 }
3799
Craig Mautner0eea92c2013-05-16 13:35:39 -07003800 void removeSleepTimeouts() {
3801 mSleepTimeout = false;
3802 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3803 }
3804
3805 final void scheduleSleepTimeout() {
3806 removeSleepTimeouts();
3807 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3808 }
3809
Craig Mautner4a1cb222013-12-04 16:14:06 -08003810 @Override
3811 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003812 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003813 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3814 }
3815
3816 @Override
3817 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003818 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3820 }
3821
3822 @Override
3823 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003824 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003825 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3826 }
3827
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003828 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003829 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003830 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003831 newDisplay = mActivityDisplays.get(displayId) == null;
3832 if (newDisplay) {
3833 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003834 if (activityDisplay.mDisplay == null) {
3835 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3836 return;
3837 }
Craig Mautner4504de52013-12-20 09:06:56 -08003838 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003839 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003840 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003841 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003843 }
3844
Andrii Kulianca007a62016-11-22 16:33:28 -08003845 /** Check if display with specified id is added to the list. */
3846 boolean isDisplayAdded(int displayId) {
3847 return mActivityDisplays.get(displayId) != null;
3848 }
3849
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003850 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003851 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003852 mService.mContext.getResources().getDimensionPixelSize(
3853 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003854 }
3855
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003856 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003857 if (displayId == DEFAULT_DISPLAY) {
3858 throw new IllegalArgumentException("Can't remove the primary display.");
3859 }
3860
Craig Mautner4a1cb222013-12-04 16:14:06 -08003861 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003862 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3863 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003864 final boolean destroyContentOnRemoval
3865 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003866 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3867 while (!stacks.isEmpty()) {
3868 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003869 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003870 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3871 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003872 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003873 } else {
3874 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3875 // a valid launch stack for all activities. This way the task history from
3876 // external display will be preserved on primary after move.
3877 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003878 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003879 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003880 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003881 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003882 }
3883 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003884 }
3885
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003886 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003887 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003888 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3889 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003890 // TODO: Update the bounds.
3891 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003892 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003893 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003894 }
3895
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003896 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003897 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003898 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003899 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003900 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003902 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003903 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003904 info.position = display != null
3905 ? display.mStacks.indexOf(stack)
3906 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003907
3908 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3909 final int numTasks = tasks.size();
3910 int[] taskIds = new int[numTasks];
3911 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003912 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003913 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003914 for (int i = 0; i < numTasks; ++i) {
3915 final TaskRecord task = tasks.get(i);
3916 taskIds[i] = task.taskId;
3917 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3918 : task.realActivity != null ? task.realActivity.flattenToString()
3919 : task.getTopActivity() != null ? task.getTopActivity().packageName
3920 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003921 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003922 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003923 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 }
3925 info.taskIds = taskIds;
3926 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003927 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003928 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003929
3930 final ActivityRecord top = stack.topRunningActivityLocked();
3931 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003932 return info;
3933 }
3934
3935 StackInfo getStackInfoLocked(int stackId) {
3936 ActivityStack stack = getStack(stackId);
3937 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003938 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003939 }
3940 return null;
3941 }
3942
3943 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003944 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003945 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3946 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003947 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003948 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003949 }
3950 }
3951 return list;
3952 }
3953
Craig Mautner15df08a2015-04-01 12:17:18 -07003954 TaskRecord getLockedTaskLocked() {
3955 final int top = mLockTaskModeTasks.size() - 1;
3956 if (top >= 0) {
3957 return mLockTaskModeTasks.get(top);
3958 }
3959 return null;
3960 }
3961
3962 boolean isLockedTask(TaskRecord task) {
3963 return mLockTaskModeTasks.contains(task);
3964 }
3965
3966 boolean isLastLockedTask(TaskRecord task) {
3967 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3968 }
3969
3970 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003971 if (!mLockTaskModeTasks.remove(task)) {
3972 return;
3973 }
3974 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3975 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003976 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003977 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3978 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003979 final Message lockTaskMsg = Message.obtain();
3980 lockTaskMsg.arg1 = task.userId;
3981 lockTaskMsg.what = LOCK_TASK_END_MSG;
3982 mHandler.sendMessage(lockTaskMsg);
3983 }
3984 }
3985
Andrii Kulianc27916642016-04-12 17:59:27 -07003986 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3987 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3988 false /* forceNonResizable */);
3989 }
3990
Jorim Jaggid53f0922016-04-06 22:16:23 -07003991 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003992 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003993 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3994 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003995 return;
3996 }
3997
Jorim Jaggicd13d332016-04-27 15:40:20 -07003998 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003999 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01004000 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07004001 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07004002
Andrii Kulianc27916642016-04-12 17:59:27 -07004003 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4004 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004005 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004006 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004007 && !topActivity.noDisplay) {
4008 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004009 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
4010 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004011 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004012 }
4013
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004014 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004015 if (mLockTaskNotify != null) {
4016 mLockTaskNotify.showToast(mLockTaskModeState);
4017 }
Jason Monka8f569c2014-07-07 12:15:21 -04004018 }
4019
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004020 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4021 if (mLockTaskModeTasks.contains(task)) {
4022 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4023 }
4024 }
4025
Craig Mautner432f64e2015-05-20 14:59:57 -07004026 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4027 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004028 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004029 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004030 final TaskRecord lockedTask = getLockedTaskLocked();
4031 if (lockedTask != null) {
4032 removeLockedTaskLocked(lockedTask);
4033 if (!mLockTaskModeTasks.isEmpty()) {
4034 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004035 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4036 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004037 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004038 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004039 return;
4040 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004041 }
Craig Mautnere0570202015-05-13 13:06:11 -07004042 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4043 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004044 return;
4045 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004046
4047 // Should have already been checked, but do it again.
4048 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004049 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4050 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004051 return;
4052 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004053 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004054 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004055 return;
4056 }
4057
4058 if (mLockTaskModeTasks.isEmpty()) {
4059 // First locktask.
4060 final Message lockTaskMsg = Message.obtain();
4061 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4062 lockTaskMsg.arg1 = task.userId;
4063 lockTaskMsg.what = LOCK_TASK_START_MSG;
4064 lockTaskMsg.arg2 = lockTaskModeState;
4065 mHandler.sendMessage(lockTaskMsg);
4066 }
4067 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004068 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4069 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004070 mLockTaskModeTasks.remove(task);
4071 mLockTaskModeTasks.add(task);
4072
4073 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004074 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004075 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004076
4077 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004078 findTaskToMoveToFrontLocked(task, 0, null, reason,
4079 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004080 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004081 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004082 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004083 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004084 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004085 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004086 }
4087
4088 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004089 return isLockTaskModeViolation(task, false);
4090 }
4091
4092 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4093 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004094 return false;
4095 }
4096 final int lockTaskAuth = task.mLockTaskAuth;
4097 switch (lockTaskAuth) {
4098 case LOCK_TASK_AUTH_DONT_LOCK:
4099 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004100 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004101 case LOCK_TASK_AUTH_LAUNCHABLE:
4102 case LOCK_TASK_AUTH_WHITELISTED:
4103 return false;
4104 case LOCK_TASK_AUTH_PINNABLE:
4105 // Pinnable tasks can't be launched on top of locktask tasks.
4106 return !mLockTaskModeTasks.isEmpty();
4107 default:
4108 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4109 return true;
4110 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004111 }
4112
Craig Mautner15df08a2015-04-01 12:17:18 -07004113 void onLockTaskPackagesUpdatedLocked() {
4114 boolean didSomething = false;
4115 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4116 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004117 final boolean wasWhitelisted =
4118 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4119 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004120 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004121 final boolean isWhitelisted =
4122 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4123 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4124 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004125 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004126 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4127 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004128 removeLockedTaskLocked(lockedTask);
4129 lockedTask.performClearTaskLocked();
4130 didSomething = true;
4131 }
4132 }
4133 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4134 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4135 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4136 final ActivityStack stack = stacks.get(stackNdx);
4137 stack.onLockTaskPackagesUpdatedLocked();
4138 }
4139 }
Craig Mautnere0570202015-05-13 13:06:11 -07004140 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004141 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004142 if (mLockTaskModeTasks.isEmpty() && task != null
4143 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4144 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004145 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4146 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4147 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4148 false);
4149 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004150 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004151 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004152 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004153 }
4154 }
4155
Benjamin Franz43261142015-02-11 15:59:44 +00004156 int getLockTaskModeState() {
4157 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004158 }
4159
Jorim Jaggife89d122015-12-22 16:28:44 +01004160 void activityRelaunchedLocked(IBinder token) {
4161 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004162 if (mService.isSleepingOrShuttingDownLocked()) {
4163 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4164 if (r != null) {
4165 r.setSleeping(true, true);
4166 }
4167 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004168 }
4169
4170 void activityRelaunchingLocked(ActivityRecord r) {
4171 mWindowManager.notifyAppRelaunching(r.appToken);
4172 }
4173
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004174 void logStackState() {
4175 mActivityMetricsLogger.logWindowState();
4176 }
4177
Winson Chung5af42fc2017-03-24 17:11:33 -07004178 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004179 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4180 // end, then ensure that we defer all in between multi-window mode changes
4181 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4182 return;
4183 }
4184
Wale Ogunwale22e25262016-02-01 10:32:02 -08004185 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4186 final ActivityRecord r = task.mActivities.get(i);
4187 if (r.app != null && r.app.thread != null) {
4188 mMultiWindowModeChangedActivities.add(r);
4189 }
4190 }
4191
4192 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4193 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4194 }
4195 }
4196
Winson Chung5af42fc2017-03-24 17:11:33 -07004197 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004198 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004199 if (prevStack == null || prevStack == stack
4200 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4201 return;
4202 }
4203
Winson Chung5af42fc2017-03-24 17:11:33 -07004204 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4205 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004206
Winson Chung5af42fc2017-03-24 17:11:33 -07004207 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4208 boolean immediate) {
4209
4210 if (immediate) {
4211 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4212 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4213 final ActivityRecord r = task.mActivities.get(i);
4214 if (r.app != null && r.app.thread != null) {
4215 r.updatePictureInPictureMode(targetStackBounds);
4216 }
4217 }
4218 } else {
4219 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4220 final ActivityRecord r = task.mActivities.get(i);
4221 if (r.app != null && r.app.thread != null) {
4222 mPipModeChangedActivities.add(r);
4223 }
4224 }
4225 mPipModeChangedTargetStackBounds = targetStackBounds;
4226
4227 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4228 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4229 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004230 }
4231 }
4232
Tony Mak853304c2016-04-18 15:17:41 +01004233 void setDockedStackMinimized(boolean minimized) {
4234 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004235 }
4236
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004237 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004238
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004239 public ActivityStackSupervisorHandler(Looper looper) {
4240 super(looper);
4241 }
4242
Winson Chung4dabf232017-01-25 13:25:22 -08004243 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004244 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004245 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4246 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004247 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004248 }
4249
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004250 @Override
4251 public void handleMessage(Message msg) {
4252 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004253 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4254 synchronized (mService) {
4255 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4256 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004257 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004258 }
4259 }
4260 } break;
4261 case REPORT_PIP_MODE_CHANGED_MSG: {
4262 synchronized (mService) {
4263 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4264 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004265 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004266 }
4267 }
4268 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004269 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004270 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4271 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004272 if (mService.mDidDexOpt) {
4273 mService.mDidDexOpt = false;
4274 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4275 nmsg.obj = msg.obj;
4276 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4277 return;
4278 }
4279 // We don't at this point know if the activity is fullscreen,
4280 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004281 activityIdleInternal((ActivityRecord) msg.obj,
4282 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004283 } break;
4284 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004285 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004286 activityIdleInternal((ActivityRecord) msg.obj,
4287 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004288 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004289 case RESUME_TOP_ACTIVITY_MSG: {
4290 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004291 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004292 }
4293 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004294 case SLEEP_TIMEOUT_MSG: {
4295 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004296 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004297 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4298 mSleepTimeout = true;
4299 checkReadyForSleepLocked();
4300 }
4301 }
4302 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004303 case LAUNCH_TIMEOUT_MSG: {
4304 if (mService.mDidDexOpt) {
4305 mService.mDidDexOpt = false;
4306 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4307 return;
4308 }
4309 synchronized (mService) {
4310 if (mLaunchingActivity.isHeld()) {
4311 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4312 if (VALIDATE_WAKE_LOCK_CALLER
4313 && Binder.getCallingUid() != Process.myUid()) {
4314 throw new IllegalStateException("Calling must be system uid");
4315 }
4316 mLaunchingActivity.release();
4317 }
4318 }
4319 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004320 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004321 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004322 } break;
4323 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004324 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004325 } break;
4326 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004327 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004328 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004329 case CONTAINER_CALLBACK_VISIBILITY: {
4330 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004331 final IActivityContainerCallback callback = container.mCallback;
4332 if (callback != null) {
4333 try {
4334 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4335 } catch (RemoteException e) {
4336 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004337 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004338 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004339 case LOCK_TASK_START_MSG: {
4340 // When lock task starts, we disable the status bars.
4341 try {
Jason Monk62515be2014-05-21 16:06:19 -04004342 if (mLockTaskNotify == null) {
4343 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004344 }
Jason Monk62515be2014-05-21 16:06:19 -04004345 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004346 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004347 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004348 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004349 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004350 flags = StatusBarManager.DISABLE_MASK
4351 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004352 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004353 flags = StatusBarManager.DISABLE_MASK
4354 & (~StatusBarManager.DISABLE_BACK)
4355 & (~StatusBarManager.DISABLE_HOME)
4356 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004357 }
4358 getStatusBarService().disable(flags, mToken,
4359 mService.mContext.getPackageName());
4360 }
4361 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004362 if (getDevicePolicyManager() != null) {
4363 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004364 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004365 }
justinzhang5286d3f2014-05-12 17:06:01 -04004366 } catch (RemoteException ex) {
4367 throw new RuntimeException(ex);
4368 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004369 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004370 case LOCK_TASK_END_MSG: {
4371 // When lock task ends, we enable the status bars.
4372 try {
Jason Monk62515be2014-05-21 16:06:19 -04004373 if (getStatusBarService() != null) {
4374 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4375 mService.mContext.getPackageName());
4376 }
4377 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004378 if (getDevicePolicyManager() != null) {
4379 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4380 msg.arg1);
4381 }
Jason Monk62515be2014-05-21 16:06:19 -04004382 if (mLockTaskNotify == null) {
4383 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4384 }
4385 mLockTaskNotify.show(false);
4386 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004387 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004388 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004389 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004390 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004391 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004392 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004393 new LockPatternUtils(mService.mContext)
4394 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004395 }
4396 } catch (SettingNotFoundException e) {
4397 // No setting, don't lock.
4398 }
justinzhang5286d3f2014-05-12 17:06:01 -04004399 } catch (RemoteException ex) {
4400 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004401 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004402 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004403 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004404 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004405 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4406 if (mLockTaskNotify == null) {
4407 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4408 }
4409 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4410 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004411 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4412 final ActivityContainer container = (ActivityContainer) msg.obj;
4413 final IActivityContainerCallback callback = container.mCallback;
4414 if (callback != null) {
4415 try {
4416 callback.onAllActivitiesComplete(container.asBinder());
4417 } catch (RemoteException e) {
4418 }
4419 }
4420 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004421 case LAUNCH_TASK_BEHIND_COMPLETE: {
4422 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004423 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004424 if (r != null) {
4425 handleLaunchTaskBehindCompleteLocked(r);
4426 }
4427 }
4428 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004429
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004430 }
4431 }
4432 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004433
Ying Wangb081a592014-04-22 15:20:16 -07004434 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004435 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4436 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004437 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004438 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004439 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004440 ActivityRecord mParentActivity = null;
4441 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004442
Craig Mautnere3a00d72014-04-16 08:31:19 -07004443 boolean mVisible = true;
4444
Craig Mautner4a1cb222013-12-04 16:14:06 -08004445 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004446 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004447
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004448 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4449 final static int CONTAINER_STATE_NO_SURFACE = 1;
4450 final static int CONTAINER_STATE_FINISHING = 2;
4451 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4452
Wale Ogunwale1666e312016-12-16 11:27:18 -08004453 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004454 synchronized (mService) {
4455 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004456 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004457 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004458
4459 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004460 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004461 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004462 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004463
Bryce Leeaf691c02017-03-20 14:20:22 -07004464 protected void createStack(int stackId, boolean onTop) {
4465 switch (stackId) {
4466 case PINNED_STACK_ID:
4467 new PinnedActivityStack(this, mRecentTasks, onTop);
4468 break;
4469 default:
4470 new ActivityStack(this, mRecentTasks, onTop);
4471 break;
4472 }
4473 }
4474
Andrii Kulian839def92016-11-02 10:58:58 -07004475 /**
4476 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004477 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004478 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004479 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004480 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004481 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004482 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004483 if (mActivityDisplay != null) {
4484 throw new IllegalStateException("ActivityContainer is already attached, " +
4485 "displayId=" + mActivityDisplay.mDisplayId);
4486 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004487 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004488 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004489 }
4490
4491 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004492 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004493 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004494 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4495 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004496 return;
4497 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004498 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004499 }
4500 }
4501
4502 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004503 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004504 synchronized (mService) {
4505 if (mActivityDisplay != null) {
4506 return mActivityDisplay.mDisplayId;
4507 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004508 }
4509 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004510 }
4511
Jeff Brownca9bc702014-02-11 14:32:56 -08004512 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004513 public int getStackId() {
4514 synchronized (mService) {
4515 return mStackId;
4516 }
4517 }
4518
4519 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004520 public boolean injectEvent(InputEvent event) {
4521 final long origId = Binder.clearCallingIdentity();
4522 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004523 synchronized (mService) {
4524 if (mActivityDisplay != null) {
4525 return mInputManagerInternal.injectInputEvent(event,
4526 mActivityDisplay.mDisplayId,
4527 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4528 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004529 }
4530 return false;
4531 } finally {
4532 Binder.restoreCallingIdentity(origId);
4533 }
4534 }
4535
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004536 @Override
4537 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004538 synchronized (mService) {
4539 if (mContainerState == CONTAINER_STATE_FINISHING) {
4540 return;
4541 }
4542 mContainerState = CONTAINER_STATE_FINISHING;
4543
Craig Mautnerd163e752014-06-13 17:18:47 -07004544 long origId = Binder.clearCallingIdentity();
4545 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004546 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004547 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004548 } finally {
4549 Binder.restoreCallingIdentity(origId);
4550 }
4551 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004552 }
4553
Andrii Kulian03c403d2016-11-11 11:14:12 -08004554 /**
4555 * Remove the stack completely. Must be called only when there are no tasks left in it,
4556 * as this method does not finish running activities.
4557 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004558 void removeLocked() {
4559 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004560 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004561 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004562 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004563 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004564 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004565 }
4566
Andrii Kulian839def92016-11-02 10:58:58 -07004567 /**
4568 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4569 * completely or re-parented.
4570 */
4571 private void removeFromDisplayLocked() {
4572 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4573 + " current displayId=" + mActivityDisplay.mDisplayId);
4574
Wale Ogunwale1666e312016-12-16 11:27:18 -08004575 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004576 mActivityDisplay = null;
4577 }
4578
4579 /**
4580 * Move the stack to specified display.
4581 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004582 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004583 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004584 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004585 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4586 + mActivityDisplay + " to display=" + activityDisplay
4587 + " Callers=" + Debug.getCallers(2));
4588
4589 removeFromDisplayLocked();
4590
4591 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004592 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004593 }
4594
Craig Mautner4a1cb222013-12-04 16:14:06 -08004595 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004596 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004597 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004598 }
4599
4600 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004601 public final int startActivityIntentSender(IIntentSender intentSender)
4602 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004603 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4604
4605 if (!(intentSender instanceof PendingIntentRecord)) {
4606 throw new IllegalArgumentException("Bad PendingIntent object");
4607 }
4608
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004609 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004610 Binder.getCallingUid(), mCurrentUser, false,
4611 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004612
4613 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4614 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4615 pendingIntent.key.requestResolvedType);
4616
4617 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4618 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4619 }
4620
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004621 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004622 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004623 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004624 throw new SecurityException(
4625 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4626 }
4627 }
4628
Craig Mautnerdf88d732014-01-27 09:21:32 -08004629 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004630 public IBinder asBinder() {
4631 return this;
4632 }
4633
Craig Mautner4504de52013-12-20 09:06:56 -08004634 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004635 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004636 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004637 }
4638
Craig Mautner4a1cb222013-12-04 16:14:06 -08004639 ActivityStackSupervisor getOuter() {
4640 return ActivityStackSupervisor.this;
4641 }
4642
Craig Mautnerd163e752014-06-13 17:18:47 -07004643 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004644 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004645 }
4646
Craig Mautner6985bad2014-04-21 15:22:06 -07004647 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004648 void setVisible(boolean visible) {
4649 if (mVisible != visible) {
4650 mVisible = visible;
4651 if (mCallback != null) {
4652 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4653 0 /* unused */, this).sendToTarget();
4654 }
4655 }
4656 }
4657
Craig Mautner6985bad2014-04-21 15:22:06 -07004658 void setDrawn() {
4659 }
4660
Craig Mautner1b4bf852014-05-26 15:06:32 -07004661 // You can always start a new task on a regular ActivityStack.
4662 boolean isEligibleForNewTasks() {
4663 return true;
4664 }
4665
Craig Mautnerd163e752014-06-13 17:18:47 -07004666 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004667 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004668 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004669 }
4670
Craig Mautner34b73df2014-01-12 21:11:08 -08004671 @Override
4672 public String toString() {
4673 return mIdString + (mActivityDisplay == null ? "N" : "A");
4674 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004675 }
4676
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004677 private class VirtualActivityContainer extends ActivityContainer {
4678 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004679 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004680
4681 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004682 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4683 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004684 mParentActivity = parent;
4685 mCallback = callback;
4686 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004687 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004688 }
4689
4690 @Override
4691 public void setSurface(Surface surface, int width, int height, int density) {
4692 super.setSurface(surface, width, height, density);
4693
4694 synchronized (mService) {
4695 final long origId = Binder.clearCallingIdentity();
4696 try {
4697 setSurfaceLocked(surface, width, height, density);
4698 } finally {
4699 Binder.restoreCallingIdentity(origId);
4700 }
4701 }
4702 }
4703
4704 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4705 if (mContainerState == CONTAINER_STATE_FINISHING) {
4706 return;
4707 }
4708 VirtualActivityDisplay virtualActivityDisplay =
4709 (VirtualActivityDisplay) mActivityDisplay;
4710 if (virtualActivityDisplay == null) {
4711 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004712 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004713 mActivityDisplay = virtualActivityDisplay;
4714 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004715 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004716 }
4717
4718 if (mSurface != null) {
4719 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004720 }
4721
Craig Mautner6985bad2014-04-21 15:22:06 -07004722 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004723 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004724 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004725 } else {
4726 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004727 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004728 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004729 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004730 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004731 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004732
Craig Mautnerd163e752014-06-13 17:18:47 -07004733 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004734
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004735 if (DEBUG_STACK) Slog.d(TAG_STACK,
4736 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004737 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004738
Craig Mautner6985bad2014-04-21 15:22:06 -07004739 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004740 boolean isAttachedLocked() {
4741 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004742 }
4743
4744 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004745 void setDrawn() {
4746 synchronized (mService) {
4747 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004748 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004749 }
4750 }
4751
Craig Mautner1b4bf852014-05-26 15:06:32 -07004752 // Never start a new task on an ActivityView if it isn't explicitly specified.
4753 @Override
4754 boolean isEligibleForNewTasks() {
4755 return false;
4756 }
4757
Craig Mautnerd163e752014-06-13 17:18:47 -07004758 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004759 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004760 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4761 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4762 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4763 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4764 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004765 }
4766 }
4767
Craig Mautner4a1cb222013-12-04 16:14:06 -08004768 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4769 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004770 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004771 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004772 int mDisplayId;
4773 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004774
Craig Mautner4a1cb222013-12-04 16:14:06 -08004775 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4776 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004777 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004778
Jose Lima4b6c6692014-08-12 17:41:12 -07004779 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004780
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004781 /** Array of all UIDs that are present on the display. */
4782 private IntArray mDisplayAccessUIDs = new IntArray();
4783
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004784 ActivityDisplay() {
4785 }
Craig Mautner4504de52013-12-20 09:06:56 -08004786
Craig Mautner1a70a162014-09-13 12:09:31 -07004787 // After instantiation, check that mDisplay is not null before using this. The alternative
4788 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004789 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004790 final Display display = mDisplayManager.getDisplay(displayId);
4791 if (display == null) {
4792 return;
4793 }
4794 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004795 }
4796
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004797 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004798 mDisplay = display;
4799 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004800 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004801
Winson Chung7c3ede32017-03-14 14:18:34 -07004802 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004803 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004804 + " to displayId=" + mDisplayId + " position=" + position);
4805 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004806 }
4807
Wale Ogunwale1666e312016-12-16 11:27:18 -08004808 void detachStack(ActivityStack stack) {
4809 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004810 + " from displayId=" + mDisplayId);
4811 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004812 }
4813
Jose Lima4b6c6692014-08-12 17:41:12 -07004814 void setVisibleBehindActivity(ActivityRecord r) {
4815 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004816 }
4817
Jose Lima4b6c6692014-08-12 17:41:12 -07004818 boolean hasVisibleBehindActivity() {
4819 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004820 }
4821
Craig Mautner34b73df2014-01-12 21:11:08 -08004822 @Override
4823 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004824 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4825 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004826
4827 @Override
4828 protected int getChildCount() {
4829 return mStacks.size();
4830 }
4831
4832 @Override
4833 protected ConfigurationContainer getChildAt(int index) {
4834 return mStacks.get(index);
4835 }
4836
4837 @Override
4838 protected ConfigurationContainer getParent() {
4839 return ActivityStackSupervisor.this;
4840 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004841
4842 boolean isPrivate() {
4843 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4844 }
4845
4846 boolean isUidPresent(int uid) {
4847 for (ActivityStack stack : mStacks) {
4848 if (stack.isUidPresent(uid)) {
4849 return true;
4850 }
4851 }
4852 return false;
4853 }
4854
4855 /** Update and get all UIDs that are present on the display and have access to it. */
4856 private IntArray getPresentUIDs() {
4857 mDisplayAccessUIDs.clear();
4858 for (ActivityStack stack : mStacks) {
4859 stack.getPresentUIDs(mDisplayAccessUIDs);
4860 }
4861 return mDisplayAccessUIDs;
4862 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004863
4864 boolean shouldDestroyContentOnRemove() {
4865 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4866 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004867 }
4868
4869 class VirtualActivityDisplay extends ActivityDisplay {
4870 VirtualDisplay mVirtualDisplay;
4871
Craig Mautner6985bad2014-04-21 15:22:06 -07004872 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004873 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004874 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4875 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4876 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4877 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004878
4879 init(mVirtualDisplay.getDisplay());
4880
Bryce Leeaf3a4002017-03-20 10:37:12 -07004881 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004882 }
4883
4884 void setSurface(Surface surface) {
4885 if (mVirtualDisplay != null) {
4886 mVirtualDisplay.setSurface(surface);
4887 }
4888 }
4889
4890 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004891 void detachStack(ActivityStack stack) {
4892 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004893 if (mVirtualDisplay != null) {
4894 mVirtualDisplay.release();
4895 mVirtualDisplay = null;
4896 }
4897 }
4898
4899 @Override
4900 public String toString() {
4901 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004902 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004903 }
Jose Lima58e66d62014-05-27 20:00:27 -07004904
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004905 ActivityStack findStackBehind(ActivityStack stack) {
4906 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004907 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004908 if (display == null) {
4909 return null;
4910 }
4911 final ArrayList<ActivityStack> stacks = display.mStacks;
4912 for (int i = stacks.size() - 1; i >= 0; i--) {
4913 if (stacks.get(i) == stack && i > 0) {
4914 return stacks.get(i - 1);
4915 }
4916 }
4917 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4918 + " in=" + stacks);
4919 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004920
Jorim Jaggic69bd222016-03-15 14:38:37 +01004921 /**
4922 * Puts a task into resizing mode during the next app transition.
4923 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004924 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004925 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004926 private void setResizingDuringAnimation(TaskRecord task) {
4927 mResizingTasksDuringAnimation.add(task.taskId);
4928 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004929 }
4930
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004931 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4932 final TaskRecord task;
4933 final int callingUid;
4934 final String callingPackage;
4935 final Intent intent;
4936 final int userId;
4937 final ActivityOptions activityOptions = (bOptions != null)
4938 ? new ActivityOptions(bOptions) : null;
4939 final int launchStackId = (activityOptions != null)
4940 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004941 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004942 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004943 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004944 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004945
4946 if (launchStackId == DOCKED_STACK_ID) {
4947 mWindowManager.setDockedStackCreateState(
4948 activityOptions.getDockCreateMode(), null /* initialBounds */);
4949
4950 // Defer updating the stack in which recents is until the app transition is done, to
4951 // not run into issues where we still need to draw the task in recents but the
4952 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004953 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004954 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4955 }
4956
Winson Chung7900bee2017-03-10 08:59:25 -08004957 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4958 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004959 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004960 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004961 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004962 throw new IllegalArgumentException(
4963 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4964 }
4965
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004966 // Since we don't have an actual source record here, we assume that the currently focused
4967 // activity was the source.
4968 final ActivityStack focusedStack = getFocusedStack();
4969 final ActivityRecord sourceRecord =
4970 focusedStack != null ? focusedStack.topActivity() : null;
4971
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004972 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004973 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004974 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4975 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004976 }
4977 }
4978
4979 // If the user must confirm credentials (e.g. when first launching a work app and the
4980 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4981 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4982 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004983 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004984 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004985 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004986 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4987 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004988
4989 // If we are launching the task in the docked stack, put it into resizing mode so
4990 // the window renders full-screen with the background filling the void. Also only
4991 // call this at the end to make sure that tasks exists on the window manager side.
4992 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004993 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004994 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004995
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004996 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004997 ActivityManager.START_TASK_TO_FRONT,
Bryce Leeaf691c02017-03-20 14:20:22 -07004998 sourceRecord != null ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004999 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005000 return ActivityManager.START_TASK_TO_FRONT;
5001 }
5002 callingUid = task.mCallingUid;
5003 callingPackage = task.mCallingPackage;
5004 intent = task.intent;
5005 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5006 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005007 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
5008 null, null, 0, 0, bOptions, userId, null, task);
5009 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005010 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005011 }
5012 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005013 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005014
5015 /**
5016 * @return a list of activities which are the top ones in each visible stack. The first
5017 * entry will be the focused activity.
5018 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005019 List<IBinder> getTopVisibleActivities() {
5020 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5021 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005022 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005023 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5024 // Traverse all stacks on a display.
5025 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5026 final ActivityStack stack = display.mStacks.get(j);
5027 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005028 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005029 == ActivityStack.STACK_VISIBLE) {
5030 final ActivityRecord top = stack.topActivity();
5031 if (top != null) {
5032 if (stack == mFocusedStack) {
5033 topActivityTokens.add(0, top.appToken);
5034 } else {
5035 topActivityTokens.add(top.appToken);
5036 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005037 }
5038 }
5039 }
5040 }
5041 return topActivityTokens;
5042 }
Bryce Lee4a194382017-04-04 14:32:48 -07005043
5044 /**
5045 * Internal container to store a match qualifier alongside a WaitResult.
5046 */
5047 static class WaitInfo {
5048 private final ComponentName mTargetComponent;
5049 private final WaitResult mResult;
5050
5051 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5052 this.mTargetComponent = targetComponent;
5053 this.mResult = result;
5054 }
5055
5056 public boolean matches(ActivityRecord record) {
5057 return mTargetComponent == null ||
5058 (TextUtils.equals(mTargetComponent.getPackageName(), record.info.packageName)
5059 && TextUtils.equals(mTargetComponent.getClassName(), record.info.name));
5060 }
5061
5062 public WaitResult getResult() {
5063 return mResult;
5064 }
5065
5066 public ComponentName getComponent() {
5067 return mTargetComponent;
5068 }
5069
5070 public void dump(PrintWriter pw, String prefix) {
5071 pw.println(prefix + "WaitInfo:");
5072 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5073 pw.println(prefix + " mResult=");
5074 mResult.dump(pw, prefix);
5075 }
5076 }
Craig Mautner27084302013-03-25 08:05:25 -07005077}