blob: f58f717878f1b6994d29de12f8af33c725b304fd [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 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
Wale Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Matthew Nga51dcaa2018-05-07 15:36:06 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Matthew Ng99b3cdc2018-05-01 14:24:38 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale30e441d2017-11-09 08:28:45 -080022import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070023import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
24import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070025import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale68278562017-09-23 17:13:55 -070026import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale2b07da82017-11-08 14:52:40 -080027import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070028import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale68278562017-09-23 17:13:55 -070029import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwalef75962a2017-08-23 14:58:04 -070030import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070031import static android.app.WindowConfiguration.activityTypeToString;
32import static android.app.WindowConfiguration.windowingModeToString;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080033import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080034import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070035import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwalef5d1e352016-09-22 08:34:42 -070036import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian7211d2e2017-01-27 15:58:05 -080037import static android.view.Display.FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -070038import static android.view.Display.INVALID_DISPLAY;
Winson Chung1dbc8112017-09-28 18:05:31 -070039
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070040import static com.android.server.am.ActivityDisplay.POSITION_BOTTOM;
41import static com.android.server.am.ActivityDisplay.POSITION_TOP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070042import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_APP;
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CLEANUP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070046import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070047import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RESULTS;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070051import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TRANSITION;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_USER_LEAVING;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_APP;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CLEANUP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070061import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RESULTS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070066import 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_TRANSITION;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_USER_LEAVING;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
73import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
74import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Bryce Lee7ace3952018-02-16 14:34:32 -080075import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
76import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
77import static com.android.server.am.ActivityStack.ActivityState.FINISHING;
Wale Ogunwale08488a62017-09-05 12:37:03 -070078import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
Bryce Lee7ace3952018-02-16 14:34:32 -080079import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
80import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -070081import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale39381972015-12-17 17:15:29 -080083import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Winson Chung6954fc92017-03-24 16:22:12 -070084import static com.android.server.am.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070085import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Winson Chung6954fc92017-03-24 16:22:12 -070086import static com.android.server.am.ActivityStackSupervisor.REMOVE_FROM_RECENTS;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070087import static com.android.server.am.ActivityStackProto.BOUNDS;
88import static com.android.server.am.ActivityStackProto.CONFIGURATION_CONTAINER;
89import static com.android.server.am.ActivityStackProto.DISPLAY_ID;
90import static com.android.server.am.ActivityStackProto.FULLSCREEN;
91import static com.android.server.am.ActivityStackProto.ID;
92import static com.android.server.am.ActivityStackProto.RESUMED_ACTIVITY;
93import static com.android.server.am.ActivityStackProto.TASKS;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010094import static android.view.WindowManager.TRANSIT_ACTIVITY_CLOSE;
95import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Adrian Roos93577212018-04-10 14:12:10 -070096import static android.view.WindowManager.TRANSIT_CRASHING_ACTIVITY_CLOSE;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010097import static android.view.WindowManager.TRANSIT_NONE;
98import static android.view.WindowManager.TRANSIT_TASK_CLOSE;
99import static android.view.WindowManager.TRANSIT_TASK_OPEN;
100import static android.view.WindowManager.TRANSIT_TASK_OPEN_BEHIND;
101import static android.view.WindowManager.TRANSIT_TASK_TO_BACK;
102import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700103
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800104import static java.lang.Integer.MAX_VALUE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105
106import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700107import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700108import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800110import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700111import android.app.ResultInfo;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700112import android.app.WindowConfiguration.ActivityType;
113import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian446e8242017-10-26 15:17:29 -0700114import android.app.servertransaction.ActivityResultItem;
Andrii Kulian9956d892018-02-14 13:48:56 -0800115import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700116import android.app.servertransaction.NewIntentItem;
117import android.app.servertransaction.WindowVisibilityItem;
118import android.app.servertransaction.DestroyActivityItem;
119import android.app.servertransaction.PauseActivityItem;
120import android.app.servertransaction.ResumeActivityItem;
121import android.app.servertransaction.StopActivityItem;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700122import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700123import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800125import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700126import android.content.res.Configuration;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700127import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -0800128import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129import android.os.Binder;
Craig Mautner329f4122013-11-07 09:10:42 -0800130import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700131import android.os.Handler;
132import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +0900133import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135import android.os.RemoteException;
136import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700137import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700138import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700139import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800141import android.util.IntArray;
Jorim Jaggi023da532016-04-20 22:42:32 -0700142import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700143import android.util.Slog;
Andrii Kulian1e32e022016-09-16 15:29:34 -0700144import android.util.SparseArray;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700145import android.util.proto.ProtoOutputStream;
Craig Mautner59c00972012-07-30 12:10:24 -0700146import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700147
Andreas Gampea36dc622018-02-05 17:19:22 -0800148import com.android.internal.annotations.GuardedBy;
Bryce Lee840c5662017-04-13 10:02:51 -0700149import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700150import com.android.internal.app.IVoiceInteractor;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700151import com.android.internal.os.BatteryStatsImpl;
152import com.android.server.Watchdog;
153import com.android.server.am.ActivityManagerService.ItemMatcher;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700154import com.android.server.wm.ConfigurationContainer;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800155import com.android.server.wm.StackWindowController;
156import com.android.server.wm.StackWindowListener;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700157import com.android.server.wm.WindowManagerService;
158
Craig Mautnercae015f2013-02-08 14:31:27 -0800159import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800160import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161import java.lang.ref.WeakReference;
162import java.util.ArrayList;
163import java.util.Iterator;
164import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800165import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700166import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167
168/**
169 * State and management of a single stack of activities.
170 */
Winson Chung55893332017-02-17 17:13:10 -0800171class ActivityStack<T extends StackWindowController> extends ConfigurationContainer
172 implements StackWindowListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800173 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700174 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
175 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800176 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700177 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700178 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700179 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700180 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700181 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700182 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700184 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
185 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
186 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
187 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
188 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700189
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700190 // Ticks during which we check progress while waiting for an app to launch.
191 static final int LAUNCH_TICK = 500;
192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193 // How long we wait until giving up on the last activity to pause. This
194 // is short because it directly impacts the responsiveness of starting the
195 // next activity.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700196 private static final int PAUSE_TIMEOUT = 500;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700197
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700198 // How long we wait for the activity to tell us it has stopped before
199 // giving up. This is a good amount of time because we really need this
Robert Carr3406d462018-03-15 16:19:07 -0700200 // from the application in order to get its saved state. Once the stop
201 // is complete we may start destroying client resources triggering
202 // crashes if the UI thread was hung. We put this timeout one second behind
203 // the ANR timeout so these situations will generate ANR instead of
204 // Surface lost or other errors.
205 private static final int STOP_TIMEOUT = 11 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700206
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700207 // How long we wait until giving up on an activity telling us it has
208 // finished destroying itself.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700209 private static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800210
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700211 // Set to false to disable the preview that is shown while a new activity
212 // is being started.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700213 private static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800214
Craig Mautner5eda9b32013-07-02 11:58:16 -0700215 // How long to wait for all background Activities to redraw following a call to
216 // convertToTranslucent().
Andrii Kulian21713ac2016-10-12 22:05:05 -0700217 private static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700218
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800219 // How many activities have to be scheduled to stop to force a stop pass.
220 private static final int MAX_STOPPING_TO_FORCE = 3;
221
Andrii Kulian1779e612016-10-12 21:58:25 -0700222 @Override
223 protected int getChildCount() {
224 return mTaskHistory.size();
225 }
226
227 @Override
228 protected ConfigurationContainer getChildAt(int index) {
229 return mTaskHistory.get(index);
230 }
231
232 @Override
233 protected ConfigurationContainer getParent() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700234 return getDisplay();
Andrii Kulian1779e612016-10-12 21:58:25 -0700235 }
236
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800237 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700238 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800239 super.onParentChanged();
240 mStackSupervisor.updateUIDsPresentOnDisplay();
241 }
242
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700243 enum ActivityState {
244 INITIALIZING,
245 RESUMED,
246 PAUSING,
247 PAUSED,
248 STOPPING,
249 STOPPED,
250 FINISHING,
251 DESTROYING,
252 DESTROYED
253 }
254
Bryce Lee840c5662017-04-13 10:02:51 -0700255 @VisibleForTesting
Wale Ogunwale06579d62016-04-30 15:29:06 -0700256 /* The various modes for the method {@link #removeTask}. */
257 // Task is being completely removed from all stacks in the system.
Bryce Lee840c5662017-04-13 10:02:51 -0700258 protected static final int REMOVE_TASK_MODE_DESTROYING = 0;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700259 // Task is being removed from this stack so we can add it to another stack. In the case we are
260 // moving we don't want to perform some operations on the task like removing it from window
261 // manager or recents.
262 static final int REMOVE_TASK_MODE_MOVING = 1;
263 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
264 // stack and the new stack will be on top of all stacks.
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700265 static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700266
Andrii Kulian1e32e022016-09-16 15:29:34 -0700267 // The height/width divide used when fitting a task within a bounds with method
268 // {@link #fitWithinBounds}.
269 // We always want the task to to be visible in the bounds without affecting its size when
270 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
271 // the input bounds right or bottom side minus the width or height divided by this value.
272 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
273
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274 final ActivityManagerService mService;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700275 private final WindowManagerService mWindowManager;
Winson Chung55893332017-02-17 17:13:10 -0800276 T mWindowContainerController;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 /**
279 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800280 * running) activities. It contains #TaskRecord objects.
281 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800282 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800283
284 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700285 * List of running activities, sorted by recent usage.
286 * The first entry in the list is the least recently used.
287 * It contains HistoryRecord objects.
288 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800289 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700290
291 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 * When we are in the process of pausing an activity, before starting the
293 * next one, this variable holds the activity that is currently being paused.
294 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800295 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700296
297 /**
298 * This is the last activity that we put into the paused state. This is
299 * used to determine if we need to do an activity transition while sleeping,
300 * when we normally hold the top activity paused.
301 */
302 ActivityRecord mLastPausedActivity = null;
303
304 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700305 * Activities that specify No History must be removed once the user navigates away from them.
306 * If the device goes to sleep with such an activity in the paused state then we save it here
307 * and finish it later if another activity replaces it on wakeup.
308 */
309 ActivityRecord mLastNoHistoryActivity = null;
310
311 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700312 * Current activity that is resumed, or null if there is none.
313 */
314 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800315
Craig Mautner5eda9b32013-07-02 11:58:16 -0700316 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
317 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
318 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
319 // Activity in mTranslucentActivityWaiting is notified via
320 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
321 // background activity being drawn then the same call will be made with a true value.
322 ActivityRecord mTranslucentActivityWaiting = null;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700323 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700324
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700325 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700326 * Set when we know we are going to be calling updateConfiguration()
327 * soon, so want to skip intermediate config checks.
328 */
329 boolean mConfigWillChange;
330
Winson Chung47900652017-04-06 18:44:25 -0700331 /**
332 * When set, will force the stack to report as invisible.
333 */
334 boolean mForceHidden = false;
335
Andrii Kulian21713ac2016-10-12 22:05:05 -0700336 private boolean mUpdateBoundsDeferred;
337 private boolean mUpdateBoundsDeferredCalled;
338 private final Rect mDeferredBounds = new Rect();
339 private final Rect mDeferredTaskBounds = new Rect();
340 private final Rect mDeferredTaskInsetBounds = new Rect();
Jorim Jaggi192086e2016-03-11 17:17:03 +0100341
Craig Mautner858d8a62013-04-23 17:08:34 -0700342 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700343
Craig Mautnerc00204b2013-03-05 15:02:14 -0800344 final int mStackId;
Craig Mautnere0a38842013-12-16 16:14:02 -0800345 /** The attached Display's unique identifier, or -1 if detached */
346 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800347
Andrii Kulian1e32e022016-09-16 15:29:34 -0700348 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
349 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800350 private final Rect mTmpRect2 = new Rect();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800351 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Andrii Kulian1e32e022016-09-16 15:29:34 -0700352
Bryce Leefbd263b42018-03-07 10:33:55 -0800353 /** List for processing through a set of activities */
354 private final ArrayList<ActivityRecord> mTmpActivities = new ArrayList<>();
355
Craig Mautner27084302013-03-25 08:05:25 -0700356 /** Run all ActivityStacks through this */
Winson Chung5af42fc2017-03-24 17:11:33 -0700357 protected final ActivityStackSupervisor mStackSupervisor;
Craig Mautner27084302013-03-25 08:05:25 -0700358
Jorim Jaggife762342016-10-13 14:33:27 +0200359 private boolean mTopActivityOccludesKeyguard;
360 private ActivityRecord mTopDismissingKeyguardActivity;
361
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700362 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
364 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
365 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
366 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700367 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700368
Andrii Kulian21713ac2016-10-12 22:05:05 -0700369 private static class ScheduleDestroyArgs {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700370 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700371 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700372 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700373 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700374 mReason = reason;
375 }
376 }
377
Zoran Marcetaf958b322012-08-09 20:27:12 +0900378 final Handler mHandler;
379
Andrii Kulian21713ac2016-10-12 22:05:05 -0700380 private class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800381
Craig Mautnerc8143c62013-09-03 12:15:57 -0700382 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900383 super(looper);
384 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700385
Zoran Marcetaf958b322012-08-09 20:27:12 +0900386 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 public void handleMessage(Message msg) {
388 switch (msg.what) {
389 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800390 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700391 // We don't at this point know if the activity is fullscreen,
392 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800393 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700394 synchronized (mService) {
395 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700396 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700397 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700398 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800399 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700401 case LAUNCH_TICK_MSG: {
402 ActivityRecord r = (ActivityRecord)msg.obj;
403 synchronized (mService) {
404 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700405 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700406 }
407 }
408 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700409 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800410 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700411 // We don't at this point know if the activity is fullscreen,
412 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800413 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800414 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800415 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700417 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700418 case STOP_TIMEOUT_MSG: {
419 ActivityRecord r = (ActivityRecord)msg.obj;
420 // We don't at this point know if the activity is fullscreen,
421 // so we need to be conservative and assume it isn't.
422 Slog.w(TAG, "Activity stop timeout for " + r);
423 synchronized (mService) {
424 if (r.isInHistory()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -0700425 r.activityStoppedLocked(null /* icicle */,
426 null /* persistentState */, null /* description */);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700427 }
428 }
429 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700430 case DESTROY_ACTIVITIES_MSG: {
431 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
432 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700433 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700434 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700435 } break;
436 case TRANSLUCENT_TIMEOUT_MSG: {
437 synchronized (mService) {
438 notifyActivityDrawnLocked(null);
439 }
440 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700441 }
442 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800443 }
444
Craig Mautner34b73df2014-01-12 21:11:08 -0800445 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800446 int count = 0;
447 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
448 count += mTaskHistory.get(taskNdx).mActivities.size();
449 }
450 return count;
451 }
452
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700453 ActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700454 int windowingMode, int activityType, boolean onTop) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700455 mStackSupervisor = supervisor;
456 mService = supervisor.mService;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800457 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
458 mWindowManager = mService.mWindowManager;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700459 mStackId = stackId;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -0700460 mCurrentUser = mService.mUserController.getCurrentUserId();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800461 mTmpRect2.setEmpty();
Andrii Kulian2fcc4512018-01-25 16:39:27 -0800462 // Set display id before setting activity and window type to make sure it won't affect
463 // stacks on a wrong display.
464 mDisplayId = display.mDisplayId;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700465 setActivityType(activityType);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800466 setWindowingMode(windowingMode);
Winson Chung55893332017-02-17 17:13:10 -0800467 mWindowContainerController = createStackWindowController(display.mDisplayId, onTop,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700468 mTmpRect2);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800469 postAddToDisplay(display, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
470 }
471
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700472 T createStackWindowController(int displayId, boolean onTop, Rect outBounds) {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700473 return (T) new StackWindowController(mStackId, this, displayId, onTop, outBounds,
474 mStackSupervisor.mWindowManager);
Winson Chung55893332017-02-17 17:13:10 -0800475 }
476
477 T getWindowContainerController() {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800478 return mWindowContainerController;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700479 }
480
Bryce Leec4ab62a2018-03-05 14:19:26 -0800481 /**
482 * This should be called when an activity in a child task changes state. This should only
483 * be called from
484 * {@link TaskRecord#onActivityStateChanged(ActivityRecord, ActivityState, String)}.
485 * @param record The {@link ActivityRecord} whose state has changed.
486 * @param state The new state.
487 * @param reason The reason for the change.
488 */
489 void onActivityStateChanged(ActivityRecord record, ActivityState state, String reason) {
490 if (record == mResumedActivity && state != RESUMED) {
Bryce Lee84730a02018-04-03 14:10:04 -0700491 setResumedActivity(null, reason + " - onActivityStateChanged");
Bryce Leec4ab62a2018-03-05 14:19:26 -0800492 }
493
494 if (state == RESUMED) {
495 if (DEBUG_STACK) Slog.v(TAG_STACK, "set resumed activity to:" + record + " reason:"
496 + reason);
Bryce Lee84730a02018-04-03 14:10:04 -0700497 setResumedActivity(record, reason + " - onActivityStateChanged");
Bryce Leec4ab62a2018-03-05 14:19:26 -0800498 mService.setResumedActivityUncheckLocked(record, reason);
499 mStackSupervisor.mRecentTasks.add(record.getTask());
500 }
501 }
502
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700503 @Override
504 public void onConfigurationChanged(Configuration newParentConfig) {
505 final int prevWindowingMode = getWindowingMode();
506 super.onConfigurationChanged(newParentConfig);
507 final ActivityDisplay display = getDisplay();
508 if (display != null && prevWindowingMode != getWindowingMode()) {
509 display.onStackWindowingModeChanged(this);
510 }
511 }
512
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700513 @Override
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800514 public void setWindowingMode(int windowingMode) {
Winson Chung3e36f822018-01-16 12:06:04 -0800515 setWindowingMode(windowingMode, false /* animate */, false /* showRecents */,
Andrii Kulian9da138a2018-04-24 17:12:44 -0700516 false /* enteringSplitScreenMode */, false /* deferEnsuringVisibility */);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800517 }
518
Wale Ogunwaledf262f52017-12-07 18:17:12 -0800519 void setWindowingMode(int preferredWindowingMode, boolean animate, boolean showRecents,
Andrii Kulian9da138a2018-04-24 17:12:44 -0700520 boolean enteringSplitScreenMode, boolean deferEnsuringVisibility) {
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800521 final boolean creating = mWindowContainerController == null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800522 final int currentMode = getWindowingMode();
523 final ActivityDisplay display = getDisplay();
524 final TaskRecord topTask = topTask();
525 final ActivityStack splitScreenStack = display.getSplitScreenPrimaryStack();
526 mTmpOptions.setLaunchWindowingMode(preferredWindowingMode);
527
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800528 // Need to make sure windowing mode is supported. If we in the process of creating the stack
529 // no need to resolve the windowing mode again as it is already resolved to the right mode.
530 int windowingMode = creating
531 ? preferredWindowingMode
532 : display.resolveWindowingMode(
533 null /* ActivityRecord */, mTmpOptions, topTask, getActivityType());
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800534 if (splitScreenStack == this && windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
535 // Resolution to split-screen secondary for the primary split-screen stack means we want
536 // to go fullscreen.
537 windowingMode = WINDOWING_MODE_FULLSCREEN;
538 }
539
Wale Ogunwaleac36e4d2017-11-29 13:30:26 -0800540 final boolean alreadyInSplitScreenMode = display.hasSplitScreenPrimaryStack();
541
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800542 // Don't send non-resizeable notifications if the windowing mode changed was a side effect
543 // of us entering split-screen mode.
544 final boolean sendNonResizeableNotification = !enteringSplitScreenMode;
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800545 // Take any required action due to us not supporting the preferred windowing mode.
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800546 if (alreadyInSplitScreenMode && windowingMode == WINDOWING_MODE_FULLSCREEN
547 && sendNonResizeableNotification && isActivityTypeStandardOrUndefined()) {
548 final boolean preferredSplitScreen =
549 preferredWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
550 || preferredWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
551 if (preferredSplitScreen || creating) {
552 // Looks like we can't launch in split screen mode or the stack we are launching
553 // doesn't support split-screen mode, go ahead an dismiss split-screen and display a
554 // warning toast about it.
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800555 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Andrii Kulian9da138a2018-04-24 17:12:44 -0700556 display.getSplitScreenPrimaryStack().setWindowingMode(WINDOWING_MODE_FULLSCREEN,
557 false /* animate */, false /* showRecents */,
558 false /* enteringSplitScreenMode */, true /* deferEnsuringVisibility */);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800559 }
560 }
561
562 if (currentMode == windowingMode) {
563 // You are already in the window mode silly...
564 return;
565 }
566
567 final WindowManagerService wm = mService.mWindowManager;
568 final ActivityRecord topActivity = getTopActivity();
569
Wale Ogunwaledf262f52017-12-07 18:17:12 -0800570 if (sendNonResizeableNotification && windowingMode != WINDOWING_MODE_FULLSCREEN
571 && topActivity != null && topActivity.isNonResizableOrForcedResizable()
572 && !topActivity.noDisplay) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800573 // Inform the user that they are starting an app that may not work correctly in
574 // multi-window mode.
575 final String packageName = topActivity.appInfo.packageName;
576 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
577 topTask.taskId, FORCED_RESIZEABLE_REASON_SPLIT_SCREEN, packageName);
578 }
579
580 wm.deferSurfaceLayout();
581 try {
582 if (!animate && topActivity != null) {
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100583 mStackSupervisor.mNoAnimActivities.add(topActivity);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800584 }
585 super.setWindowingMode(windowingMode);
586
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800587 if (creating) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800588 // Nothing else to do if we don't have a window container yet. E.g. call from ctor.
589 return;
590 }
591
592 if (windowingMode == WINDOWING_MODE_PINNED || currentMode == WINDOWING_MODE_PINNED) {
593 // TODO: Need to remove use of PinnedActivityStack for this to be supported.
594 // NOTE: Need to ASS.scheduleUpdatePictureInPictureModeIfNeeded() in
595 // setWindowModeUnchecked() when this support is added. See TaskRecord.reparent()
596 throw new IllegalArgumentException(
597 "Changing pinned windowing mode not currently supported");
598 }
599
600 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && splitScreenStack != null) {
601 // We already have a split-screen stack in this display, so just move the tasks over.
602 // TODO: Figure-out how to do all the stuff in
603 // AMS.setTaskWindowingModeSplitScreenPrimary
604 throw new IllegalArgumentException("Setting primary split-screen windowing mode"
605 + " while there is already one isn't currently supported");
606 //return;
607 }
608
609 mTmpRect2.setEmpty();
610 if (windowingMode != WINDOWING_MODE_FULLSCREEN) {
611 mWindowContainerController.getRawBounds(mTmpRect2);
612 if (windowingMode == WINDOWING_MODE_FREEFORM) {
613 if (topTask != null) {
614 // TODO: Can we consolidate this and other sites that call this methods?
615 Rect bounds = topTask().getLaunchBounds();
616 if (bounds != null) {
617 mTmpRect2.set(bounds);
618 }
619 }
620 }
621 }
622
Bryce Leef3c6a472017-11-14 14:53:06 -0800623 if (!Objects.equals(getOverrideBounds(), mTmpRect2)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800624 resize(mTmpRect2, null /* tempTaskBounds */, null /* tempTaskInsetBounds */);
625 }
626 } finally {
Wale Ogunwalec59b4f62017-11-30 11:05:43 -0800627 if (showRecents && !alreadyInSplitScreenMode && mDisplayId == DEFAULT_DISPLAY
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800628 && windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
629 // Make sure recents stack exist when creating a dock stack as it normally needs to
630 // be on the other side of the docked stack and we make visibility decisions based
631 // on that.
632 // TODO: This is only here to help out with the case where recents stack doesn't
633 // exist yet. For that case the initial size of the split-screen stack will be the
634 // the one where the home stack is visible since recents isn't visible yet, but the
635 // divider will be off. I think we should just make the initial bounds that of home
636 // so that the divider matches and remove this logic.
Winson Chungc1674272018-02-21 10:15:17 -0800637 // TODO: This is currently only called when entering split-screen while in another
638 // task, and from the tests
Matthew Ng30307122018-04-13 11:36:34 -0700639 // TODO (b/78247419): Check if launcher and overview are same then move home stack
640 // instead of recents stack. Then fix the rotation animation from fullscreen to
641 // minimized mode
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800642 final ActivityStack recentStack = display.getOrCreateStack(
643 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, ACTIVITY_TYPE_RECENTS,
644 true /* onTop */);
645 recentStack.moveToFront("setWindowingMode");
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800646 // If task moved to docked stack - show recents if needed.
Winson Chungdff7a732017-12-11 12:17:06 -0800647 mService.mWindowManager.showRecentApps();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800648 }
649 wm.continueSurfaceLayout();
650 }
651
Andrii Kulian9da138a2018-04-24 17:12:44 -0700652 if (!deferEnsuringVisibility) {
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800653 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
654 mStackSupervisor.resumeFocusedStackTopActivityLocked();
655 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800656 }
657
658 @Override
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700659 public boolean isCompatible(int windowingMode, int activityType) {
660 // TODO: Should we just move this to ConfigurationContainer?
661 if (activityType == ACTIVITY_TYPE_UNDEFINED) {
662 // Undefined activity types end up in a standard stack once the stack is created on a
663 // display, so they should be considered compatible.
664 activityType = ACTIVITY_TYPE_STANDARD;
665 }
666 final ActivityDisplay display = getDisplay();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700667 if (display != null && activityType == ACTIVITY_TYPE_STANDARD
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700668 && windowingMode == WINDOWING_MODE_UNDEFINED) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700669 // Standard activity types will mostly take on the windowing mode of the display if one
670 // isn't specified, so look-up a compatible stack based on the display's windowing mode.
671 windowingMode = display.getWindowingMode();
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700672 }
673 return super.isCompatible(windowingMode, activityType);
674 }
675
Andrii Kulian839def92016-11-02 10:58:58 -0700676 /** Adds the stack to specified display and calls WindowManager to do the same. */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700677 void reparent(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700678 // TODO: We should probably resolve the windowing mode for the stack on the new display here
679 // so that it end up in a compatible mode in the new display. e.g. split-screen secondary.
Wale Ogunwale1666e312016-12-16 11:27:18 -0800680 removeFromDisplay();
Winson Chungf34c6022018-01-25 09:44:30 -0800681 // Reparent the window container before we try to update the position when adding it to
682 // the new display below
Wale Ogunwale1666e312016-12-16 11:27:18 -0800683 mTmpRect2.setEmpty();
Winson Chungf34c6022018-01-25 09:44:30 -0800684 mWindowContainerController.reparent(activityDisplay.mDisplayId, mTmpRect2, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800685 postAddToDisplay(activityDisplay, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
Wale Ogunwale66e16852017-10-19 13:35:52 -0700686 adjustFocusToNextFocusableStack("reparent", true /* allowFocusSelf */);
Andrii Kulian250d6532017-02-08 23:30:45 -0800687 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Andrii Kulian51c1b672017-04-07 18:39:32 -0700688 // Update visibility of activities before notifying WM. This way it won't try to resize
689 // windows that are no longer visible.
690 mStackSupervisor.ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
691 !PRESERVE_WINDOWS);
Andrii Kulian839def92016-11-02 10:58:58 -0700692 }
693
694 /**
695 * Updates internal state after adding to new display.
696 * @param activityDisplay New display to which this stack was attached.
697 * @param bounds Updated bounds.
698 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700699 private void postAddToDisplay(ActivityDisplay activityDisplay, Rect bounds, boolean onTop) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700700 mDisplayId = activityDisplay.mDisplayId;
Bryce Leef3c6a472017-11-14 14:53:06 -0800701 setBounds(bounds);
Andrii Kulian1779e612016-10-12 21:58:25 -0700702 onParentChanged();
Wale Ogunwale961f4852016-02-01 20:25:54 -0800703
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700704 activityDisplay.addChild(this, onTop ? POSITION_TOP : POSITION_BOTTOM);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700705 if (inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale961f4852016-02-01 20:25:54 -0800706 // If we created a docked stack we want to resize it so it resizes all other stacks
707 // in the system.
708 mStackSupervisor.resizeDockedStackLocked(
Bryce Leef3c6a472017-11-14 14:53:06 -0800709 getOverrideBounds(), null, null, null, null, PRESERVE_WINDOWS);
Wale Ogunwale961f4852016-02-01 20:25:54 -0800710 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700711 }
712
Andrii Kulian839def92016-11-02 10:58:58 -0700713 /**
Andrii Kulian839def92016-11-02 10:58:58 -0700714 * Updates the inner state of the stack to remove it from its current parent, so it can be
715 * either destroyed completely or re-parented.
716 */
717 private void removeFromDisplay() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700718 final ActivityDisplay display = getDisplay();
719 if (display != null) {
720 display.removeChild(this);
721 }
722 mDisplayId = INVALID_DISPLAY;
Andrii Kulian839def92016-11-02 10:58:58 -0700723 }
724
725 /** Removes the stack completely. Also calls WindowManager to do the same on its side. */
726 void remove() {
727 removeFromDisplay();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800728 mWindowContainerController.removeContainer();
729 mWindowContainerController = null;
Andrii Kulian6d6fb402016-10-26 16:15:25 -0700730 onParentChanged();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700731 }
732
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700733 ActivityDisplay getDisplay() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700734 return mStackSupervisor.getActivityDisplay(mDisplayId);
735 }
736
Matthew Ngaa2b6202017-02-10 14:48:21 -0800737 /**
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700738 * @see #getStackDockedModeBounds(Rect, Rect, Rect, boolean)
Matthew Ngaa2b6202017-02-10 14:48:21 -0800739 */
740 void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
741 Rect outTempTaskBounds, boolean ignoreVisibility) {
742 mWindowContainerController.getStackDockedModeBounds(currentTempTaskBounds,
743 outStackBounds, outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800744 }
745
746 void prepareFreezingTaskBounds() {
747 mWindowContainerController.prepareFreezingTaskBounds();
748 }
749
Wale Ogunwale1666e312016-12-16 11:27:18 -0800750 void getWindowContainerBounds(Rect outBounds) {
751 if (mWindowContainerController != null) {
752 mWindowContainerController.getBounds(outBounds);
Winson Chungb00dc5e2017-01-25 09:44:25 -0800753 return;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800754 }
755 outBounds.setEmpty();
756 }
757
Matthew Ngaa2b6202017-02-10 14:48:21 -0800758 void getBoundsForNewConfiguration(Rect outBounds) {
759 mWindowContainerController.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800760 }
761
762 void positionChildWindowContainerAtTop(TaskRecord child) {
763 mWindowContainerController.positionChildAtTop(child.getWindowContainerController(),
764 true /* includingParents */);
765 }
766
Jorim Jaggi192086e2016-03-11 17:17:03 +0100767 /**
Winson Chung8bca9e42017-04-16 15:59:43 -0700768 * Returns whether to defer the scheduling of the multi-window mode.
769 */
770 boolean deferScheduleMultiWindowModeChanged() {
771 return false;
772 }
773
774 /**
Jorim Jaggi192086e2016-03-11 17:17:03 +0100775 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
776 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
777 */
778 void deferUpdateBounds() {
779 if (!mUpdateBoundsDeferred) {
780 mUpdateBoundsDeferred = true;
781 mUpdateBoundsDeferredCalled = false;
782 }
783 }
784
785 /**
786 * Continues updating bounds after updates have been deferred. If there was a resize attempt
787 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
788 * be resized to that bounds.
789 */
790 void continueUpdateBounds() {
791 final boolean wasDeferred = mUpdateBoundsDeferred;
792 mUpdateBoundsDeferred = false;
793 if (wasDeferred && mUpdateBoundsDeferredCalled) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800794 resize(mDeferredBounds.isEmpty() ? null : mDeferredBounds,
Jorim Jaggi192086e2016-03-11 17:17:03 +0100795 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
796 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
797 }
798 }
799
800 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
801 Rect tempTaskInsetBounds) {
802 if (!mUpdateBoundsDeferred) {
803 return true;
804 }
805 if (bounds != null) {
806 mDeferredBounds.set(bounds);
807 } else {
808 mDeferredBounds.setEmpty();
809 }
810 if (tempTaskBounds != null) {
811 mDeferredTaskBounds.set(tempTaskBounds);
812 } else {
813 mDeferredTaskBounds.setEmpty();
814 }
815 if (tempTaskInsetBounds != null) {
816 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
817 } else {
818 mDeferredTaskInsetBounds.setEmpty();
819 }
820 mUpdateBoundsDeferredCalled = true;
821 return false;
822 }
823
Bryce Leef3c6a472017-11-14 14:53:06 -0800824 @Override
825 public int setBounds(Rect bounds) {
826 return super.setBounds(!inMultiWindowMode() ? null : bounds);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 }
Craig Mautner5962b122012-10-05 14:45:52 -0700828
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700829 ActivityRecord topRunningActivityLocked() {
Winson Chung3f103eb2017-04-12 21:53:59 -0700830 return topRunningActivityLocked(false /* focusableOnly */);
831 }
832
Jorim Jaggiea039a82017-08-02 14:37:49 +0200833 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
834 outActivities.clear();
835 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
836 mTaskHistory.get(taskNdx).getAllRunningVisibleActivitiesLocked(outActivities);
837 }
838 }
839
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700840 private ActivityRecord topRunningActivityLocked(boolean focusableOnly) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800841 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700842 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Winson Chung3f103eb2017-04-12 21:53:59 -0700843 if (r != null && (!focusableOnly || r.isFocusable())) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700844 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800845 }
846 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 return null;
848 }
849
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700850 ActivityRecord topRunningNonOverlayTaskActivity() {
851 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
852 final TaskRecord task = mTaskHistory.get(taskNdx);
853 final ArrayList<ActivityRecord> activities = task.mActivities;
854 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
855 final ActivityRecord r = activities.get(activityNdx);
856 if (!r.finishing && !r.mTaskOverlay) {
857 return r;
858 }
859 }
860 }
861 return null;
862 }
863
864 ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800865 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
866 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800867 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800868 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
869 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -0700870 if (!r.finishing && !r.delayedResume && r != notTop && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800871 return r;
872 }
873 }
874 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 return null;
876 }
877
878 /**
879 * This is a simplified version of topRunningActivityLocked that provides a number of
880 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800881 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700882 * @param token If non-null, any history records matching this token will be skipped.
883 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800884 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700885 * @return Returns the HistoryRecord of the next activity on the stack.
886 */
887 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800888 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
889 TaskRecord task = mTaskHistory.get(taskNdx);
890 if (task.taskId == taskId) {
891 continue;
892 }
893 ArrayList<ActivityRecord> activities = task.mActivities;
894 for (int i = activities.size() - 1; i >= 0; --i) {
895 final ActivityRecord r = activities.get(i);
896 // Note: the taskId check depends on real taskId fields being non-zero
Chong Zhang87761972016-08-22 13:53:24 -0700897 if (!r.finishing && (token != r.appToken) && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800898 return r;
899 }
900 }
901 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700902 return null;
903 }
904
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800905 ActivityRecord getTopActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700906 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800907 final ActivityRecord r = mTaskHistory.get(taskNdx).getTopActivity();
908 if (r != null) {
909 return r;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700910 }
911 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700912 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700913 }
914
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700915 final TaskRecord topTask() {
916 final int size = mTaskHistory.size();
917 if (size > 0) {
918 return mTaskHistory.get(size - 1);
919 }
920 return null;
921 }
922
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800923 private TaskRecord bottomTask() {
Winson Chung1cebea62017-06-26 17:22:27 -0700924 if (mTaskHistory.isEmpty()) {
925 return null;
926 }
927 return mTaskHistory.get(0);
928 }
929
Craig Mautnerd2328952013-03-05 12:46:26 -0800930 TaskRecord taskForIdLocked(int id) {
931 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
932 final TaskRecord task = mTaskHistory.get(taskNdx);
933 if (task.taskId == id) {
934 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800935 }
936 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700937 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 }
939
Craig Mautnerd2328952013-03-05 12:46:26 -0800940 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700941 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800942 return isInStackLocked(r);
943 }
944
945 ActivityRecord isInStackLocked(ActivityRecord r) {
946 if (r == null) {
947 return null;
948 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700949 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700950 final ActivityStack stack = r.getStack();
951 if (stack != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
952 if (stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800953 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800954 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800955 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800956 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800957 }
958
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800959 boolean isInStackLocked(TaskRecord task) {
960 return mTaskHistory.contains(task);
961 }
962
963 /** Checks if there are tasks with specific UID in the stack. */
964 boolean isUidPresent(int uid) {
965 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -0800966 for (ActivityRecord r : task.mActivities) {
967 if (r.getUid() == uid) {
968 return true;
969 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800970 }
971 }
972 return false;
973 }
974
975 /** Get all UIDs that are present in the stack. */
976 void getPresentUIDs(IntArray presentUIDs) {
977 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -0800978 for (ActivityRecord r : task.mActivities) {
979 presentUIDs.add(r.getUid());
980 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800981 }
982 }
983
Winson Chungabb433b2017-03-24 09:35:42 -0700984 final void removeActivitiesFromLRUListLocked(TaskRecord task) {
985 for (ActivityRecord r : task.mActivities) {
986 mLRUActivities.remove(r);
987 }
988 }
989
Craig Mautner2420ead2013-04-01 17:13:20 -0700990 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700991 final boolean hadit = mLRUActivities.remove(r);
992 mLRUActivities.add(r);
993 return hadit;
994 }
995
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800996 final boolean isHomeOrRecentsStack() {
Wale Ogunwale68278562017-09-23 17:13:55 -0700997 return isActivityTypeHome() || isActivityTypeRecents();
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800998 }
999
Craig Mautnere0a38842013-12-16 16:14:02 -08001000 final boolean isOnHomeDisplay() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001001 return mDisplayId == DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08001002 }
1003
Wale Ogunwale66e16852017-10-19 13:35:52 -07001004 private boolean returnsToHomeStack() {
1005 return !inMultiWindowMode()
1006 && !mTaskHistory.isEmpty()
1007 && mTaskHistory.get(0).returnsToHomeStack();
1008 }
1009
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001010 void moveToFront(String reason) {
1011 moveToFront(reason, null);
1012 }
1013
1014 /**
1015 * @param reason The reason for moving the stack to the front.
1016 * @param task If non-null, the task will be moved to the top of the stack.
1017 * */
1018 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001019 if (!isAttached()) {
1020 return;
1021 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07001022
Wale Ogunwalebb285872018-03-01 13:05:30 -08001023 final ActivityDisplay display = getDisplay();
1024
1025 if (inSplitScreenSecondaryWindowingMode()) {
1026 // If the stack is in split-screen seconardy mode, we need to make sure we move the
1027 // primary split-screen stack forward in the case it is currently behind a fullscreen
1028 // stack so both halves of the split-screen appear on-top and the fullscreen stack isn't
1029 // cutting between them.
1030 // TODO(b/70677280): This is a workaround until we can fix as part of b/70677280.
1031 final ActivityStack topFullScreenStack =
1032 display.getTopStackInWindowingMode(WINDOWING_MODE_FULLSCREEN);
1033 if (topFullScreenStack != null) {
1034 final ActivityStack primarySplitScreenStack = display.getSplitScreenPrimaryStack();
1035 if (display.getIndexOf(topFullScreenStack)
1036 > display.getIndexOf(primarySplitScreenStack)) {
1037 primarySplitScreenStack.moveToFront(reason + " splitScreenToTop");
1038 }
1039 }
1040 }
1041
Wale Ogunwale66e16852017-10-19 13:35:52 -07001042 if (!isActivityTypeHome() && returnsToHomeStack()) {
1043 // Make sure the home stack is behind this stack since that is where we should return to
1044 // when this stack is no longer visible.
1045 mStackSupervisor.moveHomeStackToFront(reason + " returnToHome");
1046 }
1047
Wale Ogunwalebb285872018-03-01 13:05:30 -08001048 display.positionChildAtTop(this);
Andrii Kulian839def92016-11-02 10:58:58 -07001049 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001050 if (task != null) {
1051 insertTaskAtTop(task, null);
Wale Ogunwaleaa47c122016-09-23 16:39:53 -07001052 return;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001053 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001054 }
1055
Evan Rosky9c448172017-11-02 14:19:27 -07001056 /**
1057 * @param reason The reason for moving the stack to the back.
1058 * @param task If non-null, the task will be moved to the bottom of the stack.
1059 **/
1060 void moveToBack(String reason, TaskRecord task) {
1061 if (!isAttached()) {
1062 return;
1063 }
1064
Bryce Lee8cab4a02018-01-05 09:00:49 -08001065 /**
1066 * The intent behind moving a primary split screen stack to the back is usually to hide
1067 * behind the home stack. Exit split screen in this case.
1068 */
1069 if (getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
1070 setWindowingMode(WINDOWING_MODE_FULLSCREEN);
1071 }
1072
Evan Rosky9c448172017-11-02 14:19:27 -07001073 getDisplay().positionChildAtBottom(this);
1074 mStackSupervisor.setFocusStackUnchecked(reason, getDisplay().getTopStack());
1075 if (task != null) {
1076 insertTaskAtBottom(task);
1077 return;
Evan Rosky9c448172017-11-02 14:19:27 -07001078 }
1079 }
1080
Wale Ogunwaled046a012015-12-24 13:05:59 -08001081 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001082 final ActivityRecord r = topRunningActivityLocked();
Bryce Lee7b851cc2018-04-10 14:53:13 -07001083 return mStackSupervisor.isFocusable(this, r != null && r.isFocusable());
Wale Ogunwaled046a012015-12-24 13:05:59 -08001084 }
1085
Craig Mautnere0a38842013-12-16 16:14:02 -08001086 final boolean isAttached() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001087 return getParent() != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001088 }
1089
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090 /**
Wale Ogunwale39381972015-12-17 17:15:29 -08001091 * Returns the top activity in any existing task matching the given Intent in the input result.
1092 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001093 */
Wale Ogunwale39381972015-12-17 17:15:29 -08001094 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001095 Intent intent = target.intent;
1096 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001097 ComponentName cls = intent.getComponent();
1098 if (info.targetActivity != null) {
1099 cls = new ComponentName(info.packageName, info.targetActivity);
1100 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001101 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -07001102 boolean isDocument = intent != null & intent.isDocument();
1103 // If documentData is non-null then it must match the existing task data.
1104 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -08001105
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001106 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -08001107 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1108 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001109 if (task.voiceSession != null) {
1110 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001111 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -07001112 continue;
1113 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001114 if (task.userId != userId) {
1115 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001116 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -07001117 continue;
1118 }
Bryce Lee9f6affd2017-09-01 09:18:35 -07001119
1120 // Overlays should not be considered as the task's logical top activity.
1121 final ActivityRecord r = task.getTopActivity(false /* includeOverlays */);
Craig Mautner000f0022013-02-26 15:04:29 -08001122 if (r == null || r.finishing || r.userId != userId ||
1123 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001124 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -08001125 continue;
1126 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001127 if (!r.hasCompatibleActivityType(target)) {
Chong Zhangb546f7e2015-08-05 14:21:36 -07001128 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
1129 continue;
1130 }
Craig Mautner000f0022013-02-26 15:04:29 -08001131
Craig Mautnerd00f4742014-03-12 14:17:26 -07001132 final Intent taskIntent = task.intent;
1133 final Intent affinityIntent = task.affinityIntent;
1134 final boolean taskIsDocument;
1135 final Uri taskDocumentData;
1136 if (taskIntent != null && taskIntent.isDocument()) {
1137 taskIsDocument = true;
1138 taskDocumentData = taskIntent.getData();
1139 } else if (affinityIntent != null && affinityIntent.isDocument()) {
1140 taskIsDocument = true;
1141 taskDocumentData = affinityIntent.getData();
1142 } else {
1143 taskIsDocument = false;
1144 taskDocumentData = null;
1145 }
1146
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001147 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -07001148 + taskIntent.getComponent().flattenToShortString()
Bryce Leeaf691c02017-03-20 14:20:22 -07001149 + "/aff=" + r.getTask().rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001150 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001151 // TODO Refactor to remove duplications. Check if logic can be simplified.
1152 if (taskIntent != null && taskIntent.getComponent() != null &&
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001153 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001154 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001155 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001156 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001157 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1158 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -08001159 result.r = r;
1160 result.matchedByRootAffinity = false;
1161 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001162 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
1163 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001164 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001165 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001166 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001167 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1168 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -08001169 result.r = r;
1170 result.matchedByRootAffinity = false;
1171 break;
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001172 } else if (!isDocument && !taskIsDocument
Winson Chung5b895b72017-05-01 13:46:25 -07001173 && result.r == null && task.rootAffinity != null) {
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001174 if (task.rootAffinity.equals(target.taskAffinity)) {
1175 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
1176 // It is possible for multiple tasks to have the same root affinity especially
1177 // if they are in separate stacks. We save off this candidate, but keep looking
1178 // to see if there is a better candidate.
1179 result.r = r;
1180 result.matchedByRootAffinity = true;
1181 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001182 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184 }
1185
1186 /**
1187 * Returns the first activity (starting from the top of the stack) that
1188 * is the same as the given activity. Returns null if no such activity
1189 * is found.
1190 */
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001191 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
1192 boolean compareIntentFilters) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193 ComponentName cls = intent.getComponent();
1194 if (info.targetActivity != null) {
1195 cls = new ComponentName(info.packageName, info.targetActivity);
1196 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001197 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198
Craig Mautner000f0022013-02-26 15:04:29 -08001199 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001200 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001201 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001202
Craig Mautner000f0022013-02-26 15:04:29 -08001203 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1204 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001205 if (!r.okToShowLocked()) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -07001206 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001207 }
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001208 if (!r.finishing && r.userId == userId) {
1209 if (compareIntentFilters) {
1210 if (r.intent.filterEquals(intent)) {
1211 return r;
1212 }
1213 } else {
1214 if (r.intent.getComponent().equals(cls)) {
1215 return r;
1216 }
1217 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 }
1219 }
1220 }
1221
1222 return null;
1223 }
1224
Amith Yamasani742a6712011-05-04 14:49:28 -07001225 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -07001226 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -07001227 */
Craig Mautner93529a42013-10-04 15:03:13 -07001228 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001229 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -07001230 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001231 }
1232 mCurrentUser = userId;
1233
1234 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001235 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001236 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001237 final TaskRecord task = mTaskHistory.get(i);
1238
Chong Zhang87761972016-08-22 13:53:24 -07001239 if (task.okToShowLocked()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001240 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001241 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001242 mTaskHistory.remove(i);
1243 mTaskHistory.add(task);
1244 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001245 // Use same value for i.
1246 } else {
1247 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001248 }
1249 }
Amith Yamasani742a6712011-05-04 14:49:28 -07001250 }
1251
Craig Mautner2420ead2013-04-01 17:13:20 -07001252 void minimalResumeActivityLocked(ActivityRecord r) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001253 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
1254 + " callers=" + Debug.getCallers(5));
Bryce Leec4ab62a2018-03-05 14:19:26 -08001255 r.setState(RESUMED, "minimalResumeActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001256 r.completeResumeLocked();
Jorim Jaggide8305a2018-05-15 12:06:35 -07001257 mStackSupervisor.getLaunchTimeTracker().setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001258 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
1259 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001260 }
1261
Andrii Kulian21713ac2016-10-12 22:05:05 -07001262 private void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -07001263 // Make sure that there is no activity waiting for this to launch.
1264 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
1265 r.displayStartTime = r.fullyDrawnStartTime = 0;
1266 } else {
1267 mStackSupervisor.removeTimeoutsForActivityLocked(r);
1268 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1269 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001270 }
1271
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001272 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001273 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001274 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1275 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1276 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1277 activities.get(activityNdx).setSleeping(false);
1278 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001279 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -08001280 if (mPausingActivity != null) {
1281 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
1282 activityPausedLocked(mPausingActivity.appToken, true);
1283 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001284 }
1285
Todd Kennedy39bfee52016-02-24 10:28:21 -08001286 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1287 final String packageName = aInfo.packageName;
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001288 final int userId = UserHandle.getUserId(aInfo.uid);
1289
Todd Kennedy39bfee52016-02-24 10:28:21 -08001290 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1291 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1292 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001293 final ActivityRecord ar = activities.get(activityNdx);
1294
1295 if ((userId == ar.userId) && packageName.equals(ar.packageName)) {
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -07001296 ar.updateApplicationInfo(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08001297 }
1298 }
1299 }
1300 }
1301
David Stevens9440dc82017-03-16 19:00:20 -07001302 void checkReadyForSleep() {
1303 if (shouldSleepActivities() && goToSleepIfPossible(false /* shuttingDown */)) {
1304 mStackSupervisor.checkReadyForSleepLocked(true /* allowDelay */);
1305 }
1306 }
1307
Craig Mautner0eea92c2013-05-16 13:35:39 -07001308 /**
David Stevens9440dc82017-03-16 19:00:20 -07001309 * Tries to put the activities in the stack to sleep.
1310 *
1311 * If the stack is not in a state where its activities can be put to sleep, this function will
1312 * start any necessary actions to move the stack into such a state. It is expected that this
1313 * function get called again when those actions complete.
1314 *
1315 * @param shuttingDown true when the called because the device is shutting down.
David Stevens18abd0e2017-08-17 14:55:47 -07001316 * @return true if the stack finished going to sleep, false if the stack only started the
1317 * process of going to sleep (checkReadyForSleep will be called when that process finishes).
Craig Mautner0eea92c2013-05-16 13:35:39 -07001318 */
David Stevens9440dc82017-03-16 19:00:20 -07001319 boolean goToSleepIfPossible(boolean shuttingDown) {
1320 boolean shouldSleep = true;
1321
Craig Mautner0eea92c2013-05-16 13:35:39 -07001322 if (mResumedActivity != null) {
1323 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001324 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
1325 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1326 "Sleep => pause with userLeaving=false");
Bryce Lee5daa3122017-04-19 10:40:42 -07001327
chaviwa1d68532018-03-16 15:33:43 -07001328 startPausingLocked(false, true, null, false);
David Stevens9440dc82017-03-16 19:00:20 -07001329 shouldSleep = false ;
1330 } else if (mPausingActivity != null) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001331 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001332 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
David Stevens9440dc82017-03-16 19:00:20 -07001333 shouldSleep = false;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001334 }
David Stevens9440dc82017-03-16 19:00:20 -07001335
1336 if (!shuttingDown) {
1337 if (containsActivityFromStack(mStackSupervisor.mStoppingActivities)) {
1338 // Still need to tell some activities to stop; can't sleep yet.
1339 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
1340 + mStackSupervisor.mStoppingActivities.size() + " activities");
1341
1342 mStackSupervisor.scheduleIdleLocked();
1343 shouldSleep = false;
1344 }
1345
1346 if (containsActivityFromStack(mStackSupervisor.mGoingToSleepActivities)) {
1347 // Still need to tell some activities to sleep; can't sleep yet.
1348 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
1349 + mStackSupervisor.mGoingToSleepActivities.size() + " activities");
1350 shouldSleep = false;
1351 }
1352 }
1353
1354 if (shouldSleep) {
1355 goToSleep();
1356 }
1357
David Stevens18abd0e2017-08-17 14:55:47 -07001358 return shouldSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -07001359 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001360
David Stevens18abd0e2017-08-17 14:55:47 -07001361 void goToSleep() {
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001362 // Ensure visibility without updating configuration, as activities are about to sleep.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001363 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
1364 !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001365
Andrii Kulianf9949d52016-05-06 12:53:25 -07001366 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001367 // This ensures that the activity's onStop() is called.
1368 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1369 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1370 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1371 final ActivityRecord r = activities.get(activityNdx);
Bryce Lee7ace3952018-02-16 14:34:32 -08001372 if (r.isState(STOPPING, STOPPED, PAUSED, PAUSING)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001373 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001374 }
1375 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001376 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001377 }
Craig Mautner59c00972012-07-30 12:10:24 -07001378
David Stevens9440dc82017-03-16 19:00:20 -07001379 private boolean containsActivityFromStack(List<ActivityRecord> rs) {
1380 for (ActivityRecord r : rs) {
1381 if (r.getStack() == this) {
1382 return true;
1383 }
1384 }
1385 return false;
1386 }
1387
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001388 /**
Winson Chung4dabf232017-01-25 13:25:22 -08001389 * Schedule a pause timeout in case the app doesn't respond. We don't give it much time because
1390 * this directly impacts the responsiveness seen by the user.
1391 */
1392 private void schedulePauseTimeout(ActivityRecord r) {
1393 final Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1394 msg.obj = r;
1395 r.pauseTime = SystemClock.uptimeMillis();
1396 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1397 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
1398 }
1399
1400 /**
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001401 * Start pausing the currently resumed activity. It is an error to call this if there
1402 * is already an activity being paused or there is no resumed activity.
1403 *
1404 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1405 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1406 * screen turning off).
Wale Ogunwale950faff2016-08-08 09:51:04 -07001407 * @param resuming The activity we are currently trying to resume or null if this is not being
1408 * called as part of resuming the top activity, so we shouldn't try to instigate
1409 * a resume here if not null.
Winson Chung6954fc92017-03-24 16:22:12 -07001410 * @param pauseImmediately True if the caller does not want to wait for the activity callback to
1411 * complete pausing.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001412 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1413 * it to tell us when it is done.
1414 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001415 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
Winson Chung6954fc92017-03-24 16:22:12 -07001416 ActivityRecord resuming, boolean pauseImmediately) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001417 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001418 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
Bryce Lee7ace3952018-02-16 14:34:32 -08001419 + " state=" + mPausingActivity.getState());
David Stevens9440dc82017-03-16 19:00:20 -07001420 if (!shouldSleepActivities()) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001421 // Avoid recursion among check for sleep and complete pause during sleeping.
1422 // Because activity will be paused immediately after resume, just let pause
1423 // be completed by the order of activity paused from clients.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001424 completePauseLocked(false, resuming);
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001425 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001426 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001427 ActivityRecord prev = mResumedActivity;
Bryce Lee5daa3122017-04-19 10:40:42 -07001428
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001429 if (prev == null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07001430 if (resuming == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001431 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001432 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001433 }
1434 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001436
Andrii Kulianb1888aa2018-02-16 14:40:42 -08001437 if (prev == resuming) {
1438 Slog.wtf(TAG, "Trying to pause activity that is in process of being resumed");
1439 return false;
1440 }
1441
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001442 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001443 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001444 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001445 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001446 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1447 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Bryce Lee7ace3952018-02-16 14:34:32 -08001448 prev.setState(PAUSING, "startPausingLocked");
Bryce Leeaf691c02017-03-20 14:20:22 -07001449 prev.getTask().touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001450 clearLaunchTime(prev);
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001451
Jorim Jaggide8305a2018-05-15 12:06:35 -07001452 mStackSupervisor.getLaunchTimeTracker().stopFullyDrawnTraceIfNeeded(getWindowingMode());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001453
1454 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001455
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001456 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001457 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458 try {
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08001459 EventLogTags.writeAmPauseActivity(prev.userId, System.identityHashCode(prev),
1460 prev.shortComponentName, "userLeaving=" + userLeaving);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001461 mService.updateUsageStats(prev, false);
Andrii Kulian446e8242017-10-26 15:17:29 -07001462
Bryce Leeb0f993f2018-03-02 15:38:01 -08001463 mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001464 PauseActivityItem.obtain(prev.finishing, userLeaving,
Bryce Lee1d0d5142018-04-12 10:35:07 -07001465 prev.configChangeFlags, pauseImmediately));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 } catch (Exception e) {
1467 // Ignore exception, if process died other code will cleanup.
1468 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001469 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001470 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001471 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001472 }
1473 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001474 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001475 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001476 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001477 }
1478
1479 // If we are not going to sleep, we want to ensure the device is
1480 // awake until the next activity is started.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001481 if (!uiSleeping && !mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001482 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 }
1484
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001485 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 // Have the window manager pause its key dispatching until the new
1487 // activity has started. If we're pausing the activity just because
1488 // the screen is being turned off and the UI is sleeping, don't interrupt
1489 // key dispatch; the same activity will pick it up again on wakeup.
1490 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001491 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001492 } else if (DEBUG_PAUSE) {
1493 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494 }
1495
Winson Chung6954fc92017-03-24 16:22:12 -07001496 if (pauseImmediately) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001497 // If the caller said they don't want to wait for the pause, then complete
1498 // the pause now.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001499 completePauseLocked(false, resuming);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001500 return false;
1501
1502 } else {
Winson Chung4dabf232017-01-25 13:25:22 -08001503 schedulePauseTimeout(prev);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001504 return true;
1505 }
1506
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 } else {
1508 // This activity failed to schedule the
1509 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001510 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Wale Ogunwale950faff2016-08-08 09:51:04 -07001511 if (resuming == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001512 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001513 }
1514 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001515 }
1516 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001517
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001518 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001519 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1520 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001521
Craig Mautnerd2328952013-03-05 12:46:26 -08001522 final ActivityRecord r = isInStackLocked(token);
1523 if (r != null) {
1524 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1525 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001526 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001527 + (timeout ? " (due to timeout)" : " (pause complete)"));
Jorim Jaggife762342016-10-13 14:33:27 +02001528 mService.mWindowManager.deferSurfaceLayout();
1529 try {
Winson Chung3f103eb2017-04-12 21:53:59 -07001530 completePauseLocked(true /* resumeNext */, null /* resumingActivity */);
Jorim Jaggife762342016-10-13 14:33:27 +02001531 } finally {
1532 mService.mWindowManager.continueSurfaceLayout();
1533 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001534 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001535 } else {
1536 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1537 r.userId, System.identityHashCode(r), r.shortComponentName,
1538 mPausingActivity != null
1539 ? mPausingActivity.shortComponentName : "(none)");
Bryce Lee7ace3952018-02-16 14:34:32 -08001540 if (r.isState(PAUSING)) {
1541 r.setState(PAUSED, "activityPausedLocked");
riddle_hsu9caeef72015-10-20 16:34:05 +08001542 if (r.finishing) {
1543 if (DEBUG_PAUSE) Slog.v(TAG,
1544 "Executing finish of failed to pause activity: " + r);
Bryce Leef52974c2018-02-14 15:12:01 -08001545 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false,
1546 "activityPausedLocked");
riddle_hsu9caeef72015-10-20 16:34:05 +08001547 }
louis_chang047dfd42015-04-08 16:35:55 +08001548 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 }
1550 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001551 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001552 }
1553
Wale Ogunwale950faff2016-08-08 09:51:04 -07001554 private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001555 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001556 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001557
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001558 if (prev != null) {
chaviw4ad54912018-05-30 11:05:44 -07001559 prev.setWillCloseOrEnterPip(false);
Bryce Lee7ace3952018-02-16 14:34:32 -08001560 final boolean wasStopping = prev.isState(STOPPING);
1561 prev.setState(PAUSED, "completePausedLocked");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001562 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001563 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Bryce Leef52974c2018-02-14 15:12:01 -08001564 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false,
1565 "completedPausedLocked");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001566 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001567 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1568 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Bryce Lee4a194382017-04-04 14:32:48 -07001569 if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001570 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1571 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001572 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001573 if (prev.deferRelaunchUntilPaused) {
1574 // Complete the deferred relaunch that was waiting for pause to complete.
1575 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001576 prev.relaunchActivityLocked(false /* andResume */,
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001577 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001578 } else if (wasStopping) {
1579 // We are also stopping, the stop request must have gone soon after the pause.
1580 // We can't clobber it, because the stop confirmation will not be handled.
1581 // We don't need to schedule another stop, we only need to let it happen.
Bryce Lee7ace3952018-02-16 14:34:32 -08001582 prev.setState(STOPPING, "completePausedLocked");
David Stevens9440dc82017-03-16 19:00:20 -07001583 } else if (!prev.visible || shouldSleepOrShutDownActivities()) {
Wale Ogunwaleec950642017-04-25 07:44:21 -07001584 // Clear out any deferred client hide we might currently have.
1585 prev.setDeferHidingClient(false);
Jose Lima4b6c6692014-08-12 17:41:12 -07001586 // If we were visible then resumeTopActivities will release resources before
Wale Ogunwaleec950642017-04-25 07:44:21 -07001587 // stopping.
Winson Chung4dabf232017-01-25 13:25:22 -08001588 addToStopping(prev, true /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001589 }
1590 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001591 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001592 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001593 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001594 // It is possible the activity was freezing the screen before it was paused.
1595 // In that case go ahead and remove the freeze this activity has on the screen
1596 // since it is no longer visible.
Wale Ogunwalee8524002016-09-13 16:34:57 -07001597 if (prev != null) {
1598 prev.stopFreezingScreenLocked(true /*force*/);
1599 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001600 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001602
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001603 if (resumeNext) {
1604 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
David Stevens9440dc82017-03-16 19:00:20 -07001605 if (!topStack.shouldSleepOrShutDownActivities()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001606 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001607 } else {
David Stevens9440dc82017-03-16 19:00:20 -07001608 checkReadyForSleep();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001609 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001610 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001611 // If there are no more activities available to run, do resume anyway to start
1612 // something. Also if the top activity on the stack is not the just paused
1613 // activity, we need to go ahead and resume it to ensure we complete an
1614 // in-flight app switch.
1615 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001616 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001617 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001619
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001620 if (prev != null) {
1621 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001622
Craig Mautner525f3d92013-05-07 14:01:50 -07001623 if (prev.app != null && prev.cpuTimeAtResume > 0
1624 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001625 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1626 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001627 if (diff > 0) {
1628 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1629 synchronized (bsi) {
1630 BatteryStatsImpl.Uid.Proc ps =
1631 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001632 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001633 if (ps != null) {
1634 ps.addForegroundTimeLocked(diff);
1635 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 }
1637 }
1638 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001639 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001641
Andrii Kulian8290f8f2016-06-30 17:51:32 -07001642 // Notify when the task stack has changed, but only if visibilities changed (not just
1643 // focus). Also if there is an active pinned stack - we always want to notify it about
1644 // task stack changes, because its positioning may depend on it.
1645 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001646 || getDisplay().hasPinnedStack()) {
Yorke Leebd54c2a2016-10-25 13:49:23 -07001647 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001648 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1649 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001650
Wale Ogunwale950faff2016-08-08 09:51:04 -07001651 mStackSupervisor.ensureActivitiesVisibleLocked(resuming, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652 }
1653
Winson Chung4dabf232017-01-25 13:25:22 -08001654 void addToStopping(ActivityRecord r, boolean scheduleIdle, boolean idleDelayed) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001655 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1656 mStackSupervisor.mStoppingActivities.add(r);
Jorim Jaggi5db9ae42018-06-25 16:14:50 +02001657
1658 // Some activity is waiting for another activity to become visible before it's being
1659 // stopped, which means that we also want to wait with stopping this one to avoid
1660 // flickers.
1661 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.isEmpty()
1662 && !mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(r)) {
1663 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "adding to waiting visible activity=" + r
1664 + " existing=" + mStackSupervisor.mActivitiesWaitingForVisibleActivity);
1665 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(r);
1666 }
Chong Zhang46b1ac62016-02-18 17:53:57 -08001667 }
1668
1669 // If we already have a few activities waiting to stop, then give up
1670 // on things going idle and start clearing them out. Or if r is the
1671 // last of activity of the last task the stack will be empty and must
1672 // be cleared immediately.
1673 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1674 || (r.frontOfTask && mTaskHistory.size() <= 1);
Winson Chung4dabf232017-01-25 13:25:22 -08001675 if (scheduleIdle || forceIdle) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001676 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
Winson Chung4dabf232017-01-25 13:25:22 -08001677 + forceIdle + "immediate=" + !idleDelayed);
1678 if (!idleDelayed) {
1679 mStackSupervisor.scheduleIdleLocked();
1680 } else {
1681 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1682 }
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001683 } else {
David Stevens9440dc82017-03-16 19:00:20 -07001684 checkReadyForSleep();
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001685 }
1686 }
1687
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001688 /**
1689 * Returns true if the stack is translucent and can have other contents visible behind it if
1690 * needed. A stack is considered translucent if it don't contain a visible or
1691 * starting (about to be visible) activity that is fullscreen (opaque).
1692 * @param starting The currently starting activity or null if there is none.
1693 */
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001694 @VisibleForTesting
Wale Ogunwale66e16852017-10-19 13:35:52 -07001695 boolean isStackTranslucent(ActivityRecord starting) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001696 if (!isAttached() || mForceHidden) {
1697 return true;
1698 }
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001699 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1700 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001701 final ArrayList<ActivityRecord> activities = task.mActivities;
1702 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1703 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001704
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001705 if (r.finishing) {
1706 // We don't factor in finishing activities when determining translucency since
1707 // they will be gone soon.
1708 continue;
1709 }
1710
Wale Ogunwale2cca8622017-12-11 08:40:13 -08001711 if (!r.visibleIgnoringKeyguard && r != starting) {
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001712 // Also ignore invisible activities that are not the currently starting
1713 // activity (about to be visible).
1714 continue;
1715 }
1716
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001717 if (r.fullscreen || r.hasWallpaper) {
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001718 // Stack isn't translucent if it has at least one fullscreen activity
1719 // that is visible.
1720 return false;
1721 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001722 }
1723 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001724 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001725 }
1726
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001727 boolean isTopStackOnDisplay() {
1728 return getDisplay().isTopStack(this);
1729 }
1730
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001731 boolean isTopActivityVisible() {
1732 final ActivityRecord topActivity = getTopActivity();
1733 return topActivity != null && topActivity.visible;
1734 }
1735
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001736 /**
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001737 * Returns true if the stack should be visible.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001738 *
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001739 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001740 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001741 boolean shouldBeVisible(ActivityRecord starting) {
Winson Chung47900652017-04-06 18:44:25 -07001742 if (!isAttached() || mForceHidden) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001743 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001744 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001745 if (mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001746 return true;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001747 }
1748
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001749 final ActivityRecord top = topRunningActivityLocked();
1750 if (top == null && isInStackLocked(starting) == null && !isTopStackOnDisplay()) {
1751 // Shouldn't be visible if you don't have any running activities, not starting one, and
1752 // not the top stack on display.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001753 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001754 }
1755
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001756 final ActivityDisplay display = getDisplay();
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001757 boolean gotSplitScreenStack = false;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001758 boolean gotOpaqueSplitScreenPrimary = false;
1759 boolean gotOpaqueSplitScreenSecondary = false;
Wale Ogunwale68278562017-09-23 17:13:55 -07001760 final int windowingMode = getWindowingMode();
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001761 final boolean isAssistantType = isActivityTypeAssistant();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001762 for (int i = display.getChildCount() - 1; i >= 0; --i) {
1763 final ActivityStack other = display.getChildAt(i);
1764 if (other == this) {
1765 // Should be visible if there is no other stack occluding it.
1766 return true;
1767 }
Wale Ogunwale68278562017-09-23 17:13:55 -07001768
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001769 final int otherWindowingMode = other.getWindowingMode();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001770
1771 if (otherWindowingMode == WINDOWING_MODE_FULLSCREEN) {
Matthew Ng99b3cdc2018-05-01 14:24:38 -07001772 // In this case the home stack isn't resizeable even though we are in split-screen
1773 // mode. We still want the primary splitscreen stack to be visible as there will be
1774 // a slight hint of it in the status bar area above the non-resizeable home
Matthew Nga51dcaa2018-05-07 15:36:06 -07001775 // activity. In addition, if the fullscreen assistant is over primary splitscreen
1776 // stack, the stack should still be visible in the background as long as the recents
1777 // animation is running.
1778 final int activityType = other.getActivityType();
1779 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
1780 if (activityType == ACTIVITY_TYPE_HOME
1781 || (activityType == ACTIVITY_TYPE_ASSISTANT
1782 && mWindowManager.getRecentsAnimationController() != null)) {
1783 return true;
1784 }
Matthew Ng99b3cdc2018-05-01 14:24:38 -07001785 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07001786 if (other.isStackTranslucent(starting)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001787 // Can be visible behind a translucent fullscreen stack.
1788 continue;
1789 }
1790 return false;
1791 } else if (otherWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
1792 && !gotOpaqueSplitScreenPrimary) {
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001793 gotSplitScreenStack = true;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001794 gotOpaqueSplitScreenPrimary =
Wale Ogunwale66e16852017-10-19 13:35:52 -07001795 !other.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001796 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
1797 && gotOpaqueSplitScreenPrimary) {
1798 // Can not be visible behind another opaque stack in split-screen-primary mode.
1799 return false;
1800 }
1801 } else if (otherWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
1802 && !gotOpaqueSplitScreenSecondary) {
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001803 gotSplitScreenStack = true;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001804 gotOpaqueSplitScreenSecondary =
Wale Ogunwale66e16852017-10-19 13:35:52 -07001805 !other.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001806 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
1807 && gotOpaqueSplitScreenSecondary) {
1808 // Can not be visible behind another opaque stack in split-screen-secondary mode.
1809 return false;
1810 }
1811 }
1812 if (gotOpaqueSplitScreenPrimary && gotOpaqueSplitScreenSecondary) {
1813 // Can not be visible if we are in split-screen windowing mode and both halves of
1814 // the screen are opaque.
1815 return false;
1816 }
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001817 if (isAssistantType && gotSplitScreenStack) {
1818 // Assistant stack can't be visible behind split-screen. In addition to this not
1819 // making sense, it also works around an issue here we boost the z-order of the
1820 // assistant window surfaces in window manager whenever it is visible.
1821 return false;
1822 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001823 }
1824
1825 // Well, nothing is stopping you from being visible...
1826 return true;
Wale Ogunwale68278562017-09-23 17:13:55 -07001827 }
1828
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001829 final int rankTaskLayers(int baseLayer) {
1830 int layer = 0;
1831 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1832 final TaskRecord task = mTaskHistory.get(taskNdx);
1833 ActivityRecord r = task.topRunningActivityLocked();
1834 if (r == null || r.finishing || !r.visible) {
1835 task.mLayerRank = -1;
1836 } else {
1837 task.mLayerRank = baseLayer + layer++;
1838 }
1839 }
1840 return layer;
1841 }
1842
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 /**
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001844 * Make sure that all activities that need to be visible in the stack (that is, they
1845 * currently can be seen by the user) actually are and update their configuration.
1846 */
1847 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1848 boolean preserveWindows) {
1849 ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001850 true /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001851 }
1852
1853 /**
1854 * Ensure visibility with an option to also update the configuration of visible activities.
1855 * @see #ensureActivitiesVisibleLocked(ActivityRecord, int, boolean)
1856 * @see ActivityStackSupervisor#ensureActivitiesVisibleLocked(ActivityRecord, int, boolean)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 */
Wale Ogunwale66e16852017-10-19 13:35:52 -07001858 // TODO: Should be re-worked based on the fact that each task as a stack in most cases.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001859 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001860 boolean preserveWindows, boolean notifyClients) {
Jorim Jaggife762342016-10-13 14:33:27 +02001861 mTopActivityOccludesKeyguard = false;
1862 mTopDismissingKeyguardActivity = null;
Bryce Lee2a3cc462017-10-27 10:57:35 -07001863 mStackSupervisor.getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02001864 try {
1865 ActivityRecord top = topRunningActivityLocked();
1866 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
1867 + " configChanges=0x" + Integer.toHexString(configChanges));
1868 if (top != null) {
1869 checkTranslucentActivityWaiting(top);
1870 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001871
Jorim Jaggife762342016-10-13 14:33:27 +02001872 // If the top activity is not fullscreen, then we need to
1873 // make sure any activities under it are now visible.
1874 boolean aboveTop = top != null;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001875 final boolean stackShouldBeVisible = shouldBeVisible(starting);
1876 boolean behindFullscreenActivity = !stackShouldBeVisible;
Jorim Jaggife762342016-10-13 14:33:27 +02001877 boolean resumeNextActivity = mStackSupervisor.isFocusedStack(this)
1878 && (isInStackLocked(starting) == null);
Wale Ogunwale86dde2b2018-01-18 06:15:45 -08001879 final boolean isTopNotPinnedStack =
1880 isAttached() && getDisplay().isTopNotPinnedStack(this);
Jorim Jaggife762342016-10-13 14:33:27 +02001881 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1882 final TaskRecord task = mTaskHistory.get(taskNdx);
1883 final ArrayList<ActivityRecord> activities = task.mActivities;
1884 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1885 final ActivityRecord r = activities.get(activityNdx);
1886 if (r.finishing) {
Jorim Jaggife762342016-10-13 14:33:27 +02001887 continue;
Chong Zhang22bc8512016-04-07 13:47:18 -07001888 }
Jorim Jaggife762342016-10-13 14:33:27 +02001889 final boolean isTop = r == top;
1890 if (aboveTop && !isTop) {
1891 continue;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001892 }
Jorim Jaggife762342016-10-13 14:33:27 +02001893 aboveTop = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001894
Jorim Jaggife762342016-10-13 14:33:27 +02001895 // Check whether activity should be visible without Keyguard influence
Jorim Jaggi241ae102016-11-02 21:57:33 -07001896 final boolean visibleIgnoringKeyguard = r.shouldBeVisibleIgnoringKeyguard(
Jorim Jaggi241ae102016-11-02 21:57:33 -07001897 behindFullscreenActivity);
1898 r.visibleIgnoringKeyguard = visibleIgnoringKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +02001899
1900 // Now check whether it's really visible depending on Keyguard state.
Jorim Jaggi241ae102016-11-02 21:57:33 -07001901 final boolean reallyVisible = checkKeyguardVisibility(r,
chaviw2c500982018-01-04 17:05:05 -08001902 visibleIgnoringKeyguard, isTop && isTopNotPinnedStack);
Jorim Jaggi241ae102016-11-02 21:57:33 -07001903 if (visibleIgnoringKeyguard) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001904 behindFullscreenActivity = updateBehindFullscreen(!stackShouldBeVisible,
Wale Ogunwale66e16852017-10-19 13:35:52 -07001905 behindFullscreenActivity, r);
Jorim Jaggife762342016-10-13 14:33:27 +02001906 }
1907 if (reallyVisible) {
1908 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
Bryce Lee7ace3952018-02-16 14:34:32 -08001909 + " finishing=" + r.finishing + " state=" + r.getState());
Jorim Jaggife762342016-10-13 14:33:27 +02001910 // First: if this is not the current activity being started, make
1911 // sure it matches the current configuration.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001912 if (r != starting && notifyClients) {
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08001913 r.ensureActivityConfiguration(0 /* globalChanges */, preserveWindows,
1914 true /* ignoreStopState */);
Jorim Jaggife762342016-10-13 14:33:27 +02001915 }
1916
1917 if (r.app == null || r.app.thread == null) {
1918 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
1919 resumeNextActivity, r)) {
1920 if (activityNdx >= activities.size()) {
1921 // Record may be removed if its process needs to restart.
1922 activityNdx = activities.size() - 1;
1923 } else {
1924 resumeNextActivity = false;
1925 }
1926 }
1927 } else if (r.visible) {
1928 // If this activity is already visible, then there is nothing to do here.
1929 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1930 "Skipping: already visible at " + r);
1931
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001932 if (r.mClientVisibilityDeferred && notifyClients) {
1933 r.makeClientVisible();
1934 }
1935
Jorim Jaggife762342016-10-13 14:33:27 +02001936 if (r.handleAlreadyVisible()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001937 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001938 }
Jorim Jaggife762342016-10-13 14:33:27 +02001939 } else {
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001940 r.makeVisibleIfNeeded(starting, notifyClients);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001941 }
Jorim Jaggife762342016-10-13 14:33:27 +02001942 // Aggregate current change flags.
1943 configChanges |= r.configChangeFlags;
Craig Mautnerbb742462014-07-07 15:28:55 -07001944 } else {
Jorim Jaggife762342016-10-13 14:33:27 +02001945 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Bryce Lee7ace3952018-02-16 14:34:32 -08001946 + " finishing=" + r.finishing + " state=" + r.getState()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001947 + " stackShouldBeVisible=" + stackShouldBeVisible
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001948 + " behindFullscreenActivity=" + behindFullscreenActivity
1949 + " mLaunchTaskBehind=" + r.mLaunchTaskBehind);
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001950 makeInvisible(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001951 }
Jorim Jaggife762342016-10-13 14:33:27 +02001952 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001953 final int windowingMode = getWindowingMode();
1954 if (windowingMode == WINDOWING_MODE_FREEFORM) {
Jorim Jaggife762342016-10-13 14:33:27 +02001955 // The visibility of tasks and the activities they contain in freeform stack are
1956 // determined individually unlike other stacks where the visibility or fullscreen
1957 // status of an activity in a previous task affects other.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001958 behindFullscreenActivity = !stackShouldBeVisible;
Wale Ogunwale68278562017-09-23 17:13:55 -07001959 } else if (isActivityTypeHome()) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001960 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001961 + " stackShouldBeVisible=" + stackShouldBeVisible
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001962 + " behindFullscreenActivity=" + behindFullscreenActivity);
1963 // No other task in the home stack should be visible behind the home activity.
1964 // Home activities is usually a translucent activity with the wallpaper behind
1965 // them. However, when they don't have the wallpaper behind them, we want to
1966 // show activities in the next application stack behind them vs. another
1967 // task in the home stack like recents.
1968 behindFullscreenActivity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001969 }
1970 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001971
Jorim Jaggife762342016-10-13 14:33:27 +02001972 if (mTranslucentActivityWaiting != null &&
1973 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1974 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1975 notifyActivityDrawnLocked(null);
1976 }
1977 } finally {
Bryce Lee2a3cc462017-10-27 10:57:35 -07001978 mStackSupervisor.getKeyguardController().endActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02001979 }
1980 }
1981
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01001982 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
1983 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1984 mTaskHistory.get(taskNdx).addStartingWindowsForVisibleActivities(taskSwitch);
1985 }
1986 }
1987
Jorim Jaggife762342016-10-13 14:33:27 +02001988 /**
1989 * @return true if the top visible activity wants to occlude the Keyguard, false otherwise
1990 */
1991 boolean topActivityOccludesKeyguard() {
1992 return mTopActivityOccludesKeyguard;
1993 }
1994
1995 /**
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001996 * Returns true if this stack should be resized to match the bounds specified by
1997 * {@link ActivityOptions#setLaunchBounds} when launching an activity into the stack.
1998 */
1999 boolean resizeStackWithLaunchBounds() {
2000 return inPinnedWindowingMode();
2001 }
2002
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002003 @Override
2004 public boolean supportsSplitScreenWindowingMode() {
2005 final TaskRecord topTask = topTask();
2006 return super.supportsSplitScreenWindowingMode()
2007 && (topTask == null || topTask.supportsSplitScreenWindowingMode());
2008 }
2009
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002010 /** @return True if the resizing of the primary-split-screen stack affects this stack size. */
2011 boolean affectedBySplitScreenResize() {
2012 if (!supportsSplitScreenWindowingMode()) {
2013 return false;
2014 }
2015 final int windowingMode = getWindowingMode();
2016 return windowingMode != WINDOWING_MODE_FREEFORM && windowingMode != WINDOWING_MODE_PINNED;
2017 }
2018
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002019 /**
Jorim Jaggife762342016-10-13 14:33:27 +02002020 * @return the top most visible activity that wants to dismiss Keyguard
2021 */
2022 ActivityRecord getTopDismissingKeyguardActivity() {
2023 return mTopDismissingKeyguardActivity;
2024 }
2025
2026 /**
2027 * Checks whether {@param r} should be visible depending on Keyguard state and updates
2028 * {@link #mTopActivityOccludesKeyguard} and {@link #mTopDismissingKeyguardActivity} if
2029 * necessary.
2030 *
2031 * @return true if {@param r} is visible taken Keyguard state into account, false otherwise
2032 */
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002033 boolean checkKeyguardVisibility(ActivityRecord r, boolean shouldBeVisible, boolean isTop) {
Lucas Dupin47a65c72018-02-15 14:16:18 -08002034 final int displayId = mDisplayId != INVALID_DISPLAY ? mDisplayId : DEFAULT_DISPLAY;
2035 final boolean keyguardOrAodShowing = mStackSupervisor.getKeyguardController()
2036 .isKeyguardOrAodShowing(displayId);
Bryce Lee2a3cc462017-10-27 10:57:35 -07002037 final boolean keyguardLocked = mStackSupervisor.getKeyguardController().isKeyguardLocked();
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002038 final boolean showWhenLocked = r.canShowWhenLocked();
Jorim Jaggie69c9312016-10-31 18:24:38 -07002039 final boolean dismissKeyguard = r.hasDismissKeyguardWindows();
Jorim Jaggife762342016-10-13 14:33:27 +02002040 if (shouldBeVisible) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002041 if (dismissKeyguard && mTopDismissingKeyguardActivity == null) {
Jorim Jaggife762342016-10-13 14:33:27 +02002042 mTopDismissingKeyguardActivity = r;
2043 }
2044
2045 // Only the top activity may control occluded, as we can't occlude the Keyguard if the
2046 // top app doesn't want to occlude it.
2047 if (isTop) {
2048 mTopActivityOccludesKeyguard |= showWhenLocked;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07002049 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002050
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002051 final boolean canShowWithKeyguard = canShowWithInsecureKeyguard()
Bryce Lee2a3cc462017-10-27 10:57:35 -07002052 && mStackSupervisor.getKeyguardController().canDismissKeyguard();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002053 if (canShowWithKeyguard) {
2054 return true;
2055 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 }
Lucas Dupin47a65c72018-02-15 14:16:18 -08002057 if (keyguardOrAodShowing) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002058 // If keyguard is showing, nothing is visible, except if we are able to dismiss Keyguard
Lucas Dupin47a65c72018-02-15 14:16:18 -08002059 // right away and AOD isn't visible.
Bryce Lee2a3cc462017-10-27 10:57:35 -07002060 return shouldBeVisible && mStackSupervisor.getKeyguardController()
Jorim Jaggi07961872016-11-23 11:28:57 +01002061 .canShowActivityWhileKeyguardShowing(r, dismissKeyguard);
Jorim Jaggife762342016-10-13 14:33:27 +02002062 } else if (keyguardLocked) {
Bryce Lee2a3cc462017-10-27 10:57:35 -07002063 return shouldBeVisible && mStackSupervisor.getKeyguardController().canShowWhileOccluded(
Jorim Jaggi07961872016-11-23 11:28:57 +01002064 dismissKeyguard, showWhenLocked);
Jorim Jaggife762342016-10-13 14:33:27 +02002065 } else {
2066 return shouldBeVisible;
Craig Mautnereb8abf72014-07-02 15:04:09 -07002067 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 }
Craig Mautner58547802013-03-05 08:23:53 -08002069
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002070 /**
2071 * Check if the display to which this stack is attached has
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002072 * {@link Display#FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD} applied.
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002073 */
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002074 private boolean canShowWithInsecureKeyguard() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002075 final ActivityDisplay activityDisplay = getDisplay();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002076 if (activityDisplay == null) {
2077 throw new IllegalStateException("Stack is not attached to any display, stackId="
2078 + mStackId);
2079 }
2080
2081 final int flags = activityDisplay.mDisplay.getFlags();
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002082 return (flags & FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002083 }
2084
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002085 private void checkTranslucentActivityWaiting(ActivityRecord top) {
2086 if (mTranslucentActivityWaiting != top) {
2087 mUndrawnActivitiesBelowTopTranslucent.clear();
2088 if (mTranslucentActivityWaiting != null) {
2089 // Call the callback with a timeout indication.
2090 notifyActivityDrawnLocked(null);
2091 mTranslucentActivityWaiting = null;
2092 }
2093 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2094 }
2095 }
2096
2097 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08002098 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002099 // We need to make sure the app is running if it's the top, or it is just made visible from
2100 // invisible. If the app is already visible, it must have died while it was visible. In this
2101 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
2102 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002103 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002104 // This activity needs to be visible, but isn't even running...
2105 // get it started and resume if no other stack in this stack is resumed.
2106 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
2107 if (r != starting) {
2108 r.startFreezingScreenLocked(r.app, configChanges);
2109 }
2110 if (!r.visible || r.mLaunchTaskBehind) {
2111 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002112 r.setVisible(true);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002113 }
2114 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002115 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002116 return true;
2117 }
2118 }
2119 return false;
2120 }
2121
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002122 // TODO: Should probably be moved into ActivityRecord.
2123 private void makeInvisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002124 if (!r.visible) {
2125 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
2126 return;
2127 }
2128 // Now for any activities that aren't visible to the user, make sure they no longer are
2129 // keeping the screen frozen.
Bryce Lee7ace3952018-02-16 14:34:32 -08002130 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.getState());
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002131 try {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002132 final boolean canEnterPictureInPicture = r.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002133 "makeInvisible", true /* beforeStopping */);
Wale Ogunwale08488a62017-09-05 12:37:03 -07002134 // Defer telling the client it is hidden if it can enter Pip and isn't current paused,
2135 // stopped or stopping. This gives it a chance to enter Pip in onPause().
2136 // TODO: There is still a question surrounding activities in multi-window mode that want
2137 // to enter Pip after they are paused, but are still visible. I they should be okay to
2138 // enter Pip in those cases, but not "auto-Pip" which is what this condition covers and
2139 // the current contract for "auto-Pip" is that the app should enter it before onPause
2140 // returns. Just need to confirm this reasoning makes sense.
Wale Ogunwale89973222017-04-23 18:39:45 -07002141 final boolean deferHidingClient = canEnterPictureInPicture
Bryce Lee7ace3952018-02-16 14:34:32 -08002142 && !r.isState(STOPPING, STOPPED, PAUSED);
Wale Ogunwaleec950642017-04-25 07:44:21 -07002143 r.setDeferHidingClient(deferHidingClient);
2144 r.setVisible(false);
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002145
Bryce Lee7ace3952018-02-16 14:34:32 -08002146 switch (r.getState()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002147 case STOPPING:
2148 case STOPPED:
2149 if (r.app != null && r.app.thread != null) {
2150 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2151 "Scheduling invisibility: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08002152 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08002153 WindowVisibilityItem.obtain(false /* showWindow */));
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002154 }
Winson Chungc2baac02017-01-11 13:34:47 -08002155
2156 // Reset the flag indicating that an app can enter picture-in-picture once the
2157 // activity is hidden
Winson Chungf7e03e12017-08-22 11:32:16 -07002158 r.supportsEnterPipOnTaskSwitch = false;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002159 break;
2160
2161 case INITIALIZING:
2162 case RESUMED:
2163 case PAUSING:
2164 case PAUSED:
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002165 addToStopping(r, true /* scheduleIdle */,
2166 canEnterPictureInPicture /* idleDelayed */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002167 break;
2168
2169 default:
2170 break;
2171 }
2172 } catch (Exception e) {
2173 // Just skip on any failure; we'll make it visible when it next restarts.
2174 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
2175 }
2176 }
2177
2178 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002179 ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002180 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002181 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08002182 + " stackInvisible=" + stackInvisible
2183 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08002184 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002185 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002186 }
2187 return behindFullscreenActivity;
2188 }
2189
Todd Kennedyaab56db2015-01-30 09:39:53 -08002190 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002191 mTranslucentActivityWaiting = r;
2192 mUndrawnActivitiesBelowTopTranslucent.clear();
2193 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
2194 }
2195
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002196 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2197 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2198 final TaskRecord task = mTaskHistory.get(taskNdx);
2199 final ArrayList<ActivityRecord> activities = task.mActivities;
2200 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2201 final ActivityRecord r = activities.get(activityNdx);
2202 if ( r.appTimeTracker != except) {
2203 r.appTimeTracker = null;
2204 }
2205 }
2206 }
2207 }
2208
Craig Mautner5eda9b32013-07-02 11:58:16 -07002209 /**
2210 * Called as activities below the top translucent activity are redrawn. When the last one is
2211 * redrawn notify the top activity by calling
2212 * {@link Activity#onTranslucentConversionComplete}.
2213 *
2214 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2215 * occurred and the activity will be notified immediately.
2216 */
2217 void notifyActivityDrawnLocked(ActivityRecord r) {
2218 if ((r == null)
2219 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2220 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2221 // The last undrawn activity below the top has just been drawn. If there is an
2222 // opaque activity at the top, notify it that it can become translucent safely now.
2223 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2224 mTranslucentActivityWaiting = null;
2225 mUndrawnActivitiesBelowTopTranslucent.clear();
2226 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2227
Craig Mautner71dd1b62014-02-18 15:48:52 -08002228 if (waitingActivity != null) {
2229 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
2230 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
2231 try {
2232 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
2233 waitingActivity.appToken, r != null);
2234 } catch (RemoteException e) {
2235 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002236 }
2237 }
2238 }
2239 }
2240
Craig Mautnera61bc652013-10-28 15:43:18 -07002241 /** If any activities below the top running one are in the INITIALIZING state and they have a
2242 * starting window displayed then remove that starting window. It is possible that the activity
2243 * in this state will never resumed in which case that starting window will be orphaned. */
2244 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002245 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002246 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002247 // We don't want to clear starting window for activities that aren't behind fullscreen
2248 // activities as we need to display their starting window until they are done initializing.
2249 boolean behindFullscreenActivity = false;
Wale Ogunwale98742a52016-07-12 10:29:12 -07002250
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002251 if (!shouldBeVisible(null)) {
Wale Ogunwale98742a52016-07-12 10:29:12 -07002252 // The stack is not visible, so no activity in it should be displaying a starting
2253 // window. Mark all activities below top and behind fullscreen.
2254 aboveTop = false;
2255 behindFullscreenActivity = true;
2256 }
2257
Craig Mautnera61bc652013-10-28 15:43:18 -07002258 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2259 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2260 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2261 final ActivityRecord r = activities.get(activityNdx);
2262 if (aboveTop) {
2263 if (r == topActivity) {
2264 aboveTop = false;
2265 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002266 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002267 continue;
2268 }
2269
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002270 r.removeOrphanedStartingWindow(behindFullscreenActivity);
Wale Ogunwale68741142016-05-17 09:40:02 -07002271 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002272 }
2273 }
2274 }
2275
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 /**
2277 * Ensure that the top activity in the stack is resumed.
2278 *
2279 * @param prev The previously resumed activity, for when in the process
2280 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002281 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 *
2283 * @return Returns true if something is being resumed, or false if
2284 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002285 *
2286 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2287 * non-focused stack to be resumed.
2288 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2289 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002290 */
Andreas Gampea36dc622018-02-05 17:19:22 -08002291 @GuardedBy("mService")
Wale Ogunwaled046a012015-12-24 13:05:59 -08002292 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002293 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002294 // Don't even start recursing.
2295 return false;
2296 }
2297
2298 boolean result = false;
2299 try {
2300 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002301 mStackSupervisor.inResumeTopActivity = true;
Bryce Lee69ad8182017-09-28 10:01:36 -07002302 result = resumeTopActivityInnerLocked(prev, options);
Bryce Leefc7cedd2018-03-01 15:38:07 -08002303
2304 // When resuming the top activity, it may be necessary to pause the top activity (for
2305 // example, returning to the lock screen. We suppress the normal pause logic in
2306 // {@link #resumeTopActivityUncheckedLocked}, since the top activity is resumed at the
2307 // end. We call the {@link ActivityStackSupervisor#checkReadyForSleepLocked} again here
2308 // to ensure any necessary pause logic occurs. In the case where the Activity will be
2309 // shown regardless of the lock screen, the call to
2310 // {@link ActivityStackSupervisor#checkReadyForSleepLocked} is skipped.
2311 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
2312 if (next == null || !next.canTurnScreenOn()) {
2313 checkReadyForSleep();
2314 }
Craig Mautner544efa72014-09-04 16:41:20 -07002315 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002316 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002317 }
chaviw59b98852017-06-13 12:05:44 -07002318
Craig Mautner544efa72014-09-04 16:41:20 -07002319 return result;
2320 }
2321
Bryce Leec4ab62a2018-03-05 14:19:26 -08002322 /**
2323 * Returns the currently resumed activity.
2324 */
2325 protected ActivityRecord getResumedActivity() {
2326 return mResumedActivity;
2327 }
2328
Bryce Lee84730a02018-04-03 14:10:04 -07002329 private void setResumedActivity(ActivityRecord r, String reason) {
2330 if (mResumedActivity == r) {
2331 return;
2332 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08002333
Bryce Lee84730a02018-04-03 14:10:04 -07002334 if (DEBUG_STACK) Slog.d(TAG_STACK, "setResumedActivity stack:" + this + " + from: "
2335 + mResumedActivity + " to:" + r + " reason:" + reason);
2336 mResumedActivity = r;
Chong Zhang6cda19c2016-06-14 19:07:56 -07002337 }
2338
Andreas Gampea36dc622018-02-05 17:19:22 -08002339 @GuardedBy("mService")
Chong Zhang280d3322015-11-03 17:27:26 -08002340 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002341 if (!mService.mBooting && !mService.mBooted) {
2342 // Not ready yet!
2343 return false;
2344 }
2345
Winson Chung3f103eb2017-04-12 21:53:59 -07002346 // Find the next top-most activity to resume in this stack that is not finishing and is
2347 // focusable. If it is not focusable, we will fall into the case below to resume the
2348 // top activity in the next focusable task.
2349 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
Andrii Kulian7fc22812016-12-28 13:04:11 -08002350
2351 final boolean hasRunningActivity = next != null;
2352
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002353 // TODO: Maybe this entire condition can get removed?
Wale Ogunwale66e16852017-10-19 13:35:52 -07002354 if (hasRunningActivity && !isAttached()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002355 return false;
2356 }
2357
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002358 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 // Remember how we'll process this pause/resume situation, and ensure
2361 // that the state is reset however we wind up proceeding.
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08002362 boolean userLeaving = mStackSupervisor.mUserLeaving;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002363 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364
Andrii Kulian7fc22812016-12-28 13:04:11 -08002365 if (!hasRunningActivity) {
2366 // There are no activities left in the stack, let's look somewhere else.
2367 return resumeTopActivityInNextFocusableStack(prev, options, "noMoreActivities");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 }
2369
2370 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002371
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 // If the top activity is the resumed one, nothing to do.
Bryce Lee7ace3952018-02-16 14:34:32 -08002373 if (mResumedActivity == next && next.isState(RESUMED)
2374 && mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 // Make sure we have executed any pending transitions, since there
2376 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002377 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002378 if (DEBUG_STATES) Slog.d(TAG_STATES,
2379 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002380 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381 return false;
2382 }
2383
2384 // If we are sleeping, and there is no resumed activity, and the top
2385 // activity is paused, well that is the state we want.
David Stevens9440dc82017-03-16 19:00:20 -07002386 if (shouldSleepOrShutDownActivities()
p13451dbad2872012-04-18 11:39:23 +09002387 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002388 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389 // Make sure we have executed any pending transitions, since there
2390 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002391 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002392 if (DEBUG_STATES) Slog.d(TAG_STATES,
2393 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002394 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002395 return false;
2396 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002397
2398 // Make sure that the user who owns this activity is started. If not,
2399 // we will just leave it as is because someone should be bringing
2400 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002401 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002402 Slog.w(TAG, "Skipping resume of top activity " + next
2403 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002404 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002405 return false;
2406 }
2407
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002408 // The activity may be waiting for stop, but that is no longer
2409 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002410 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002411 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002412 next.sleeping = false;
Bryce Lee4a194382017-04-04 14:32:48 -07002413 mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002414
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002415 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002417 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002418 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002419 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002420 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002421 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002422 return false;
2423 }
2424
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002425 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2426
Wale Ogunwale89973222017-04-23 18:39:45 -07002427 boolean lastResumedCanPip = false;
chaviw4ad54912018-05-30 11:05:44 -07002428 ActivityRecord lastResumed = null;
Wale Ogunwale89973222017-04-23 18:39:45 -07002429 final ActivityStack lastFocusedStack = mStackSupervisor.getLastStack();
2430 if (lastFocusedStack != null && lastFocusedStack != this) {
2431 // So, why aren't we using prev here??? See the param comment on the method. prev doesn't
2432 // represent the last resumed activity. However, the last focus stack does if it isn't null.
chaviw4ad54912018-05-30 11:05:44 -07002433 lastResumed = lastFocusedStack.mResumedActivity;
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08002434 if (userLeaving && inMultiWindowMode() && lastFocusedStack.shouldBeVisible(next)) {
2435 // The user isn't leaving if this stack is the multi-window mode and the last
2436 // focused stack should still be visible.
2437 if(DEBUG_USER_LEAVING) Slog.i(TAG_USER_LEAVING, "Overriding userLeaving to false"
2438 + " next=" + next + " lastResumed=" + lastResumed);
2439 userLeaving = false;
2440 }
Wale Ogunwale89973222017-04-23 18:39:45 -07002441 lastResumedCanPip = lastResumed != null && lastResumed.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002442 "resumeTopActivity", userLeaving /* beforeStopping */);
Wale Ogunwale89973222017-04-23 18:39:45 -07002443 }
Winson Chungc2baac02017-01-11 13:34:47 -08002444 // If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002445 // to be paused, while at the same time resuming the new resume activity only if the
2446 // previous activity can't go into Pip since we want to give Pip activities a chance to
2447 // enter Pip before resuming the next activity.
Wale Ogunwale89973222017-04-23 18:39:45 -07002448 final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0
2449 && !lastResumedCanPip;
2450
Winson Chungc2baac02017-01-11 13:34:47 -08002451 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002452 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002453 if (DEBUG_STATES) Slog.d(TAG_STATES,
2454 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Winson Chungc2baac02017-01-11 13:34:47 -08002455 pausing |= startPausingLocked(userLeaving, false, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002456 }
Winson Chungc2baac02017-01-11 13:34:47 -08002457 if (pausing && !resumeWhilePausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002458 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002459 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002460 // At this point we want to put the upcoming activity's process
2461 // at the top of the LRU list, since we know we will be needing it
2462 // very soon and it would be a waste to let it get killed if it
2463 // happens to be sitting towards the end.
2464 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002465 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002466 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002467 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
chaviw4ad54912018-05-30 11:05:44 -07002468 if (lastResumed != null) {
2469 lastResumed.setWillCloseOrEnterPip(true);
2470 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002471 return true;
Bryce Lee7ace3952018-02-16 14:34:32 -08002472 } else if (mResumedActivity == next && next.isState(RESUMED)
2473 && mStackSupervisor.allResumedActivitiesComplete()) {
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002474 // It is possible for the activity to be resumed when we paused back stacks above if the
2475 // next activity doesn't have to wait for pause to complete.
2476 // So, nothing else to-do except:
2477 // Make sure we have executed any pending transitions, since there
2478 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002479 executeAppTransition(options);
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002480 if (DEBUG_STATES) Slog.d(TAG_STATES,
2481 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
2482 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2483 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002484 }
2485
Christopher Tated3f175c2012-06-14 14:16:54 -07002486 // If the most recent activity was noHistory but was only stopped rather
2487 // than stopped+finished because the device went to sleep, we need to make
2488 // sure to finish it as we're making a new activity topmost.
David Stevens9440dc82017-03-16 19:00:20 -07002489 if (shouldSleepActivities() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002490 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002491 if (DEBUG_STATES) Slog.d(TAG_STATES,
2492 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002493 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002494 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002495 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002496 }
2497
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002498 if (prev != null && prev != next) {
Bryce Lee4a194382017-04-04 14:32:48 -07002499 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Craig Mautner8c14c152015-01-15 17:32:07 -08002500 && next != null && !next.nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07002501 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002502 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2503 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002504 } else {
2505 // The next activity is already visible, so hide the previous
2506 // activity's windows right now so we can show the new one ASAP.
2507 // We only do this if the previous is finishing, which should mean
2508 // it is on top of the one being resumed so hiding it quickly
2509 // is good. Otherwise, we want to do the normal route of allowing
2510 // the resumed activity to be shown so we can decide if the
2511 // previous should actually be hidden depending on whether the
2512 // new one is found to be full-screen or not.
2513 if (prev.finishing) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002514 prev.setVisibility(false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002515 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2516 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Bryce Lee4a194382017-04-04 14:32:48 -07002517 + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002518 + ", nowVisible=" + next.nowVisible);
2519 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002520 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002521 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002522 + ", waitingVisible="
Bryce Lee4a194382017-04-04 14:32:48 -07002523 + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002524 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002525 }
2526 }
2527 }
2528
Dianne Hackborne7f97212011-02-24 14:40:20 -08002529 // Launching this app's activity, make sure the app is no longer
2530 // considered stopped.
2531 try {
2532 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002533 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002534 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002535 } catch (IllegalArgumentException e) {
2536 Slog.w(TAG, "Failed trying to unstop package "
2537 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002538 }
2539
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002540 // We are starting up the next activity, so tell the window manager
2541 // that the previous one will be hidden soon. This way it can know
2542 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002543 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 if (prev != null) {
2545 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002546 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 "Prepare close transition: prev=" + prev);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002548 if (mStackSupervisor.mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002549 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002550 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002551 } else {
Bryce Leeaf691c02017-03-20 14:20:22 -07002552 mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002553 ? TRANSIT_ACTIVITY_CLOSE
2554 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002556 prev.setVisibility(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002557 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002558 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2559 "Prepare open transition: prev=" + prev);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002560 if (mStackSupervisor.mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002561 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002562 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002563 } else {
Bryce Leeaf691c02017-03-20 14:20:22 -07002564 mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002565 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002566 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002567 ? TRANSIT_TASK_OPEN_BEHIND
2568 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 }
2570 }
Craig Mautner967212c2013-04-13 21:10:58 -07002571 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002572 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002573 if (mStackSupervisor.mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002574 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002575 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002576 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002577 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 }
2579 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002580
Craig Mautner525f3d92013-05-07 14:01:50 -07002581 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002582 next.applyOptionsLocked();
2583 } else {
2584 next.clearOptionsLocked();
2585 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002587 mStackSupervisor.mNoAnimActivities.clear();
2588
Craig Mautnercf910b02013-04-23 11:23:27 -07002589 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002590 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002591 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2592 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593
Chong Zhangd127c6d2016-05-02 16:36:41 -07002594 // If the previous activity is translucent, force a visibility update of
2595 // the next activity, so that it's added to WM's opening app list, and
2596 // transition animation can be set up properly.
2597 // For example, pressing Home button with a translucent activity in focus.
2598 // Launcher is already visible in this case. If we don't add it to opening
2599 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2600 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2601 final boolean lastActivityTranslucent = lastStack != null
Bryce Leef3c6a472017-11-14 14:53:06 -08002602 && (lastStack.inMultiWindowMode()
Chong Zhangd127c6d2016-05-02 16:36:41 -07002603 || (lastStack.mLastPausedActivity != null
2604 && !lastStack.mLastPausedActivity.fullscreen));
2605
Bryce Lee69ad8182017-09-28 10:01:36 -07002606 // The contained logic must be synchronized, since we are both changing the visibility
2607 // and updating the {@link Configuration}. {@link ActivityRecord#setVisibility} will
2608 // ultimately cause the client code to schedule a layout. Since layouts retrieve the
2609 // current {@link Configuration}, we must ensure that the below code updates it before
2610 // the layout can occur.
2611 synchronized(mWindowManager.getWindowManagerLock()) {
2612 // This activity is now becoming visible.
2613 if (!next.visible || next.stopped || lastActivityTranslucent) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002614 next.setVisibility(true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002615 }
Craig Mautner58547802013-03-05 08:23:53 -08002616
Bryce Lee69ad8182017-09-28 10:01:36 -07002617 // schedule launch ticks to collect information about slow apps.
2618 next.startLaunchTickingLocked();
2619
2620 ActivityRecord lastResumedActivity =
2621 lastStack == null ? null :lastStack.mResumedActivity;
Bryce Lee7ace3952018-02-16 14:34:32 -08002622 final ActivityState lastState = next.getState();
Bryce Lee69ad8182017-09-28 10:01:36 -07002623
2624 mService.updateCpuStats();
2625
2626 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next
2627 + " (in existing)");
2628
Bryce Leec4ab62a2018-03-05 14:19:26 -08002629 next.setState(RESUMED, "resumeTopActivityInnerLocked");
Bryce Lee69ad8182017-09-28 10:01:36 -07002630
2631 mService.updateLruProcessLocked(next.app, true, null);
2632 updateLRUListLocked(next);
2633 mService.updateOomAdjLocked();
2634
2635 // Have the window manager re-evaluate the orientation of
2636 // the screen based on the new activity order.
2637 boolean notUpdated = true;
2638
2639 if (mStackSupervisor.isFocusedStack(this)) {
Andrii Kulianf4479ee2018-05-23 17:52:48 -07002640 // We have special rotation behavior when here is some active activity that
2641 // requests specific orientation or Keyguard is locked. Make sure all activity
2642 // visibilities are set correctly as well as the transition is updated if needed
2643 // to get the correct rotation behavior. Otherwise the following call to update
2644 // the orientation may cause incorrect configurations delivered to client as a
2645 // result of invisible window resize.
Bryce Lee69ad8182017-09-28 10:01:36 -07002646 // TODO: Remove this once visibilities are set correctly immediately when
2647 // starting an activity.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07002648 notUpdated = !mStackSupervisor.ensureVisibilityAndConfig(next, mDisplayId,
2649 true /* markFrozenIfConfigChanged */, false /* deferResume */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002650 }
2651
Bryce Lee69ad8182017-09-28 10:01:36 -07002652 if (notUpdated) {
2653 // The configuration update wasn't able to keep the existing
2654 // instance of the activity, and instead started a new one.
2655 // We should be all done, but let's just make sure our activity
2656 // is still at the top and schedule another run if something
2657 // weird happened.
2658 ActivityRecord nextNext = topRunningActivityLocked();
2659 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
2660 "Activity config changed during resume: " + next
2661 + ", new next: " + nextNext);
2662 if (nextNext != next) {
2663 // Do over!
2664 mStackSupervisor.scheduleResumeTopActivities();
2665 }
2666 if (!next.visible || next.stopped) {
2667 next.setVisibility(true);
2668 }
2669 next.completeResumeLocked();
2670 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2671 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002672 }
2673
Bryce Lee69ad8182017-09-28 10:01:36 -07002674 try {
Andrii Kulian9956d892018-02-14 13:48:56 -08002675 final ClientTransaction transaction = ClientTransaction.obtain(next.app.thread,
2676 next.appToken);
Bryce Lee69ad8182017-09-28 10:01:36 -07002677 // Deliver all pending results.
2678 ArrayList<ResultInfo> a = next.results;
2679 if (a != null) {
2680 final int N = a.size();
2681 if (!next.finishing && N > 0) {
2682 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2683 "Delivering results to " + next + ": " + a);
Andrii Kulian9956d892018-02-14 13:48:56 -08002684 transaction.addCallback(ActivityResultItem.obtain(a));
Bryce Lee69ad8182017-09-28 10:01:36 -07002685 }
2686 }
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002687
Bryce Lee69ad8182017-09-28 10:01:36 -07002688 if (next.newIntents != null) {
Andrii Kulian9956d892018-02-14 13:48:56 -08002689 transaction.addCallback(NewIntentItem.obtain(next.newIntents,
2690 false /* andPause */));
Bryce Lee69ad8182017-09-28 10:01:36 -07002691 }
Craig Mautner58547802013-03-05 08:23:53 -08002692
Bryce Lee69ad8182017-09-28 10:01:36 -07002693 // Well the app will no longer be stopped.
2694 // Clear app token stopped state in window manager if needed.
2695 next.notifyAppResumed(next.stopped);
Craig Mautner58547802013-03-05 08:23:53 -08002696
Bryce Lee69ad8182017-09-28 10:01:36 -07002697 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2698 System.identityHashCode(next), next.getTask().taskId,
2699 next.shortComponentName);
2700
2701 next.sleeping = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05002702 mService.getAppWarningsLocked().onResumeActivity(next);
Bryce Lee69ad8182017-09-28 10:01:36 -07002703 mService.showAskCompatModeDialogLocked(next);
2704 next.app.pendingUiClean = true;
2705 next.app.forceProcessStateUpTo(mService.mTopProcessState);
2706 next.clearOptionsLocked();
Andrii Kulian9956d892018-02-14 13:48:56 -08002707 transaction.setLifecycleStateRequest(
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08002708 ResumeActivityItem.obtain(next.app.repProcState,
Bryce Lee1d0d5142018-04-12 10:35:07 -07002709 mService.isNextTransitionForward()));
Bryce Leeb0f993f2018-03-02 15:38:01 -08002710 mService.getLifecycleManager().scheduleTransaction(transaction);
Bryce Lee69ad8182017-09-28 10:01:36 -07002711
2712 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed "
2713 + next);
2714 } catch (Exception e) {
2715 // Whoops, need to restart this activity!
2716 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
2717 + lastState + ": " + next);
Bryce Lee7ace3952018-02-16 14:34:32 -08002718 next.setState(lastState, "resumeTopActivityInnerLocked");
Bryce Lee4c401382018-03-13 19:23:03 -07002719
2720 // lastResumedActivity being non-null implies there is a lastStack present.
2721 if (lastResumedActivity != null) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08002722 lastResumedActivity.setState(RESUMED, "resumeTopActivityInnerLocked");
Bryce Lee69ad8182017-09-28 10:01:36 -07002723 }
Bryce Lee4c401382018-03-13 19:23:03 -07002724
Bryce Lee69ad8182017-09-28 10:01:36 -07002725 Slog.i(TAG, "Restarting because process died: " + next);
2726 if (!next.hasBeenLaunched) {
2727 next.hasBeenLaunched = true;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002728 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null
2729 && lastStack.isTopStackOnDisplay()) {
Bryce Lee69ad8182017-09-28 10:01:36 -07002730 next.showStartingWindow(null /* prev */, false /* newTask */,
2731 false /* taskSwitch */);
2732 }
2733 mStackSupervisor.startSpecificActivityLocked(next, true, false);
2734 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2735 return true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002736 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002737 }
2738
2739 // From this point on, if something goes wrong there is no way
2740 // to recover the activity.
2741 try {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002742 next.completeResumeLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 } catch (Exception e) {
2744 // If any exception gets thrown, toss away this
2745 // activity and try the next one.
2746 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002747 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002748 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002749 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002750 return true;
2751 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002752 } else {
2753 // Whoops, need to restart this activity!
2754 if (!next.hasBeenLaunched) {
2755 next.hasBeenLaunched = true;
2756 } else {
2757 if (SHOW_APP_STARTING_PREVIEW) {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002758 next.showStartingWindow(null /* prev */, false /* newTask */,
2759 false /* taskSwich */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002761 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002762 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002763 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002764 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002765 }
2766
Craig Mautnercf910b02013-04-23 11:23:27 -07002767 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002768 return true;
2769 }
2770
Andrii Kulian7fc22812016-12-28 13:04:11 -08002771 private boolean resumeTopActivityInNextFocusableStack(ActivityRecord prev,
2772 ActivityOptions options, String reason) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002773 if (adjustFocusToNextFocusableStack(reason)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002774 // Try to move focus to the next visible stack with a running activity if this
2775 // stack is not covering the entire screen or is on a secondary display (with no home
2776 // stack).
2777 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2778 mStackSupervisor.getFocusedStack(), prev, null);
2779 }
2780
2781 // Let's just start up the Launcher...
2782 ActivityOptions.abort(options);
2783 if (DEBUG_STATES) Slog.d(TAG_STATES,
2784 "resumeTopActivityInNextFocusableStack: " + reason + ", go home");
2785 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2786 // Only resume home if on home display
2787 return isOnHomeDisplay() &&
2788 mStackSupervisor.resumeHomeStackTask(prev, reason);
2789 }
2790
riddle_hsuc215a4f2014-12-27 12:10:45 +08002791 private TaskRecord getNextTask(TaskRecord targetTask) {
2792 final int index = mTaskHistory.indexOf(targetTask);
2793 if (index >= 0) {
2794 final int numTasks = mTaskHistory.size();
2795 for (int i = index + 1; i < numTasks; ++i) {
2796 TaskRecord task = mTaskHistory.get(i);
2797 if (task.userId == targetTask.userId) {
2798 return task;
2799 }
2800 }
2801 }
2802 return null;
2803 }
2804
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002805 /** Returns the position the input task should be placed in this stack. */
2806 int getAdjustedPositionForTask(TaskRecord task, int suggestedPosition,
2807 ActivityRecord starting) {
2808
2809 int maxPosition = mTaskHistory.size();
2810 if ((starting != null && starting.okToShowLocked())
2811 || (starting == null && task.okToShowLocked())) {
2812 // If the task or starting activity can be shown, then whatever position is okay.
2813 return Math.min(suggestedPosition, maxPosition);
2814 }
2815
2816 // The task can't be shown, put non-current user tasks below current user tasks.
2817 while (maxPosition > 0) {
2818 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2819 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2820 || tmpTask.topRunningActivityLocked() == null) {
2821 break;
2822 }
2823 maxPosition--;
2824 }
2825
2826 return Math.min(suggestedPosition, maxPosition);
2827 }
2828
Andrii Kuliand2765632016-12-12 22:26:34 -08002829 /**
2830 * Used from {@link ActivityStack#positionTask(TaskRecord, int)}.
2831 * @see ActivityManagerService#positionTaskInStack(int, int, int).
2832 */
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002833 private void insertTaskAtPosition(TaskRecord task, int position) {
2834 if (position >= mTaskHistory.size()) {
2835 insertTaskAtTop(task, null);
2836 return;
Evan Rosky9c448172017-11-02 14:19:27 -07002837 } else if (position <= 0) {
2838 insertTaskAtBottom(task);
2839 return;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002840 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002841 position = getAdjustedPositionForTask(task, position, null /* starting */);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002842 mTaskHistory.remove(task);
2843 mTaskHistory.add(position, task);
Bryce Leef3c6a472017-11-14 14:53:06 -08002844 mWindowContainerController.positionChildAt(task.getWindowContainerController(), position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002845 updateTaskMovement(task, true);
2846 }
2847
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002848 private void insertTaskAtTop(TaskRecord task, ActivityRecord starting) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002849 // TODO: Better place to put all the code below...may be addTask...
2850 mTaskHistory.remove(task);
2851 // Now put task at top.
2852 final int position = getAdjustedPositionForTask(task, mTaskHistory.size(), starting);
2853 mTaskHistory.add(position, task);
2854 updateTaskMovement(task, true);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002855 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
2856 true /* includingParents */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002857 }
2858
Evan Rosky9c448172017-11-02 14:19:27 -07002859 private void insertTaskAtBottom(TaskRecord task) {
2860 // Unlike insertTaskAtPosition, this will also position parents of the windowcontroller.
2861 mTaskHistory.remove(task);
2862 final int position = getAdjustedPositionForTask(task, 0, null);
2863 mTaskHistory.add(position, task);
2864 updateTaskMovement(task, true);
2865 mWindowContainerController.positionChildAtBottom(task.getWindowContainerController(),
2866 true /* includingParents */);
2867 }
2868
Bryce Leead5b8322018-03-08 14:28:52 -08002869 void startActivityLocked(ActivityRecord r, ActivityRecord focusedTopActivity,
Winson Chungb5c41b72016-12-07 15:00:47 -08002870 boolean newTask, boolean keepCurTransition, ActivityOptions options) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002871 TaskRecord rTask = r.getTask();
Craig Mautnerd2328952013-03-05 12:46:26 -08002872 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002873 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2874 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002875 // Last activity in task had been removed or ActivityManagerService is reusing task.
2876 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002877 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002878 insertTaskAtTop(rTask, r);
Craig Mautner77878772013-03-04 19:46:24 -08002879 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002880 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881 if (!newTask) {
2882 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002883 boolean startIt = true;
2884 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2885 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002886 if (task.getTopActivity() == null) {
2887 // All activities in task are finishing.
2888 continue;
2889 }
Bryce Leeaf691c02017-03-20 14:20:22 -07002890 if (task == rTask) {
Craig Mautner70a86932013-02-28 22:37:44 -08002891 // Here it is! Now, if this is not yet visible to the
2892 // user, then just add it without starting; it will
2893 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002894 if (!startIt) {
2895 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2896 + task, new RuntimeException("here").fillInStackTrace());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002897 r.createWindowContainer();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002898 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002899 return;
2900 }
2901 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002902 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002903 startIt = false;
2904 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 }
2906 }
2907
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002908 // Place a new activity at top of stack, so it is next to interact with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002909
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002910 // If we are not placing the new activity frontmost, we do not want to deliver the
2911 // onUserLeaving callback to the actual frontmost activity
Bryce Leeaf691c02017-03-20 14:20:22 -07002912 final TaskRecord activityTask = r.getTask();
2913 if (task == activityTask && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002914 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002915 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002916 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 }
Craig Mautner70a86932013-02-28 22:37:44 -08002918
Bryce Leeaf691c02017-03-20 14:20:22 -07002919 task = activityTask;
Craig Mautner70a86932013-02-28 22:37:44 -08002920
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002921 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002922 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002923 new RuntimeException("here").fillInStackTrace());
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002924 // TODO: Need to investigate if it is okay for the controller to already be created by the
2925 // time we get to this point. I think it is, but need to double check.
2926 // Use test in b/34179495 to trace the call path.
2927 if (r.getWindowContainerController() == null) {
2928 r.createWindowContainer();
2929 }
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002930 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002931
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002932 if (!isHomeOrRecentsStack() || numActivities() > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002933 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002934 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002935 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002936 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002937 mStackSupervisor.mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002939 int transit = TRANSIT_ACTIVITY_OPEN;
2940 if (newTask) {
2941 if (r.mLaunchTaskBehind) {
2942 transit = TRANSIT_TASK_OPEN_BEHIND;
2943 } else {
Winson Chungc2baac02017-01-11 13:34:47 -08002944 // If a new task is being launched, then mark the existing top activity as
Winson Chung942a85c2017-07-11 15:07:45 -07002945 // supporting picture-in-picture while pausing only if the starting activity
2946 // would not be considered an overlay on top of the current activity
2947 // (eg. not fullscreen, or the assistant)
Winson Chungf7e03e12017-08-22 11:32:16 -07002948 if (canEnterPipOnTaskSwitch(focusedTopActivity,
2949 null /* toFrontTask */, r, options)) {
2950 focusedTopActivity.supportsEnterPipOnTaskSwitch = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08002951 }
2952 transit = TRANSIT_TASK_OPEN;
2953 }
2954 }
2955 mWindowManager.prepareAppTransition(transit, keepCurTransition);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002956 mStackSupervisor.mNoAnimActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002958 boolean doShow = true;
2959 if (newTask) {
2960 // Even though this activity is starting fresh, we still need
2961 // to reset it to make sure we apply affinities to move any
2962 // existing activities from other tasks in to it.
2963 // If the caller has requested that the target task be
2964 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002965 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002966 resetTaskIfNeededLocked(r, r);
2967 doShow = topRunningNonDelayedActivityLocked(null) == r;
2968 }
Chong Zhang280d3322015-11-03 17:27:26 -08002969 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002970 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2971 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002973 if (r.mLaunchTaskBehind) {
2974 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2975 // tell WindowManager that r is visible even though it is at the back of the stack.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002976 r.setVisibility(true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002977 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002978 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979 // Figure out if we are transitioning from another activity that is
2980 // "has the same starting icon" as the next one. This allows the
2981 // window manager to keep the previous window it had previously
2982 // created, if it still had one.
Bryce Leeaf691c02017-03-20 14:20:22 -07002983 TaskRecord prevTask = r.getTask();
2984 ActivityRecord prev = prevTask.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985 if (prev != null) {
2986 // We don't want to reuse the previous starting preview if:
2987 // (1) The current activity is in a different task.
Bryce Leeaf691c02017-03-20 14:20:22 -07002988 if (prev.getTask() != prevTask) {
Craig Mautner29219d92013-04-16 20:19:12 -07002989 prev = null;
2990 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002992 else if (prev.nowVisible) {
2993 prev = null;
2994 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002995 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002996 r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002997 }
2998 } else {
2999 // If this is the first activity, don't do any fancy animations,
3000 // because there is nothing for it to animate on top of.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003001 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08003003 }
3004
Winson Chungf7e03e12017-08-22 11:32:16 -07003005 /**
3006 * @return Whether the switch to another task can trigger the currently running activity to
3007 * enter PiP while it is pausing (if supported). Only one of {@param toFrontTask} or
3008 * {@param toFrontActivity} should be set.
3009 */
3010 private boolean canEnterPipOnTaskSwitch(ActivityRecord pipCandidate,
3011 TaskRecord toFrontTask, ActivityRecord toFrontActivity, ActivityOptions opts) {
3012 if (opts != null && opts.disallowEnterPictureInPictureWhileLaunching()) {
3013 // Ensure the caller has requested not to trigger auto-enter PiP
3014 return false;
3015 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003016 if (pipCandidate == null || pipCandidate.inPinnedWindowingMode()) {
Winson Chungf7e03e12017-08-22 11:32:16 -07003017 // Ensure that we do not trigger entering PiP an activity on the pinned stack
3018 return false;
3019 }
Wale Ogunwale68278562017-09-23 17:13:55 -07003020 final ActivityStack targetStack = toFrontTask != null
3021 ? toFrontTask.getStack() : toFrontActivity.getStack();
3022 if (targetStack != null && targetStack.isActivityTypeAssistant()) {
Winson Chungf7e03e12017-08-22 11:32:16 -07003023 // Ensure the task/activity being brought forward is not the assistant
3024 return false;
3025 }
Winson Chungf7e03e12017-08-22 11:32:16 -07003026 return true;
3027 }
3028
Jorim Jaggi02886a82016-12-06 09:10:06 -08003029 private boolean isTaskSwitch(ActivityRecord r,
3030 ActivityRecord topFocusedActivity) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003031 return topFocusedActivity != null && r.getTask() != topFocusedActivity.getTask();
Jorim Jaggi02886a82016-12-06 09:10:06 -08003032 }
3033
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003034 /**
3035 * Perform a reset of the given task, if needed as part of launching it.
3036 * Returns the new HistoryRecord at the top of the task.
3037 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08003038 /**
3039 * Helper method for #resetTaskIfNeededLocked.
3040 * We are inside of the task being reset... we'll either finish this activity, push it out
3041 * for another task, or leave it as-is.
3042 * @param task The task containing the Activity (taskTop) that might be reset.
3043 * @param forceReset
3044 * @return An ActivityOptions that needs to be processed.
3045 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07003046 private ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003047 ActivityOptions topOptions = null;
3048
3049 int replyChainEnd = -1;
3050 boolean canMoveOptions = true;
3051
3052 // We only do this for activities that are not the root of the task (since if we finish
3053 // the root, we may no longer have the task!).
3054 final ArrayList<ActivityRecord> activities = task.mActivities;
3055 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003056 final int rootActivityNdx = task.findEffectiveRootIndex();
3057 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003058 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003059 if (target.frontOfTask)
3060 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003061
3062 final int flags = target.info.flags;
3063 final boolean finishOnTaskLaunch =
3064 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3065 final boolean allowTaskReparenting =
3066 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3067 final boolean clearWhenTaskReset =
3068 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
3069
3070 if (!finishOnTaskLaunch
3071 && !clearWhenTaskReset
3072 && target.resultTo != null) {
3073 // If this activity is sending a reply to a previous
3074 // activity, we can't do anything with it now until
3075 // we reach the start of the reply chain.
3076 // XXX note that we are assuming the result is always
3077 // to the previous activity, which is almost always
3078 // the case but we really shouldn't count on.
3079 if (replyChainEnd < 0) {
3080 replyChainEnd = i;
3081 }
3082 } else if (!finishOnTaskLaunch
3083 && !clearWhenTaskReset
3084 && allowTaskReparenting
3085 && target.taskAffinity != null
3086 && !target.taskAffinity.equals(task.affinity)) {
3087 // If this activity has an affinity for another
3088 // task, then we need to move it out of here. We will
3089 // move it as far out of the way as possible, to the
3090 // bottom of the activity stack. This also keeps it
3091 // correctly ordered with any activities we previously
3092 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08003093 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003094 final ActivityRecord bottom =
3095 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08003096 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003097 if (bottom != null && target.taskAffinity != null
Bryce Leeaf691c02017-03-20 14:20:22 -07003098 && target.taskAffinity.equals(bottom.getTask().affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003099 // If the activity currently at the bottom has the
3100 // same task affinity as the one we are moving,
3101 // then merge it into the same task.
Bryce Leeaf691c02017-03-20 14:20:22 -07003102 targetTask = bottom.getTask();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003103 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003104 + " out to bottom task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003105 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003106 targetTask = createTaskRecord(
3107 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003108 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08003109 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003110 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003111 + " out to new task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003112 }
3113
Craig Mautner525f3d92013-05-07 14:01:50 -07003114 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003115 final int start = replyChainEnd < 0 ? i : replyChainEnd;
3116 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07003117 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003118 if (p.finishing) {
3119 continue;
3120 }
3121
Craig Mautnere3a74d52013-02-22 14:14:58 -08003122 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003123 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003124 topOptions = p.takeOptionsLocked();
3125 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003126 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003127 }
3128 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003129 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3130 "Removing activity " + p + " from task=" + task + " adding to task="
3131 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003132 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Bryce Leeaf691c02017-03-20 14:20:22 -07003133 "Pushing next activity " + p + " out to target's task " + target);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08003134 p.reparent(targetTask, 0 /* position - bottom */, "resetTargetTaskIfNeeded");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003135 }
3136
Wale Ogunwale1666e312016-12-16 11:27:18 -08003137 mWindowContainerController.positionChildAtBottom(
Wale Ogunwale66e16852017-10-19 13:35:52 -07003138 targetTask.getWindowContainerController(), false /* includingParents */);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003139 replyChainEnd = -1;
3140 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
3141 // If the activity should just be removed -- either
3142 // because it asks for it, or the task should be
3143 // cleared -- then finish it and anything that is
3144 // part of its reply chain.
3145 int end;
3146 if (clearWhenTaskReset) {
3147 // In this case, we want to finish this activity
3148 // and everything above it, so be sneaky and pretend
3149 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08003150 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003151 } else if (replyChainEnd < 0) {
3152 end = i;
3153 } else {
3154 end = replyChainEnd;
3155 }
Craig Mautner525f3d92013-05-07 14:01:50 -07003156 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003157 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003158 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003159 if (p.finishing) {
3160 continue;
3161 }
3162 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003163 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003164 topOptions = p.takeOptionsLocked();
3165 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003166 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003167 }
3168 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003169 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08003170 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08003171 if (finishActivityLocked(
3172 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003173 end--;
3174 srcPos--;
3175 }
3176 }
3177 replyChainEnd = -1;
3178 } else {
3179 // If we were in the middle of a chain, well the
3180 // activity that started it all doesn't want anything
3181 // special, so leave it all as-is.
3182 replyChainEnd = -1;
3183 }
3184 }
3185
3186 return topOptions;
3187 }
3188
3189 /**
3190 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
3191 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
3192 * @param affinityTask The task we are looking for an affinity to.
3193 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
3194 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
3195 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
3196 */
Craig Mautner525f3d92013-05-07 14:01:50 -07003197 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08003198 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003199 int replyChainEnd = -1;
3200 final int taskId = task.taskId;
3201 final String taskAffinity = task.affinity;
3202
3203 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
3204 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003205 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
3206
3207 // Do not operate on or below the effective root Activity.
3208 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003209 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003210 if (target.frontOfTask)
3211 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003212
3213 final int flags = target.info.flags;
3214 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3215 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3216
3217 if (target.resultTo != null) {
3218 // If this activity is sending a reply to a previous
3219 // activity, we can't do anything with it now until
3220 // we reach the start of the reply chain.
3221 // XXX note that we are assuming the result is always
3222 // to the previous activity, which is almost always
3223 // the case but we really shouldn't count on.
3224 if (replyChainEnd < 0) {
3225 replyChainEnd = i;
3226 }
3227 } else if (topTaskIsHigher
3228 && allowTaskReparenting
3229 && taskAffinity != null
3230 && taskAffinity.equals(target.taskAffinity)) {
3231 // This activity has an affinity for our task. Either remove it if we are
3232 // clearing or move it over to our task. Note that
3233 // we currently punt on the case where we are resetting a
3234 // task that is not at the top but who has activities above
3235 // with an affinity to it... this is really not a normal
3236 // case, and we will need to later pull that task to the front
3237 // and usually at that point we will do the reset and pick
3238 // up those remaining activities. (This only happens if
3239 // someone starts an activity in a new task from an activity
3240 // in a task that is not currently on top.)
3241 if (forceReset || finishOnTaskLaunch) {
3242 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003243 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3244 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003245 for (int srcPos = start; srcPos >= i; --srcPos) {
3246 final ActivityRecord p = activities.get(srcPos);
3247 if (p.finishing) {
3248 continue;
3249 }
Todd Kennedy539db512014-12-15 09:57:55 -08003250 finishActivityLocked(
3251 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003252 }
3253 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003254 if (taskInsertionPoint < 0) {
3255 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003256
Craig Mautner77878772013-03-04 19:46:24 -08003257 }
Craig Mautner77878772013-03-04 19:46:24 -08003258
3259 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003260 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3261 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3262 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003263 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003264 final ActivityRecord p = activities.get(srcPos);
Winson Chung30480042017-01-26 10:55:34 -08003265 p.reparent(task, taskInsertionPoint, "resetAffinityTaskIfNeededLocked");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003266
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003267 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3268 "Removing and adding activity " + p + " to stack at " + task
3269 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003270 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3271 + " from " + srcPos + " in to resetting task " + task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003272 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08003273 mWindowContainerController.positionChildAtTop(
3274 task.getWindowContainerController(), true /* includingParents */);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003275
3276 // Now we've moved it in to place... but what if this is
3277 // a singleTop activity and we have put it on top of another
3278 // instance of the same activity? Then we drop the instance
3279 // below so it remains singleTop.
3280 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3281 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003282 int targetNdx = taskActivities.indexOf(target);
3283 if (targetNdx > 0) {
3284 ActivityRecord p = taskActivities.get(targetNdx - 1);
3285 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003286 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3287 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003288 }
3289 }
3290 }
3291 }
3292
3293 replyChainEnd = -1;
3294 }
3295 }
Craig Mautner77878772013-03-04 19:46:24 -08003296 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003297 }
3298
Craig Mautner8849a5e2013-04-02 16:41:03 -07003299 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003300 ActivityRecord newActivity) {
Winson Chung5fa39752017-10-04 14:50:15 -07003301 final boolean forceReset =
Craig Mautnere3a74d52013-02-22 14:14:58 -08003302 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
Bryce Leeaf691c02017-03-20 14:20:22 -07003303 final TaskRecord task = taskTop.getTask();
Craig Mautnere3a74d52013-02-22 14:14:58 -08003304
3305 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3306 * for remaining tasks. Used for later tasks to reparent to task. */
3307 boolean taskFound = false;
3308
3309 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3310 ActivityOptions topOptions = null;
3311
Craig Mautner77878772013-03-04 19:46:24 -08003312 // Preserve the location for reparenting in the new task.
3313 int reparentInsertionPoint = -1;
3314
Craig Mautnere3a74d52013-02-22 14:14:58 -08003315 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3316 final TaskRecord targetTask = mTaskHistory.get(i);
3317
3318 if (targetTask == task) {
3319 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3320 taskFound = true;
3321 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003322 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3323 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003324 }
3325 }
3326
Craig Mautner70a86932013-02-28 22:37:44 -08003327 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003328 if (taskNdx >= 0) {
3329 do {
3330 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3331 } while (taskTop == null && taskNdx >= 0);
3332 }
Craig Mautner70a86932013-02-28 22:37:44 -08003333
Craig Mautnere3a74d52013-02-22 14:14:58 -08003334 if (topOptions != null) {
3335 // If we got some ActivityOptions from an activity on top that
3336 // was removed from the task, propagate them to the new real top.
3337 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003338 taskTop.updateOptionsLocked(topOptions);
3339 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003340 topOptions.abort();
3341 }
3342 }
3343
3344 return taskTop;
3345 }
3346
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3348 String resultWho, int requestCode, int resultCode, Intent data) {
3349
3350 if (callingUid > 0) {
3351 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003352 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003353 }
3354
3355 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3356 + " : who=" + resultWho + " req=" + requestCode
3357 + " res=" + resultCode + " data=" + data);
3358 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3359 try {
3360 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3361 list.add(new ResultInfo(resultWho, requestCode,
3362 resultCode, data));
Bryce Leeb0f993f2018-03-02 15:38:01 -08003363 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08003364 ActivityResultItem.obtain(list));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 return;
3366 } catch (Exception e) {
3367 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3368 }
3369 }
3370
3371 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3372 }
3373
Shunta Sato63b8ee32016-07-11 13:32:52 +09003374 /** Returns true if the task is one of the task finishing on-top of the top running task. */
Wale Ogunwale66e16852017-10-19 13:35:52 -07003375 private boolean isATopFinishingTask(TaskRecord task) {
Shunta Sato63b8ee32016-07-11 13:32:52 +09003376 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3377 final TaskRecord current = mTaskHistory.get(i);
3378 final ActivityRecord r = current.topRunningActivityLocked();
3379 if (r != null) {
3380 // We got a top running activity, so there isn't a top finishing task...
3381 return false;
3382 }
3383 if (current == task) {
3384 return true;
3385 }
3386 }
3387 return false;
3388 }
3389
Wale Ogunwale66e16852017-10-19 13:35:52 -07003390 private void adjustFocusedActivityStack(ActivityRecord r, String reason) {
Shunta Sato63b8ee32016-07-11 13:32:52 +09003391 if (!mStackSupervisor.isFocusedStack(this) ||
3392 ((mResumedActivity != r) && (mResumedActivity != null))) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003393 return;
3394 }
3395
3396 final ActivityRecord next = topRunningActivityLocked();
3397 final String myReason = reason + " adjustFocus";
Bryce Lee3345c4e2017-04-25 07:40:41 -07003398
Wale Ogunwale66e16852017-10-19 13:35:52 -07003399 if (next == r) {
3400 mStackSupervisor.moveFocusableActivityStackToFrontLocked(
3401 mStackSupervisor.topRunningActivityLocked(), myReason);
3402 return;
Wale Ogunwaled045c822015-12-02 09:14:28 -08003403 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003404
Wale Ogunwale66e16852017-10-19 13:35:52 -07003405 if (next != null && isFocusable()) {
3406 // Keep focus in stack if we have a top running activity and are focusable.
3407 return;
3408 }
3409
3410 // Task is not guaranteed to be non-null. For example, destroying the
3411 // {@link ActivityRecord} will disassociate the task from the activity.
3412 final TaskRecord task = r.getTask();
3413
3414 if (task == null) {
3415 throw new IllegalStateException("activity no longer associated with task:" + r);
3416 }
3417
3418 // Move focus to next focusable stack if possible.
3419 if (adjustFocusToNextFocusableStack(myReason)) {
3420 return;
3421 }
3422
3423 // Whatever...go home.
3424 mStackSupervisor.moveHomeStackTaskToTop(myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003425 }
3426
Andrii Kulian250d6532017-02-08 23:30:45 -08003427 /** Find next proper focusable stack and make it focused. */
Wale Ogunwale17696de2018-03-15 16:41:05 -07003428 boolean adjustFocusToNextFocusableStack(String reason) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07003429 return adjustFocusToNextFocusableStack(reason, false /* allowFocusSelf */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003430 }
3431
3432 /**
3433 * Find next proper focusable stack and make it focused.
3434 * @param allowFocusSelf Is the focus allowed to remain on the same stack.
3435 */
Wale Ogunwale66e16852017-10-19 13:35:52 -07003436 private boolean adjustFocusToNextFocusableStack(String reason, boolean allowFocusSelf) {
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08003437 final ActivityStack stack =
3438 mStackSupervisor.getNextFocusableStackLocked(this, !allowFocusSelf);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003439 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003440 if (stack == null) {
3441 return false;
3442 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003443
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003444 final ActivityRecord top = stack.topRunningActivityLocked();
3445
Wale Ogunwale66e16852017-10-19 13:35:52 -07003446 if (stack.isActivityTypeHome() && (top == null || !top.visible)) {
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003447 // If we will be focusing on the home stack next and its current top activity isn't
Wale Ogunwale66e16852017-10-19 13:35:52 -07003448 // visible, then use the move the home stack task to top to make the activity visible.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003449 return mStackSupervisor.moveHomeStackTaskToTop(reason);
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003450 }
Chong Zhang6cda19c2016-06-14 19:07:56 -07003451
3452 stack.moveToFront(myReason);
3453 return true;
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003454 }
3455
Craig Mautnerf3333272013-04-22 10:55:53 -07003456 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003457 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003458 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3459 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3460 if (!r.finishing) {
David Stevens9440dc82017-03-16 19:00:20 -07003461 if (!shouldSleepActivities()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003462 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003463 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3464 "stop-no-history", false)) {
Bryce Lee3345c4e2017-04-25 07:40:41 -07003465 // If {@link requestFinishActivityLocked} returns {@code true},
Wale Ogunwale66e16852017-10-19 13:35:52 -07003466 // {@link adjustFocusedActivityStack} would have been already called.
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003467 r.resumeKeyDispatchingLocked();
3468 return;
3469 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003470 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003471 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003472 + " on stop because we're just sleeping");
3473 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003474 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003475 }
3476
3477 if (r.app != null && r.app.thread != null) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07003478 adjustFocusedActivityStack(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003479 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480 try {
3481 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003482 if (DEBUG_STATES) Slog.v(TAG_STATES,
3483 "Moving to STOPPING: " + r + " (stop requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08003484 r.setState(STOPPING, "stopActivityLocked");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003485 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3486 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003487 if (!r.visible) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003488 r.setVisible(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003489 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003490 EventLogTags.writeAmStopActivity(
3491 r.userId, System.identityHashCode(r), r.shortComponentName);
Bryce Leeb0f993f2018-03-02 15:38:01 -08003492 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Bryce Lee1d0d5142018-04-12 10:35:07 -07003493 StopActivityItem.obtain(r.visible, r.configChangeFlags));
David Stevens9440dc82017-03-16 19:00:20 -07003494 if (shouldSleepOrShutDownActivities()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003495 r.setSleeping(true);
3496 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003497 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003498 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003499 } catch (Exception e) {
3500 // Maybe just ignore exceptions here... if the process
3501 // has crashed, our death notification will clean things
3502 // up.
3503 Slog.w(TAG, "Exception thrown during pause", e);
3504 // Just in case, assume it to be stopped.
3505 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003506 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Bryce Lee7ace3952018-02-16 14:34:32 -08003507 r.setState(STOPPED, "stopActivityLocked");
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003508 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003509 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003510 }
3511 }
3512 }
3513 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003514
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003515 /**
3516 * @return Returns true if the activity is being finished, false if for
3517 * some reason it is being left as-is.
3518 */
3519 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003520 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003521 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003522 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3523 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003524 + ", result=" + resultCode + ", data=" + resultData
3525 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003526 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003527 return false;
3528 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003529
Craig Mautnerd44711d2013-02-23 11:24:36 -08003530 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531 return true;
3532 }
3533
Craig Mautnerd2328952013-03-05 12:46:26 -08003534 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003535 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3536 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3537 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3538 ActivityRecord r = activities.get(activityNdx);
3539 if (r.resultTo == self && r.requestCode == requestCode) {
3540 if ((r.resultWho == null && resultWho == null) ||
3541 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3542 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3543 false);
3544 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003545 }
3546 }
3547 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003548 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003549 }
3550
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07003551 /**
3552 * Finish the topmost activity that belongs to the crashed app. We may also finish the activity
3553 * that requested launch of the crashed one to prevent launch-crash loop.
3554 * @param app The app that crashed.
3555 * @param reason Reason to perform this action.
3556 * @return The task that was finished in this stack, {@code null} if top running activity does
3557 * not belong to the crashed app.
3558 */
3559 final TaskRecord finishTopCrashedActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003560 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003561 TaskRecord finishedTask = null;
3562 if (r == null || r.app != app) {
3563 return null;
3564 }
3565 Slog.w(TAG, " Force finishing activity "
3566 + r.intent.getComponent().flattenToShortString());
Bryce Leeaf691c02017-03-20 14:20:22 -07003567 finishedTask = r.getTask();
3568 int taskNdx = mTaskHistory.indexOf(finishedTask);
3569 final TaskRecord task = finishedTask;
3570 int activityNdx = task.mActivities.indexOf(r);
Jorim Jaggi9c52ebb2018-06-01 14:45:24 +02003571 mWindowManager.prepareAppTransition(TRANSIT_CRASHING_ACTIVITY_CLOSE,
3572 false /* alwaysKeepCurrent */);
Adrian Roos20d7df32016-01-12 18:59:43 +01003573 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Bryce Leeaf691c02017-03-20 14:20:22 -07003574 finishedTask = task;
Adrian Roos20d7df32016-01-12 18:59:43 +01003575 // Also terminate any activities below it that aren't yet
3576 // stopped, to avoid a situation where one will get
3577 // re-start our crashing activity once it gets resumed again.
3578 --activityNdx;
3579 if (activityNdx < 0) {
3580 do {
3581 --taskNdx;
3582 if (taskNdx < 0) {
3583 break;
3584 }
3585 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3586 } while (activityNdx < 0);
3587 }
3588 if (activityNdx >= 0) {
3589 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Bryce Lee7ace3952018-02-16 14:34:32 -08003590 if (r.isState(RESUMED, PAUSING, PAUSED)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003591 if (!r.isActivityTypeHome() || mService.mHomeProcess != r.app) {
Adrian Roos20d7df32016-01-12 18:59:43 +01003592 Slog.w(TAG, " Force finishing activity "
3593 + r.intent.getComponent().flattenToShortString());
3594 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003595 }
3596 }
3597 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003598 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003599 }
3600
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003601 final void finishVoiceTask(IVoiceInteractionSession session) {
3602 IBinder sessionBinder = session.asBinder();
3603 boolean didOne = false;
3604 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3605 TaskRecord tr = mTaskHistory.get(taskNdx);
3606 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3607 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3608 ActivityRecord r = tr.mActivities.get(activityNdx);
3609 if (!r.finishing) {
3610 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3611 false);
3612 didOne = true;
3613 }
3614 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003615 } else {
3616 // Check if any of the activities are using voice
3617 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3618 ActivityRecord r = tr.mActivities.get(activityNdx);
3619 if (r.voiceSession != null
3620 && r.voiceSession.asBinder() == sessionBinder) {
3621 // Inform of cancellation
3622 r.clearVoiceSessionLocked();
3623 try {
3624 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3625 null);
3626 } catch (RemoteException re) {
3627 // Ok
3628 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003629 mService.finishRunningVoiceLocked();
3630 break;
3631 }
3632 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003633 }
3634 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003635
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003636 if (didOne) {
3637 mService.updateOomAdjLocked();
3638 }
3639 }
3640
Craig Mautnerd2328952013-03-05 12:46:26 -08003641 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003642 ArrayList<ActivityRecord> activities = r.getTask().mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003643 for (int index = activities.indexOf(r); index >= 0; --index) {
3644 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003645 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003646 break;
3647 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003648 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003649 }
3650 return true;
3651 }
3652
Andrii Kulian21713ac2016-10-12 22:05:05 -07003653 private void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
Dianne Hackborn5c607432012-02-28 14:44:19 -08003654 // send the result
3655 ActivityRecord resultTo = r.resultTo;
3656 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003657 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003658 + " who=" + r.resultWho + " req=" + r.requestCode
3659 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003660 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003661 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003662 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003663 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003664 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003665 if (r.info.applicationInfo.uid > 0) {
3666 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3667 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003668 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003669 }
3670 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3671 resultData);
3672 r.resultTo = null;
3673 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003674 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003675
3676 // Make sure this HistoryRecord is not holding on to other resources,
3677 // because clients have remote IPC references to this object so we
3678 // can't assume that will go away and want to avoid circular IPC refs.
3679 r.results = null;
3680 r.pendingResults = null;
3681 r.newIntents = null;
3682 r.icicle = null;
3683 }
3684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003685 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003686 * See {@link #finishActivityLocked(ActivityRecord, int, Intent, String, boolean, boolean)}
3687 */
3688 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3689 String reason, boolean oomAdj) {
3690 return finishActivityLocked(r, resultCode, resultData, reason, oomAdj, !PAUSE_IMMEDIATELY);
3691 }
3692
3693 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003694 * @return Returns true if this activity has been removed from the history
3695 * list, or false if it is still in the list and will be removed later.
3696 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003697 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
Winson Chung6954fc92017-03-24 16:22:12 -07003698 String reason, boolean oomAdj, boolean pauseImmediately) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003699 if (r.finishing) {
3700 Slog.w(TAG, "Duplicate finish request for " + r);
3701 return false;
3702 }
3703
Jorim Jaggife762342016-10-13 14:33:27 +02003704 mWindowManager.deferSurfaceLayout();
3705 try {
3706 r.makeFinishingLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07003707 final TaskRecord task = r.getTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003708 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3709 r.userId, System.identityHashCode(r),
3710 task.taskId, r.shortComponentName, reason);
3711 final ArrayList<ActivityRecord> activities = task.mActivities;
3712 final int index = activities.indexOf(r);
3713 if (index < (activities.size() - 1)) {
3714 task.setFrontOfTask();
3715 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3716 // If the caller asked that this activity (and all above it)
3717 // be cleared when the task is reset, don't lose that information,
3718 // but propagate it up to the next activity.
3719 ActivityRecord next = activities.get(index+1);
3720 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3721 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003722 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003723
Jorim Jaggife762342016-10-13 14:33:27 +02003724 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003725
Wale Ogunwale66e16852017-10-19 13:35:52 -07003726 adjustFocusedActivityStack(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003727
Jorim Jaggife762342016-10-13 14:33:27 +02003728 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003729
Yorke Leebdef5372017-04-10 16:38:51 -07003730 final boolean endTask = index <= 0 && !task.isClearingToReuseTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003731 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
3732 if (mResumedActivity == r) {
3733 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
3734 "Prepare close transition: finishing " + r);
Winson Chung6954fc92017-03-24 16:22:12 -07003735 if (endTask) {
3736 mService.mTaskChangeNotificationController.notifyTaskRemovalStarted(
3737 task.taskId);
3738 }
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003739 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003740
Jorim Jaggife762342016-10-13 14:33:27 +02003741 // Tell window manager to prepare for this one to be removed.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003742 r.setVisibility(false);
Jorim Jaggife762342016-10-13 14:33:27 +02003743
3744 if (mPausingActivity == null) {
3745 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3746 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3747 "finish() => pause with userLeaving=false");
Winson Chung6954fc92017-03-24 16:22:12 -07003748 startPausingLocked(false, false, null, pauseImmediately);
Jorim Jaggife762342016-10-13 14:33:27 +02003749 }
3750
3751 if (endTask) {
Bryce Lee2b8e0372018-04-05 17:01:37 -07003752 mService.getLockTaskController().clearLockedTask(task);
Jorim Jaggife762342016-10-13 14:33:27 +02003753 }
Bryce Lee7ace3952018-02-16 14:34:32 -08003754 } else if (!r.isState(PAUSING)) {
Jorim Jaggife762342016-10-13 14:33:27 +02003755 // If the activity is PAUSING, we will complete the finish once
3756 // it is done pausing; else we can just directly finish it here.
3757 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
3758 if (r.visible) {
Winson Chung6954fc92017-03-24 16:22:12 -07003759 prepareActivityHideTransitionAnimation(r, transit);
3760 }
3761
3762 final int finishMode = (r.visible || r.nowVisible) ? FINISH_AFTER_VISIBLE
3763 : FINISH_AFTER_PAUSE;
Bryce Leef52974c2018-02-14 15:12:01 -08003764 final boolean removedActivity = finishCurrentActivityLocked(r, finishMode, oomAdj,
3765 "finishActivityLocked") == null;
Winson Chung6954fc92017-03-24 16:22:12 -07003766
3767 // The following code is an optimization. When the last non-task overlay activity
3768 // is removed from the task, we remove the entire task from the stack. However,
3769 // since that is done after the scheduled destroy callback from the activity, that
3770 // call to change the visibility of the task overlay activities would be out of
3771 // sync with the activitiy visibility being set for this finishing activity above.
3772 // In this case, we can set the visibility of all the task overlay activities when
3773 // we detect the last one is finishing to keep them in sync.
3774 if (task.onlyHasTaskOverlayActivities(true /* excludeFinishing */)) {
3775 for (ActivityRecord taskOverlay : task.mActivities) {
3776 if (!taskOverlay.mTaskOverlay) {
3777 continue;
3778 }
3779 prepareActivityHideTransitionAnimation(taskOverlay, transit);
Jorim Jaggife762342016-10-13 14:33:27 +02003780 }
3781 }
Winson Chung6954fc92017-03-24 16:22:12 -07003782 return removedActivity;
Jorim Jaggife762342016-10-13 14:33:27 +02003783 } else {
3784 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
3785 }
3786
3787 return false;
3788 } finally {
3789 mWindowManager.continueSurfaceLayout();
3790 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003791 }
3792
Winson Chung6954fc92017-03-24 16:22:12 -07003793 private void prepareActivityHideTransitionAnimation(ActivityRecord r, int transit) {
3794 mWindowManager.prepareAppTransition(transit, false);
3795 r.setVisibility(false);
3796 mWindowManager.executeAppTransition();
Bryce Lee4a194382017-04-04 14:32:48 -07003797 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(r)) {
3798 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(r);
Winson Chung6954fc92017-03-24 16:22:12 -07003799 }
3800 }
3801
Craig Mautnerf3333272013-04-22 10:55:53 -07003802 static final int FINISH_IMMEDIATELY = 0;
3803 static final int FINISH_AFTER_PAUSE = 1;
3804 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003805
Bryce Leef52974c2018-02-14 15:12:01 -08003806 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj,
3807 String reason) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808 // First things first: if this activity is currently visible,
3809 // and the resumed activity is not yet visible, then hold off on
3810 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003811
Bryce Leec961e0a2018-04-13 17:58:02 -07003812 // The activity that we are finishing may be over the lock screen. In this case, we do not
3813 // want to consider activities that cannot be shown on the lock screen as running and should
3814 // proceed with finishing the activity if there is no valid next top running activity.
3815 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked(
3816 true /* considerKeyguardState */);
Chong Zhang824b6dc2016-04-27 14:11:12 -07003817
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003818 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003819 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003820 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Winson Chung4dabf232017-01-25 13:25:22 -08003821 addToStopping(r, false /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003823 if (DEBUG_STATES) Slog.v(TAG_STATES,
3824 "Moving to STOPPING: "+ r + " (finish requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08003825 r.setState(STOPPING, "finishCurrentActivityLocked");
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003826 if (oomAdj) {
3827 mService.updateOomAdjLocked();
3828 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003829 return r;
3830 }
3831
3832 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003833 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003834 mStackSupervisor.mGoingToSleepActivities.remove(r);
Bryce Lee4a194382017-04-04 14:32:48 -07003835 mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(r);
Bryce Lee7ace3952018-02-16 14:34:32 -08003836 final ActivityState prevState = r.getState();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003837 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08003838
Bryce Lee7ace3952018-02-16 14:34:32 -08003839 r.setState(FINISHING, "finishCurrentActivityLocked");
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003840 final boolean finishingActivityInNonFocusedStack
Andrii Kulian02b7a832016-10-06 23:11:56 -07003841 = r.getStack() != mStackSupervisor.getFocusedStack()
Bryce Lee7ace3952018-02-16 14:34:32 -08003842 && prevState == PAUSED && mode == FINISH_AFTER_VISIBLE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003843
3844 if (mode == FINISH_IMMEDIATELY
Bryce Lee7ace3952018-02-16 14:34:32 -08003845 || (prevState == PAUSED
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003846 && (mode == FINISH_AFTER_PAUSE || inPinnedWindowingMode()))
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003847 || finishingActivityInNonFocusedStack
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003848 || prevState == STOPPING
3849 || prevState == STOPPED
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003850 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003851 r.makeFinishingLocked();
Bryce Leef52974c2018-02-14 15:12:01 -08003852 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm:" + reason);
Andrii Kulian7318d632016-07-20 18:59:28 -07003853
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003854 if (finishingActivityInNonFocusedStack) {
3855 // Finishing activity that was in paused state and it was in not currently focused
3856 // stack, need to make something visible in its place.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003857 mStackSupervisor.ensureVisibilityAndConfig(next, mDisplayId,
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003858 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Andrii Kulian7318d632016-07-20 18:59:28 -07003859 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003860 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003861 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003862 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003863 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003864 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3865 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003866 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003867 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003868
3869 // Need to go through the full pause cycle to get this
3870 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003871 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003872 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003873 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003874 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003875 return r;
3876 }
3877
Craig Mautneree36c772014-07-16 14:56:05 -07003878 void finishAllActivitiesLocked(boolean immediately) {
3879 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003880 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3881 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3882 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3883 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003884 noActivitiesInStack = false;
3885 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003886 continue;
3887 }
Craig Mautneree36c772014-07-16 14:56:05 -07003888 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Bryce Leef52974c2018-02-14 15:12:01 -08003889 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false,
3890 "finishAllActivitiesLocked");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003891 }
3892 }
Craig Mautneree36c772014-07-16 14:56:05 -07003893 if (noActivitiesInStack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003894 remove();
Craig Mautneree36c772014-07-16 14:56:05 -07003895 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003896 }
3897
Wale Ogunwale66e16852017-10-19 13:35:52 -07003898 /** @return true if the stack behind this one is a standard activity type. */
3899 boolean inFrontOfStandardStack() {
3900 final ActivityDisplay display = getDisplay();
3901 if (display == null) {
3902 return false;
3903 }
3904 final int index = display.getIndexOf(this);
3905 if (index == 0) {
3906 return false;
3907 }
3908 final ActivityStack stackBehind = display.getChildAt(index - 1);
3909 return stackBehind.isActivityTypeStandard();
3910 }
3911
3912 boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003913 // Basic case: for simple app-centric recents, we need to recreate
3914 // the task if the affinity has changed.
Bryce Leeaf691c02017-03-20 14:20:22 -07003915 if (srec == null || srec.getTask().affinity == null ||
3916 !srec.getTask().affinity.equals(destAffinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003917 return true;
3918 }
3919 // Document-centric case: an app may be split in to multiple documents;
3920 // they need to re-create their task if this current activity is the root
3921 // of a document, unless simply finishing it will return them to the the
3922 // correct app behind.
Bryce Leeaf691c02017-03-20 14:20:22 -07003923 final TaskRecord task = srec.getTask();
Wale Ogunwale66e16852017-10-19 13:35:52 -07003924 if (srec.frontOfTask && task.getBaseIntent() != null && task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003925 // Okay, this activity is at the root of its task. What to do, what to do...
Wale Ogunwale66e16852017-10-19 13:35:52 -07003926 if (!inFrontOfStandardStack()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003927 // Finishing won't return to an application, so we need to recreate.
3928 return true;
3929 }
3930 // We now need to get the task below it to determine what to do.
Bryce Leeaf691c02017-03-20 14:20:22 -07003931 int taskIdx = mTaskHistory.indexOf(task);
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003932 if (taskIdx <= 0) {
3933 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3934 return false;
3935 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003936 final TaskRecord prevTask = mTaskHistory.get(taskIdx);
Bryce Leeaf691c02017-03-20 14:20:22 -07003937 if (!task.affinity.equals(prevTask.affinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003938 // These are different apps, so need to recreate.
3939 return true;
3940 }
3941 }
3942 return false;
3943 }
3944
Wale Ogunwale7d701172015-03-11 15:36:30 -07003945 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003946 Intent resultData) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003947 final TaskRecord task = srec.getTask();
Craig Mautner0247fc82013-02-28 14:32:06 -08003948 final ArrayList<ActivityRecord> activities = task.mActivities;
3949 final int start = activities.indexOf(srec);
3950 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003951 return false;
3952 }
3953 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003954 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003955 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003956 final ComponentName dest = destIntent.getComponent();
3957 if (start > 0 && dest != null) {
3958 for (int i = finishTo; i >= 0; i--) {
3959 ActivityRecord r = activities.get(i);
3960 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003961 r.info.name.equals(dest.getClassName())) {
3962 finishTo = i;
3963 parent = r;
3964 foundParentInTask = true;
3965 break;
3966 }
3967 }
3968 }
3969
3970 IActivityController controller = mService.mController;
3971 if (controller != null) {
3972 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3973 if (next != null) {
3974 // ask watcher if this is allowed
3975 boolean resumeOK = true;
3976 try {
3977 resumeOK = controller.activityResuming(next.packageName);
3978 } catch (RemoteException e) {
3979 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003980 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003981 }
3982
3983 if (!resumeOK) {
3984 return false;
3985 }
3986 }
3987 }
3988 final long origId = Binder.clearCallingIdentity();
3989 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003990 ActivityRecord r = activities.get(i);
3991 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003992 // Only return the supplied result for the first activity finished
3993 resultCode = Activity.RESULT_CANCELED;
3994 resultData = null;
3995 }
3996
3997 if (parent != null && foundParentInTask) {
3998 final int parentLaunchMode = parent.info.launchMode;
3999 final int destIntentFlags = destIntent.getFlags();
4000 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
4001 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
4002 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
4003 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004004 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
4005 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004006 } else {
4007 try {
4008 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -07004009 destIntent.getComponent(), ActivityManagerService.STOCK_PM_FLAGS,
4010 srec.userId);
Bryce Lee4c9a5972017-12-01 22:14:24 -08004011 // TODO(b/64750076): Check if calling pid should really be -1.
4012 final int res = mService.getActivityStartController()
4013 .obtainStarter(destIntent, "navigateUpTo")
4014 .setCaller(srec.app.thread)
4015 .setActivityInfo(aInfo)
4016 .setResultTo(parent.appToken)
4017 .setCallingPid(-1)
4018 .setCallingUid(parent.launchedFromUid)
4019 .setCallingPackage(parent.launchedFromPackage)
4020 .setRealCallingPid(-1)
4021 .setRealCallingUid(parent.launchedFromUid)
4022 .setComponentSpecified(true)
4023 .execute();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004024 foundParentInTask = res == ActivityManager.START_SUCCESS;
4025 } catch (RemoteException e) {
4026 foundParentInTask = false;
4027 }
4028 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08004029 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004030 }
4031 }
4032 Binder.restoreCallingIdentity(origId);
4033 return foundParentInTask;
4034 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004035
4036 /**
4037 * Remove any state associated with the {@link ActivityRecord}. This should be called whenever
4038 * an activity moves away from the stack.
4039 */
4040 void onActivityRemovedFromStack(ActivityRecord r) {
Bryce Lee84730a02018-04-03 14:10:04 -07004041 removeTimeoutsForActivityLocked(r);
4042
4043 if (mResumedActivity != null && mResumedActivity == r) {
4044 setResumedActivity(null, "onActivityRemovedFromStack");
Bryce Leeaf691c02017-03-20 14:20:22 -07004045 }
Bryce Lee84730a02018-04-03 14:10:04 -07004046 if (mPausingActivity != null && mPausingActivity == r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004047 mPausingActivity = null;
4048 }
Bryce Lee84730a02018-04-03 14:10:04 -07004049 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004050
Bryce Lee84730a02018-04-03 14:10:04 -07004051 void onActivityAddedToStack(ActivityRecord r) {
4052 if(r.getState() == RESUMED) {
4053 setResumedActivity(r, "onActivityAddedToStack");
4054 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004055 }
4056
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004057 /**
4058 * Perform the common clean-up of an activity record. This is called both
4059 * as part of destroyActivityLocked() (when destroying the client-side
4060 * representation) and cleaning things up as a result of its hosting
4061 * processing going away, in which case there is no remaining client-side
4062 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08004063 *
4064 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004065 */
Bryce Leec9406602018-03-09 11:02:10 -08004066 private void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices, boolean setState) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004067 onActivityRemovedFromStack(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004068
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004069 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004070 r.frozenBeforeDestroy = false;
4071
Bryce Leec9406602018-03-09 11:02:10 -08004072 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004073 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004074 r.setState(DESTROYED, "cleanupActivityLocked");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004075 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004076 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004077 }
4078
Bryce Lee4a194382017-04-04 14:32:48 -07004079 // Inform supervisor the activity has been removed.
4080 mStackSupervisor.cleanupActivity(r);
4081
Craig Mautner2420ead2013-04-01 17:13:20 -07004082
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004083 // Remove any pending results.
4084 if (r.finishing && r.pendingResults != null) {
4085 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
4086 PendingIntentRecord rec = apr.get();
4087 if (rec != null) {
4088 mService.cancelIntentSenderLocked(rec, false);
4089 }
4090 }
4091 r.pendingResults = null;
4092 }
4093
4094 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07004095 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004096 }
4097
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004098 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004099 removeTimeoutsForActivityLocked(r);
Wale Ogunwale8fd75422016-06-24 14:20:37 -07004100 // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
4101 // manager so it can update its bookkeeping.
4102 mWindowManager.notifyAppRelaunchesCleared(r.appToken);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004103 }
4104
Winson Chung4dabf232017-01-25 13:25:22 -08004105 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004106 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004107 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004108 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004109 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07004110 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004111 }
4112
Chong Zhangbffd8892016-08-08 11:16:06 -07004113 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004114 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07004115 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004116 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
4117 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
4118
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004119 r.takeFromHistory();
4120 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004121 if (DEBUG_STATES) Slog.v(TAG_STATES,
4122 "Moving to DESTROYED: " + r + " (removed from history)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004123 r.setState(DESTROYED, "removeActivityFromHistoryLocked");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004124 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004125 r.app = null;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08004126 r.removeWindowContainer();
Bryce Leeaf691c02017-03-20 14:20:22 -07004127 final TaskRecord task = r.getTask();
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004128 final boolean lastActivity = task != null ? task.removeActivity(r) : false;
Winson Chung6954fc92017-03-24 16:22:12 -07004129 // If we are removing the last activity in the task, not including task overlay activities,
4130 // then fall through into the block below to remove the entire task itself
4131 final boolean onlyHasTaskOverlays = task != null
4132 ? task.onlyHasTaskOverlayActivities(false /* excludingFinishing */) : false;
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004133
Winson Chung6954fc92017-03-24 16:22:12 -07004134 if (lastActivity || onlyHasTaskOverlays) {
4135 if (DEBUG_STACK) {
4136 Slog.i(TAG_STACK,
4137 "removeActivityFromHistoryLocked: last activity removed from " + this
4138 + " onlyHasTaskOverlays=" + onlyHasTaskOverlays);
4139 }
4140
Bryce Leed6d26752017-05-25 13:57:46 -07004141 // The following block can be executed multiple times if there is more than one overlay.
4142 // {@link ActivityStackSupervisor#removeTaskByIdLocked} handles this by reverse lookup
4143 // of the task by id and exiting early if not found.
Winson Chunge3c21e02017-04-11 18:02:17 -07004144 if (onlyHasTaskOverlays) {
4145 // When destroying a task, tell the supervisor to remove it so that any activity it
4146 // has can be cleaned up correctly. This is currently the only place where we remove
4147 // a task with the DESTROYING mode, so instead of passing the onlyHasTaskOverlays
4148 // state into removeTask(), we just clear the task here before the other residual
4149 // work.
4150 // TODO: If the callers to removeTask() changes such that we have multiple places
4151 // where we are destroying the task, move this back into removeTask()
4152 mStackSupervisor.removeTaskByIdLocked(task.taskId, false /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07004153 !REMOVE_FROM_RECENTS, PAUSE_IMMEDIATELY, reason);
Winson Chunge3c21e02017-04-11 18:02:17 -07004154 }
Bryce Lee1d930582017-05-01 08:35:24 -07004155
Bryce Leed6d26752017-05-25 13:57:46 -07004156 // We must keep the task around until all activities are destroyed. The following
4157 // statement will only execute once since overlays are also considered activities.
Bryce Lee1d930582017-05-01 08:35:24 -07004158 if (lastActivity) {
4159 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
4160 }
Craig Mautner312ba862014-02-10 17:55:01 -08004161 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004162 cleanUpActivityServicesLocked(r);
4163 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004164 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004165
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004166 /**
4167 * Perform clean-up of service connections in an activity record.
4168 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07004169 private void cleanUpActivityServicesLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004170 // Throw away any services that have been bound by this activity.
4171 if (r.connections != null) {
4172 Iterator<ConnectionRecord> it = r.connections.iterator();
4173 while (it.hasNext()) {
4174 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004175 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004176 }
4177 r.connections = null;
4178 }
4179 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004180
Craig Mautneree2e45a2014-06-27 12:10:03 -07004181 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004182 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004183 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004184 mHandler.sendMessage(msg);
4185 }
4186
Andrii Kulian21713ac2016-10-12 22:05:05 -07004187 private void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004188 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004189 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004190 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4191 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4192 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4193 final ActivityRecord r = activities.get(activityNdx);
4194 if (r.finishing) {
4195 continue;
4196 }
4197 if (r.fullscreen) {
4198 lastIsOpaque = true;
4199 }
4200 if (owner != null && r.app != owner) {
4201 continue;
4202 }
4203 if (!lastIsOpaque) {
4204 continue;
4205 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004206 if (r.isDestroyable()) {
Bryce Lee7ace3952018-02-16 14:34:32 -08004207 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r
4208 + " in state " + r.getState()
Craig Mautnerd44711d2013-02-23 11:24:36 -08004209 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004210 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004211 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004212 activityRemoved = true;
4213 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004214 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004215 }
4216 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004217 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004218 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004219 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004220 }
4221
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004222 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
4223 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004224 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Bryce Lee7ace3952018-02-16 14:34:32 -08004225 "Destroying " + r + " in state " + r.getState() + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004226 + " pausing=" + mPausingActivity + " for reason " + reason);
4227 return destroyActivityLocked(r, true, reason);
4228 }
4229 return false;
4230 }
4231
4232 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
4233 String reason) {
4234 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004235 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004236 int maxTasks = tasks.size() / 4;
4237 if (maxTasks < 1) {
4238 maxTasks = 1;
4239 }
4240 int numReleased = 0;
4241 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
4242 final TaskRecord task = mTaskHistory.get(taskNdx);
4243 if (!tasks.contains(task)) {
4244 continue;
4245 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004246 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004247 int curNum = 0;
4248 final ArrayList<ActivityRecord> activities = task.mActivities;
4249 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
4250 final ActivityRecord activity = activities.get(actNdx);
4251 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004252 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Bryce Lee7ace3952018-02-16 14:34:32 -08004253 + " in state " + activity.getState() + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004254 + " pausing=" + mPausingActivity + " for reason " + reason);
4255 destroyActivityLocked(activity, true, reason);
4256 if (activities.get(actNdx) != activity) {
4257 // Was removed from list, back up so we don't miss the next one.
4258 actNdx--;
4259 }
4260 curNum++;
4261 }
4262 }
4263 if (curNum > 0) {
4264 numReleased += curNum;
4265 maxTasks--;
4266 if (mTaskHistory.get(taskNdx) != task) {
4267 // The entire task got removed, back up so we don't miss the next one.
4268 taskNdx--;
4269 }
4270 }
4271 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004272 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
4273 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004274 return numReleased;
4275 }
4276
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004277 /**
4278 * Destroy the current CLIENT SIDE instance of an activity. This may be
4279 * called both when actually finishing an activity, or when performing
4280 * a configuration switch where we destroy the current client-side object
4281 * but then create a new client-side object for this same HistoryRecord.
4282 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07004283 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004284 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
4285 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004286 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Bryce Leeb0f993f2018-03-02 15:38:01 -08004287
4288 if (r.isState(DESTROYING, DESTROYED)) {
Bryce Leec9406602018-03-09 11:02:10 -08004289 if (DEBUG_STATES) Slog.v(TAG_STATES, "activity " + r + " already destroying."
Bryce Leeb0f993f2018-03-02 15:38:01 -08004290 + "skipping request with reason:" + reason);
4291 return false;
4292 }
4293
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004294 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004295 r.userId, System.identityHashCode(r),
Bryce Leeaf691c02017-03-20 14:20:22 -07004296 r.getTask().taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004297
4298 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004299
Bryce Leec9406602018-03-09 11:02:10 -08004300 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004301
4302 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004303
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004304 if (hadApp) {
4305 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004306 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004307 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
4308 mService.mHeavyWeightProcess = null;
4309 mService.mHandler.sendEmptyMessage(
4310 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
4311 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004312 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07004313 // Update any services we are bound to that might care about whether
4314 // their client may have activities.
4315 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07004316 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07004317 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07004318 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004319 }
4320 }
4321
4322 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004323
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004324 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004325 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08004326 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Bryce Lee1d0d5142018-04-12 10:35:07 -07004327 DestroyActivityItem.obtain(r.finishing, r.configChangeFlags));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004328 } catch (Exception e) {
4329 // We can just ignore exceptions here... if the process
4330 // has crashed, our death notification will clean things
4331 // up.
4332 //Slog.w(TAG, "Exception thrown during finish", e);
4333 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004334 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004335 removedFromHistory = true;
4336 skipDestroy = true;
4337 }
4338 }
4339
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004340 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004341
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004342 // If the activity is finishing, we need to wait on removing it
4343 // from the list to give it a chance to do its cleanup. During
4344 // that time it may make calls back with its token so we need to
4345 // be able to find it on the list and so we don't want to remove
4346 // it from the list yet. Otherwise, we can just immediately put
4347 // it in the destroyed state since we are not removing it from the
4348 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004349 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004350 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004351 + " (destroy requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004352 r.setState(DESTROYING,
4353 "destroyActivityLocked. finishing and not skipping destroy");
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07004354 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004355 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4356 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004357 if (DEBUG_STATES) Slog.v(TAG_STATES,
4358 "Moving to DESTROYED: " + r + " (destroy skipped)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004359 r.setState(DESTROYED,
4360 "destroyActivityLocked. not finishing or skipping destroy");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004361 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004362 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004363 }
4364 } else {
4365 // remove this record from the history.
4366 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004367 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004368 removedFromHistory = true;
4369 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004370 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004371 r.setState(DESTROYED, "destroyActivityLocked. not finishing and had no app");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004372 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004373 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004374 }
4375 }
4376
4377 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004378
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004379 if (!mLRUActivities.remove(r) && hadApp) {
4380 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4381 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004382
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004383 return removedFromHistory;
4384 }
4385
Craig Mautner299f9602015-01-26 09:47:33 -08004386 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004387 final long origId = Binder.clearCallingIdentity();
4388 try {
Bryce Lee7ace3952018-02-16 14:34:32 -08004389 activityDestroyedLocked(ActivityRecord.forTokenLocked(token), reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004390 } finally {
4391 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004392 }
4393 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004394
Bryce Leec9406602018-03-09 11:02:10 -08004395 /**
4396 * This method is to only be called from the client via binder when the activity is destroyed
4397 * AND finished.
4398 */
Bryce Lee7ace3952018-02-16 14:34:32 -08004399 final void activityDestroyedLocked(ActivityRecord record, String reason) {
4400 if (record != null) {
4401 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, record);
4402 }
4403
4404 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + record);
4405
4406 if (isInStackLocked(record) != null) {
4407 if (record.isState(DESTROYING, DESTROYED)) {
Bryce Leec9406602018-03-09 11:02:10 -08004408 cleanUpActivityLocked(record, true, false);
Bryce Lee7ace3952018-02-16 14:34:32 -08004409 removeActivityFromHistoryLocked(record, reason);
4410 }
4411 }
4412
4413 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4414 }
4415
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004416 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4417 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004418 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004419 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4420 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004421 while (i > 0) {
4422 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004423 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004424 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004425 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004426 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004427 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004428 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004429 }
4430 }
4431 }
4432
Andrii Kulian21713ac2016-10-12 22:05:05 -07004433 private boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004434 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004435 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4436 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004437 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4438 "mGoingToSleepActivities");
Bryce Lee4a194382017-04-04 14:32:48 -07004439 removeHistoryRecordsForAppLocked(mStackSupervisor.mActivitiesWaitingForVisibleActivity, app,
4440 "mActivitiesWaitingForVisibleActivity");
Craig Mautnerf3333272013-04-22 10:55:53 -07004441 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4442 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004443
4444 boolean hasVisibleActivities = false;
4445
4446 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004447 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004448 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4449 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004450 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4451 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Bryce Leefbd263b42018-03-07 10:33:55 -08004452 mTmpActivities.clear();
4453 mTmpActivities.addAll(activities);
4454
4455 while (!mTmpActivities.isEmpty()) {
4456 final int targetIndex = mTmpActivities.size() - 1;
4457 final ActivityRecord r = mTmpActivities.remove(targetIndex);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004458 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
Bryce Leefbd263b42018-03-07 10:33:55 -08004459 "Record #" + targetIndex + " " + r + ": app=" + r.app);
4460
Craig Mautner0247fc82013-02-28 14:32:06 -08004461 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004462 if (r.visible) {
4463 hasVisibleActivities = true;
4464 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004465 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004466 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4467 // Don't currently have state for the activity, or
4468 // it is finishing -- always remove it.
4469 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004470 } else if (!r.visible && r.launchCount > 2 &&
4471 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004472 // We have launched this activity too many times since it was
4473 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004474 // (Note if the activity is visible, we don't remove the record.
4475 // We leave the dead window on the screen but the process will
4476 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004477 remove = true;
4478 } else {
4479 // The process may be gone, but the activity lives on!
4480 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004481 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004482 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004483 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4484 "Removing activity " + r + " from stack at " + i
4485 + ": haveState=" + r.haveState
4486 + " stateNotNeeded=" + r.stateNotNeeded
4487 + " finishing=" + r.finishing
Bryce Lee7ace3952018-02-16 14:34:32 -08004488 + " state=" + r.getState() + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004489 if (!r.finishing) {
4490 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4491 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4492 r.userId, System.identityHashCode(r),
Bryce Leeaf691c02017-03-20 14:20:22 -07004493 r.getTask().taskId, r.shortComponentName,
Craig Mautner0247fc82013-02-28 14:32:06 -08004494 "proc died without state saved");
Bryce Lee7ace3952018-02-16 14:34:32 -08004495 if (r.getState() == RESUMED) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07004496 mService.updateUsageStats(r, false);
4497 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004498 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004499 } else {
4500 // We have the current state for this activity, so
4501 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004502 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4503 if (DEBUG_APP) Slog.v(TAG_APP,
4504 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004505 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004506 // Set nowVisible to previous visible state. If the app was visible while
4507 // it died, we leave the dead window on screen so it's basically visible.
4508 // This is needed when user later tap on the dead window, we need to stop
4509 // other apps when user transfers focus to the restarted activity.
4510 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004511 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004512 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004513 "App died, clearing saved state of " + r);
4514 r.icicle = null;
4515 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004516 }
Bryce Leec9406602018-03-09 11:02:10 -08004517 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004518 if (remove) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004519 removeActivityFromHistoryLocked(r, "appDied");
Craig Mautneracebdc82015-02-24 10:53:03 -08004520 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004521 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004522 }
4523 }
4524
4525 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004526 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004527
chaviw0d562bf2018-03-15 14:24:14 -07004528 private void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004529 if (options != null) {
chaviw0d562bf2018-03-15 14:24:14 -07004530 ActivityRecord r = topRunningActivityLocked();
4531 if (r != null && !r.isState(RESUMED)) {
4532 r.updateOptionsLocked(options);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004533 } else {
4534 ActivityOptions.abort(options);
4535 }
4536 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004537 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004538 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004539
Andrii Kulian21713ac2016-10-12 22:05:05 -07004540 private void updateTaskMovement(TaskRecord task, boolean toFront) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004541 if (task.isPersistable) {
4542 task.mLastTimeMoved = System.currentTimeMillis();
4543 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4544 // recently will be most negative, tasks sent to the bottom before that will be less
4545 // negative. Similarly for recent tasks moved to the top which will be most positive.
4546 if (!toFront) {
4547 task.mLastTimeMoved *= -1;
4548 }
4549 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004550 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004551 }
4552
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004553 void moveHomeStackTaskToTop() {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004554 if (!isActivityTypeHome()) {
4555 throw new IllegalStateException("Calling moveHomeStackTaskToTop() on non-home stack: "
4556 + this);
4557 }
Craig Mautnera82aa092013-09-13 15:34:08 -07004558 final int top = mTaskHistory.size() - 1;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004559 if (top >= 0) {
4560 final TaskRecord task = mTaskHistory.get(top);
4561 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4562 "moveHomeStackTaskToTop: moving " + task);
4563 mTaskHistory.remove(top);
4564 mTaskHistory.add(top, task);
4565 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004566 }
4567 }
4568
chaviw0d562bf2018-03-15 14:24:14 -07004569 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
4570 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004571 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004572
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004573 final ActivityStack topStack = getDisplay().getTopStack();
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004574 final ActivityRecord topActivity = topStack != null ? topStack.getTopActivity() : null;
Craig Mautner11bf9a52013-02-19 14:08:51 -08004575 final int numTasks = mTaskHistory.size();
4576 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004577 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004578 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004579 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004580 ActivityOptions.abort(options);
4581 } else {
chaviw0d562bf2018-03-15 14:24:14 -07004582 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004583 }
4584 return;
4585 }
4586
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004587 if (timeTracker != null) {
4588 // The caller wants a time tracker associated with this task.
4589 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4590 tr.mActivities.get(i).appTimeTracker = timeTracker;
4591 }
4592 }
4593
Chavi Weingarten3a748552018-05-14 17:32:42 +00004594 try {
4595 // Defer updating the IME target since the new IME target will try to get computed
4596 // before updating all closing and opening apps, which can cause the ime target to
4597 // get calculated incorrectly.
4598 getDisplay().deferUpdateImeTarget();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004599
Chavi Weingarten3a748552018-05-14 17:32:42 +00004600 // Shift all activities with this task up to the top
4601 // of the stack, keeping them in the same internal order.
4602 insertTaskAtTop(tr, null);
4603
4604 // Don't refocus if invisible to current user
4605 final ActivityRecord top = tr.getTopActivity();
4606 if (top == null || !top.okToShowLocked()) {
4607 if (top != null) {
4608 mStackSupervisor.mRecentTasks.add(top.getTask());
4609 }
4610 ActivityOptions.abort(options);
4611 return;
Winson Chung1dbc8112017-09-28 18:05:31 -07004612 }
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004613
Chavi Weingarten3a748552018-05-14 17:32:42 +00004614 // Set focus to the top running activity of this stack.
4615 final ActivityRecord r = topRunningActivityLocked();
4616 mStackSupervisor.moveFocusableActivityStackToFrontLocked(r, reason);
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004617
Chavi Weingarten3a748552018-05-14 17:32:42 +00004618 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
4619 if (noAnimation) {
4620 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
4621 if (r != null) {
4622 mStackSupervisor.mNoAnimActivities.add(r);
4623 }
4624 ActivityOptions.abort(options);
4625 } else {
4626 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004627 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004628 // If a new task is moved to the front, then mark the existing top activity as
4629 // supporting
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004630
Chavi Weingarten3a748552018-05-14 17:32:42 +00004631 // picture-in-picture while paused only if the task would not be considered an oerlay
4632 // on top
4633 // of the current activity (eg. not fullscreen, or the assistant)
4634 if (canEnterPipOnTaskSwitch(topActivity, tr, null /* toFrontActivity */,
4635 options)) {
4636 topActivity.supportsEnterPipOnTaskSwitch = true;
4637 }
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004638
Chavi Weingarten3a748552018-05-14 17:32:42 +00004639 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4640 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
4641
4642 mService.mTaskChangeNotificationController.notifyTaskMovedToFront(tr.taskId);
4643 } finally {
4644 getDisplay().continueUpdateImeTarget();
4645 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004646 }
4647
4648 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004649 * Worker method for rearranging history stack. Implements the function of moving all
4650 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004651 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004652 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004653 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4654 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004655 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004656 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004657 * @return Returns true if the move completed, false if not.
4658 */
Craig Mautner299f9602015-01-26 09:47:33 -08004659 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004660 final TaskRecord tr = taskForIdLocked(taskId);
4661 if (tr == null) {
4662 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4663 return false;
4664 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004665 Slog.i(TAG, "moveTaskToBack: " + tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004666
Charles Heff9b4dff2017-09-22 10:18:37 +01004667 // In LockTask mode, moving a locked task to the back of the stack may expose unlocked
4668 // ones. Therefore we need to check if this operation is allowed.
Bryce Lee2b8e0372018-04-05 17:01:37 -07004669 if (!mService.getLockTaskController().canMoveTaskToBack(tr)) {
Winson Chung261c0f32017-03-07 16:54:31 -08004670 return false;
4671 }
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004673 // If we have a watcher, preflight the move before committing to it. First check
4674 // for *other* available tasks, but if none are available, then try again allowing the
4675 // current task to be selected.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004676 if (isTopStackOnDisplay() && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004677 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004678 if (next == null) {
4679 next = topRunningActivityLocked(null, 0);
4680 }
4681 if (next != null) {
4682 // ask watcher if this is allowed
4683 boolean moveOK = true;
4684 try {
4685 moveOK = mService.mController.activityResuming(next.packageName);
4686 } catch (RemoteException e) {
4687 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004688 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004689 }
4690 if (!moveOK) {
4691 return false;
4692 }
4693 }
4694 }
4695
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004696 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004697
Wale Ogunwale66e16852017-10-19 13:35:52 -07004698 mTaskHistory.remove(tr);
4699 mTaskHistory.add(0, tr);
4700 updateTaskMovement(tr, false);
Wale Ogunwale42709242015-08-11 13:54:42 -07004701
Wale Ogunwale66e16852017-10-19 13:35:52 -07004702 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Evan Rosky9c448172017-11-02 14:19:27 -07004703 moveToBack("moveTaskToBackLocked", tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004704
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004705 if (inPinnedWindowingMode()) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004706 mStackSupervisor.removeStack(this);
Winson Chung261c0f32017-03-07 16:54:31 -08004707 return true;
4708 }
4709
Wale Ogunwaled046a012015-12-24 13:05:59 -08004710 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004711 return true;
4712 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004713
Andrii Kulian21713ac2016-10-12 22:05:05 -07004714 static void logStartActivity(int tag, ActivityRecord r, TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004715 final Uri data = r.intent.getData();
4716 final String strData = data != null ? data.toSafeString() : null;
4717
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004718 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004719 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004720 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004721 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004722 }
4723
4724 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004725 * Ensures all visible activities at or below the input activity have the right configuration.
4726 */
4727 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4728 if (start == null || !start.visible) {
4729 return;
4730 }
4731
Bryce Leeaf691c02017-03-20 14:20:22 -07004732 final TaskRecord startTask = start.getTask();
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004733 boolean behindFullscreen = false;
4734 boolean updatedConfig = false;
4735
4736 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4737 final TaskRecord task = mTaskHistory.get(taskIndex);
4738 final ArrayList<ActivityRecord> activities = task.mActivities;
4739 int activityIndex =
Bryce Leeaf691c02017-03-20 14:20:22 -07004740 (start.getTask() == task) ? activities.indexOf(start) : activities.size() - 1;
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004741 for (; activityIndex >= 0; --activityIndex) {
4742 final ActivityRecord r = activities.get(activityIndex);
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08004743 updatedConfig |= r.ensureActivityConfiguration(0 /* globalChanges */,
Andrii Kulian21713ac2016-10-12 22:05:05 -07004744 preserveWindow);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004745 if (r.fullscreen) {
4746 behindFullscreen = true;
4747 break;
4748 }
4749 }
4750 if (behindFullscreen) {
4751 break;
4752 }
4753 }
4754 if (updatedConfig) {
Wale Ogunwale089586f2016-06-20 14:16:22 -07004755 // Ensure the resumed state of the focus activity if we updated the configuration of
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004756 // any activity.
4757 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4758 }
4759 }
4760
Wale Ogunwale1666e312016-12-16 11:27:18 -08004761 // TODO: Figure-out a way to consolidate with resize() method below.
4762 @Override
4763 public void requestResize(Rect bounds) {
4764 mService.resizeStack(mStackId, bounds, true /* allowResizeInDockedMode */,
4765 false /* preserveWindows */, false /* animate */, -1 /* animationDuration */);
4766 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07004767
Wale Ogunwale1666e312016-12-16 11:27:18 -08004768 // TODO: Can only be called from special methods in ActivityStackSupervisor.
4769 // Need to consolidate those calls points into this resize method so anyone can call directly.
4770 void resize(Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004771 if (!updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
4772 return;
4773 }
4774
4775 // Update override configurations of all tasks in the stack.
4776 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
Andrii Kulian1e32e022016-09-16 15:29:34 -07004777 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
4778
4779 mTmpBounds.clear();
Andrii Kulian1e32e022016-09-16 15:29:34 -07004780 mTmpInsetBounds.clear();
4781
chaviwbe43ac82018-04-04 15:14:49 -07004782 synchronized (mWindowManager.getWindowManagerLock()) {
4783 for (int i = mTaskHistory.size() - 1; i >= 0; i--) {
4784 final TaskRecord task = mTaskHistory.get(i);
4785 if (task.isResizeable()) {
4786 if (inFreeformWindowingMode()) {
4787 // TODO: Can be removed now since each freeform task is in its own stack.
4788 // For freeform stack we don't adjust the size of the tasks to match that
4789 // of the stack, but we do try to make sure the tasks are still contained
4790 // with the bounds of the stack.
4791 mTmpRect2.set(task.getOverrideBounds());
4792 fitWithinBounds(mTmpRect2, bounds);
4793 task.updateOverrideConfiguration(mTmpRect2);
4794 } else {
4795 task.updateOverrideConfiguration(taskBounds, insetBounds);
4796 }
4797 }
4798
4799 mTmpBounds.put(task.taskId, task.getOverrideBounds());
4800 if (tempTaskInsetBounds != null) {
4801 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004802 }
4803 }
4804
chaviwbe43ac82018-04-04 15:14:49 -07004805 mWindowContainerController.resize(bounds, mTmpBounds, mTmpInsetBounds);
4806 setBounds(bounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004807 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07004808 }
4809
Winson Chung15036ca2018-05-31 15:51:47 -07004810 void onPipAnimationEndResize() {
4811 mWindowContainerController.onPipAnimationEndResize();
4812 }
4813
Andrii Kulian1e32e022016-09-16 15:29:34 -07004814
4815 /**
4816 * Adjust bounds to stay within stack bounds.
4817 *
4818 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4819 * that keep them unchanged, but be contained within the stack bounds.
4820 *
4821 * @param bounds Bounds to be adjusted.
4822 * @param stackBounds Bounds within which the other bounds should remain.
4823 */
4824 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08004825 if (stackBounds == null || stackBounds.isEmpty() || stackBounds.contains(bounds)) {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004826 return;
4827 }
4828
4829 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4830 final int maxRight = stackBounds.right
4831 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4832 int horizontalDiff = stackBounds.left - bounds.left;
4833 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4834 || (bounds.left + horizontalDiff >= maxRight)) {
4835 horizontalDiff = maxRight - bounds.left;
4836 }
4837 bounds.left += horizontalDiff;
4838 bounds.right += horizontalDiff;
4839 }
4840
4841 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4842 final int maxBottom = stackBounds.bottom
4843 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4844 int verticalDiff = stackBounds.top - bounds.top;
4845 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4846 || (bounds.top + verticalDiff >= maxBottom)) {
4847 verticalDiff = maxBottom - bounds.top;
4848 }
4849 bounds.top += verticalDiff;
4850 bounds.bottom += verticalDiff;
4851 }
4852 }
4853
Craig Mautnercae015f2013-02-08 14:31:27 -08004854 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004855 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4856 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4857 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4858 final ActivityRecord r = activities.get(activityNdx);
4859 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004860 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004861 }
4862 if (r.fullscreen && !r.finishing) {
4863 return false;
4864 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004865 }
4866 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004867 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004868 if (r == null) {
4869 return false;
4870 }
4871 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4872 + " would have returned true for r=" + r);
4873 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004874 }
4875
4876 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004877 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4878 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4879 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4880 final ActivityRecord r = activities.get(activityNdx);
4881 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004882 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004883 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004884 }
4885 }
4886 }
4887
Wale Ogunwale540e1232015-05-01 15:35:39 -07004888 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4889 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004890 boolean didSomething = false;
4891 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004892 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004893 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4894 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Bryce Leefbd263b42018-03-07 10:33:55 -08004895 mTmpActivities.clear();
4896 mTmpActivities.addAll(activities);
4897
4898 while (!mTmpActivities.isEmpty()) {
4899 ActivityRecord r = mTmpActivities.remove(0);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004900 final boolean sameComponent =
4901 (r.packageName.equals(packageName) && (filterByClasses == null
4902 || filterByClasses.contains(r.realActivity.getClassName())))
4903 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004904 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Bryce Leeaf691c02017-03-20 14:20:22 -07004905 && (sameComponent || r.getTask() == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004906 && (r.app == null || evenPersistent || !r.app.persistent)) {
4907 if (!doit) {
4908 if (r.finishing) {
4909 // If this activity is just finishing, then it is not
4910 // interesting as far as something to stop.
4911 continue;
4912 }
4913 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004914 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004915 if (r.isActivityTypeHome()) {
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004916 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4917 Slog.i(TAG, "Skip force-stop again " + r);
4918 continue;
4919 } else {
4920 homeActivity = r.realActivity;
4921 }
4922 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004923 didSomething = true;
4924 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004925 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004926 if (r.app != null) {
4927 r.app.removed = true;
4928 }
4929 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004930 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004931 lastTask = r.getTask();
Bryce Leefbd263b42018-03-07 10:33:55 -08004932 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4933 true);
Craig Mautnercae015f2013-02-08 14:31:27 -08004934 }
4935 }
4936 }
4937 return didSomething;
4938 }
4939
Winson Chung61c9e5a2017-10-11 10:39:32 -07004940 /**
4941 * @return The set of running tasks through {@param tasksOut} that are available to the caller.
4942 * If {@param ignoreActivityType} or {@param ignoreWindowingMode} are not undefined,
4943 * then skip running tasks that match those types.
4944 */
4945 void getRunningTasks(List<TaskRecord> tasksOut, @ActivityType int ignoreActivityType,
4946 @WindowingMode int ignoreWindowingMode, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004947 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4948 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004949 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004950 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004951 if (task.getTopActivity() == null) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004952 // Skip if there are no activities in the task
riddle_hsuddc74152015-04-07 11:30:09 +08004953 continue;
4954 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004955 if (!allowed && !task.isActivityTypeHome() && task.effectiveUid != callingUid) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004956 // Skip if the caller can't fetch this task
Dianne Hackborn09233282014-04-30 11:33:59 -07004957 continue;
4958 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004959 if (ignoreActivityType != ACTIVITY_TYPE_UNDEFINED
4960 && task.getActivityType() == ignoreActivityType) {
4961 // Skip ignored activity type
4962 continue;
Craig Mautneraab647e2013-02-28 16:31:36 -08004963 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004964 if (ignoreWindowingMode != WINDOWING_MODE_UNDEFINED
4965 && task.getWindowingMode() == ignoreWindowingMode) {
4966 // Skip ignored windowing mode
4967 continue;
4968 }
riddle_hsuddc74152015-04-07 11:30:09 +08004969 if (focusedStack && topTask) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004970 // For the focused stack top task, update the last stack active time so that it can
4971 // be used to determine the order of the tasks (it may not be set for newly created
4972 // tasks)
4973 task.lastActiveTime = SystemClock.elapsedRealtime();
riddle_hsuddc74152015-04-07 11:30:09 +08004974 topTask = false;
4975 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004976 tasksOut.add(task);
Craig Mautnercae015f2013-02-08 14:31:27 -08004977 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004978 }
4979
Andrii Kulian21713ac2016-10-12 22:05:05 -07004980 void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004981 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004982 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004983 if (top >= 0) {
4984 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4985 int activityTop = activities.size() - 1;
Dianne Hackborn354736e2016-08-22 17:00:05 -07004986 if (activityTop >= 0) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004987 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4988 "unhandled-back", true);
4989 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004990 }
4991 }
4992
Craig Mautner6b74cb52013-09-27 17:02:21 -07004993 /**
4994 * Reset local parameters because an app's activity died.
4995 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004996 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004997 */
4998 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004999 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005000 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07005001 "App died while pausing: " + mPausingActivity);
5002 mPausingActivity = null;
5003 }
5004 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
5005 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07005006 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07005007 }
5008
Craig Mautner19091252013-10-05 00:03:53 -07005009 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07005010 }
5011
Craig Mautnercae015f2013-02-08 14:31:27 -08005012 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08005013 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5014 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5015 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5016 final ActivityRecord r = activities.get(activityNdx);
5017 if (r.app == app) {
5018 Slog.w(TAG, " Force finishing activity "
5019 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07005020 // Force the destroy to skip right to removal.
5021 r.app = null;
Adrian Roos93577212018-04-10 14:12:10 -07005022 mWindowManager.prepareAppTransition(TRANSIT_CRASHING_ACTIVITY_CLOSE,
Jorim Jaggi9c52ebb2018-06-01 14:45:24 +02005023 false /* alwaysKeepCurrent */);
Bryce Leef52974c2018-02-14 15:12:01 -08005024 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false,
5025 "handleAppCrashedLocked");
Craig Mautnerd44711d2013-02-23 11:24:36 -08005026 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005027 }
5028 }
5029 }
5030
Dianne Hackborn390517b2013-05-30 15:03:32 -07005031 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005032 boolean dumpClient, String dumpPackage, boolean needSep) {
5033
5034 if (mTaskHistory.isEmpty()) {
5035 return false;
5036 }
5037 final String prefix = " ";
Craig Mautneraab647e2013-02-28 16:31:36 -08005038 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5039 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005040 if (needSep) {
5041 pw.println("");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005042 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005043 pw.println(prefix + "Task id #" + task.taskId);
Bryce Leef3c6a472017-11-14 14:53:06 -08005044 pw.println(prefix + "mBounds=" + task.getOverrideBounds());
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005045 pw.println(prefix + "mMinWidth=" + task.mMinWidth);
5046 pw.println(prefix + "mMinHeight=" + task.mMinHeight);
5047 pw.println(prefix + "mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
5048 pw.println(prefix + "* " + task);
5049 task.dump(pw, prefix + " ");
5050 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
5051 prefix, "Hist", true, !dumpAll, dumpClient, dumpPackage, false, null, task);
Craig Mautneraab647e2013-02-28 16:31:36 -08005052 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005053 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08005054 }
5055
5056 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Winson Chung6998bc42017-02-28 17:07:05 -08005057 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautnercae015f2013-02-08 14:31:27 -08005058
5059 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005060 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5061 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08005062 }
5063 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005064 final int top = mTaskHistory.size() - 1;
5065 if (top >= 0) {
5066 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
5067 int listTop = list.size() - 1;
5068 if (listTop >= 0) {
5069 activities.add(list.get(listTop));
5070 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005071 }
5072 } else {
5073 ItemMatcher matcher = new ItemMatcher();
5074 matcher.build(name);
5075
Craig Mautneraab647e2013-02-28 16:31:36 -08005076 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5077 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
5078 if (matcher.match(r1, r1.intent.getComponent())) {
5079 activities.add(r1);
5080 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005081 }
5082 }
5083 }
5084
5085 return activities;
5086 }
5087
5088 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07005089 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08005090
5091 // All activities that came from the package must be
5092 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08005093 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5094 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5095 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5096 final ActivityRecord a = activities.get(activityNdx);
5097 if (a.info.packageName.equals(packageName)) {
5098 a.forceNewConfig = true;
5099 if (starting != null && a == starting && a.visible) {
5100 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08005101 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08005102 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005103 }
5104 }
5105 }
5106
5107 return starting;
5108 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005109
Wale Ogunwale000957c2015-04-03 08:19:12 -07005110 /**
5111 * Removes the input task from this stack.
5112 * @param task to remove.
5113 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07005114 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
5115 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07005116 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07005117 void removeTask(TaskRecord task, String reason, int mode) {
Bryce Leeaf691c02017-03-20 14:20:22 -07005118 for (ActivityRecord record : task.mActivities) {
5119 onActivityRemovedFromStack(record);
Craig Mautner04a0ea62014-01-13 12:51:26 -08005120 }
5121
Bryce Lee2b8e0372018-04-05 17:01:37 -07005122 final boolean removed = mTaskHistory.remove(task);
5123
5124 if (removed) {
5125 EventLog.writeEvent(EventLogTags.AM_REMOVE_TASK, task.taskId, getStackId());
5126 }
5127
Winson Chungabb433b2017-03-24 09:35:42 -07005128 removeActivitiesFromLRUListLocked(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005129 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07005130
Wale Ogunwale06579d62016-04-30 15:29:06 -07005131 if (mode == REMOVE_TASK_MODE_DESTROYING && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005132 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07005133 final boolean isVoiceSession = task.voiceSession != null;
5134 if (isVoiceSession) {
5135 try {
5136 task.voiceSession.taskFinished(task.intent, task.taskId);
5137 } catch (RemoteException e) {
5138 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07005139 }
Craig Mautner41db4a72014-05-07 17:20:56 -07005140 if (task.autoRemoveFromRecents() || isVoiceSession) {
5141 // Task creator asked to remove this when done, or this task was a voice
5142 // interaction, so it should not remain on the recent tasks list.
Winson Chung1dbc8112017-09-28 18:05:31 -07005143 mStackSupervisor.mRecentTasks.remove(task);
Craig Mautner41db4a72014-05-07 17:20:56 -07005144 }
Bryce Lee840c5662017-04-13 10:02:51 -07005145
5146 task.removeWindowContainer();
Dianne Hackborn91097de2014-04-04 18:02:06 -07005147 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005148
5149 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005150 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07005151 // We only need to adjust focused stack if this stack is in focus and we are not in the
5152 // process of moving the task to the top of the stack that will be focused.
5153 if (isOnHomeDisplay() && mode != REMOVE_TASK_MODE_MOVING_TO_TOP
5154 && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005155 String myReason = reason + " leftTaskHistoryEmpty";
Bryce Leef3c6a472017-11-14 14:53:06 -08005156 if (!inMultiWindowMode() || !adjustFocusToNextFocusableStack(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005157 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005158 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005159 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07005160 if (isAttached()) {
5161 getDisplay().positionChildAtBottom(this);
Craig Mautner593a4e62014-01-15 17:55:51 -08005162 }
Wale Ogunwale388945c2017-10-04 12:13:46 -07005163 if (!isActivityTypeHome()) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07005164 remove();
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005165 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005166 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005167
Andrii Kulian02b7a832016-10-06 23:11:56 -07005168 task.setStack(null);
Winson Chungc81c0ce2017-03-17 12:27:30 -07005169
5170 // Notify if a task from the pinned stack is being removed (or moved depending on the mode)
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005171 if (inPinnedWindowingMode()) {
Winson Chungc81c0ce2017-03-17 12:27:30 -07005172 mService.mTaskChangeNotificationController.notifyActivityUnpinned();
5173 }
Craig Mautner0247fc82013-02-28 14:32:06 -08005174 }
5175
Dianne Hackborn91097de2014-04-04 18:02:06 -07005176 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5177 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07005178 boolean toTop) {
Bryce Leeb802ea12017-11-15 21:25:03 -08005179 return createTaskRecord(taskId, info, intent, voiceSession, voiceInteractor, toTop,
5180 null /*activity*/, null /*source*/, null /*options*/);
5181 }
5182
5183 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5184 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
5185 boolean toTop, ActivityRecord activity, ActivityRecord source,
5186 ActivityOptions options) {
Garfield Tan9b1efea2017-12-05 16:43:46 -08005187 final TaskRecord task = TaskRecord.create(
5188 mService, taskId, info, intent, voiceSession, voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005189 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005190 addTask(task, toTop, "createTaskRecord");
Lucas Dupin47a65c72018-02-15 14:16:18 -08005191 final int displayId = mDisplayId != INVALID_DISPLAY ? mDisplayId : DEFAULT_DISPLAY;
Bryce Lee2a3cc462017-10-27 10:57:35 -07005192 final boolean isLockscreenShown = mService.mStackSupervisor.getKeyguardController()
Lucas Dupin47a65c72018-02-15 14:16:18 -08005193 .isKeyguardOrAodShowing(displayId);
Bryce Leeec55eb02017-12-05 20:51:27 -08005194 if (!mStackSupervisor.getLaunchParamsController()
Bryce Leeb802ea12017-11-15 21:25:03 -08005195 .layoutTask(task, info.windowLayout, activity, source, options)
Bryce Leef3c6a472017-11-14 14:53:06 -08005196 && !matchParentBounds() && task.isResizeable() && !isLockscreenShown) {
5197 task.updateOverrideConfiguration(getOverrideBounds());
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005198 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005199 task.createWindowContainer(toTop, (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005200 return task;
5201 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005202
5203 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005204 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005205 }
5206
Wale Ogunwale5f986092015-12-04 15:35:38 -08005207 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Winson Chung5af42fc2017-03-24 17:11:33 -07005208 addTask(task, toTop ? MAX_VALUE : 0, true /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005209 if (toTop) {
5210 // TODO: figure-out a way to remove this call.
Wale Ogunwale1666e312016-12-16 11:27:18 -08005211 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
5212 true /* includingParents */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005213 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005214 }
5215
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005216 // TODO: This shouldn't allow automatic reparenting. Remove the call to preAddTask and deal
5217 // with the fall-out...
Winson Chung5af42fc2017-03-24 17:11:33 -07005218 void addTask(final TaskRecord task, int position, boolean schedulePictureInPictureModeChange,
5219 String reason) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005220 // TODO: Is this remove really needed? Need to look into the call path for the other addTask
5221 mTaskHistory.remove(task);
5222 position = getAdjustedPositionForTask(task, position, null /* starting */);
5223 final boolean toTop = position >= mTaskHistory.size();
Wale Ogunwale06579d62016-04-30 15:29:06 -07005224 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005225
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005226 mTaskHistory.add(position, task);
Andrii Kulian02b7a832016-10-06 23:11:56 -07005227 task.setStack(this);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005228
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005229 updateTaskMovement(task, toTop);
5230
Winson Chung5af42fc2017-03-24 17:11:33 -07005231 postAddTask(task, prevStack, schedulePictureInPictureModeChange);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005232 }
5233
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005234 void positionChildAt(TaskRecord task, int index) {
5235
5236 if (task.getStack() != this) {
5237 throw new IllegalArgumentException("AS.positionChildAt: task=" + task
5238 + " is not a child of stack=" + this + " current parent=" + task.getStack());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005239 }
5240
5241 task.updateOverrideConfigurationForStack(this);
5242
Jorim Jaggi023da532016-04-20 22:42:32 -07005243 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07005244 final boolean wasResumed = topRunningActivity == task.getStack().mResumedActivity;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005245 insertTaskAtPosition(task, index);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005246 task.setStack(this);
Winson Chung5af42fc2017-03-24 17:11:33 -07005247 postAddTask(task, null /* prevStack */, true /* schedulePictureInPictureModeChange */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005248
Jorim Jaggi023da532016-04-20 22:42:32 -07005249 if (wasResumed) {
5250 if (mResumedActivity != null) {
5251 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5252 + " other stack to this stack mResumedActivity=" + mResumedActivity
5253 + " other mResumedActivity=" + topRunningActivity);
5254 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08005255 topRunningActivity.setState(RESUMED, "positionChildAt");
Jorim Jaggi023da532016-04-20 22:42:32 -07005256 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005257
5258 // The task might have already been running and its visibility needs to be synchronized with
5259 // the visibility of the stack / windows.
5260 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
5261 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005262 }
5263
Wale Ogunwale06579d62016-04-30 15:29:06 -07005264 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005265 final ActivityStack prevStack = task.getStack();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005266 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005267 prevStack.removeTask(task, reason,
5268 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005269 }
5270 return prevStack;
5271 }
5272
Winson Chung5af42fc2017-03-24 17:11:33 -07005273 /**
5274 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
5275 * change. Callers may set this to false if they are explicitly scheduling PiP mode
5276 * changes themselves, like during the PiP animation
5277 */
5278 private void postAddTask(TaskRecord task, ActivityStack prevStack,
5279 boolean schedulePictureInPictureModeChange) {
5280 if (schedulePictureInPictureModeChange && prevStack != null) {
5281 mStackSupervisor.scheduleUpdatePictureInPictureModeIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005282 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005283 try {
5284 task.voiceSession.taskStarted(task.intent, task.taskId);
5285 } catch (RemoteException e) {
5286 }
5287 }
5288 }
5289
Winson Chungc2baac02017-01-11 13:34:47 -08005290 void moveToFrontAndResumeStateIfNeeded(ActivityRecord r, boolean moveToFront, boolean setResume,
5291 boolean setPause, String reason) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08005292 if (!moveToFront) {
5293 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005294 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005295
5296 // If the activity owns the last resumed activity, transfer that together,
5297 // so that we don't resume the same activity again in the new stack.
5298 // Apps may depend on onResume()/onPause() being called in pairs.
5299 if (setResume) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08005300 r.setState(RESUMED, "moveToFrontAndResumeStateIfNeeded");
Winson Chungabb433b2017-03-24 09:35:42 -07005301 updateLRUListLocked(r);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005302 }
Winson Chungc2baac02017-01-11 13:34:47 -08005303 // If the activity was previously pausing, then ensure we transfer that as well
5304 if (setPause) {
5305 mPausingActivity = r;
Winson Chung4dabf232017-01-25 13:25:22 -08005306 schedulePauseTimeout(r);
Winson Chungc2baac02017-01-11 13:34:47 -08005307 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005308 // Move the stack in which we are placing the activity to the front. The call will also
5309 // make sure the activity focus is set.
5310 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005311 }
5312
Craig Mautnerc00204b2013-03-05 15:02:14 -08005313 public int getStackId() {
5314 return mStackId;
5315 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005316
5317 @Override
5318 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005319 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005320 + " stackId=" + mStackId + " type=" + activityTypeToString(getActivityType())
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07005321 + " mode=" + windowingModeToString(getWindowingMode())
Wale Ogunwale8e923af2018-03-23 08:48:11 -07005322 + " visible=" + shouldBeVisible(null /* starting */)
5323 + " translucent=" + isStackTranslucent(null /* starting */)
5324 + ", "
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005325 + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005326 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005327
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07005328 void onLockTaskPackagesUpdated() {
Craig Mautner15df08a2015-04-01 12:17:18 -07005329 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5330 mTaskHistory.get(taskNdx).setLockTaskAuth();
5331 }
5332 }
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005333
5334 void executeAppTransition(ActivityOptions options) {
5335 mWindowManager.executeAppTransition();
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005336 ActivityOptions.abort(options);
5337 }
David Stevens9440dc82017-03-16 19:00:20 -07005338
5339 boolean shouldSleepActivities() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07005340 final ActivityDisplay display = getDisplay();
Bryce Lee271617a2018-03-15 10:39:12 -07005341
5342 // Do not sleep activities in this stack if we're marked as focused and the keyguard
5343 // is in the process of going away.
5344 if (mStackSupervisor.getFocusedStack() == this
5345 && mStackSupervisor.getKeyguardController().isKeyguardGoingAway()) {
5346 return false;
5347 }
5348
David Stevens9440dc82017-03-16 19:00:20 -07005349 return display != null ? display.isSleeping() : mService.isSleepingLocked();
5350 }
5351
5352 boolean shouldSleepOrShutDownActivities() {
5353 return shouldSleepActivities() || mService.isShuttingDownLocked();
5354 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07005355
5356 public void writeToProto(ProtoOutputStream proto, long fieldId) {
5357 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02005358 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005359 proto.write(ID, mStackId);
5360 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5361 final TaskRecord task = mTaskHistory.get(taskNdx);
5362 task.writeToProto(proto, TASKS);
5363 }
5364 if (mResumedActivity != null) {
5365 mResumedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
5366 }
5367 proto.write(DISPLAY_ID, mDisplayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08005368 if (!matchParentBounds()) {
5369 final Rect bounds = getOverrideBounds();
5370 bounds.writeToProto(proto, BOUNDS);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005371 }
Bryce Leef3c6a472017-11-14 14:53:06 -08005372
5373 // TODO: Remove, no longer needed with windowingMode.
5374 proto.write(FULLSCREEN, matchParentBounds());
Steven Timotius4346f0a2017-09-12 11:07:21 -07005375 proto.end(token);
5376 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005377}