blob: 9fb3e11446cee86ede6344c62f3e4f6234c2561b [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;
20import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
22import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale68278562017-09-23 17:13:55 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale2b07da82017-11-08 14:52:40 -080025import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070026import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale68278562017-09-23 17:13:55 -070027import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwalef75962a2017-08-23 14:58:04 -070028import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070029import static android.app.WindowConfiguration.activityTypeToString;
30import static android.app.WindowConfiguration.windowingModeToString;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080031import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080032import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070033import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwalef5d1e352016-09-22 08:34:42 -070034import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian7211d2e2017-01-27 15:58:05 -080035import static android.view.Display.FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -070036import static android.view.Display.INVALID_DISPLAY;
Winson Chung1dbc8112017-09-28 18:05:31 -070037
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070038import static com.android.server.am.ActivityDisplay.POSITION_BOTTOM;
39import static com.android.server.am.ActivityDisplay.POSITION_TOP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070040import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
41import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_APP;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CLEANUP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070044import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070045import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RESULTS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070049import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TRANSITION;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_USER_LEAVING;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_APP;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CLEANUP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070059import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RESULTS;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070064import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TRANSITION;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_USER_LEAVING;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
72import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Bryce Lee7ace3952018-02-16 14:34:32 -080073import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
74import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
75import static com.android.server.am.ActivityStack.ActivityState.FINISHING;
Wale Ogunwale08488a62017-09-05 12:37:03 -070076import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
Bryce Lee7ace3952018-02-16 14:34:32 -080077import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
78import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -070079import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
80import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale39381972015-12-17 17:15:29 -080081import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Winson Chung6954fc92017-03-24 16:22:12 -070082import static com.android.server.am.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070083import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Winson Chung6954fc92017-03-24 16:22:12 -070084import static com.android.server.am.ActivityStackSupervisor.REMOVE_FROM_RECENTS;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070085import static com.android.server.am.ActivityStackProto.BOUNDS;
86import static com.android.server.am.ActivityStackProto.CONFIGURATION_CONTAINER;
87import static com.android.server.am.ActivityStackProto.DISPLAY_ID;
88import static com.android.server.am.ActivityStackProto.FULLSCREEN;
89import static com.android.server.am.ActivityStackProto.ID;
90import static com.android.server.am.ActivityStackProto.RESUMED_ACTIVITY;
91import static com.android.server.am.ActivityStackProto.TASKS;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010092import static android.view.WindowManager.TRANSIT_ACTIVITY_CLOSE;
93import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Adrian Roos93577212018-04-10 14:12:10 -070094import static android.view.WindowManager.TRANSIT_CRASHING_ACTIVITY_CLOSE;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010095import static android.view.WindowManager.TRANSIT_NONE;
96import static android.view.WindowManager.TRANSIT_TASK_CLOSE;
97import static android.view.WindowManager.TRANSIT_TASK_OPEN;
98import static android.view.WindowManager.TRANSIT_TASK_OPEN_BEHIND;
99import static android.view.WindowManager.TRANSIT_TASK_TO_BACK;
100import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700101
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800102import static java.lang.Integer.MAX_VALUE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700103
104import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700105import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700106import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700107import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800108import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109import android.app.ResultInfo;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700110import android.app.WindowConfiguration.ActivityType;
111import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian446e8242017-10-26 15:17:29 -0700112import android.app.servertransaction.ActivityResultItem;
Andrii Kulian9956d892018-02-14 13:48:56 -0800113import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700114import android.app.servertransaction.NewIntentItem;
115import android.app.servertransaction.WindowVisibilityItem;
116import android.app.servertransaction.DestroyActivityItem;
117import android.app.servertransaction.PauseActivityItem;
118import android.app.servertransaction.ResumeActivityItem;
119import android.app.servertransaction.StopActivityItem;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700122import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800123import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124import android.content.res.Configuration;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700125import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -0800126import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127import android.os.Binder;
Craig Mautner329f4122013-11-07 09:10:42 -0800128import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129import android.os.Handler;
130import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +0900131import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133import android.os.RemoteException;
134import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -0800135import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700136import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700137import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700138import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700139import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800140import android.util.IntArray;
Jorim Jaggi023da532016-04-20 22:42:32 -0700141import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142import android.util.Slog;
Andrii Kulian1e32e022016-09-16 15:29:34 -0700143import android.util.SparseArray;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700144import android.util.proto.ProtoOutputStream;
Craig Mautner59c00972012-07-30 12:10:24 -0700145import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700146
Andreas Gampea36dc622018-02-05 17:19:22 -0800147import com.android.internal.annotations.GuardedBy;
Bryce Lee840c5662017-04-13 10:02:51 -0700148import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700149import com.android.internal.app.IVoiceInteractor;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700150import com.android.internal.os.BatteryStatsImpl;
151import com.android.server.Watchdog;
152import com.android.server.am.ActivityManagerService.ItemMatcher;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700153import com.android.server.wm.ConfigurationContainer;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800154import com.android.server.wm.StackWindowController;
155import com.android.server.wm.StackWindowListener;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700156import com.android.server.wm.WindowManagerService;
157
Craig Mautnercae015f2013-02-08 14:31:27 -0800158import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800159import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160import java.lang.ref.WeakReference;
161import java.util.ArrayList;
162import java.util.Iterator;
163import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800164import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700165import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167/**
168 * State and management of a single stack of activities.
169 */
Winson Chung55893332017-02-17 17:13:10 -0800170class ActivityStack<T extends StackWindowController> extends ConfigurationContainer
171 implements StackWindowListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800172 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700173 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
174 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800175 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700176 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700177 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700178 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700179 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700180 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700181 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700183 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
184 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
185 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
186 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
187 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700188
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700189 // Ticks during which we check progress while waiting for an app to launch.
190 static final int LAUNCH_TICK = 500;
191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 // How long we wait until giving up on the last activity to pause. This
193 // is short because it directly impacts the responsiveness of starting the
194 // next activity.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700195 private static final int PAUSE_TIMEOUT = 500;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700196
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700197 // How long we wait for the activity to tell us it has stopped before
198 // giving up. This is a good amount of time because we really need this
Robert Carr3406d462018-03-15 16:19:07 -0700199 // from the application in order to get its saved state. Once the stop
200 // is complete we may start destroying client resources triggering
201 // crashes if the UI thread was hung. We put this timeout one second behind
202 // the ANR timeout so these situations will generate ANR instead of
203 // Surface lost or other errors.
204 private static final int STOP_TIMEOUT = 11 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700205
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700206 // How long we wait until giving up on an activity telling us it has
207 // finished destroying itself.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700208 private static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800209
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700210 // Set to false to disable the preview that is shown while a new activity
211 // is being started.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700212 private static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800213
Craig Mautner5eda9b32013-07-02 11:58:16 -0700214 // How long to wait for all background Activities to redraw following a call to
215 // convertToTranslucent().
Andrii Kulian21713ac2016-10-12 22:05:05 -0700216 private static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700217
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800218 // How many activities have to be scheduled to stop to force a stop pass.
219 private static final int MAX_STOPPING_TO_FORCE = 3;
220
Andrii Kulian1779e612016-10-12 21:58:25 -0700221 @Override
222 protected int getChildCount() {
223 return mTaskHistory.size();
224 }
225
226 @Override
227 protected ConfigurationContainer getChildAt(int index) {
228 return mTaskHistory.get(index);
229 }
230
231 @Override
232 protected ConfigurationContainer getParent() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700233 return getDisplay();
Andrii Kulian1779e612016-10-12 21:58:25 -0700234 }
235
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800236 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700237 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800238 super.onParentChanged();
239 mStackSupervisor.updateUIDsPresentOnDisplay();
240 }
241
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700242 enum ActivityState {
243 INITIALIZING,
244 RESUMED,
245 PAUSING,
246 PAUSED,
247 STOPPING,
248 STOPPED,
249 FINISHING,
250 DESTROYING,
251 DESTROYED
252 }
253
Bryce Lee840c5662017-04-13 10:02:51 -0700254 @VisibleForTesting
Wale Ogunwale06579d62016-04-30 15:29:06 -0700255 /* The various modes for the method {@link #removeTask}. */
256 // Task is being completely removed from all stacks in the system.
Bryce Lee840c5662017-04-13 10:02:51 -0700257 protected static final int REMOVE_TASK_MODE_DESTROYING = 0;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700258 // Task is being removed from this stack so we can add it to another stack. In the case we are
259 // moving we don't want to perform some operations on the task like removing it from window
260 // manager or recents.
261 static final int REMOVE_TASK_MODE_MOVING = 1;
262 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
263 // stack and the new stack will be on top of all stacks.
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700264 static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700265
Andrii Kulian1e32e022016-09-16 15:29:34 -0700266 // The height/width divide used when fitting a task within a bounds with method
267 // {@link #fitWithinBounds}.
268 // We always want the task to to be visible in the bounds without affecting its size when
269 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
270 // the input bounds right or bottom side minus the width or height divided by this value.
271 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
272
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273 final ActivityManagerService mService;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700274 private final WindowManagerService mWindowManager;
Winson Chung55893332017-02-17 17:13:10 -0800275 T mWindowContainerController;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800276
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700277 /**
278 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800279 * running) activities. It contains #TaskRecord objects.
280 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800281 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800282
283 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700284 * List of running activities, sorted by recent usage.
285 * The first entry in the list is the least recently used.
286 * It contains HistoryRecord objects.
287 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800288 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289
290 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 * When we are in the process of pausing an activity, before starting the
292 * next one, this variable holds the activity that is currently being paused.
293 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800294 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700295
296 /**
297 * This is the last activity that we put into the paused state. This is
298 * used to determine if we need to do an activity transition while sleeping,
299 * when we normally hold the top activity paused.
300 */
301 ActivityRecord mLastPausedActivity = null;
302
303 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700304 * Activities that specify No History must be removed once the user navigates away from them.
305 * If the device goes to sleep with such an activity in the paused state then we save it here
306 * and finish it later if another activity replaces it on wakeup.
307 */
308 ActivityRecord mLastNoHistoryActivity = null;
309
310 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700311 * Current activity that is resumed, or null if there is none.
312 */
313 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800314
Craig Mautner5eda9b32013-07-02 11:58:16 -0700315 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
316 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
317 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
318 // Activity in mTranslucentActivityWaiting is notified via
319 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
320 // background activity being drawn then the same call will be made with a true value.
321 ActivityRecord mTranslucentActivityWaiting = null;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700322 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700323
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700324 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700325 * Set when we know we are going to be calling updateConfiguration()
326 * soon, so want to skip intermediate config checks.
327 */
328 boolean mConfigWillChange;
329
Winson Chung47900652017-04-06 18:44:25 -0700330 /**
331 * When set, will force the stack to report as invisible.
332 */
333 boolean mForceHidden = false;
334
Andrii Kulian21713ac2016-10-12 22:05:05 -0700335 private boolean mUpdateBoundsDeferred;
336 private boolean mUpdateBoundsDeferredCalled;
337 private final Rect mDeferredBounds = new Rect();
338 private final Rect mDeferredTaskBounds = new Rect();
339 private final Rect mDeferredTaskInsetBounds = new Rect();
Jorim Jaggi192086e2016-03-11 17:17:03 +0100340
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700341 long mLaunchStartTime = 0;
342 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800343
Craig Mautner858d8a62013-04-23 17:08:34 -0700344 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700345
Craig Mautnerc00204b2013-03-05 15:02:14 -0800346 final int mStackId;
Craig Mautnere0a38842013-12-16 16:14:02 -0800347 /** The attached Display's unique identifier, or -1 if detached */
348 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800349
Andrii Kulian1e32e022016-09-16 15:29:34 -0700350 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
351 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800352 private final Rect mTmpRect2 = new Rect();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800353 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Andrii Kulian1e32e022016-09-16 15:29:34 -0700354
Bryce Leefbd263b42018-03-07 10:33:55 -0800355 /** List for processing through a set of activities */
356 private final ArrayList<ActivityRecord> mTmpActivities = new ArrayList<>();
357
Craig Mautner27084302013-03-25 08:05:25 -0700358 /** Run all ActivityStacks through this */
Winson Chung5af42fc2017-03-24 17:11:33 -0700359 protected final ActivityStackSupervisor mStackSupervisor;
Craig Mautner27084302013-03-25 08:05:25 -0700360
Jorim Jaggife762342016-10-13 14:33:27 +0200361 private boolean mTopActivityOccludesKeyguard;
362 private ActivityRecord mTopDismissingKeyguardActivity;
363
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700364 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700365 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
366 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
367 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
368 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700369 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700370
Andrii Kulian21713ac2016-10-12 22:05:05 -0700371 private static class ScheduleDestroyArgs {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700372 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700373 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700374 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700375 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700376 mReason = reason;
377 }
378 }
379
Zoran Marcetaf958b322012-08-09 20:27:12 +0900380 final Handler mHandler;
381
Andrii Kulian21713ac2016-10-12 22:05:05 -0700382 private class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800383
Craig Mautnerc8143c62013-09-03 12:15:57 -0700384 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900385 super(looper);
386 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387
Zoran Marcetaf958b322012-08-09 20:27:12 +0900388 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700389 public void handleMessage(Message msg) {
390 switch (msg.what) {
391 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800392 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 // We don't at this point know if the activity is fullscreen,
394 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800395 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700396 synchronized (mService) {
397 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700398 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700399 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700400 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800401 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700402 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700403 case LAUNCH_TICK_MSG: {
404 ActivityRecord r = (ActivityRecord)msg.obj;
405 synchronized (mService) {
406 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700407 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700408 }
409 }
410 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700411 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800412 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700413 // We don't at this point know if the activity is fullscreen,
414 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800415 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800416 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800417 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800418 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700419 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700420 case STOP_TIMEOUT_MSG: {
421 ActivityRecord r = (ActivityRecord)msg.obj;
422 // We don't at this point know if the activity is fullscreen,
423 // so we need to be conservative and assume it isn't.
424 Slog.w(TAG, "Activity stop timeout for " + r);
425 synchronized (mService) {
426 if (r.isInHistory()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -0700427 r.activityStoppedLocked(null /* icicle */,
428 null /* persistentState */, null /* description */);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700429 }
430 }
431 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700432 case DESTROY_ACTIVITIES_MSG: {
433 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
434 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700435 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700436 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700437 } break;
438 case TRANSLUCENT_TIMEOUT_MSG: {
439 synchronized (mService) {
440 notifyActivityDrawnLocked(null);
441 }
442 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700443 }
444 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800445 }
446
Craig Mautner34b73df2014-01-12 21:11:08 -0800447 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800448 int count = 0;
449 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
450 count += mTaskHistory.get(taskNdx).mActivities.size();
451 }
452 return count;
453 }
454
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700455 ActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700456 int windowingMode, int activityType, boolean onTop) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700457 mStackSupervisor = supervisor;
458 mService = supervisor.mService;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800459 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
460 mWindowManager = mService.mWindowManager;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700461 mStackId = stackId;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -0700462 mCurrentUser = mService.mUserController.getCurrentUserId();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800463 mTmpRect2.setEmpty();
Andrii Kulian2fcc4512018-01-25 16:39:27 -0800464 // Set display id before setting activity and window type to make sure it won't affect
465 // stacks on a wrong display.
466 mDisplayId = display.mDisplayId;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700467 setActivityType(activityType);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800468 setWindowingMode(windowingMode);
Winson Chung55893332017-02-17 17:13:10 -0800469 mWindowContainerController = createStackWindowController(display.mDisplayId, onTop,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700470 mTmpRect2);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800471 postAddToDisplay(display, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
472 }
473
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700474 T createStackWindowController(int displayId, boolean onTop, Rect outBounds) {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700475 return (T) new StackWindowController(mStackId, this, displayId, onTop, outBounds,
476 mStackSupervisor.mWindowManager);
Winson Chung55893332017-02-17 17:13:10 -0800477 }
478
479 T getWindowContainerController() {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800480 return mWindowContainerController;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700481 }
482
Bryce Leec4ab62a2018-03-05 14:19:26 -0800483 /**
484 * This should be called when an activity in a child task changes state. This should only
485 * be called from
486 * {@link TaskRecord#onActivityStateChanged(ActivityRecord, ActivityState, String)}.
487 * @param record The {@link ActivityRecord} whose state has changed.
488 * @param state The new state.
489 * @param reason The reason for the change.
490 */
491 void onActivityStateChanged(ActivityRecord record, ActivityState state, String reason) {
492 if (record == mResumedActivity && state != RESUMED) {
Bryce Lee84730a02018-04-03 14:10:04 -0700493 setResumedActivity(null, reason + " - onActivityStateChanged");
Bryce Leec4ab62a2018-03-05 14:19:26 -0800494 }
495
496 if (state == RESUMED) {
497 if (DEBUG_STACK) Slog.v(TAG_STACK, "set resumed activity to:" + record + " reason:"
498 + reason);
Bryce Lee84730a02018-04-03 14:10:04 -0700499 setResumedActivity(record, reason + " - onActivityStateChanged");
Bryce Leec4ab62a2018-03-05 14:19:26 -0800500 mService.setResumedActivityUncheckLocked(record, reason);
501 mStackSupervisor.mRecentTasks.add(record.getTask());
502 }
503 }
504
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700505 @Override
506 public void onConfigurationChanged(Configuration newParentConfig) {
507 final int prevWindowingMode = getWindowingMode();
508 super.onConfigurationChanged(newParentConfig);
509 final ActivityDisplay display = getDisplay();
510 if (display != null && prevWindowingMode != getWindowingMode()) {
511 display.onStackWindowingModeChanged(this);
512 }
513 }
514
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700515 @Override
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800516 public void setWindowingMode(int windowingMode) {
Winson Chung3e36f822018-01-16 12:06:04 -0800517 setWindowingMode(windowingMode, false /* animate */, false /* showRecents */,
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800518 false /* enteringSplitScreenMode */);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800519 }
520
Wale Ogunwaledf262f52017-12-07 18:17:12 -0800521 void setWindowingMode(int preferredWindowingMode, boolean animate, boolean showRecents,
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800522 boolean enteringSplitScreenMode) {
523 final boolean creating = mWindowContainerController == null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800524 final int currentMode = getWindowingMode();
525 final ActivityDisplay display = getDisplay();
526 final TaskRecord topTask = topTask();
527 final ActivityStack splitScreenStack = display.getSplitScreenPrimaryStack();
528 mTmpOptions.setLaunchWindowingMode(preferredWindowingMode);
529
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800530 // Need to make sure windowing mode is supported. If we in the process of creating the stack
531 // no need to resolve the windowing mode again as it is already resolved to the right mode.
532 int windowingMode = creating
533 ? preferredWindowingMode
534 : display.resolveWindowingMode(
535 null /* ActivityRecord */, mTmpOptions, topTask, getActivityType());
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800536 if (splitScreenStack == this && windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
537 // Resolution to split-screen secondary for the primary split-screen stack means we want
538 // to go fullscreen.
539 windowingMode = WINDOWING_MODE_FULLSCREEN;
540 }
541
Wale Ogunwaleac36e4d2017-11-29 13:30:26 -0800542 final boolean alreadyInSplitScreenMode = display.hasSplitScreenPrimaryStack();
543
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800544 // Don't send non-resizeable notifications if the windowing mode changed was a side effect
545 // of us entering split-screen mode.
546 final boolean sendNonResizeableNotification = !enteringSplitScreenMode;
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800547 // Take any required action due to us not supporting the preferred windowing mode.
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800548 if (alreadyInSplitScreenMode && windowingMode == WINDOWING_MODE_FULLSCREEN
549 && sendNonResizeableNotification && isActivityTypeStandardOrUndefined()) {
550 final boolean preferredSplitScreen =
551 preferredWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
552 || preferredWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
553 if (preferredSplitScreen || creating) {
554 // Looks like we can't launch in split screen mode or the stack we are launching
555 // doesn't support split-screen mode, go ahead an dismiss split-screen and display a
556 // warning toast about it.
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800557 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
558 display.getSplitScreenPrimaryStack().setWindowingMode(WINDOWING_MODE_FULLSCREEN);
559 }
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
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800639 final ActivityStack recentStack = display.getOrCreateStack(
640 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, ACTIVITY_TYPE_RECENTS,
641 true /* onTop */);
642 recentStack.moveToFront("setWindowingMode");
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800643 // If task moved to docked stack - show recents if needed.
Winson Chungdff7a732017-12-11 12:17:06 -0800644 mService.mWindowManager.showRecentApps();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800645 }
646 wm.continueSurfaceLayout();
647 }
648
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800649 // Don't ensure visible activities if the windowing mode change was a side effect of us
650 // entering split-screen mode.
651 if (!enteringSplitScreenMode) {
652 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
653 mStackSupervisor.resumeFocusedStackTopActivityLocked();
654 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800655 }
656
657 @Override
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700658 public boolean isCompatible(int windowingMode, int activityType) {
659 // TODO: Should we just move this to ConfigurationContainer?
660 if (activityType == ACTIVITY_TYPE_UNDEFINED) {
661 // Undefined activity types end up in a standard stack once the stack is created on a
662 // display, so they should be considered compatible.
663 activityType = ACTIVITY_TYPE_STANDARD;
664 }
665 final ActivityDisplay display = getDisplay();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700666 if (display != null && activityType == ACTIVITY_TYPE_STANDARD
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700667 && windowingMode == WINDOWING_MODE_UNDEFINED) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700668 // Standard activity types will mostly take on the windowing mode of the display if one
669 // isn't specified, so look-up a compatible stack based on the display's windowing mode.
670 windowingMode = display.getWindowingMode();
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700671 }
672 return super.isCompatible(windowingMode, activityType);
673 }
674
Andrii Kulian839def92016-11-02 10:58:58 -0700675 /** Adds the stack to specified display and calls WindowManager to do the same. */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700676 void reparent(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700677 // TODO: We should probably resolve the windowing mode for the stack on the new display here
678 // 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 -0800679 removeFromDisplay();
Winson Chungf34c6022018-01-25 09:44:30 -0800680 // Reparent the window container before we try to update the position when adding it to
681 // the new display below
Wale Ogunwale1666e312016-12-16 11:27:18 -0800682 mTmpRect2.setEmpty();
Winson Chungf34c6022018-01-25 09:44:30 -0800683 mWindowContainerController.reparent(activityDisplay.mDisplayId, mTmpRect2, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800684 postAddToDisplay(activityDisplay, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
Wale Ogunwale66e16852017-10-19 13:35:52 -0700685 adjustFocusToNextFocusableStack("reparent", true /* allowFocusSelf */);
Andrii Kulian250d6532017-02-08 23:30:45 -0800686 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Andrii Kulian51c1b672017-04-07 18:39:32 -0700687 // Update visibility of activities before notifying WM. This way it won't try to resize
688 // windows that are no longer visible.
689 mStackSupervisor.ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
690 !PRESERVE_WINDOWS);
Andrii Kulian839def92016-11-02 10:58:58 -0700691 }
692
693 /**
694 * Updates internal state after adding to new display.
695 * @param activityDisplay New display to which this stack was attached.
696 * @param bounds Updated bounds.
697 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700698 private void postAddToDisplay(ActivityDisplay activityDisplay, Rect bounds, boolean onTop) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700699 mDisplayId = activityDisplay.mDisplayId;
Bryce Leef3c6a472017-11-14 14:53:06 -0800700 setBounds(bounds);
Andrii Kulian1779e612016-10-12 21:58:25 -0700701 onParentChanged();
Wale Ogunwale961f4852016-02-01 20:25:54 -0800702
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700703 activityDisplay.addChild(this, onTop ? POSITION_TOP : POSITION_BOTTOM);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700704 if (inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale961f4852016-02-01 20:25:54 -0800705 // If we created a docked stack we want to resize it so it resizes all other stacks
706 // in the system.
707 mStackSupervisor.resizeDockedStackLocked(
Bryce Leef3c6a472017-11-14 14:53:06 -0800708 getOverrideBounds(), null, null, null, null, PRESERVE_WINDOWS);
Wale Ogunwale961f4852016-02-01 20:25:54 -0800709 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700710 }
711
Andrii Kulian839def92016-11-02 10:58:58 -0700712 /**
Andrii Kulian839def92016-11-02 10:58:58 -0700713 * Updates the inner state of the stack to remove it from its current parent, so it can be
714 * either destroyed completely or re-parented.
715 */
716 private void removeFromDisplay() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700717 final ActivityDisplay display = getDisplay();
718 if (display != null) {
719 display.removeChild(this);
720 }
721 mDisplayId = INVALID_DISPLAY;
Andrii Kulian839def92016-11-02 10:58:58 -0700722 }
723
724 /** Removes the stack completely. Also calls WindowManager to do the same on its side. */
725 void remove() {
726 removeFromDisplay();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800727 mWindowContainerController.removeContainer();
728 mWindowContainerController = null;
Andrii Kulian6d6fb402016-10-26 16:15:25 -0700729 onParentChanged();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700730 }
731
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700732 ActivityDisplay getDisplay() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700733 return mStackSupervisor.getActivityDisplay(mDisplayId);
734 }
735
Matthew Ngaa2b6202017-02-10 14:48:21 -0800736 /**
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700737 * @see #getStackDockedModeBounds(Rect, Rect, Rect, boolean)
Matthew Ngaa2b6202017-02-10 14:48:21 -0800738 */
739 void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
740 Rect outTempTaskBounds, boolean ignoreVisibility) {
741 mWindowContainerController.getStackDockedModeBounds(currentTempTaskBounds,
742 outStackBounds, outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800743 }
744
745 void prepareFreezingTaskBounds() {
746 mWindowContainerController.prepareFreezingTaskBounds();
747 }
748
Wale Ogunwale1666e312016-12-16 11:27:18 -0800749 void getWindowContainerBounds(Rect outBounds) {
750 if (mWindowContainerController != null) {
751 mWindowContainerController.getBounds(outBounds);
Winson Chungb00dc5e2017-01-25 09:44:25 -0800752 return;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800753 }
754 outBounds.setEmpty();
755 }
756
Matthew Ngaa2b6202017-02-10 14:48:21 -0800757 void getBoundsForNewConfiguration(Rect outBounds) {
758 mWindowContainerController.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800759 }
760
761 void positionChildWindowContainerAtTop(TaskRecord child) {
762 mWindowContainerController.positionChildAtTop(child.getWindowContainerController(),
763 true /* includingParents */);
764 }
765
Jorim Jaggi192086e2016-03-11 17:17:03 +0100766 /**
Winson Chung8bca9e42017-04-16 15:59:43 -0700767 * Returns whether to defer the scheduling of the multi-window mode.
768 */
769 boolean deferScheduleMultiWindowModeChanged() {
770 return false;
771 }
772
773 /**
Jorim Jaggi192086e2016-03-11 17:17:03 +0100774 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
775 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
776 */
777 void deferUpdateBounds() {
778 if (!mUpdateBoundsDeferred) {
779 mUpdateBoundsDeferred = true;
780 mUpdateBoundsDeferredCalled = false;
781 }
782 }
783
784 /**
785 * Continues updating bounds after updates have been deferred. If there was a resize attempt
786 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
787 * be resized to that bounds.
788 */
789 void continueUpdateBounds() {
790 final boolean wasDeferred = mUpdateBoundsDeferred;
791 mUpdateBoundsDeferred = false;
792 if (wasDeferred && mUpdateBoundsDeferredCalled) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800793 resize(mDeferredBounds.isEmpty() ? null : mDeferredBounds,
Jorim Jaggi192086e2016-03-11 17:17:03 +0100794 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
795 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
796 }
797 }
798
799 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
800 Rect tempTaskInsetBounds) {
801 if (!mUpdateBoundsDeferred) {
802 return true;
803 }
804 if (bounds != null) {
805 mDeferredBounds.set(bounds);
806 } else {
807 mDeferredBounds.setEmpty();
808 }
809 if (tempTaskBounds != null) {
810 mDeferredTaskBounds.set(tempTaskBounds);
811 } else {
812 mDeferredTaskBounds.setEmpty();
813 }
814 if (tempTaskInsetBounds != null) {
815 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
816 } else {
817 mDeferredTaskInsetBounds.setEmpty();
818 }
819 mUpdateBoundsDeferredCalled = true;
820 return false;
821 }
822
Bryce Leef3c6a472017-11-14 14:53:06 -0800823 @Override
824 public int setBounds(Rect bounds) {
825 return super.setBounds(!inMultiWindowMode() ? null : bounds);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 }
Craig Mautner5962b122012-10-05 14:45:52 -0700827
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700828 ActivityRecord topRunningActivityLocked() {
Winson Chung3f103eb2017-04-12 21:53:59 -0700829 return topRunningActivityLocked(false /* focusableOnly */);
830 }
831
Jorim Jaggiea039a82017-08-02 14:37:49 +0200832 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
833 outActivities.clear();
834 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
835 mTaskHistory.get(taskNdx).getAllRunningVisibleActivitiesLocked(outActivities);
836 }
837 }
838
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700839 private ActivityRecord topRunningActivityLocked(boolean focusableOnly) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800840 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700841 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Winson Chung3f103eb2017-04-12 21:53:59 -0700842 if (r != null && (!focusableOnly || r.isFocusable())) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700843 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800844 }
845 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846 return null;
847 }
848
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700849 ActivityRecord topRunningNonOverlayTaskActivity() {
850 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
851 final TaskRecord task = mTaskHistory.get(taskNdx);
852 final ArrayList<ActivityRecord> activities = task.mActivities;
853 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
854 final ActivityRecord r = activities.get(activityNdx);
855 if (!r.finishing && !r.mTaskOverlay) {
856 return r;
857 }
858 }
859 }
860 return null;
861 }
862
863 ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800864 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
865 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800866 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800867 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
868 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -0700869 if (!r.finishing && !r.delayedResume && r != notTop && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800870 return r;
871 }
872 }
873 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700874 return null;
875 }
876
877 /**
878 * This is a simplified version of topRunningActivityLocked that provides a number of
879 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800880 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700881 * @param token If non-null, any history records matching this token will be skipped.
882 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800883 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700884 * @return Returns the HistoryRecord of the next activity on the stack.
885 */
886 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800887 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
888 TaskRecord task = mTaskHistory.get(taskNdx);
889 if (task.taskId == taskId) {
890 continue;
891 }
892 ArrayList<ActivityRecord> activities = task.mActivities;
893 for (int i = activities.size() - 1; i >= 0; --i) {
894 final ActivityRecord r = activities.get(i);
895 // Note: the taskId check depends on real taskId fields being non-zero
Chong Zhang87761972016-08-22 13:53:24 -0700896 if (!r.finishing && (token != r.appToken) && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800897 return r;
898 }
899 }
900 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700901 return null;
902 }
903
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800904 ActivityRecord getTopActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700905 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800906 final ActivityRecord r = mTaskHistory.get(taskNdx).getTopActivity();
907 if (r != null) {
908 return r;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700909 }
910 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700911 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700912 }
913
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700914 final TaskRecord topTask() {
915 final int size = mTaskHistory.size();
916 if (size > 0) {
917 return mTaskHistory.get(size - 1);
918 }
919 return null;
920 }
921
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800922 private TaskRecord bottomTask() {
Winson Chung1cebea62017-06-26 17:22:27 -0700923 if (mTaskHistory.isEmpty()) {
924 return null;
925 }
926 return mTaskHistory.get(0);
927 }
928
Craig Mautnerd2328952013-03-05 12:46:26 -0800929 TaskRecord taskForIdLocked(int id) {
930 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
931 final TaskRecord task = mTaskHistory.get(taskNdx);
932 if (task.taskId == id) {
933 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800934 }
935 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700936 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 }
938
Craig Mautnerd2328952013-03-05 12:46:26 -0800939 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700940 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800941 return isInStackLocked(r);
942 }
943
944 ActivityRecord isInStackLocked(ActivityRecord r) {
945 if (r == null) {
946 return null;
947 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700948 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700949 final ActivityStack stack = r.getStack();
950 if (stack != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
951 if (stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800952 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800953 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800954 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800955 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800956 }
957
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800958 boolean isInStackLocked(TaskRecord task) {
959 return mTaskHistory.contains(task);
960 }
961
962 /** Checks if there are tasks with specific UID in the stack. */
963 boolean isUidPresent(int uid) {
964 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -0800965 for (ActivityRecord r : task.mActivities) {
966 if (r.getUid() == uid) {
967 return true;
968 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800969 }
970 }
971 return false;
972 }
973
974 /** Get all UIDs that are present in the stack. */
975 void getPresentUIDs(IntArray presentUIDs) {
976 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -0800977 for (ActivityRecord r : task.mActivities) {
978 presentUIDs.add(r.getUid());
979 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800980 }
981 }
982
Winson Chungabb433b2017-03-24 09:35:42 -0700983 final void removeActivitiesFromLRUListLocked(TaskRecord task) {
984 for (ActivityRecord r : task.mActivities) {
985 mLRUActivities.remove(r);
986 }
987 }
988
Craig Mautner2420ead2013-04-01 17:13:20 -0700989 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700990 final boolean hadit = mLRUActivities.remove(r);
991 mLRUActivities.add(r);
992 return hadit;
993 }
994
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800995 final boolean isHomeOrRecentsStack() {
Wale Ogunwale68278562017-09-23 17:13:55 -0700996 return isActivityTypeHome() || isActivityTypeRecents();
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800997 }
998
Craig Mautnere0a38842013-12-16 16:14:02 -0800999 final boolean isOnHomeDisplay() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001000 return mDisplayId == DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08001001 }
1002
Wale Ogunwale66e16852017-10-19 13:35:52 -07001003 private boolean returnsToHomeStack() {
1004 return !inMultiWindowMode()
1005 && !mTaskHistory.isEmpty()
1006 && mTaskHistory.get(0).returnsToHomeStack();
1007 }
1008
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001009 void moveToFront(String reason) {
1010 moveToFront(reason, null);
1011 }
1012
1013 /**
1014 * @param reason The reason for moving the stack to the front.
1015 * @param task If non-null, the task will be moved to the top of the stack.
1016 * */
1017 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001018 if (!isAttached()) {
1019 return;
1020 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07001021
Wale Ogunwalebb285872018-03-01 13:05:30 -08001022 final ActivityDisplay display = getDisplay();
1023
1024 if (inSplitScreenSecondaryWindowingMode()) {
1025 // If the stack is in split-screen seconardy mode, we need to make sure we move the
1026 // primary split-screen stack forward in the case it is currently behind a fullscreen
1027 // stack so both halves of the split-screen appear on-top and the fullscreen stack isn't
1028 // cutting between them.
1029 // TODO(b/70677280): This is a workaround until we can fix as part of b/70677280.
1030 final ActivityStack topFullScreenStack =
1031 display.getTopStackInWindowingMode(WINDOWING_MODE_FULLSCREEN);
1032 if (topFullScreenStack != null) {
1033 final ActivityStack primarySplitScreenStack = display.getSplitScreenPrimaryStack();
1034 if (display.getIndexOf(topFullScreenStack)
1035 > display.getIndexOf(primarySplitScreenStack)) {
1036 primarySplitScreenStack.moveToFront(reason + " splitScreenToTop");
1037 }
1038 }
1039 }
1040
Wale Ogunwale66e16852017-10-19 13:35:52 -07001041 if (!isActivityTypeHome() && returnsToHomeStack()) {
1042 // Make sure the home stack is behind this stack since that is where we should return to
1043 // when this stack is no longer visible.
1044 mStackSupervisor.moveHomeStackToFront(reason + " returnToHome");
1045 }
1046
Wale Ogunwalebb285872018-03-01 13:05:30 -08001047 display.positionChildAtTop(this);
Andrii Kulian839def92016-11-02 10:58:58 -07001048 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001049 if (task != null) {
1050 insertTaskAtTop(task, null);
Wale Ogunwaleaa47c122016-09-23 16:39:53 -07001051 return;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001052 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001053 }
1054
Evan Rosky9c448172017-11-02 14:19:27 -07001055 /**
1056 * @param reason The reason for moving the stack to the back.
1057 * @param task If non-null, the task will be moved to the bottom of the stack.
1058 **/
1059 void moveToBack(String reason, TaskRecord task) {
1060 if (!isAttached()) {
1061 return;
1062 }
1063
Bryce Lee8cab4a02018-01-05 09:00:49 -08001064 /**
1065 * The intent behind moving a primary split screen stack to the back is usually to hide
1066 * behind the home stack. Exit split screen in this case.
1067 */
1068 if (getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
1069 setWindowingMode(WINDOWING_MODE_FULLSCREEN);
1070 }
1071
Evan Rosky9c448172017-11-02 14:19:27 -07001072 getDisplay().positionChildAtBottom(this);
1073 mStackSupervisor.setFocusStackUnchecked(reason, getDisplay().getTopStack());
1074 if (task != null) {
1075 insertTaskAtBottom(task);
1076 return;
Evan Rosky9c448172017-11-02 14:19:27 -07001077 }
1078 }
1079
Wale Ogunwaled046a012015-12-24 13:05:59 -08001080 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001081 final ActivityRecord r = topRunningActivityLocked();
Bryce Lee7b851cc2018-04-10 14:53:13 -07001082 return mStackSupervisor.isFocusable(this, r != null && r.isFocusable());
Wale Ogunwaled046a012015-12-24 13:05:59 -08001083 }
1084
Craig Mautnere0a38842013-12-16 16:14:02 -08001085 final boolean isAttached() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001086 return getParent() != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001087 }
1088
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 /**
Wale Ogunwale39381972015-12-17 17:15:29 -08001090 * Returns the top activity in any existing task matching the given Intent in the input result.
1091 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092 */
Wale Ogunwale39381972015-12-17 17:15:29 -08001093 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001094 Intent intent = target.intent;
1095 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096 ComponentName cls = intent.getComponent();
1097 if (info.targetActivity != null) {
1098 cls = new ComponentName(info.packageName, info.targetActivity);
1099 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001100 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -07001101 boolean isDocument = intent != null & intent.isDocument();
1102 // If documentData is non-null then it must match the existing task data.
1103 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -08001104
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001105 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -08001106 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1107 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001108 if (task.voiceSession != null) {
1109 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001110 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -07001111 continue;
1112 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001113 if (task.userId != userId) {
1114 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001115 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -07001116 continue;
1117 }
Bryce Lee9f6affd2017-09-01 09:18:35 -07001118
1119 // Overlays should not be considered as the task's logical top activity.
1120 final ActivityRecord r = task.getTopActivity(false /* includeOverlays */);
Craig Mautner000f0022013-02-26 15:04:29 -08001121 if (r == null || r.finishing || r.userId != userId ||
1122 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001123 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -08001124 continue;
1125 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001126 if (!r.hasCompatibleActivityType(target)) {
Chong Zhangb546f7e2015-08-05 14:21:36 -07001127 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
1128 continue;
1129 }
Craig Mautner000f0022013-02-26 15:04:29 -08001130
Craig Mautnerd00f4742014-03-12 14:17:26 -07001131 final Intent taskIntent = task.intent;
1132 final Intent affinityIntent = task.affinityIntent;
1133 final boolean taskIsDocument;
1134 final Uri taskDocumentData;
1135 if (taskIntent != null && taskIntent.isDocument()) {
1136 taskIsDocument = true;
1137 taskDocumentData = taskIntent.getData();
1138 } else if (affinityIntent != null && affinityIntent.isDocument()) {
1139 taskIsDocument = true;
1140 taskDocumentData = affinityIntent.getData();
1141 } else {
1142 taskIsDocument = false;
1143 taskDocumentData = null;
1144 }
1145
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001146 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -07001147 + taskIntent.getComponent().flattenToShortString()
Bryce Leeaf691c02017-03-20 14:20:22 -07001148 + "/aff=" + r.getTask().rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001149 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001150 // TODO Refactor to remove duplications. Check if logic can be simplified.
1151 if (taskIntent != null && taskIntent.getComponent() != null &&
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001152 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001153 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001154 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001155 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001156 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1157 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -08001158 result.r = r;
1159 result.matchedByRootAffinity = false;
1160 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001161 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
1162 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001163 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001164 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001165 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001166 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1167 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -08001168 result.r = r;
1169 result.matchedByRootAffinity = false;
1170 break;
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001171 } else if (!isDocument && !taskIsDocument
Winson Chung5b895b72017-05-01 13:46:25 -07001172 && result.r == null && task.rootAffinity != null) {
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001173 if (task.rootAffinity.equals(target.taskAffinity)) {
1174 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
1175 // It is possible for multiple tasks to have the same root affinity especially
1176 // if they are in separate stacks. We save off this candidate, but keep looking
1177 // to see if there is a better candidate.
1178 result.r = r;
1179 result.matchedByRootAffinity = true;
1180 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001181 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001182 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183 }
1184
1185 /**
1186 * Returns the first activity (starting from the top of the stack) that
1187 * is the same as the given activity. Returns null if no such activity
1188 * is found.
1189 */
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001190 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
1191 boolean compareIntentFilters) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001192 ComponentName cls = intent.getComponent();
1193 if (info.targetActivity != null) {
1194 cls = new ComponentName(info.packageName, info.targetActivity);
1195 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001196 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197
Craig Mautner000f0022013-02-26 15:04:29 -08001198 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001199 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001200 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001201
Craig Mautner000f0022013-02-26 15:04:29 -08001202 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1203 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001204 if (!r.okToShowLocked()) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -07001205 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001206 }
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001207 if (!r.finishing && r.userId == userId) {
1208 if (compareIntentFilters) {
1209 if (r.intent.filterEquals(intent)) {
1210 return r;
1211 }
1212 } else {
1213 if (r.intent.getComponent().equals(cls)) {
1214 return r;
1215 }
1216 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001217 }
1218 }
1219 }
1220
1221 return null;
1222 }
1223
Amith Yamasani742a6712011-05-04 14:49:28 -07001224 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -07001225 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -07001226 */
Craig Mautner93529a42013-10-04 15:03:13 -07001227 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001228 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -07001229 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001230 }
1231 mCurrentUser = userId;
1232
1233 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001234 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001235 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001236 final TaskRecord task = mTaskHistory.get(i);
1237
Chong Zhang87761972016-08-22 13:53:24 -07001238 if (task.okToShowLocked()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001239 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001240 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001241 mTaskHistory.remove(i);
1242 mTaskHistory.add(task);
1243 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001244 // Use same value for i.
1245 } else {
1246 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001247 }
1248 }
Amith Yamasani742a6712011-05-04 14:49:28 -07001249 }
1250
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 void minimalResumeActivityLocked(ActivityRecord r) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001252 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
1253 + " callers=" + Debug.getCallers(5));
Bryce Leec4ab62a2018-03-05 14:19:26 -08001254 r.setState(RESUMED, "minimalResumeActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001255 r.completeResumeLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -07001256 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001257 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
1258 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 }
1260
Narayan Kamath7829c812015-06-08 17:39:43 +01001261 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001262 if (mFullyDrawnStartTime != 0) {
1263 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1264 }
Narayan Kamath7829c812015-06-08 17:39:43 +01001265 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -07001266 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1267 }
1268
1269 private void stopFullyDrawnTraceIfNeeded() {
1270 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
1271 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1272 mFullyDrawnStartTime = 0;
1273 }
1274 }
1275
Craig Mautnere79d42682013-04-01 19:01:53 -07001276 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001277 if (r.displayStartTime == 0) {
1278 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
1279 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001280 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001281 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001282 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001283 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001284 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001285 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001287 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001288
Andrii Kulian21713ac2016-10-12 22:05:05 -07001289 private void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -07001290 // Make sure that there is no activity waiting for this to launch.
1291 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
1292 r.displayStartTime = r.fullyDrawnStartTime = 0;
1293 } else {
1294 mStackSupervisor.removeTimeoutsForActivityLocked(r);
1295 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1296 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001297 }
1298
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001299 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001300 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001301 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1302 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1303 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1304 activities.get(activityNdx).setSleeping(false);
1305 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001306 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -08001307 if (mPausingActivity != null) {
1308 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
1309 activityPausedLocked(mPausingActivity.appToken, true);
1310 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001311 }
1312
Todd Kennedy39bfee52016-02-24 10:28:21 -08001313 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1314 final String packageName = aInfo.packageName;
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001315 final int userId = UserHandle.getUserId(aInfo.uid);
1316
Todd Kennedy39bfee52016-02-24 10:28:21 -08001317 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1318 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1319 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001320 final ActivityRecord ar = activities.get(activityNdx);
1321
1322 if ((userId == ar.userId) && packageName.equals(ar.packageName)) {
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -07001323 ar.updateApplicationInfo(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08001324 }
1325 }
1326 }
1327 }
1328
David Stevens9440dc82017-03-16 19:00:20 -07001329 void checkReadyForSleep() {
1330 if (shouldSleepActivities() && goToSleepIfPossible(false /* shuttingDown */)) {
1331 mStackSupervisor.checkReadyForSleepLocked(true /* allowDelay */);
1332 }
1333 }
1334
Craig Mautner0eea92c2013-05-16 13:35:39 -07001335 /**
David Stevens9440dc82017-03-16 19:00:20 -07001336 * Tries to put the activities in the stack to sleep.
1337 *
1338 * If the stack is not in a state where its activities can be put to sleep, this function will
1339 * start any necessary actions to move the stack into such a state. It is expected that this
1340 * function get called again when those actions complete.
1341 *
1342 * @param shuttingDown true when the called because the device is shutting down.
David Stevens18abd0e2017-08-17 14:55:47 -07001343 * @return true if the stack finished going to sleep, false if the stack only started the
1344 * process of going to sleep (checkReadyForSleep will be called when that process finishes).
Craig Mautner0eea92c2013-05-16 13:35:39 -07001345 */
David Stevens9440dc82017-03-16 19:00:20 -07001346 boolean goToSleepIfPossible(boolean shuttingDown) {
1347 boolean shouldSleep = true;
1348
Craig Mautner0eea92c2013-05-16 13:35:39 -07001349 if (mResumedActivity != null) {
1350 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001351 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
1352 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1353 "Sleep => pause with userLeaving=false");
Bryce Lee5daa3122017-04-19 10:40:42 -07001354
chaviwa1d68532018-03-16 15:33:43 -07001355 startPausingLocked(false, true, null, false);
David Stevens9440dc82017-03-16 19:00:20 -07001356 shouldSleep = false ;
1357 } else if (mPausingActivity != null) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001358 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001359 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
David Stevens9440dc82017-03-16 19:00:20 -07001360 shouldSleep = false;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001361 }
David Stevens9440dc82017-03-16 19:00:20 -07001362
1363 if (!shuttingDown) {
1364 if (containsActivityFromStack(mStackSupervisor.mStoppingActivities)) {
1365 // Still need to tell some activities to stop; can't sleep yet.
1366 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
1367 + mStackSupervisor.mStoppingActivities.size() + " activities");
1368
1369 mStackSupervisor.scheduleIdleLocked();
1370 shouldSleep = false;
1371 }
1372
1373 if (containsActivityFromStack(mStackSupervisor.mGoingToSleepActivities)) {
1374 // Still need to tell some activities to sleep; can't sleep yet.
1375 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
1376 + mStackSupervisor.mGoingToSleepActivities.size() + " activities");
1377 shouldSleep = false;
1378 }
1379 }
1380
1381 if (shouldSleep) {
1382 goToSleep();
1383 }
1384
David Stevens18abd0e2017-08-17 14:55:47 -07001385 return shouldSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -07001386 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001387
David Stevens18abd0e2017-08-17 14:55:47 -07001388 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001389 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001390
Andrii Kulianf9949d52016-05-06 12:53:25 -07001391 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001392 // This ensures that the activity's onStop() is called.
1393 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1394 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1395 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1396 final ActivityRecord r = activities.get(activityNdx);
Bryce Lee7ace3952018-02-16 14:34:32 -08001397 if (r.isState(STOPPING, STOPPED, PAUSED, PAUSING)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001398 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001399 }
1400 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001401 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 }
Craig Mautner59c00972012-07-30 12:10:24 -07001403
David Stevens9440dc82017-03-16 19:00:20 -07001404 private boolean containsActivityFromStack(List<ActivityRecord> rs) {
1405 for (ActivityRecord r : rs) {
1406 if (r.getStack() == this) {
1407 return true;
1408 }
1409 }
1410 return false;
1411 }
1412
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001413 /**
Winson Chung4dabf232017-01-25 13:25:22 -08001414 * Schedule a pause timeout in case the app doesn't respond. We don't give it much time because
1415 * this directly impacts the responsiveness seen by the user.
1416 */
1417 private void schedulePauseTimeout(ActivityRecord r) {
1418 final Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1419 msg.obj = r;
1420 r.pauseTime = SystemClock.uptimeMillis();
1421 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1422 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
1423 }
1424
1425 /**
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001426 * Start pausing the currently resumed activity. It is an error to call this if there
1427 * is already an activity being paused or there is no resumed activity.
1428 *
1429 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1430 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1431 * screen turning off).
Wale Ogunwale950faff2016-08-08 09:51:04 -07001432 * @param resuming The activity we are currently trying to resume or null if this is not being
1433 * called as part of resuming the top activity, so we shouldn't try to instigate
1434 * a resume here if not null.
Winson Chung6954fc92017-03-24 16:22:12 -07001435 * @param pauseImmediately True if the caller does not want to wait for the activity callback to
1436 * complete pausing.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001437 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1438 * it to tell us when it is done.
1439 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001440 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
Winson Chung6954fc92017-03-24 16:22:12 -07001441 ActivityRecord resuming, boolean pauseImmediately) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001442 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001443 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
Bryce Lee7ace3952018-02-16 14:34:32 -08001444 + " state=" + mPausingActivity.getState());
David Stevens9440dc82017-03-16 19:00:20 -07001445 if (!shouldSleepActivities()) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001446 // Avoid recursion among check for sleep and complete pause during sleeping.
1447 // Because activity will be paused immediately after resume, just let pause
1448 // be completed by the order of activity paused from clients.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001449 completePauseLocked(false, resuming);
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001450 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001451 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001452 ActivityRecord prev = mResumedActivity;
Bryce Lee5daa3122017-04-19 10:40:42 -07001453
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 if (prev == null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07001455 if (resuming == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001456 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001457 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001458 }
1459 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001460 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001461
Andrii Kulianb1888aa2018-02-16 14:40:42 -08001462 if (prev == resuming) {
1463 Slog.wtf(TAG, "Trying to pause activity that is in process of being resumed");
1464 return false;
1465 }
1466
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001467 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001468 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001469 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001470 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001471 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1472 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Bryce Lee7ace3952018-02-16 14:34:32 -08001473 prev.setState(PAUSING, "startPausingLocked");
Bryce Leeaf691c02017-03-20 14:20:22 -07001474 prev.getTask().touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001475 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001476 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001477
Dianne Hackborncee04b52013-07-03 17:01:28 -07001478 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479
1480 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001481
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001483 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484 try {
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08001485 EventLogTags.writeAmPauseActivity(prev.userId, System.identityHashCode(prev),
1486 prev.shortComponentName, "userLeaving=" + userLeaving);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001487 mService.updateUsageStats(prev, false);
Andrii Kulian446e8242017-10-26 15:17:29 -07001488
Bryce Leeb0f993f2018-03-02 15:38:01 -08001489 mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001490 PauseActivityItem.obtain(prev.finishing, userLeaving,
Bryce Lee1d0d5142018-04-12 10:35:07 -07001491 prev.configChangeFlags, pauseImmediately));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001492 } catch (Exception e) {
1493 // Ignore exception, if process died other code will cleanup.
1494 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001495 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001497 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001498 }
1499 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001500 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001502 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 }
1504
1505 // If we are not going to sleep, we want to ensure the device is
1506 // awake until the next activity is started.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001507 if (!uiSleeping && !mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001508 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509 }
1510
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001511 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 // Have the window manager pause its key dispatching until the new
1513 // activity has started. If we're pausing the activity just because
1514 // the screen is being turned off and the UI is sleeping, don't interrupt
1515 // key dispatch; the same activity will pick it up again on wakeup.
1516 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001517 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001518 } else if (DEBUG_PAUSE) {
1519 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 }
1521
Winson Chung6954fc92017-03-24 16:22:12 -07001522 if (pauseImmediately) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001523 // If the caller said they don't want to wait for the pause, then complete
1524 // the pause now.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001525 completePauseLocked(false, resuming);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001526 return false;
1527
1528 } else {
Winson Chung4dabf232017-01-25 13:25:22 -08001529 schedulePauseTimeout(prev);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001530 return true;
1531 }
1532
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 } else {
1534 // This activity failed to schedule the
1535 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001536 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Wale Ogunwale950faff2016-08-08 09:51:04 -07001537 if (resuming == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001538 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001539 }
1540 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541 }
1542 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001543
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001544 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001545 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1546 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547
Craig Mautnerd2328952013-03-05 12:46:26 -08001548 final ActivityRecord r = isInStackLocked(token);
1549 if (r != null) {
1550 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1551 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001552 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001553 + (timeout ? " (due to timeout)" : " (pause complete)"));
Jorim Jaggife762342016-10-13 14:33:27 +02001554 mService.mWindowManager.deferSurfaceLayout();
1555 try {
Winson Chung3f103eb2017-04-12 21:53:59 -07001556 completePauseLocked(true /* resumeNext */, null /* resumingActivity */);
Jorim Jaggife762342016-10-13 14:33:27 +02001557 } finally {
1558 mService.mWindowManager.continueSurfaceLayout();
1559 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001560 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001561 } else {
1562 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1563 r.userId, System.identityHashCode(r), r.shortComponentName,
1564 mPausingActivity != null
1565 ? mPausingActivity.shortComponentName : "(none)");
Bryce Lee7ace3952018-02-16 14:34:32 -08001566 if (r.isState(PAUSING)) {
1567 r.setState(PAUSED, "activityPausedLocked");
riddle_hsu9caeef72015-10-20 16:34:05 +08001568 if (r.finishing) {
1569 if (DEBUG_PAUSE) Slog.v(TAG,
1570 "Executing finish of failed to pause activity: " + r);
Bryce Leef52974c2018-02-14 15:12:01 -08001571 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false,
1572 "activityPausedLocked");
riddle_hsu9caeef72015-10-20 16:34:05 +08001573 }
louis_chang047dfd42015-04-08 16:35:55 +08001574 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 }
1576 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001577 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 }
1579
Wale Ogunwale950faff2016-08-08 09:51:04 -07001580 private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001581 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001582 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001583
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001584 if (prev != null) {
Bryce Lee7ace3952018-02-16 14:34:32 -08001585 final boolean wasStopping = prev.isState(STOPPING);
1586 prev.setState(PAUSED, "completePausedLocked");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001587 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001588 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Bryce Leef52974c2018-02-14 15:12:01 -08001589 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false,
1590 "completedPausedLocked");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001591 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001592 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1593 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Bryce Lee4a194382017-04-04 14:32:48 -07001594 if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001595 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1596 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001597 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001598 if (prev.deferRelaunchUntilPaused) {
1599 // Complete the deferred relaunch that was waiting for pause to complete.
1600 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001601 prev.relaunchActivityLocked(false /* andResume */,
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001602 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001603 } else if (wasStopping) {
1604 // We are also stopping, the stop request must have gone soon after the pause.
1605 // We can't clobber it, because the stop confirmation will not be handled.
1606 // We don't need to schedule another stop, we only need to let it happen.
Bryce Lee7ace3952018-02-16 14:34:32 -08001607 prev.setState(STOPPING, "completePausedLocked");
David Stevens9440dc82017-03-16 19:00:20 -07001608 } else if (!prev.visible || shouldSleepOrShutDownActivities()) {
Wale Ogunwaleec950642017-04-25 07:44:21 -07001609 // Clear out any deferred client hide we might currently have.
1610 prev.setDeferHidingClient(false);
Jose Lima4b6c6692014-08-12 17:41:12 -07001611 // If we were visible then resumeTopActivities will release resources before
Wale Ogunwaleec950642017-04-25 07:44:21 -07001612 // stopping.
Winson Chung4dabf232017-01-25 13:25:22 -08001613 addToStopping(prev, true /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001614 }
1615 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001616 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001617 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001618 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001619 // It is possible the activity was freezing the screen before it was paused.
1620 // In that case go ahead and remove the freeze this activity has on the screen
1621 // since it is no longer visible.
Wale Ogunwalee8524002016-09-13 16:34:57 -07001622 if (prev != null) {
1623 prev.stopFreezingScreenLocked(true /*force*/);
1624 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001625 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001627
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001628 if (resumeNext) {
1629 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
David Stevens9440dc82017-03-16 19:00:20 -07001630 if (!topStack.shouldSleepOrShutDownActivities()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001631 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001632 } else {
David Stevens9440dc82017-03-16 19:00:20 -07001633 checkReadyForSleep();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001634 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001635 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001636 // If there are no more activities available to run, do resume anyway to start
1637 // something. Also if the top activity on the stack is not the just paused
1638 // activity, we need to go ahead and resume it to ensure we complete an
1639 // in-flight app switch.
1640 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001641 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001642 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001643 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001644
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001645 if (prev != null) {
1646 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001647
Craig Mautner525f3d92013-05-07 14:01:50 -07001648 if (prev.app != null && prev.cpuTimeAtResume > 0
1649 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001650 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1651 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001652 if (diff > 0) {
1653 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1654 synchronized (bsi) {
1655 BatteryStatsImpl.Uid.Proc ps =
1656 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001657 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001658 if (ps != null) {
1659 ps.addForegroundTimeLocked(diff);
1660 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 }
1662 }
1663 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001664 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001666
Andrii Kulian8290f8f2016-06-30 17:51:32 -07001667 // Notify when the task stack has changed, but only if visibilities changed (not just
1668 // focus). Also if there is an active pinned stack - we always want to notify it about
1669 // task stack changes, because its positioning may depend on it.
1670 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001671 || getDisplay().hasPinnedStack()) {
Yorke Leebd54c2a2016-10-25 13:49:23 -07001672 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001673 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1674 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001675
Wale Ogunwale950faff2016-08-08 09:51:04 -07001676 mStackSupervisor.ensureActivitiesVisibleLocked(resuming, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677 }
1678
Winson Chung4dabf232017-01-25 13:25:22 -08001679 void addToStopping(ActivityRecord r, boolean scheduleIdle, boolean idleDelayed) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001680 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1681 mStackSupervisor.mStoppingActivities.add(r);
1682 }
1683
1684 // If we already have a few activities waiting to stop, then give up
1685 // on things going idle and start clearing them out. Or if r is the
1686 // last of activity of the last task the stack will be empty and must
1687 // be cleared immediately.
1688 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1689 || (r.frontOfTask && mTaskHistory.size() <= 1);
Winson Chung4dabf232017-01-25 13:25:22 -08001690 if (scheduleIdle || forceIdle) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001691 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
Winson Chung4dabf232017-01-25 13:25:22 -08001692 + forceIdle + "immediate=" + !idleDelayed);
1693 if (!idleDelayed) {
1694 mStackSupervisor.scheduleIdleLocked();
1695 } else {
1696 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1697 }
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001698 } else {
David Stevens9440dc82017-03-16 19:00:20 -07001699 checkReadyForSleep();
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001700 }
1701 }
1702
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001703 /**
1704 * Returns true if the stack is translucent and can have other contents visible behind it if
1705 * needed. A stack is considered translucent if it don't contain a visible or
1706 * starting (about to be visible) activity that is fullscreen (opaque).
1707 * @param starting The currently starting activity or null if there is none.
1708 */
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001709 @VisibleForTesting
Wale Ogunwale66e16852017-10-19 13:35:52 -07001710 boolean isStackTranslucent(ActivityRecord starting) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001711 if (!isAttached() || mForceHidden) {
1712 return true;
1713 }
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001714 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1715 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001716 final ArrayList<ActivityRecord> activities = task.mActivities;
1717 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1718 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001719
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001720 if (r.finishing) {
1721 // We don't factor in finishing activities when determining translucency since
1722 // they will be gone soon.
1723 continue;
1724 }
1725
Wale Ogunwale2cca8622017-12-11 08:40:13 -08001726 if (!r.visibleIgnoringKeyguard && r != starting) {
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001727 // Also ignore invisible activities that are not the currently starting
1728 // activity (about to be visible).
1729 continue;
1730 }
1731
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001732 if (r.fullscreen || r.hasWallpaper) {
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001733 // Stack isn't translucent if it has at least one fullscreen activity
1734 // that is visible.
1735 return false;
1736 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001737 }
1738 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001739 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001740 }
1741
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001742 boolean isTopStackOnDisplay() {
1743 return getDisplay().isTopStack(this);
1744 }
1745
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001746 boolean isTopActivityVisible() {
1747 final ActivityRecord topActivity = getTopActivity();
1748 return topActivity != null && topActivity.visible;
1749 }
1750
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001751 /**
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001752 * Returns true if the stack should be visible.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001753 *
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001754 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001755 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001756 boolean shouldBeVisible(ActivityRecord starting) {
Winson Chung47900652017-04-06 18:44:25 -07001757 if (!isAttached() || mForceHidden) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001758 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001759 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001760 if (mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001761 return true;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001762 }
1763
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001764 final ActivityRecord top = topRunningActivityLocked();
1765 if (top == null && isInStackLocked(starting) == null && !isTopStackOnDisplay()) {
1766 // Shouldn't be visible if you don't have any running activities, not starting one, and
1767 // not the top stack on display.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001768 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001769 }
1770
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001771 final ActivityDisplay display = getDisplay();
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001772 boolean gotSplitScreenStack = false;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001773 boolean gotOpaqueSplitScreenPrimary = false;
1774 boolean gotOpaqueSplitScreenSecondary = false;
Wale Ogunwale68278562017-09-23 17:13:55 -07001775 final int windowingMode = getWindowingMode();
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001776 final boolean isAssistantType = isActivityTypeAssistant();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001777 for (int i = display.getChildCount() - 1; i >= 0; --i) {
1778 final ActivityStack other = display.getChildAt(i);
1779 if (other == this) {
1780 // Should be visible if there is no other stack occluding it.
1781 return true;
1782 }
Wale Ogunwale68278562017-09-23 17:13:55 -07001783
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001784 final int otherWindowingMode = other.getWindowingMode();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001785
1786 if (otherWindowingMode == WINDOWING_MODE_FULLSCREEN) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07001787 if (other.isStackTranslucent(starting)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001788 // Can be visible behind a translucent fullscreen stack.
1789 continue;
1790 }
1791 return false;
1792 } else if (otherWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
1793 && !gotOpaqueSplitScreenPrimary) {
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001794 gotSplitScreenStack = true;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001795 gotOpaqueSplitScreenPrimary =
Wale Ogunwale66e16852017-10-19 13:35:52 -07001796 !other.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001797 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
1798 && gotOpaqueSplitScreenPrimary) {
1799 // Can not be visible behind another opaque stack in split-screen-primary mode.
1800 return false;
1801 }
1802 } else if (otherWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
1803 && !gotOpaqueSplitScreenSecondary) {
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001804 gotSplitScreenStack = true;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001805 gotOpaqueSplitScreenSecondary =
Wale Ogunwale66e16852017-10-19 13:35:52 -07001806 !other.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001807 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
1808 && gotOpaqueSplitScreenSecondary) {
1809 // Can not be visible behind another opaque stack in split-screen-secondary mode.
1810 return false;
1811 }
1812 }
1813 if (gotOpaqueSplitScreenPrimary && gotOpaqueSplitScreenSecondary) {
1814 // Can not be visible if we are in split-screen windowing mode and both halves of
1815 // the screen are opaque.
1816 return false;
1817 }
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001818 if (isAssistantType && gotSplitScreenStack) {
1819 // Assistant stack can't be visible behind split-screen. In addition to this not
1820 // making sense, it also works around an issue here we boost the z-order of the
1821 // assistant window surfaces in window manager whenever it is visible.
1822 return false;
1823 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001824 }
1825
1826 // Well, nothing is stopping you from being visible...
1827 return true;
Wale Ogunwale68278562017-09-23 17:13:55 -07001828 }
1829
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001830 final int rankTaskLayers(int baseLayer) {
1831 int layer = 0;
1832 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1833 final TaskRecord task = mTaskHistory.get(taskNdx);
1834 ActivityRecord r = task.topRunningActivityLocked();
1835 if (r == null || r.finishing || !r.visible) {
1836 task.mLayerRank = -1;
1837 } else {
1838 task.mLayerRank = baseLayer + layer++;
1839 }
1840 }
1841 return layer;
1842 }
1843
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001844 /**
1845 * Make sure that all activities that need to be visible (that is, they
1846 * currently can be seen by the user) actually are.
1847 */
Wale Ogunwale66e16852017-10-19 13:35:52 -07001848 // 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 -07001849 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1850 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02001851 mTopActivityOccludesKeyguard = false;
1852 mTopDismissingKeyguardActivity = null;
Bryce Lee2a3cc462017-10-27 10:57:35 -07001853 mStackSupervisor.getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02001854 try {
1855 ActivityRecord top = topRunningActivityLocked();
1856 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
1857 + " configChanges=0x" + Integer.toHexString(configChanges));
1858 if (top != null) {
1859 checkTranslucentActivityWaiting(top);
1860 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001861
Jorim Jaggife762342016-10-13 14:33:27 +02001862 // If the top activity is not fullscreen, then we need to
1863 // make sure any activities under it are now visible.
1864 boolean aboveTop = top != null;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001865 final boolean stackShouldBeVisible = shouldBeVisible(starting);
1866 boolean behindFullscreenActivity = !stackShouldBeVisible;
Jorim Jaggife762342016-10-13 14:33:27 +02001867 boolean resumeNextActivity = mStackSupervisor.isFocusedStack(this)
1868 && (isInStackLocked(starting) == null);
Wale Ogunwale86dde2b2018-01-18 06:15:45 -08001869 final boolean isTopNotPinnedStack =
1870 isAttached() && getDisplay().isTopNotPinnedStack(this);
Jorim Jaggife762342016-10-13 14:33:27 +02001871 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1872 final TaskRecord task = mTaskHistory.get(taskNdx);
1873 final ArrayList<ActivityRecord> activities = task.mActivities;
1874 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1875 final ActivityRecord r = activities.get(activityNdx);
1876 if (r.finishing) {
Jorim Jaggife762342016-10-13 14:33:27 +02001877 continue;
Chong Zhang22bc8512016-04-07 13:47:18 -07001878 }
Jorim Jaggife762342016-10-13 14:33:27 +02001879 final boolean isTop = r == top;
1880 if (aboveTop && !isTop) {
1881 continue;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001882 }
Jorim Jaggife762342016-10-13 14:33:27 +02001883 aboveTop = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001884
Jorim Jaggife762342016-10-13 14:33:27 +02001885 // Check whether activity should be visible without Keyguard influence
Jorim Jaggi241ae102016-11-02 21:57:33 -07001886 final boolean visibleIgnoringKeyguard = r.shouldBeVisibleIgnoringKeyguard(
Jorim Jaggi241ae102016-11-02 21:57:33 -07001887 behindFullscreenActivity);
1888 r.visibleIgnoringKeyguard = visibleIgnoringKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +02001889
1890 // Now check whether it's really visible depending on Keyguard state.
Jorim Jaggi241ae102016-11-02 21:57:33 -07001891 final boolean reallyVisible = checkKeyguardVisibility(r,
chaviw2c500982018-01-04 17:05:05 -08001892 visibleIgnoringKeyguard, isTop && isTopNotPinnedStack);
Jorim Jaggi241ae102016-11-02 21:57:33 -07001893 if (visibleIgnoringKeyguard) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001894 behindFullscreenActivity = updateBehindFullscreen(!stackShouldBeVisible,
Wale Ogunwale66e16852017-10-19 13:35:52 -07001895 behindFullscreenActivity, r);
Jorim Jaggife762342016-10-13 14:33:27 +02001896 }
1897 if (reallyVisible) {
1898 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
Bryce Lee7ace3952018-02-16 14:34:32 -08001899 + " finishing=" + r.finishing + " state=" + r.getState());
Jorim Jaggife762342016-10-13 14:33:27 +02001900 // First: if this is not the current activity being started, make
1901 // sure it matches the current configuration.
1902 if (r != starting) {
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08001903 // Ensure activity configuration ignoring stop state since we are
1904 // becoming visible.
1905 r.ensureActivityConfiguration(0 /* globalChanges */, preserveWindows,
1906 true /* ignoreStopState */);
Jorim Jaggife762342016-10-13 14:33:27 +02001907 }
1908
1909 if (r.app == null || r.app.thread == null) {
1910 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
1911 resumeNextActivity, r)) {
1912 if (activityNdx >= activities.size()) {
1913 // Record may be removed if its process needs to restart.
1914 activityNdx = activities.size() - 1;
1915 } else {
1916 resumeNextActivity = false;
1917 }
1918 }
1919 } else if (r.visible) {
1920 // If this activity is already visible, then there is nothing to do here.
1921 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1922 "Skipping: already visible at " + r);
1923
1924 if (r.handleAlreadyVisible()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001925 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001926 }
Jorim Jaggife762342016-10-13 14:33:27 +02001927 } else {
1928 r.makeVisibleIfNeeded(starting);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001929 }
Jorim Jaggife762342016-10-13 14:33:27 +02001930 // Aggregate current change flags.
1931 configChanges |= r.configChangeFlags;
Craig Mautnerbb742462014-07-07 15:28:55 -07001932 } else {
Jorim Jaggife762342016-10-13 14:33:27 +02001933 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Bryce Lee7ace3952018-02-16 14:34:32 -08001934 + " finishing=" + r.finishing + " state=" + r.getState()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001935 + " stackShouldBeVisible=" + stackShouldBeVisible
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001936 + " behindFullscreenActivity=" + behindFullscreenActivity
1937 + " mLaunchTaskBehind=" + r.mLaunchTaskBehind);
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001938 makeInvisible(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001939 }
Jorim Jaggife762342016-10-13 14:33:27 +02001940 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001941 final int windowingMode = getWindowingMode();
1942 if (windowingMode == WINDOWING_MODE_FREEFORM) {
Jorim Jaggife762342016-10-13 14:33:27 +02001943 // The visibility of tasks and the activities they contain in freeform stack are
1944 // determined individually unlike other stacks where the visibility or fullscreen
1945 // status of an activity in a previous task affects other.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001946 behindFullscreenActivity = !stackShouldBeVisible;
Wale Ogunwale68278562017-09-23 17:13:55 -07001947 } else if (isActivityTypeHome()) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001948 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001949 + " stackShouldBeVisible=" + stackShouldBeVisible
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001950 + " behindFullscreenActivity=" + behindFullscreenActivity);
1951 // No other task in the home stack should be visible behind the home activity.
1952 // Home activities is usually a translucent activity with the wallpaper behind
1953 // them. However, when they don't have the wallpaper behind them, we want to
1954 // show activities in the next application stack behind them vs. another
1955 // task in the home stack like recents.
1956 behindFullscreenActivity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957 }
1958 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001959
Jorim Jaggife762342016-10-13 14:33:27 +02001960 if (mTranslucentActivityWaiting != null &&
1961 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1962 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1963 notifyActivityDrawnLocked(null);
1964 }
1965 } finally {
Bryce Lee2a3cc462017-10-27 10:57:35 -07001966 mStackSupervisor.getKeyguardController().endActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02001967 }
1968 }
1969
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01001970 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
1971 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1972 mTaskHistory.get(taskNdx).addStartingWindowsForVisibleActivities(taskSwitch);
1973 }
1974 }
1975
Jorim Jaggife762342016-10-13 14:33:27 +02001976 /**
1977 * @return true if the top visible activity wants to occlude the Keyguard, false otherwise
1978 */
1979 boolean topActivityOccludesKeyguard() {
1980 return mTopActivityOccludesKeyguard;
1981 }
1982
1983 /**
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001984 * Returns true if this stack should be resized to match the bounds specified by
1985 * {@link ActivityOptions#setLaunchBounds} when launching an activity into the stack.
1986 */
1987 boolean resizeStackWithLaunchBounds() {
1988 return inPinnedWindowingMode();
1989 }
1990
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001991 @Override
1992 public boolean supportsSplitScreenWindowingMode() {
1993 final TaskRecord topTask = topTask();
1994 return super.supportsSplitScreenWindowingMode()
1995 && (topTask == null || topTask.supportsSplitScreenWindowingMode());
1996 }
1997
Wale Ogunwale2b07da82017-11-08 14:52:40 -08001998 /** @return True if the resizing of the primary-split-screen stack affects this stack size. */
1999 boolean affectedBySplitScreenResize() {
2000 if (!supportsSplitScreenWindowingMode()) {
2001 return false;
2002 }
2003 final int windowingMode = getWindowingMode();
2004 return windowingMode != WINDOWING_MODE_FREEFORM && windowingMode != WINDOWING_MODE_PINNED;
2005 }
2006
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002007 /**
Jorim Jaggife762342016-10-13 14:33:27 +02002008 * @return the top most visible activity that wants to dismiss Keyguard
2009 */
2010 ActivityRecord getTopDismissingKeyguardActivity() {
2011 return mTopDismissingKeyguardActivity;
2012 }
2013
2014 /**
2015 * Checks whether {@param r} should be visible depending on Keyguard state and updates
2016 * {@link #mTopActivityOccludesKeyguard} and {@link #mTopDismissingKeyguardActivity} if
2017 * necessary.
2018 *
2019 * @return true if {@param r} is visible taken Keyguard state into account, false otherwise
2020 */
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002021 boolean checkKeyguardVisibility(ActivityRecord r, boolean shouldBeVisible, boolean isTop) {
Lucas Dupin47a65c72018-02-15 14:16:18 -08002022 final int displayId = mDisplayId != INVALID_DISPLAY ? mDisplayId : DEFAULT_DISPLAY;
2023 final boolean keyguardOrAodShowing = mStackSupervisor.getKeyguardController()
2024 .isKeyguardOrAodShowing(displayId);
Bryce Lee2a3cc462017-10-27 10:57:35 -07002025 final boolean keyguardLocked = mStackSupervisor.getKeyguardController().isKeyguardLocked();
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002026 final boolean showWhenLocked = r.canShowWhenLocked();
Jorim Jaggie69c9312016-10-31 18:24:38 -07002027 final boolean dismissKeyguard = r.hasDismissKeyguardWindows();
Jorim Jaggife762342016-10-13 14:33:27 +02002028 if (shouldBeVisible) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002029 if (dismissKeyguard && mTopDismissingKeyguardActivity == null) {
Jorim Jaggife762342016-10-13 14:33:27 +02002030 mTopDismissingKeyguardActivity = r;
2031 }
2032
2033 // Only the top activity may control occluded, as we can't occlude the Keyguard if the
2034 // top app doesn't want to occlude it.
2035 if (isTop) {
2036 mTopActivityOccludesKeyguard |= showWhenLocked;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07002037 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002038
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002039 final boolean canShowWithKeyguard = canShowWithInsecureKeyguard()
Bryce Lee2a3cc462017-10-27 10:57:35 -07002040 && mStackSupervisor.getKeyguardController().canDismissKeyguard();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002041 if (canShowWithKeyguard) {
2042 return true;
2043 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002044 }
Lucas Dupin47a65c72018-02-15 14:16:18 -08002045 if (keyguardOrAodShowing) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002046 // If keyguard is showing, nothing is visible, except if we are able to dismiss Keyguard
Lucas Dupin47a65c72018-02-15 14:16:18 -08002047 // right away and AOD isn't visible.
Bryce Lee2a3cc462017-10-27 10:57:35 -07002048 return shouldBeVisible && mStackSupervisor.getKeyguardController()
Jorim Jaggi07961872016-11-23 11:28:57 +01002049 .canShowActivityWhileKeyguardShowing(r, dismissKeyguard);
Jorim Jaggife762342016-10-13 14:33:27 +02002050 } else if (keyguardLocked) {
Bryce Lee2a3cc462017-10-27 10:57:35 -07002051 return shouldBeVisible && mStackSupervisor.getKeyguardController().canShowWhileOccluded(
Jorim Jaggi07961872016-11-23 11:28:57 +01002052 dismissKeyguard, showWhenLocked);
Jorim Jaggife762342016-10-13 14:33:27 +02002053 } else {
2054 return shouldBeVisible;
Craig Mautnereb8abf72014-07-02 15:04:09 -07002055 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 }
Craig Mautner58547802013-03-05 08:23:53 -08002057
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002058 /**
2059 * Check if the display to which this stack is attached has
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002060 * {@link Display#FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD} applied.
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002061 */
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002062 private boolean canShowWithInsecureKeyguard() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002063 final ActivityDisplay activityDisplay = getDisplay();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002064 if (activityDisplay == null) {
2065 throw new IllegalStateException("Stack is not attached to any display, stackId="
2066 + mStackId);
2067 }
2068
2069 final int flags = activityDisplay.mDisplay.getFlags();
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002070 return (flags & FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002071 }
2072
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002073 private void checkTranslucentActivityWaiting(ActivityRecord top) {
2074 if (mTranslucentActivityWaiting != top) {
2075 mUndrawnActivitiesBelowTopTranslucent.clear();
2076 if (mTranslucentActivityWaiting != null) {
2077 // Call the callback with a timeout indication.
2078 notifyActivityDrawnLocked(null);
2079 mTranslucentActivityWaiting = null;
2080 }
2081 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2082 }
2083 }
2084
2085 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08002086 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002087 // We need to make sure the app is running if it's the top, or it is just made visible from
2088 // invisible. If the app is already visible, it must have died while it was visible. In this
2089 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
2090 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002091 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002092 // This activity needs to be visible, but isn't even running...
2093 // get it started and resume if no other stack in this stack is resumed.
2094 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
2095 if (r != starting) {
2096 r.startFreezingScreenLocked(r.app, configChanges);
2097 }
2098 if (!r.visible || r.mLaunchTaskBehind) {
2099 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002100 r.setVisible(true);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002101 }
2102 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002103 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002104 return true;
2105 }
2106 }
2107 return false;
2108 }
2109
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002110 // TODO: Should probably be moved into ActivityRecord.
2111 private void makeInvisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002112 if (!r.visible) {
2113 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
2114 return;
2115 }
2116 // Now for any activities that aren't visible to the user, make sure they no longer are
2117 // keeping the screen frozen.
Bryce Lee7ace3952018-02-16 14:34:32 -08002118 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.getState());
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002119 try {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002120 final boolean canEnterPictureInPicture = r.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002121 "makeInvisible", true /* beforeStopping */);
Wale Ogunwale08488a62017-09-05 12:37:03 -07002122 // Defer telling the client it is hidden if it can enter Pip and isn't current paused,
2123 // stopped or stopping. This gives it a chance to enter Pip in onPause().
2124 // TODO: There is still a question surrounding activities in multi-window mode that want
2125 // to enter Pip after they are paused, but are still visible. I they should be okay to
2126 // enter Pip in those cases, but not "auto-Pip" which is what this condition covers and
2127 // the current contract for "auto-Pip" is that the app should enter it before onPause
2128 // returns. Just need to confirm this reasoning makes sense.
Wale Ogunwale89973222017-04-23 18:39:45 -07002129 final boolean deferHidingClient = canEnterPictureInPicture
Bryce Lee7ace3952018-02-16 14:34:32 -08002130 && !r.isState(STOPPING, STOPPED, PAUSED);
Wale Ogunwaleec950642017-04-25 07:44:21 -07002131 r.setDeferHidingClient(deferHidingClient);
2132 r.setVisible(false);
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002133
Bryce Lee7ace3952018-02-16 14:34:32 -08002134 switch (r.getState()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002135 case STOPPING:
2136 case STOPPED:
2137 if (r.app != null && r.app.thread != null) {
2138 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2139 "Scheduling invisibility: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08002140 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08002141 WindowVisibilityItem.obtain(false /* showWindow */));
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002142 }
Winson Chungc2baac02017-01-11 13:34:47 -08002143
2144 // Reset the flag indicating that an app can enter picture-in-picture once the
2145 // activity is hidden
Winson Chungf7e03e12017-08-22 11:32:16 -07002146 r.supportsEnterPipOnTaskSwitch = false;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002147 break;
2148
2149 case INITIALIZING:
2150 case RESUMED:
2151 case PAUSING:
2152 case PAUSED:
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002153 addToStopping(r, true /* scheduleIdle */,
2154 canEnterPictureInPicture /* idleDelayed */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002155 break;
2156
2157 default:
2158 break;
2159 }
2160 } catch (Exception e) {
2161 // Just skip on any failure; we'll make it visible when it next restarts.
2162 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
2163 }
2164 }
2165
2166 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002167 ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002168 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002169 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08002170 + " stackInvisible=" + stackInvisible
2171 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08002172 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002173 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002174 }
2175 return behindFullscreenActivity;
2176 }
2177
Todd Kennedyaab56db2015-01-30 09:39:53 -08002178 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002179 mTranslucentActivityWaiting = r;
2180 mUndrawnActivitiesBelowTopTranslucent.clear();
2181 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
2182 }
2183
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002184 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2185 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2186 final TaskRecord task = mTaskHistory.get(taskNdx);
2187 final ArrayList<ActivityRecord> activities = task.mActivities;
2188 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2189 final ActivityRecord r = activities.get(activityNdx);
2190 if ( r.appTimeTracker != except) {
2191 r.appTimeTracker = null;
2192 }
2193 }
2194 }
2195 }
2196
Craig Mautner5eda9b32013-07-02 11:58:16 -07002197 /**
2198 * Called as activities below the top translucent activity are redrawn. When the last one is
2199 * redrawn notify the top activity by calling
2200 * {@link Activity#onTranslucentConversionComplete}.
2201 *
2202 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2203 * occurred and the activity will be notified immediately.
2204 */
2205 void notifyActivityDrawnLocked(ActivityRecord r) {
2206 if ((r == null)
2207 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2208 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2209 // The last undrawn activity below the top has just been drawn. If there is an
2210 // opaque activity at the top, notify it that it can become translucent safely now.
2211 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2212 mTranslucentActivityWaiting = null;
2213 mUndrawnActivitiesBelowTopTranslucent.clear();
2214 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2215
Craig Mautner71dd1b62014-02-18 15:48:52 -08002216 if (waitingActivity != null) {
2217 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
2218 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
2219 try {
2220 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
2221 waitingActivity.appToken, r != null);
2222 } catch (RemoteException e) {
2223 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002224 }
2225 }
2226 }
2227 }
2228
Craig Mautnera61bc652013-10-28 15:43:18 -07002229 /** If any activities below the top running one are in the INITIALIZING state and they have a
2230 * starting window displayed then remove that starting window. It is possible that the activity
2231 * in this state will never resumed in which case that starting window will be orphaned. */
2232 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002233 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002234 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002235 // We don't want to clear starting window for activities that aren't behind fullscreen
2236 // activities as we need to display their starting window until they are done initializing.
2237 boolean behindFullscreenActivity = false;
Wale Ogunwale98742a52016-07-12 10:29:12 -07002238
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002239 if (!shouldBeVisible(null)) {
Wale Ogunwale98742a52016-07-12 10:29:12 -07002240 // The stack is not visible, so no activity in it should be displaying a starting
2241 // window. Mark all activities below top and behind fullscreen.
2242 aboveTop = false;
2243 behindFullscreenActivity = true;
2244 }
2245
Craig Mautnera61bc652013-10-28 15:43:18 -07002246 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2247 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2248 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2249 final ActivityRecord r = activities.get(activityNdx);
2250 if (aboveTop) {
2251 if (r == topActivity) {
2252 aboveTop = false;
2253 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002254 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002255 continue;
2256 }
2257
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002258 r.removeOrphanedStartingWindow(behindFullscreenActivity);
Wale Ogunwale68741142016-05-17 09:40:02 -07002259 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002260 }
2261 }
2262 }
2263
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002264 /**
2265 * Ensure that the top activity in the stack is resumed.
2266 *
2267 * @param prev The previously resumed activity, for when in the process
2268 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002269 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002270 *
2271 * @return Returns true if something is being resumed, or false if
2272 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002273 *
2274 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2275 * non-focused stack to be resumed.
2276 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2277 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 */
Andreas Gampea36dc622018-02-05 17:19:22 -08002279 @GuardedBy("mService")
Wale Ogunwaled046a012015-12-24 13:05:59 -08002280 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002281 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002282 // Don't even start recursing.
2283 return false;
2284 }
2285
2286 boolean result = false;
2287 try {
2288 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002289 mStackSupervisor.inResumeTopActivity = true;
Bryce Lee69ad8182017-09-28 10:01:36 -07002290 result = resumeTopActivityInnerLocked(prev, options);
Bryce Leefc7cedd2018-03-01 15:38:07 -08002291
2292 // When resuming the top activity, it may be necessary to pause the top activity (for
2293 // example, returning to the lock screen. We suppress the normal pause logic in
2294 // {@link #resumeTopActivityUncheckedLocked}, since the top activity is resumed at the
2295 // end. We call the {@link ActivityStackSupervisor#checkReadyForSleepLocked} again here
2296 // to ensure any necessary pause logic occurs. In the case where the Activity will be
2297 // shown regardless of the lock screen, the call to
2298 // {@link ActivityStackSupervisor#checkReadyForSleepLocked} is skipped.
2299 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
2300 if (next == null || !next.canTurnScreenOn()) {
2301 checkReadyForSleep();
2302 }
Craig Mautner544efa72014-09-04 16:41:20 -07002303 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002304 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002305 }
chaviw59b98852017-06-13 12:05:44 -07002306
Craig Mautner544efa72014-09-04 16:41:20 -07002307 return result;
2308 }
2309
Bryce Leec4ab62a2018-03-05 14:19:26 -08002310 /**
2311 * Returns the currently resumed activity.
2312 */
2313 protected ActivityRecord getResumedActivity() {
2314 return mResumedActivity;
2315 }
2316
Bryce Lee84730a02018-04-03 14:10:04 -07002317 private void setResumedActivity(ActivityRecord r, String reason) {
2318 if (mResumedActivity == r) {
2319 return;
2320 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08002321
Bryce Lee84730a02018-04-03 14:10:04 -07002322 if (DEBUG_STACK) Slog.d(TAG_STACK, "setResumedActivity stack:" + this + " + from: "
2323 + mResumedActivity + " to:" + r + " reason:" + reason);
2324 mResumedActivity = r;
Chong Zhang6cda19c2016-06-14 19:07:56 -07002325 }
2326
Andreas Gampea36dc622018-02-05 17:19:22 -08002327 @GuardedBy("mService")
Chong Zhang280d3322015-11-03 17:27:26 -08002328 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002329 if (!mService.mBooting && !mService.mBooted) {
2330 // Not ready yet!
2331 return false;
2332 }
2333
Winson Chung3f103eb2017-04-12 21:53:59 -07002334 // Find the next top-most activity to resume in this stack that is not finishing and is
2335 // focusable. If it is not focusable, we will fall into the case below to resume the
2336 // top activity in the next focusable task.
2337 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
Andrii Kulian7fc22812016-12-28 13:04:11 -08002338
2339 final boolean hasRunningActivity = next != null;
2340
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002341 // TODO: Maybe this entire condition can get removed?
Wale Ogunwale66e16852017-10-19 13:35:52 -07002342 if (hasRunningActivity && !isAttached()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002343 return false;
2344 }
2345
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002346 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002347
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 // Remember how we'll process this pause/resume situation, and ensure
2349 // that the state is reset however we wind up proceeding.
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08002350 boolean userLeaving = mStackSupervisor.mUserLeaving;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002351 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352
Andrii Kulian7fc22812016-12-28 13:04:11 -08002353 if (!hasRunningActivity) {
2354 // There are no activities left in the stack, let's look somewhere else.
2355 return resumeTopActivityInNextFocusableStack(prev, options, "noMoreActivities");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002356 }
2357
2358 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 // If the top activity is the resumed one, nothing to do.
Bryce Lee7ace3952018-02-16 14:34:32 -08002361 if (mResumedActivity == next && next.isState(RESUMED)
2362 && mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002363 // Make sure we have executed any pending transitions, since there
2364 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002365 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002366 if (DEBUG_STATES) Slog.d(TAG_STATES,
2367 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002368 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002369 return false;
2370 }
2371
2372 // If we are sleeping, and there is no resumed activity, and the top
2373 // activity is paused, well that is the state we want.
David Stevens9440dc82017-03-16 19:00:20 -07002374 if (shouldSleepOrShutDownActivities()
p13451dbad2872012-04-18 11:39:23 +09002375 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002376 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002377 // Make sure we have executed any pending transitions, since there
2378 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002379 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002380 if (DEBUG_STATES) Slog.d(TAG_STATES,
2381 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002382 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 return false;
2384 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002385
2386 // Make sure that the user who owns this activity is started. If not,
2387 // we will just leave it as is because someone should be bringing
2388 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002389 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002390 Slog.w(TAG, "Skipping resume of top activity " + next
2391 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002392 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002393 return false;
2394 }
2395
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002396 // The activity may be waiting for stop, but that is no longer
2397 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002398 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002399 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002400 next.sleeping = false;
Bryce Lee4a194382017-04-04 14:32:48 -07002401 mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002402
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002403 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002405 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002406 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002407 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002408 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002409 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002410 return false;
2411 }
2412
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002413 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2414
Wale Ogunwale89973222017-04-23 18:39:45 -07002415 boolean lastResumedCanPip = false;
2416 final ActivityStack lastFocusedStack = mStackSupervisor.getLastStack();
2417 if (lastFocusedStack != null && lastFocusedStack != this) {
2418 // So, why aren't we using prev here??? See the param comment on the method. prev doesn't
2419 // represent the last resumed activity. However, the last focus stack does if it isn't null.
2420 final ActivityRecord lastResumed = lastFocusedStack.mResumedActivity;
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08002421 if (userLeaving && inMultiWindowMode() && lastFocusedStack.shouldBeVisible(next)) {
2422 // The user isn't leaving if this stack is the multi-window mode and the last
2423 // focused stack should still be visible.
2424 if(DEBUG_USER_LEAVING) Slog.i(TAG_USER_LEAVING, "Overriding userLeaving to false"
2425 + " next=" + next + " lastResumed=" + lastResumed);
2426 userLeaving = false;
2427 }
Wale Ogunwale89973222017-04-23 18:39:45 -07002428 lastResumedCanPip = lastResumed != null && lastResumed.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002429 "resumeTopActivity", userLeaving /* beforeStopping */);
Wale Ogunwale89973222017-04-23 18:39:45 -07002430 }
Winson Chungc2baac02017-01-11 13:34:47 -08002431 // If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002432 // to be paused, while at the same time resuming the new resume activity only if the
2433 // previous activity can't go into Pip since we want to give Pip activities a chance to
2434 // enter Pip before resuming the next activity.
Wale Ogunwale89973222017-04-23 18:39:45 -07002435 final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0
2436 && !lastResumedCanPip;
2437
Winson Chungc2baac02017-01-11 13:34:47 -08002438 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002439 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002440 if (DEBUG_STATES) Slog.d(TAG_STATES,
2441 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Winson Chungc2baac02017-01-11 13:34:47 -08002442 pausing |= startPausingLocked(userLeaving, false, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002443 }
Winson Chungc2baac02017-01-11 13:34:47 -08002444 if (pausing && !resumeWhilePausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002445 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002446 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002447 // At this point we want to put the upcoming activity's process
2448 // at the top of the LRU list, since we know we will be needing it
2449 // very soon and it would be a waste to let it get killed if it
2450 // happens to be sitting towards the end.
2451 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002452 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002453 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002454 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002455 return true;
Bryce Lee7ace3952018-02-16 14:34:32 -08002456 } else if (mResumedActivity == next && next.isState(RESUMED)
2457 && mStackSupervisor.allResumedActivitiesComplete()) {
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002458 // It is possible for the activity to be resumed when we paused back stacks above if the
2459 // next activity doesn't have to wait for pause to complete.
2460 // So, nothing else to-do except:
2461 // Make sure we have executed any pending transitions, since there
2462 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002463 executeAppTransition(options);
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002464 if (DEBUG_STATES) Slog.d(TAG_STATES,
2465 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
2466 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2467 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002468 }
2469
Christopher Tated3f175c2012-06-14 14:16:54 -07002470 // If the most recent activity was noHistory but was only stopped rather
2471 // than stopped+finished because the device went to sleep, we need to make
2472 // sure to finish it as we're making a new activity topmost.
David Stevens9440dc82017-03-16 19:00:20 -07002473 if (shouldSleepActivities() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002474 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002475 if (DEBUG_STATES) Slog.d(TAG_STATES,
2476 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002477 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002478 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002479 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002480 }
2481
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002482 if (prev != null && prev != next) {
Bryce Lee4a194382017-04-04 14:32:48 -07002483 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Craig Mautner8c14c152015-01-15 17:32:07 -08002484 && next != null && !next.nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07002485 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002486 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2487 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002488 } else {
2489 // The next activity is already visible, so hide the previous
2490 // activity's windows right now so we can show the new one ASAP.
2491 // We only do this if the previous is finishing, which should mean
2492 // it is on top of the one being resumed so hiding it quickly
2493 // is good. Otherwise, we want to do the normal route of allowing
2494 // the resumed activity to be shown so we can decide if the
2495 // previous should actually be hidden depending on whether the
2496 // new one is found to be full-screen or not.
2497 if (prev.finishing) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002498 prev.setVisibility(false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002499 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2500 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Bryce Lee4a194382017-04-04 14:32:48 -07002501 + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002502 + ", nowVisible=" + next.nowVisible);
2503 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002504 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002505 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002506 + ", waitingVisible="
Bryce Lee4a194382017-04-04 14:32:48 -07002507 + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002508 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002509 }
2510 }
2511 }
2512
Dianne Hackborne7f97212011-02-24 14:40:20 -08002513 // Launching this app's activity, make sure the app is no longer
2514 // considered stopped.
2515 try {
2516 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002517 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002518 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002519 } catch (IllegalArgumentException e) {
2520 Slog.w(TAG, "Failed trying to unstop package "
2521 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002522 }
2523
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002524 // We are starting up the next activity, so tell the window manager
2525 // that the previous one will be hidden soon. This way it can know
2526 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002527 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002528 if (prev != null) {
2529 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002530 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 "Prepare close transition: prev=" + prev);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002532 if (mStackSupervisor.mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002533 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002534 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 } else {
Bryce Leeaf691c02017-03-20 14:20:22 -07002536 mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002537 ? TRANSIT_ACTIVITY_CLOSE
2538 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002539 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002540 prev.setVisibility(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002541 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002542 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2543 "Prepare open transition: prev=" + prev);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002544 if (mStackSupervisor.mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002545 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002546 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 } else {
Bryce Leeaf691c02017-03-20 14:20:22 -07002548 mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002549 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002550 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002551 ? TRANSIT_TASK_OPEN_BEHIND
2552 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002553 }
2554 }
Craig Mautner967212c2013-04-13 21:10:58 -07002555 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002556 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002557 if (mStackSupervisor.mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002558 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002559 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002561 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002562 }
2563 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002564
Craig Mautner525f3d92013-05-07 14:01:50 -07002565 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002566 next.applyOptionsLocked();
2567 } else {
2568 next.clearOptionsLocked();
2569 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002571 mStackSupervisor.mNoAnimActivities.clear();
2572
Craig Mautnercf910b02013-04-23 11:23:27 -07002573 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002574 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002575 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2576 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002577
Chong Zhangd127c6d2016-05-02 16:36:41 -07002578 // If the previous activity is translucent, force a visibility update of
2579 // the next activity, so that it's added to WM's opening app list, and
2580 // transition animation can be set up properly.
2581 // For example, pressing Home button with a translucent activity in focus.
2582 // Launcher is already visible in this case. If we don't add it to opening
2583 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2584 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2585 final boolean lastActivityTranslucent = lastStack != null
Bryce Leef3c6a472017-11-14 14:53:06 -08002586 && (lastStack.inMultiWindowMode()
Chong Zhangd127c6d2016-05-02 16:36:41 -07002587 || (lastStack.mLastPausedActivity != null
2588 && !lastStack.mLastPausedActivity.fullscreen));
2589
Bryce Lee69ad8182017-09-28 10:01:36 -07002590 // The contained logic must be synchronized, since we are both changing the visibility
2591 // and updating the {@link Configuration}. {@link ActivityRecord#setVisibility} will
2592 // ultimately cause the client code to schedule a layout. Since layouts retrieve the
2593 // current {@link Configuration}, we must ensure that the below code updates it before
2594 // the layout can occur.
2595 synchronized(mWindowManager.getWindowManagerLock()) {
2596 // This activity is now becoming visible.
2597 if (!next.visible || next.stopped || lastActivityTranslucent) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002598 next.setVisibility(true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002599 }
Craig Mautner58547802013-03-05 08:23:53 -08002600
Bryce Lee69ad8182017-09-28 10:01:36 -07002601 // schedule launch ticks to collect information about slow apps.
2602 next.startLaunchTickingLocked();
2603
2604 ActivityRecord lastResumedActivity =
2605 lastStack == null ? null :lastStack.mResumedActivity;
Bryce Lee7ace3952018-02-16 14:34:32 -08002606 final ActivityState lastState = next.getState();
Bryce Lee69ad8182017-09-28 10:01:36 -07002607
2608 mService.updateCpuStats();
2609
2610 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next
2611 + " (in existing)");
2612
Bryce Leec4ab62a2018-03-05 14:19:26 -08002613 next.setState(RESUMED, "resumeTopActivityInnerLocked");
Bryce Lee69ad8182017-09-28 10:01:36 -07002614
2615 mService.updateLruProcessLocked(next.app, true, null);
2616 updateLRUListLocked(next);
2617 mService.updateOomAdjLocked();
2618
2619 // Have the window manager re-evaluate the orientation of
2620 // the screen based on the new activity order.
2621 boolean notUpdated = true;
2622
2623 if (mStackSupervisor.isFocusedStack(this)) {
2624
2625 // We have special rotation behavior when Keyguard is locked. Make sure all
2626 // activity visibilities are set correctly as well as the transition is updated
2627 // if needed to get the correct rotation behavior.
2628 // TODO: Remove this once visibilities are set correctly immediately when
2629 // starting an activity.
Bryce Lee2a3cc462017-10-27 10:57:35 -07002630 if (mStackSupervisor.getKeyguardController().isKeyguardLocked()) {
Bryce Lee69ad8182017-09-28 10:01:36 -07002631 mStackSupervisor.ensureActivitiesVisibleLocked(null /* starting */,
2632 0 /* configChanges */, false /* preserveWindows */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 }
Bryce Lee69ad8182017-09-28 10:01:36 -07002634 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
2635 mStackSupervisor.getDisplayOverrideConfiguration(mDisplayId),
2636 next.mayFreezeScreenLocked(next.app) ? next.appToken : null,
2637 mDisplayId);
2638 if (config != null) {
2639 next.frozenBeforeDestroy = true;
2640 }
2641 notUpdated = !mService.updateDisplayOverrideConfigurationLocked(config, next,
2642 false /* deferResume */, mDisplayId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 }
2644
Bryce Lee69ad8182017-09-28 10:01:36 -07002645 if (notUpdated) {
2646 // The configuration update wasn't able to keep the existing
2647 // instance of the activity, and instead started a new one.
2648 // We should be all done, but let's just make sure our activity
2649 // is still at the top and schedule another run if something
2650 // weird happened.
2651 ActivityRecord nextNext = topRunningActivityLocked();
2652 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
2653 "Activity config changed during resume: " + next
2654 + ", new next: " + nextNext);
2655 if (nextNext != next) {
2656 // Do over!
2657 mStackSupervisor.scheduleResumeTopActivities();
2658 }
2659 if (!next.visible || next.stopped) {
2660 next.setVisibility(true);
2661 }
2662 next.completeResumeLocked();
2663 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2664 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002665 }
2666
Bryce Lee69ad8182017-09-28 10:01:36 -07002667 try {
Andrii Kulian9956d892018-02-14 13:48:56 -08002668 final ClientTransaction transaction = ClientTransaction.obtain(next.app.thread,
2669 next.appToken);
Bryce Lee69ad8182017-09-28 10:01:36 -07002670 // Deliver all pending results.
2671 ArrayList<ResultInfo> a = next.results;
2672 if (a != null) {
2673 final int N = a.size();
2674 if (!next.finishing && N > 0) {
2675 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2676 "Delivering results to " + next + ": " + a);
Andrii Kulian9956d892018-02-14 13:48:56 -08002677 transaction.addCallback(ActivityResultItem.obtain(a));
Bryce Lee69ad8182017-09-28 10:01:36 -07002678 }
2679 }
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002680
Bryce Lee69ad8182017-09-28 10:01:36 -07002681 if (next.newIntents != null) {
Andrii Kulian9956d892018-02-14 13:48:56 -08002682 transaction.addCallback(NewIntentItem.obtain(next.newIntents,
2683 false /* andPause */));
Bryce Lee69ad8182017-09-28 10:01:36 -07002684 }
Craig Mautner58547802013-03-05 08:23:53 -08002685
Bryce Lee69ad8182017-09-28 10:01:36 -07002686 // Well the app will no longer be stopped.
2687 // Clear app token stopped state in window manager if needed.
2688 next.notifyAppResumed(next.stopped);
Craig Mautner58547802013-03-05 08:23:53 -08002689
Bryce Lee69ad8182017-09-28 10:01:36 -07002690 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2691 System.identityHashCode(next), next.getTask().taskId,
2692 next.shortComponentName);
2693
2694 next.sleeping = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05002695 mService.getAppWarningsLocked().onResumeActivity(next);
Bryce Lee69ad8182017-09-28 10:01:36 -07002696 mService.showAskCompatModeDialogLocked(next);
2697 next.app.pendingUiClean = true;
2698 next.app.forceProcessStateUpTo(mService.mTopProcessState);
2699 next.clearOptionsLocked();
Andrii Kulian9956d892018-02-14 13:48:56 -08002700 transaction.setLifecycleStateRequest(
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08002701 ResumeActivityItem.obtain(next.app.repProcState,
Bryce Lee1d0d5142018-04-12 10:35:07 -07002702 mService.isNextTransitionForward()));
Bryce Leeb0f993f2018-03-02 15:38:01 -08002703 mService.getLifecycleManager().scheduleTransaction(transaction);
Bryce Lee69ad8182017-09-28 10:01:36 -07002704
2705 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed "
2706 + next);
2707 } catch (Exception e) {
2708 // Whoops, need to restart this activity!
2709 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
2710 + lastState + ": " + next);
Bryce Lee7ace3952018-02-16 14:34:32 -08002711 next.setState(lastState, "resumeTopActivityInnerLocked");
Bryce Lee4c401382018-03-13 19:23:03 -07002712
2713 // lastResumedActivity being non-null implies there is a lastStack present.
2714 if (lastResumedActivity != null) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08002715 lastResumedActivity.setState(RESUMED, "resumeTopActivityInnerLocked");
Bryce Lee69ad8182017-09-28 10:01:36 -07002716 }
Bryce Lee4c401382018-03-13 19:23:03 -07002717
Bryce Lee69ad8182017-09-28 10:01:36 -07002718 Slog.i(TAG, "Restarting because process died: " + next);
2719 if (!next.hasBeenLaunched) {
2720 next.hasBeenLaunched = true;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002721 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null
2722 && lastStack.isTopStackOnDisplay()) {
Bryce Lee69ad8182017-09-28 10:01:36 -07002723 next.showStartingWindow(null /* prev */, false /* newTask */,
2724 false /* taskSwitch */);
2725 }
2726 mStackSupervisor.startSpecificActivityLocked(next, true, false);
2727 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2728 return true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002729 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 }
2731
2732 // From this point on, if something goes wrong there is no way
2733 // to recover the activity.
2734 try {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002735 next.completeResumeLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 } catch (Exception e) {
2737 // If any exception gets thrown, toss away this
2738 // activity and try the next one.
2739 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002740 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002741 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002742 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 return true;
2744 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 } else {
2746 // Whoops, need to restart this activity!
2747 if (!next.hasBeenLaunched) {
2748 next.hasBeenLaunched = true;
2749 } else {
2750 if (SHOW_APP_STARTING_PREVIEW) {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002751 next.showStartingWindow(null /* prev */, false /* newTask */,
2752 false /* taskSwich */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002753 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002754 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002756 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002757 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002758 }
2759
Craig Mautnercf910b02013-04-23 11:23:27 -07002760 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 return true;
2762 }
2763
Andrii Kulian7fc22812016-12-28 13:04:11 -08002764 private boolean resumeTopActivityInNextFocusableStack(ActivityRecord prev,
2765 ActivityOptions options, String reason) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002766 if (adjustFocusToNextFocusableStack(reason)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002767 // Try to move focus to the next visible stack with a running activity if this
2768 // stack is not covering the entire screen or is on a secondary display (with no home
2769 // stack).
2770 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2771 mStackSupervisor.getFocusedStack(), prev, null);
2772 }
2773
2774 // Let's just start up the Launcher...
2775 ActivityOptions.abort(options);
2776 if (DEBUG_STATES) Slog.d(TAG_STATES,
2777 "resumeTopActivityInNextFocusableStack: " + reason + ", go home");
2778 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2779 // Only resume home if on home display
2780 return isOnHomeDisplay() &&
2781 mStackSupervisor.resumeHomeStackTask(prev, reason);
2782 }
2783
riddle_hsuc215a4f2014-12-27 12:10:45 +08002784 private TaskRecord getNextTask(TaskRecord targetTask) {
2785 final int index = mTaskHistory.indexOf(targetTask);
2786 if (index >= 0) {
2787 final int numTasks = mTaskHistory.size();
2788 for (int i = index + 1; i < numTasks; ++i) {
2789 TaskRecord task = mTaskHistory.get(i);
2790 if (task.userId == targetTask.userId) {
2791 return task;
2792 }
2793 }
2794 }
2795 return null;
2796 }
2797
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002798 /** Returns the position the input task should be placed in this stack. */
2799 int getAdjustedPositionForTask(TaskRecord task, int suggestedPosition,
2800 ActivityRecord starting) {
2801
2802 int maxPosition = mTaskHistory.size();
2803 if ((starting != null && starting.okToShowLocked())
2804 || (starting == null && task.okToShowLocked())) {
2805 // If the task or starting activity can be shown, then whatever position is okay.
2806 return Math.min(suggestedPosition, maxPosition);
2807 }
2808
2809 // The task can't be shown, put non-current user tasks below current user tasks.
2810 while (maxPosition > 0) {
2811 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2812 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2813 || tmpTask.topRunningActivityLocked() == null) {
2814 break;
2815 }
2816 maxPosition--;
2817 }
2818
2819 return Math.min(suggestedPosition, maxPosition);
2820 }
2821
Andrii Kuliand2765632016-12-12 22:26:34 -08002822 /**
2823 * Used from {@link ActivityStack#positionTask(TaskRecord, int)}.
2824 * @see ActivityManagerService#positionTaskInStack(int, int, int).
2825 */
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002826 private void insertTaskAtPosition(TaskRecord task, int position) {
2827 if (position >= mTaskHistory.size()) {
2828 insertTaskAtTop(task, null);
2829 return;
Evan Rosky9c448172017-11-02 14:19:27 -07002830 } else if (position <= 0) {
2831 insertTaskAtBottom(task);
2832 return;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002833 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002834 position = getAdjustedPositionForTask(task, position, null /* starting */);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002835 mTaskHistory.remove(task);
2836 mTaskHistory.add(position, task);
Bryce Leef3c6a472017-11-14 14:53:06 -08002837 mWindowContainerController.positionChildAt(task.getWindowContainerController(), position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002838 updateTaskMovement(task, true);
2839 }
2840
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002841 private void insertTaskAtTop(TaskRecord task, ActivityRecord starting) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002842 // TODO: Better place to put all the code below...may be addTask...
2843 mTaskHistory.remove(task);
2844 // Now put task at top.
2845 final int position = getAdjustedPositionForTask(task, mTaskHistory.size(), starting);
2846 mTaskHistory.add(position, task);
2847 updateTaskMovement(task, true);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002848 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
2849 true /* includingParents */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002850 }
2851
Evan Rosky9c448172017-11-02 14:19:27 -07002852 private void insertTaskAtBottom(TaskRecord task) {
2853 // Unlike insertTaskAtPosition, this will also position parents of the windowcontroller.
2854 mTaskHistory.remove(task);
2855 final int position = getAdjustedPositionForTask(task, 0, null);
2856 mTaskHistory.add(position, task);
2857 updateTaskMovement(task, true);
2858 mWindowContainerController.positionChildAtBottom(task.getWindowContainerController(),
2859 true /* includingParents */);
2860 }
2861
Bryce Leead5b8322018-03-08 14:28:52 -08002862 void startActivityLocked(ActivityRecord r, ActivityRecord focusedTopActivity,
Winson Chungb5c41b72016-12-07 15:00:47 -08002863 boolean newTask, boolean keepCurTransition, ActivityOptions options) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002864 TaskRecord rTask = r.getTask();
Craig Mautnerd2328952013-03-05 12:46:26 -08002865 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002866 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2867 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002868 // Last activity in task had been removed or ActivityManagerService is reusing task.
2869 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002870 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002871 insertTaskAtTop(rTask, r);
Craig Mautner77878772013-03-04 19:46:24 -08002872 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002873 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002874 if (!newTask) {
2875 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002876 boolean startIt = true;
2877 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2878 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002879 if (task.getTopActivity() == null) {
2880 // All activities in task are finishing.
2881 continue;
2882 }
Bryce Leeaf691c02017-03-20 14:20:22 -07002883 if (task == rTask) {
Craig Mautner70a86932013-02-28 22:37:44 -08002884 // Here it is! Now, if this is not yet visible to the
2885 // user, then just add it without starting; it will
2886 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002887 if (!startIt) {
2888 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2889 + task, new RuntimeException("here").fillInStackTrace());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002890 r.createWindowContainer();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002891 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002892 return;
2893 }
2894 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002895 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002896 startIt = false;
2897 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898 }
2899 }
2900
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002901 // Place a new activity at top of stack, so it is next to interact with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002902
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002903 // If we are not placing the new activity frontmost, we do not want to deliver the
2904 // onUserLeaving callback to the actual frontmost activity
Bryce Leeaf691c02017-03-20 14:20:22 -07002905 final TaskRecord activityTask = r.getTask();
2906 if (task == activityTask && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002907 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002908 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002909 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002910 }
Craig Mautner70a86932013-02-28 22:37:44 -08002911
Bryce Leeaf691c02017-03-20 14:20:22 -07002912 task = activityTask;
Craig Mautner70a86932013-02-28 22:37:44 -08002913
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002914 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002915 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002916 new RuntimeException("here").fillInStackTrace());
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002917 // TODO: Need to investigate if it is okay for the controller to already be created by the
2918 // time we get to this point. I think it is, but need to double check.
2919 // Use test in b/34179495 to trace the call path.
2920 if (r.getWindowContainerController() == null) {
2921 r.createWindowContainer();
2922 }
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002923 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002924
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002925 if (!isHomeOrRecentsStack() || numActivities() > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002926 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002928 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002929 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002930 mStackSupervisor.mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002932 int transit = TRANSIT_ACTIVITY_OPEN;
2933 if (newTask) {
2934 if (r.mLaunchTaskBehind) {
2935 transit = TRANSIT_TASK_OPEN_BEHIND;
2936 } else {
Winson Chungc2baac02017-01-11 13:34:47 -08002937 // If a new task is being launched, then mark the existing top activity as
Winson Chung942a85c2017-07-11 15:07:45 -07002938 // supporting picture-in-picture while pausing only if the starting activity
2939 // would not be considered an overlay on top of the current activity
2940 // (eg. not fullscreen, or the assistant)
Winson Chungf7e03e12017-08-22 11:32:16 -07002941 if (canEnterPipOnTaskSwitch(focusedTopActivity,
2942 null /* toFrontTask */, r, options)) {
2943 focusedTopActivity.supportsEnterPipOnTaskSwitch = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08002944 }
2945 transit = TRANSIT_TASK_OPEN;
2946 }
2947 }
2948 mWindowManager.prepareAppTransition(transit, keepCurTransition);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002949 mStackSupervisor.mNoAnimActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002950 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002951 boolean doShow = true;
2952 if (newTask) {
2953 // Even though this activity is starting fresh, we still need
2954 // to reset it to make sure we apply affinities to move any
2955 // existing activities from other tasks in to it.
2956 // If the caller has requested that the target task be
2957 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002958 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002959 resetTaskIfNeededLocked(r, r);
2960 doShow = topRunningNonDelayedActivityLocked(null) == r;
2961 }
Chong Zhang280d3322015-11-03 17:27:26 -08002962 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002963 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2964 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002966 if (r.mLaunchTaskBehind) {
2967 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2968 // tell WindowManager that r is visible even though it is at the back of the stack.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002969 r.setVisibility(true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002970 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002971 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 // Figure out if we are transitioning from another activity that is
2973 // "has the same starting icon" as the next one. This allows the
2974 // window manager to keep the previous window it had previously
2975 // created, if it still had one.
Bryce Leeaf691c02017-03-20 14:20:22 -07002976 TaskRecord prevTask = r.getTask();
2977 ActivityRecord prev = prevTask.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002978 if (prev != null) {
2979 // We don't want to reuse the previous starting preview if:
2980 // (1) The current activity is in a different task.
Bryce Leeaf691c02017-03-20 14:20:22 -07002981 if (prev.getTask() != prevTask) {
Craig Mautner29219d92013-04-16 20:19:12 -07002982 prev = null;
2983 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002984 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002985 else if (prev.nowVisible) {
2986 prev = null;
2987 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002989 r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002990 }
2991 } else {
2992 // If this is the first activity, don't do any fancy animations,
2993 // because there is nothing for it to animate on top of.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002994 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002995 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002996 }
2997
Winson Chungf7e03e12017-08-22 11:32:16 -07002998 /**
2999 * @return Whether the switch to another task can trigger the currently running activity to
3000 * enter PiP while it is pausing (if supported). Only one of {@param toFrontTask} or
3001 * {@param toFrontActivity} should be set.
3002 */
3003 private boolean canEnterPipOnTaskSwitch(ActivityRecord pipCandidate,
3004 TaskRecord toFrontTask, ActivityRecord toFrontActivity, ActivityOptions opts) {
3005 if (opts != null && opts.disallowEnterPictureInPictureWhileLaunching()) {
3006 // Ensure the caller has requested not to trigger auto-enter PiP
3007 return false;
3008 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003009 if (pipCandidate == null || pipCandidate.inPinnedWindowingMode()) {
Winson Chungf7e03e12017-08-22 11:32:16 -07003010 // Ensure that we do not trigger entering PiP an activity on the pinned stack
3011 return false;
3012 }
Wale Ogunwale68278562017-09-23 17:13:55 -07003013 final ActivityStack targetStack = toFrontTask != null
3014 ? toFrontTask.getStack() : toFrontActivity.getStack();
3015 if (targetStack != null && targetStack.isActivityTypeAssistant()) {
Winson Chungf7e03e12017-08-22 11:32:16 -07003016 // Ensure the task/activity being brought forward is not the assistant
3017 return false;
3018 }
Winson Chungf7e03e12017-08-22 11:32:16 -07003019 return true;
3020 }
3021
Jorim Jaggi02886a82016-12-06 09:10:06 -08003022 private boolean isTaskSwitch(ActivityRecord r,
3023 ActivityRecord topFocusedActivity) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003024 return topFocusedActivity != null && r.getTask() != topFocusedActivity.getTask();
Jorim Jaggi02886a82016-12-06 09:10:06 -08003025 }
3026
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 /**
3028 * Perform a reset of the given task, if needed as part of launching it.
3029 * Returns the new HistoryRecord at the top of the task.
3030 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08003031 /**
3032 * Helper method for #resetTaskIfNeededLocked.
3033 * We are inside of the task being reset... we'll either finish this activity, push it out
3034 * for another task, or leave it as-is.
3035 * @param task The task containing the Activity (taskTop) that might be reset.
3036 * @param forceReset
3037 * @return An ActivityOptions that needs to be processed.
3038 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07003039 private ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003040 ActivityOptions topOptions = null;
3041
3042 int replyChainEnd = -1;
3043 boolean canMoveOptions = true;
3044
3045 // We only do this for activities that are not the root of the task (since if we finish
3046 // the root, we may no longer have the task!).
3047 final ArrayList<ActivityRecord> activities = task.mActivities;
3048 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003049 final int rootActivityNdx = task.findEffectiveRootIndex();
3050 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003051 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003052 if (target.frontOfTask)
3053 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003054
3055 final int flags = target.info.flags;
3056 final boolean finishOnTaskLaunch =
3057 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3058 final boolean allowTaskReparenting =
3059 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3060 final boolean clearWhenTaskReset =
3061 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
3062
3063 if (!finishOnTaskLaunch
3064 && !clearWhenTaskReset
3065 && target.resultTo != null) {
3066 // If this activity is sending a reply to a previous
3067 // activity, we can't do anything with it now until
3068 // we reach the start of the reply chain.
3069 // XXX note that we are assuming the result is always
3070 // to the previous activity, which is almost always
3071 // the case but we really shouldn't count on.
3072 if (replyChainEnd < 0) {
3073 replyChainEnd = i;
3074 }
3075 } else if (!finishOnTaskLaunch
3076 && !clearWhenTaskReset
3077 && allowTaskReparenting
3078 && target.taskAffinity != null
3079 && !target.taskAffinity.equals(task.affinity)) {
3080 // If this activity has an affinity for another
3081 // task, then we need to move it out of here. We will
3082 // move it as far out of the way as possible, to the
3083 // bottom of the activity stack. This also keeps it
3084 // correctly ordered with any activities we previously
3085 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08003086 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003087 final ActivityRecord bottom =
3088 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08003089 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003090 if (bottom != null && target.taskAffinity != null
Bryce Leeaf691c02017-03-20 14:20:22 -07003091 && target.taskAffinity.equals(bottom.getTask().affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003092 // If the activity currently at the bottom has the
3093 // same task affinity as the one we are moving,
3094 // then merge it into the same task.
Bryce Leeaf691c02017-03-20 14:20:22 -07003095 targetTask = bottom.getTask();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003096 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003097 + " out to bottom task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003098 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003099 targetTask = createTaskRecord(
3100 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003101 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08003102 targetTask.affinityIntent = target.intent;
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 new task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003105 }
3106
Craig Mautner525f3d92013-05-07 14:01:50 -07003107 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003108 final int start = replyChainEnd < 0 ? i : replyChainEnd;
3109 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07003110 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003111 if (p.finishing) {
3112 continue;
3113 }
3114
Craig Mautnere3a74d52013-02-22 14:14:58 -08003115 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003116 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003117 topOptions = p.takeOptionsLocked();
3118 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003119 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003120 }
3121 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003122 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3123 "Removing activity " + p + " from task=" + task + " adding to task="
3124 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003125 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Bryce Leeaf691c02017-03-20 14:20:22 -07003126 "Pushing next activity " + p + " out to target's task " + target);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08003127 p.reparent(targetTask, 0 /* position - bottom */, "resetTargetTaskIfNeeded");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003128 }
3129
Wale Ogunwale1666e312016-12-16 11:27:18 -08003130 mWindowContainerController.positionChildAtBottom(
Wale Ogunwale66e16852017-10-19 13:35:52 -07003131 targetTask.getWindowContainerController(), false /* includingParents */);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003132 replyChainEnd = -1;
3133 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
3134 // If the activity should just be removed -- either
3135 // because it asks for it, or the task should be
3136 // cleared -- then finish it and anything that is
3137 // part of its reply chain.
3138 int end;
3139 if (clearWhenTaskReset) {
3140 // In this case, we want to finish this activity
3141 // and everything above it, so be sneaky and pretend
3142 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08003143 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003144 } else if (replyChainEnd < 0) {
3145 end = i;
3146 } else {
3147 end = replyChainEnd;
3148 }
Craig Mautner525f3d92013-05-07 14:01:50 -07003149 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003150 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003151 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003152 if (p.finishing) {
3153 continue;
3154 }
3155 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003156 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003157 topOptions = p.takeOptionsLocked();
3158 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003159 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003160 }
3161 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003162 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08003163 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08003164 if (finishActivityLocked(
3165 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003166 end--;
3167 srcPos--;
3168 }
3169 }
3170 replyChainEnd = -1;
3171 } else {
3172 // If we were in the middle of a chain, well the
3173 // activity that started it all doesn't want anything
3174 // special, so leave it all as-is.
3175 replyChainEnd = -1;
3176 }
3177 }
3178
3179 return topOptions;
3180 }
3181
3182 /**
3183 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
3184 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
3185 * @param affinityTask The task we are looking for an affinity to.
3186 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
3187 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
3188 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
3189 */
Craig Mautner525f3d92013-05-07 14:01:50 -07003190 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08003191 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003192 int replyChainEnd = -1;
3193 final int taskId = task.taskId;
3194 final String taskAffinity = task.affinity;
3195
3196 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
3197 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003198 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
3199
3200 // Do not operate on or below the effective root Activity.
3201 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003202 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003203 if (target.frontOfTask)
3204 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003205
3206 final int flags = target.info.flags;
3207 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3208 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3209
3210 if (target.resultTo != null) {
3211 // If this activity is sending a reply to a previous
3212 // activity, we can't do anything with it now until
3213 // we reach the start of the reply chain.
3214 // XXX note that we are assuming the result is always
3215 // to the previous activity, which is almost always
3216 // the case but we really shouldn't count on.
3217 if (replyChainEnd < 0) {
3218 replyChainEnd = i;
3219 }
3220 } else if (topTaskIsHigher
3221 && allowTaskReparenting
3222 && taskAffinity != null
3223 && taskAffinity.equals(target.taskAffinity)) {
3224 // This activity has an affinity for our task. Either remove it if we are
3225 // clearing or move it over to our task. Note that
3226 // we currently punt on the case where we are resetting a
3227 // task that is not at the top but who has activities above
3228 // with an affinity to it... this is really not a normal
3229 // case, and we will need to later pull that task to the front
3230 // and usually at that point we will do the reset and pick
3231 // up those remaining activities. (This only happens if
3232 // someone starts an activity in a new task from an activity
3233 // in a task that is not currently on top.)
3234 if (forceReset || finishOnTaskLaunch) {
3235 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003236 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3237 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003238 for (int srcPos = start; srcPos >= i; --srcPos) {
3239 final ActivityRecord p = activities.get(srcPos);
3240 if (p.finishing) {
3241 continue;
3242 }
Todd Kennedy539db512014-12-15 09:57:55 -08003243 finishActivityLocked(
3244 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003245 }
3246 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003247 if (taskInsertionPoint < 0) {
3248 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003249
Craig Mautner77878772013-03-04 19:46:24 -08003250 }
Craig Mautner77878772013-03-04 19:46:24 -08003251
3252 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003253 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3254 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3255 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003256 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003257 final ActivityRecord p = activities.get(srcPos);
Winson Chung30480042017-01-26 10:55:34 -08003258 p.reparent(task, taskInsertionPoint, "resetAffinityTaskIfNeededLocked");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003259
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003260 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3261 "Removing and adding activity " + p + " to stack at " + task
3262 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003263 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3264 + " from " + srcPos + " in to resetting task " + task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003265 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08003266 mWindowContainerController.positionChildAtTop(
3267 task.getWindowContainerController(), true /* includingParents */);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003268
3269 // Now we've moved it in to place... but what if this is
3270 // a singleTop activity and we have put it on top of another
3271 // instance of the same activity? Then we drop the instance
3272 // below so it remains singleTop.
3273 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3274 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003275 int targetNdx = taskActivities.indexOf(target);
3276 if (targetNdx > 0) {
3277 ActivityRecord p = taskActivities.get(targetNdx - 1);
3278 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003279 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3280 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003281 }
3282 }
3283 }
3284 }
3285
3286 replyChainEnd = -1;
3287 }
3288 }
Craig Mautner77878772013-03-04 19:46:24 -08003289 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003290 }
3291
Craig Mautner8849a5e2013-04-02 16:41:03 -07003292 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003293 ActivityRecord newActivity) {
Winson Chung5fa39752017-10-04 14:50:15 -07003294 final boolean forceReset =
Craig Mautnere3a74d52013-02-22 14:14:58 -08003295 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
Bryce Leeaf691c02017-03-20 14:20:22 -07003296 final TaskRecord task = taskTop.getTask();
Craig Mautnere3a74d52013-02-22 14:14:58 -08003297
3298 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3299 * for remaining tasks. Used for later tasks to reparent to task. */
3300 boolean taskFound = false;
3301
3302 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3303 ActivityOptions topOptions = null;
3304
Craig Mautner77878772013-03-04 19:46:24 -08003305 // Preserve the location for reparenting in the new task.
3306 int reparentInsertionPoint = -1;
3307
Craig Mautnere3a74d52013-02-22 14:14:58 -08003308 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3309 final TaskRecord targetTask = mTaskHistory.get(i);
3310
3311 if (targetTask == task) {
3312 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3313 taskFound = true;
3314 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003315 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3316 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003317 }
3318 }
3319
Craig Mautner70a86932013-02-28 22:37:44 -08003320 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003321 if (taskNdx >= 0) {
3322 do {
3323 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3324 } while (taskTop == null && taskNdx >= 0);
3325 }
Craig Mautner70a86932013-02-28 22:37:44 -08003326
Craig Mautnere3a74d52013-02-22 14:14:58 -08003327 if (topOptions != null) {
3328 // If we got some ActivityOptions from an activity on top that
3329 // was removed from the task, propagate them to the new real top.
3330 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003331 taskTop.updateOptionsLocked(topOptions);
3332 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003333 topOptions.abort();
3334 }
3335 }
3336
3337 return taskTop;
3338 }
3339
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003340 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3341 String resultWho, int requestCode, int resultCode, Intent data) {
3342
3343 if (callingUid > 0) {
3344 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003345 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003346 }
3347
3348 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3349 + " : who=" + resultWho + " req=" + requestCode
3350 + " res=" + resultCode + " data=" + data);
3351 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3352 try {
3353 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3354 list.add(new ResultInfo(resultWho, requestCode,
3355 resultCode, data));
Bryce Leeb0f993f2018-03-02 15:38:01 -08003356 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08003357 ActivityResultItem.obtain(list));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 return;
3359 } catch (Exception e) {
3360 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3361 }
3362 }
3363
3364 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3365 }
3366
Shunta Sato63b8ee32016-07-11 13:32:52 +09003367 /** 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 -07003368 private boolean isATopFinishingTask(TaskRecord task) {
Shunta Sato63b8ee32016-07-11 13:32:52 +09003369 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3370 final TaskRecord current = mTaskHistory.get(i);
3371 final ActivityRecord r = current.topRunningActivityLocked();
3372 if (r != null) {
3373 // We got a top running activity, so there isn't a top finishing task...
3374 return false;
3375 }
3376 if (current == task) {
3377 return true;
3378 }
3379 }
3380 return false;
3381 }
3382
Wale Ogunwale66e16852017-10-19 13:35:52 -07003383 private void adjustFocusedActivityStack(ActivityRecord r, String reason) {
Shunta Sato63b8ee32016-07-11 13:32:52 +09003384 if (!mStackSupervisor.isFocusedStack(this) ||
3385 ((mResumedActivity != r) && (mResumedActivity != null))) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003386 return;
3387 }
3388
3389 final ActivityRecord next = topRunningActivityLocked();
3390 final String myReason = reason + " adjustFocus";
Bryce Lee3345c4e2017-04-25 07:40:41 -07003391
Wale Ogunwale66e16852017-10-19 13:35:52 -07003392 if (next == r) {
3393 mStackSupervisor.moveFocusableActivityStackToFrontLocked(
3394 mStackSupervisor.topRunningActivityLocked(), myReason);
3395 return;
Wale Ogunwaled045c822015-12-02 09:14:28 -08003396 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003397
Wale Ogunwale66e16852017-10-19 13:35:52 -07003398 if (next != null && isFocusable()) {
3399 // Keep focus in stack if we have a top running activity and are focusable.
3400 return;
3401 }
3402
3403 // Task is not guaranteed to be non-null. For example, destroying the
3404 // {@link ActivityRecord} will disassociate the task from the activity.
3405 final TaskRecord task = r.getTask();
3406
3407 if (task == null) {
3408 throw new IllegalStateException("activity no longer associated with task:" + r);
3409 }
3410
3411 // Move focus to next focusable stack if possible.
3412 if (adjustFocusToNextFocusableStack(myReason)) {
3413 return;
3414 }
3415
3416 // Whatever...go home.
3417 mStackSupervisor.moveHomeStackTaskToTop(myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003418 }
3419
Andrii Kulian250d6532017-02-08 23:30:45 -08003420 /** Find next proper focusable stack and make it focused. */
Wale Ogunwale17696de2018-03-15 16:41:05 -07003421 boolean adjustFocusToNextFocusableStack(String reason) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07003422 return adjustFocusToNextFocusableStack(reason, false /* allowFocusSelf */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003423 }
3424
3425 /**
3426 * Find next proper focusable stack and make it focused.
3427 * @param allowFocusSelf Is the focus allowed to remain on the same stack.
3428 */
Wale Ogunwale66e16852017-10-19 13:35:52 -07003429 private boolean adjustFocusToNextFocusableStack(String reason, boolean allowFocusSelf) {
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08003430 final ActivityStack stack =
3431 mStackSupervisor.getNextFocusableStackLocked(this, !allowFocusSelf);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003432 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003433 if (stack == null) {
3434 return false;
3435 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003436
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003437 final ActivityRecord top = stack.topRunningActivityLocked();
3438
Wale Ogunwale66e16852017-10-19 13:35:52 -07003439 if (stack.isActivityTypeHome() && (top == null || !top.visible)) {
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003440 // If we will be focusing on the home stack next and its current top activity isn't
Wale Ogunwale66e16852017-10-19 13:35:52 -07003441 // visible, then use the move the home stack task to top to make the activity visible.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003442 return mStackSupervisor.moveHomeStackTaskToTop(reason);
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003443 }
Chong Zhang6cda19c2016-06-14 19:07:56 -07003444
3445 stack.moveToFront(myReason);
3446 return true;
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003447 }
3448
Craig Mautnerf3333272013-04-22 10:55:53 -07003449 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003450 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003451 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3452 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3453 if (!r.finishing) {
David Stevens9440dc82017-03-16 19:00:20 -07003454 if (!shouldSleepActivities()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003455 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003456 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3457 "stop-no-history", false)) {
Bryce Lee3345c4e2017-04-25 07:40:41 -07003458 // If {@link requestFinishActivityLocked} returns {@code true},
Wale Ogunwale66e16852017-10-19 13:35:52 -07003459 // {@link adjustFocusedActivityStack} would have been already called.
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003460 r.resumeKeyDispatchingLocked();
3461 return;
3462 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003463 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003464 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003465 + " on stop because we're just sleeping");
3466 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003467 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003468 }
3469
3470 if (r.app != null && r.app.thread != null) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07003471 adjustFocusedActivityStack(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003472 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473 try {
3474 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003475 if (DEBUG_STATES) Slog.v(TAG_STATES,
3476 "Moving to STOPPING: " + r + " (stop requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08003477 r.setState(STOPPING, "stopActivityLocked");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003478 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3479 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480 if (!r.visible) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003481 r.setVisible(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003482 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003483 EventLogTags.writeAmStopActivity(
3484 r.userId, System.identityHashCode(r), r.shortComponentName);
Bryce Leeb0f993f2018-03-02 15:38:01 -08003485 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Bryce Lee1d0d5142018-04-12 10:35:07 -07003486 StopActivityItem.obtain(r.visible, r.configChangeFlags));
David Stevens9440dc82017-03-16 19:00:20 -07003487 if (shouldSleepOrShutDownActivities()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003488 r.setSleeping(true);
3489 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003490 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003491 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003492 } catch (Exception e) {
3493 // Maybe just ignore exceptions here... if the process
3494 // has crashed, our death notification will clean things
3495 // up.
3496 Slog.w(TAG, "Exception thrown during pause", e);
3497 // Just in case, assume it to be stopped.
3498 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003499 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Bryce Lee7ace3952018-02-16 14:34:32 -08003500 r.setState(STOPPED, "stopActivityLocked");
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003501 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003502 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003503 }
3504 }
3505 }
3506 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003507
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 /**
3509 * @return Returns true if the activity is being finished, false if for
3510 * some reason it is being left as-is.
3511 */
3512 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003513 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003514 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003515 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3516 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003517 + ", result=" + resultCode + ", data=" + resultData
3518 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003519 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003520 return false;
3521 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522
Craig Mautnerd44711d2013-02-23 11:24:36 -08003523 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003524 return true;
3525 }
3526
Craig Mautnerd2328952013-03-05 12:46:26 -08003527 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003528 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3529 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3530 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3531 ActivityRecord r = activities.get(activityNdx);
3532 if (r.resultTo == self && r.requestCode == requestCode) {
3533 if ((r.resultWho == null && resultWho == null) ||
3534 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3535 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3536 false);
3537 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003538 }
3539 }
3540 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003541 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003542 }
3543
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07003544 /**
3545 * Finish the topmost activity that belongs to the crashed app. We may also finish the activity
3546 * that requested launch of the crashed one to prevent launch-crash loop.
3547 * @param app The app that crashed.
3548 * @param reason Reason to perform this action.
3549 * @return The task that was finished in this stack, {@code null} if top running activity does
3550 * not belong to the crashed app.
3551 */
3552 final TaskRecord finishTopCrashedActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003553 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003554 TaskRecord finishedTask = null;
3555 if (r == null || r.app != app) {
3556 return null;
3557 }
3558 Slog.w(TAG, " Force finishing activity "
3559 + r.intent.getComponent().flattenToShortString());
Bryce Leeaf691c02017-03-20 14:20:22 -07003560 finishedTask = r.getTask();
3561 int taskNdx = mTaskHistory.indexOf(finishedTask);
3562 final TaskRecord task = finishedTask;
3563 int activityNdx = task.mActivities.indexOf(r);
Adrian Roos93577212018-04-10 14:12:10 -07003564 mWindowManager.prepareAppTransition(TRANSIT_CRASHING_ACTIVITY_CLOSE, false /* TODO */,
3565 0, true /* forceOverride */);
Adrian Roos20d7df32016-01-12 18:59:43 +01003566 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Bryce Leeaf691c02017-03-20 14:20:22 -07003567 finishedTask = task;
Adrian Roos20d7df32016-01-12 18:59:43 +01003568 // Also terminate any activities below it that aren't yet
3569 // stopped, to avoid a situation where one will get
3570 // re-start our crashing activity once it gets resumed again.
3571 --activityNdx;
3572 if (activityNdx < 0) {
3573 do {
3574 --taskNdx;
3575 if (taskNdx < 0) {
3576 break;
3577 }
3578 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3579 } while (activityNdx < 0);
3580 }
3581 if (activityNdx >= 0) {
3582 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Bryce Lee7ace3952018-02-16 14:34:32 -08003583 if (r.isState(RESUMED, PAUSING, PAUSED)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003584 if (!r.isActivityTypeHome() || mService.mHomeProcess != r.app) {
Adrian Roos20d7df32016-01-12 18:59:43 +01003585 Slog.w(TAG, " Force finishing activity "
3586 + r.intent.getComponent().flattenToShortString());
3587 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003588 }
3589 }
3590 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003591 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003592 }
3593
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003594 final void finishVoiceTask(IVoiceInteractionSession session) {
3595 IBinder sessionBinder = session.asBinder();
3596 boolean didOne = false;
3597 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3598 TaskRecord tr = mTaskHistory.get(taskNdx);
3599 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3600 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3601 ActivityRecord r = tr.mActivities.get(activityNdx);
3602 if (!r.finishing) {
3603 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3604 false);
3605 didOne = true;
3606 }
3607 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003608 } else {
3609 // Check if any of the activities are using voice
3610 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3611 ActivityRecord r = tr.mActivities.get(activityNdx);
3612 if (r.voiceSession != null
3613 && r.voiceSession.asBinder() == sessionBinder) {
3614 // Inform of cancellation
3615 r.clearVoiceSessionLocked();
3616 try {
3617 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3618 null);
3619 } catch (RemoteException re) {
3620 // Ok
3621 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003622 mService.finishRunningVoiceLocked();
3623 break;
3624 }
3625 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003626 }
3627 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003628
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003629 if (didOne) {
3630 mService.updateOomAdjLocked();
3631 }
3632 }
3633
Craig Mautnerd2328952013-03-05 12:46:26 -08003634 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003635 ArrayList<ActivityRecord> activities = r.getTask().mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003636 for (int index = activities.indexOf(r); index >= 0; --index) {
3637 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003638 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003639 break;
3640 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003641 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003642 }
3643 return true;
3644 }
3645
Andrii Kulian21713ac2016-10-12 22:05:05 -07003646 private void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
Dianne Hackborn5c607432012-02-28 14:44:19 -08003647 // send the result
3648 ActivityRecord resultTo = r.resultTo;
3649 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003650 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003651 + " who=" + r.resultWho + " req=" + r.requestCode
3652 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003653 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003654 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003655 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003656 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003657 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003658 if (r.info.applicationInfo.uid > 0) {
3659 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3660 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003661 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003662 }
3663 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3664 resultData);
3665 r.resultTo = null;
3666 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003667 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003668
3669 // Make sure this HistoryRecord is not holding on to other resources,
3670 // because clients have remote IPC references to this object so we
3671 // can't assume that will go away and want to avoid circular IPC refs.
3672 r.results = null;
3673 r.pendingResults = null;
3674 r.newIntents = null;
3675 r.icicle = null;
3676 }
3677
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003678 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003679 * See {@link #finishActivityLocked(ActivityRecord, int, Intent, String, boolean, boolean)}
3680 */
3681 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3682 String reason, boolean oomAdj) {
3683 return finishActivityLocked(r, resultCode, resultData, reason, oomAdj, !PAUSE_IMMEDIATELY);
3684 }
3685
3686 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003687 * @return Returns true if this activity has been removed from the history
3688 * list, or false if it is still in the list and will be removed later.
3689 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003690 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
Winson Chung6954fc92017-03-24 16:22:12 -07003691 String reason, boolean oomAdj, boolean pauseImmediately) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 if (r.finishing) {
3693 Slog.w(TAG, "Duplicate finish request for " + r);
3694 return false;
3695 }
3696
Jorim Jaggife762342016-10-13 14:33:27 +02003697 mWindowManager.deferSurfaceLayout();
3698 try {
3699 r.makeFinishingLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07003700 final TaskRecord task = r.getTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003701 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3702 r.userId, System.identityHashCode(r),
3703 task.taskId, r.shortComponentName, reason);
3704 final ArrayList<ActivityRecord> activities = task.mActivities;
3705 final int index = activities.indexOf(r);
3706 if (index < (activities.size() - 1)) {
3707 task.setFrontOfTask();
3708 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3709 // If the caller asked that this activity (and all above it)
3710 // be cleared when the task is reset, don't lose that information,
3711 // but propagate it up to the next activity.
3712 ActivityRecord next = activities.get(index+1);
3713 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3714 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003715 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003716
Jorim Jaggife762342016-10-13 14:33:27 +02003717 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003718
Wale Ogunwale66e16852017-10-19 13:35:52 -07003719 adjustFocusedActivityStack(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003720
Jorim Jaggife762342016-10-13 14:33:27 +02003721 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003722
Yorke Leebdef5372017-04-10 16:38:51 -07003723 final boolean endTask = index <= 0 && !task.isClearingToReuseTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003724 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
3725 if (mResumedActivity == r) {
3726 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
3727 "Prepare close transition: finishing " + r);
Winson Chung6954fc92017-03-24 16:22:12 -07003728 if (endTask) {
3729 mService.mTaskChangeNotificationController.notifyTaskRemovalStarted(
3730 task.taskId);
3731 }
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003732 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733
Jorim Jaggife762342016-10-13 14:33:27 +02003734 // Tell window manager to prepare for this one to be removed.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003735 r.setVisibility(false);
Jorim Jaggife762342016-10-13 14:33:27 +02003736
3737 if (mPausingActivity == null) {
3738 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3739 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3740 "finish() => pause with userLeaving=false");
Winson Chung6954fc92017-03-24 16:22:12 -07003741 startPausingLocked(false, false, null, pauseImmediately);
Jorim Jaggife762342016-10-13 14:33:27 +02003742 }
3743
3744 if (endTask) {
Bryce Lee2b8e0372018-04-05 17:01:37 -07003745 mService.getLockTaskController().clearLockedTask(task);
Jorim Jaggife762342016-10-13 14:33:27 +02003746 }
Bryce Lee7ace3952018-02-16 14:34:32 -08003747 } else if (!r.isState(PAUSING)) {
Jorim Jaggife762342016-10-13 14:33:27 +02003748 // If the activity is PAUSING, we will complete the finish once
3749 // it is done pausing; else we can just directly finish it here.
3750 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
3751 if (r.visible) {
Winson Chung6954fc92017-03-24 16:22:12 -07003752 prepareActivityHideTransitionAnimation(r, transit);
3753 }
3754
3755 final int finishMode = (r.visible || r.nowVisible) ? FINISH_AFTER_VISIBLE
3756 : FINISH_AFTER_PAUSE;
Bryce Leef52974c2018-02-14 15:12:01 -08003757 final boolean removedActivity = finishCurrentActivityLocked(r, finishMode, oomAdj,
3758 "finishActivityLocked") == null;
Winson Chung6954fc92017-03-24 16:22:12 -07003759
3760 // The following code is an optimization. When the last non-task overlay activity
3761 // is removed from the task, we remove the entire task from the stack. However,
3762 // since that is done after the scheduled destroy callback from the activity, that
3763 // call to change the visibility of the task overlay activities would be out of
3764 // sync with the activitiy visibility being set for this finishing activity above.
3765 // In this case, we can set the visibility of all the task overlay activities when
3766 // we detect the last one is finishing to keep them in sync.
3767 if (task.onlyHasTaskOverlayActivities(true /* excludeFinishing */)) {
3768 for (ActivityRecord taskOverlay : task.mActivities) {
3769 if (!taskOverlay.mTaskOverlay) {
3770 continue;
3771 }
3772 prepareActivityHideTransitionAnimation(taskOverlay, transit);
Jorim Jaggife762342016-10-13 14:33:27 +02003773 }
3774 }
Winson Chung6954fc92017-03-24 16:22:12 -07003775 return removedActivity;
Jorim Jaggife762342016-10-13 14:33:27 +02003776 } else {
3777 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
3778 }
3779
3780 return false;
3781 } finally {
3782 mWindowManager.continueSurfaceLayout();
3783 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003784 }
3785
Winson Chung6954fc92017-03-24 16:22:12 -07003786 private void prepareActivityHideTransitionAnimation(ActivityRecord r, int transit) {
3787 mWindowManager.prepareAppTransition(transit, false);
3788 r.setVisibility(false);
3789 mWindowManager.executeAppTransition();
Bryce Lee4a194382017-04-04 14:32:48 -07003790 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(r)) {
3791 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(r);
Winson Chung6954fc92017-03-24 16:22:12 -07003792 }
3793 }
3794
Craig Mautnerf3333272013-04-22 10:55:53 -07003795 static final int FINISH_IMMEDIATELY = 0;
3796 static final int FINISH_AFTER_PAUSE = 1;
3797 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798
Bryce Leef52974c2018-02-14 15:12:01 -08003799 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj,
3800 String reason) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003801 // First things first: if this activity is currently visible,
3802 // and the resumed activity is not yet visible, then hold off on
3803 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003804
Bryce Leec961e0a2018-04-13 17:58:02 -07003805 // The activity that we are finishing may be over the lock screen. In this case, we do not
3806 // want to consider activities that cannot be shown on the lock screen as running and should
3807 // proceed with finishing the activity if there is no valid next top running activity.
3808 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked(
3809 true /* considerKeyguardState */);
Chong Zhang824b6dc2016-04-27 14:11:12 -07003810
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003811 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003812 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003813 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Winson Chung4dabf232017-01-25 13:25:22 -08003814 addToStopping(r, false /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003815 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003816 if (DEBUG_STATES) Slog.v(TAG_STATES,
3817 "Moving to STOPPING: "+ r + " (finish requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08003818 r.setState(STOPPING, "finishCurrentActivityLocked");
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003819 if (oomAdj) {
3820 mService.updateOomAdjLocked();
3821 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822 return r;
3823 }
3824
3825 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003826 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003827 mStackSupervisor.mGoingToSleepActivities.remove(r);
Bryce Lee4a194382017-04-04 14:32:48 -07003828 mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(r);
Bryce Lee7ace3952018-02-16 14:34:32 -08003829 final ActivityState prevState = r.getState();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003830 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08003831
Bryce Lee7ace3952018-02-16 14:34:32 -08003832 r.setState(FINISHING, "finishCurrentActivityLocked");
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003833 final boolean finishingActivityInNonFocusedStack
Andrii Kulian02b7a832016-10-06 23:11:56 -07003834 = r.getStack() != mStackSupervisor.getFocusedStack()
Bryce Lee7ace3952018-02-16 14:34:32 -08003835 && prevState == PAUSED && mode == FINISH_AFTER_VISIBLE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003836
3837 if (mode == FINISH_IMMEDIATELY
Bryce Lee7ace3952018-02-16 14:34:32 -08003838 || (prevState == PAUSED
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003839 && (mode == FINISH_AFTER_PAUSE || inPinnedWindowingMode()))
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003840 || finishingActivityInNonFocusedStack
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003841 || prevState == STOPPING
3842 || prevState == STOPPED
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003843 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003844 r.makeFinishingLocked();
Bryce Leef52974c2018-02-14 15:12:01 -08003845 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm:" + reason);
Andrii Kulian7318d632016-07-20 18:59:28 -07003846
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003847 if (finishingActivityInNonFocusedStack) {
3848 // Finishing activity that was in paused state and it was in not currently focused
3849 // stack, need to make something visible in its place.
Andrii Kulian7318d632016-07-20 18:59:28 -07003850 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3851 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003852 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003853 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003854 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003855 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003856 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3857 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003858 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003859 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003860
3861 // Need to go through the full pause cycle to get this
3862 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003863 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003864 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003865 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003866 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003867 return r;
3868 }
3869
Craig Mautneree36c772014-07-16 14:56:05 -07003870 void finishAllActivitiesLocked(boolean immediately) {
3871 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003872 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3873 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3874 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3875 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003876 noActivitiesInStack = false;
3877 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003878 continue;
3879 }
Craig Mautneree36c772014-07-16 14:56:05 -07003880 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Bryce Leef52974c2018-02-14 15:12:01 -08003881 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false,
3882 "finishAllActivitiesLocked");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003883 }
3884 }
Craig Mautneree36c772014-07-16 14:56:05 -07003885 if (noActivitiesInStack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003886 remove();
Craig Mautneree36c772014-07-16 14:56:05 -07003887 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003888 }
3889
Wale Ogunwale66e16852017-10-19 13:35:52 -07003890 /** @return true if the stack behind this one is a standard activity type. */
3891 boolean inFrontOfStandardStack() {
3892 final ActivityDisplay display = getDisplay();
3893 if (display == null) {
3894 return false;
3895 }
3896 final int index = display.getIndexOf(this);
3897 if (index == 0) {
3898 return false;
3899 }
3900 final ActivityStack stackBehind = display.getChildAt(index - 1);
3901 return stackBehind.isActivityTypeStandard();
3902 }
3903
3904 boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003905 // Basic case: for simple app-centric recents, we need to recreate
3906 // the task if the affinity has changed.
Bryce Leeaf691c02017-03-20 14:20:22 -07003907 if (srec == null || srec.getTask().affinity == null ||
3908 !srec.getTask().affinity.equals(destAffinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003909 return true;
3910 }
3911 // Document-centric case: an app may be split in to multiple documents;
3912 // they need to re-create their task if this current activity is the root
3913 // of a document, unless simply finishing it will return them to the the
3914 // correct app behind.
Bryce Leeaf691c02017-03-20 14:20:22 -07003915 final TaskRecord task = srec.getTask();
Wale Ogunwale66e16852017-10-19 13:35:52 -07003916 if (srec.frontOfTask && task.getBaseIntent() != null && task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003917 // Okay, this activity is at the root of its task. What to do, what to do...
Wale Ogunwale66e16852017-10-19 13:35:52 -07003918 if (!inFrontOfStandardStack()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003919 // Finishing won't return to an application, so we need to recreate.
3920 return true;
3921 }
3922 // We now need to get the task below it to determine what to do.
Bryce Leeaf691c02017-03-20 14:20:22 -07003923 int taskIdx = mTaskHistory.indexOf(task);
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003924 if (taskIdx <= 0) {
3925 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3926 return false;
3927 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003928 final TaskRecord prevTask = mTaskHistory.get(taskIdx);
Bryce Leeaf691c02017-03-20 14:20:22 -07003929 if (!task.affinity.equals(prevTask.affinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003930 // These are different apps, so need to recreate.
3931 return true;
3932 }
3933 }
3934 return false;
3935 }
3936
Wale Ogunwale7d701172015-03-11 15:36:30 -07003937 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003938 Intent resultData) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003939 final TaskRecord task = srec.getTask();
Craig Mautner0247fc82013-02-28 14:32:06 -08003940 final ArrayList<ActivityRecord> activities = task.mActivities;
3941 final int start = activities.indexOf(srec);
3942 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003943 return false;
3944 }
3945 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003946 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003947 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003948 final ComponentName dest = destIntent.getComponent();
3949 if (start > 0 && dest != null) {
3950 for (int i = finishTo; i >= 0; i--) {
3951 ActivityRecord r = activities.get(i);
3952 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003953 r.info.name.equals(dest.getClassName())) {
3954 finishTo = i;
3955 parent = r;
3956 foundParentInTask = true;
3957 break;
3958 }
3959 }
3960 }
3961
3962 IActivityController controller = mService.mController;
3963 if (controller != null) {
3964 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3965 if (next != null) {
3966 // ask watcher if this is allowed
3967 boolean resumeOK = true;
3968 try {
3969 resumeOK = controller.activityResuming(next.packageName);
3970 } catch (RemoteException e) {
3971 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003972 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003973 }
3974
3975 if (!resumeOK) {
3976 return false;
3977 }
3978 }
3979 }
3980 final long origId = Binder.clearCallingIdentity();
3981 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003982 ActivityRecord r = activities.get(i);
3983 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003984 // Only return the supplied result for the first activity finished
3985 resultCode = Activity.RESULT_CANCELED;
3986 resultData = null;
3987 }
3988
3989 if (parent != null && foundParentInTask) {
3990 final int parentLaunchMode = parent.info.launchMode;
3991 final int destIntentFlags = destIntent.getFlags();
3992 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3993 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3994 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3995 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003996 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3997 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003998 } else {
3999 try {
4000 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -07004001 destIntent.getComponent(), ActivityManagerService.STOCK_PM_FLAGS,
4002 srec.userId);
Bryce Lee4c9a5972017-12-01 22:14:24 -08004003 // TODO(b/64750076): Check if calling pid should really be -1.
4004 final int res = mService.getActivityStartController()
4005 .obtainStarter(destIntent, "navigateUpTo")
4006 .setCaller(srec.app.thread)
4007 .setActivityInfo(aInfo)
4008 .setResultTo(parent.appToken)
4009 .setCallingPid(-1)
4010 .setCallingUid(parent.launchedFromUid)
4011 .setCallingPackage(parent.launchedFromPackage)
4012 .setRealCallingPid(-1)
4013 .setRealCallingUid(parent.launchedFromUid)
4014 .setComponentSpecified(true)
4015 .execute();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004016 foundParentInTask = res == ActivityManager.START_SUCCESS;
4017 } catch (RemoteException e) {
4018 foundParentInTask = false;
4019 }
4020 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08004021 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004022 }
4023 }
4024 Binder.restoreCallingIdentity(origId);
4025 return foundParentInTask;
4026 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004027
4028 /**
4029 * Remove any state associated with the {@link ActivityRecord}. This should be called whenever
4030 * an activity moves away from the stack.
4031 */
4032 void onActivityRemovedFromStack(ActivityRecord r) {
Bryce Lee84730a02018-04-03 14:10:04 -07004033 removeTimeoutsForActivityLocked(r);
4034
4035 if (mResumedActivity != null && mResumedActivity == r) {
4036 setResumedActivity(null, "onActivityRemovedFromStack");
Bryce Leeaf691c02017-03-20 14:20:22 -07004037 }
Bryce Lee84730a02018-04-03 14:10:04 -07004038 if (mPausingActivity != null && mPausingActivity == r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004039 mPausingActivity = null;
4040 }
Bryce Lee84730a02018-04-03 14:10:04 -07004041 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004042
Bryce Lee84730a02018-04-03 14:10:04 -07004043 void onActivityAddedToStack(ActivityRecord r) {
4044 if(r.getState() == RESUMED) {
4045 setResumedActivity(r, "onActivityAddedToStack");
4046 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004047 }
4048
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004049 /**
4050 * Perform the common clean-up of an activity record. This is called both
4051 * as part of destroyActivityLocked() (when destroying the client-side
4052 * representation) and cleaning things up as a result of its hosting
4053 * processing going away, in which case there is no remaining client-side
4054 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08004055 *
4056 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004057 */
Bryce Leec9406602018-03-09 11:02:10 -08004058 private void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices, boolean setState) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004059 onActivityRemovedFromStack(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004060
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004061 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004062 r.frozenBeforeDestroy = false;
4063
Bryce Leec9406602018-03-09 11:02:10 -08004064 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004065 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004066 r.setState(DESTROYED, "cleanupActivityLocked");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004067 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004068 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004069 }
4070
Bryce Lee4a194382017-04-04 14:32:48 -07004071 // Inform supervisor the activity has been removed.
4072 mStackSupervisor.cleanupActivity(r);
4073
Craig Mautner2420ead2013-04-01 17:13:20 -07004074
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004075 // Remove any pending results.
4076 if (r.finishing && r.pendingResults != null) {
4077 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
4078 PendingIntentRecord rec = apr.get();
4079 if (rec != null) {
4080 mService.cancelIntentSenderLocked(rec, false);
4081 }
4082 }
4083 r.pendingResults = null;
4084 }
4085
4086 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07004087 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004088 }
4089
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004090 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004091 removeTimeoutsForActivityLocked(r);
Wale Ogunwale8fd75422016-06-24 14:20:37 -07004092 // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
4093 // manager so it can update its bookkeeping.
4094 mWindowManager.notifyAppRelaunchesCleared(r.appToken);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004095 }
4096
Winson Chung4dabf232017-01-25 13:25:22 -08004097 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004098 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004099 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004100 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004101 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07004102 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004103 }
4104
Chong Zhangbffd8892016-08-08 11:16:06 -07004105 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004106 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07004107 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004108 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
4109 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
4110
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004111 r.takeFromHistory();
4112 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004113 if (DEBUG_STATES) Slog.v(TAG_STATES,
4114 "Moving to DESTROYED: " + r + " (removed from history)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004115 r.setState(DESTROYED, "removeActivityFromHistoryLocked");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004116 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004117 r.app = null;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08004118 r.removeWindowContainer();
Bryce Leeaf691c02017-03-20 14:20:22 -07004119 final TaskRecord task = r.getTask();
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004120 final boolean lastActivity = task != null ? task.removeActivity(r) : false;
Winson Chung6954fc92017-03-24 16:22:12 -07004121 // If we are removing the last activity in the task, not including task overlay activities,
4122 // then fall through into the block below to remove the entire task itself
4123 final boolean onlyHasTaskOverlays = task != null
4124 ? task.onlyHasTaskOverlayActivities(false /* excludingFinishing */) : false;
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004125
Winson Chung6954fc92017-03-24 16:22:12 -07004126 if (lastActivity || onlyHasTaskOverlays) {
4127 if (DEBUG_STACK) {
4128 Slog.i(TAG_STACK,
4129 "removeActivityFromHistoryLocked: last activity removed from " + this
4130 + " onlyHasTaskOverlays=" + onlyHasTaskOverlays);
4131 }
4132
Bryce Leed6d26752017-05-25 13:57:46 -07004133 // The following block can be executed multiple times if there is more than one overlay.
4134 // {@link ActivityStackSupervisor#removeTaskByIdLocked} handles this by reverse lookup
4135 // of the task by id and exiting early if not found.
Winson Chunge3c21e02017-04-11 18:02:17 -07004136 if (onlyHasTaskOverlays) {
4137 // When destroying a task, tell the supervisor to remove it so that any activity it
4138 // has can be cleaned up correctly. This is currently the only place where we remove
4139 // a task with the DESTROYING mode, so instead of passing the onlyHasTaskOverlays
4140 // state into removeTask(), we just clear the task here before the other residual
4141 // work.
4142 // TODO: If the callers to removeTask() changes such that we have multiple places
4143 // where we are destroying the task, move this back into removeTask()
4144 mStackSupervisor.removeTaskByIdLocked(task.taskId, false /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07004145 !REMOVE_FROM_RECENTS, PAUSE_IMMEDIATELY, reason);
Winson Chunge3c21e02017-04-11 18:02:17 -07004146 }
Bryce Lee1d930582017-05-01 08:35:24 -07004147
Bryce Leed6d26752017-05-25 13:57:46 -07004148 // We must keep the task around until all activities are destroyed. The following
4149 // statement will only execute once since overlays are also considered activities.
Bryce Lee1d930582017-05-01 08:35:24 -07004150 if (lastActivity) {
4151 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
4152 }
Craig Mautner312ba862014-02-10 17:55:01 -08004153 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004154 cleanUpActivityServicesLocked(r);
4155 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004157
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004158 /**
4159 * Perform clean-up of service connections in an activity record.
4160 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07004161 private void cleanUpActivityServicesLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004162 // Throw away any services that have been bound by this activity.
4163 if (r.connections != null) {
4164 Iterator<ConnectionRecord> it = r.connections.iterator();
4165 while (it.hasNext()) {
4166 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004167 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004168 }
4169 r.connections = null;
4170 }
4171 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004172
Craig Mautneree2e45a2014-06-27 12:10:03 -07004173 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004174 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004175 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004176 mHandler.sendMessage(msg);
4177 }
4178
Andrii Kulian21713ac2016-10-12 22:05:05 -07004179 private void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004180 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004181 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004182 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4183 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4184 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4185 final ActivityRecord r = activities.get(activityNdx);
4186 if (r.finishing) {
4187 continue;
4188 }
4189 if (r.fullscreen) {
4190 lastIsOpaque = true;
4191 }
4192 if (owner != null && r.app != owner) {
4193 continue;
4194 }
4195 if (!lastIsOpaque) {
4196 continue;
4197 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004198 if (r.isDestroyable()) {
Bryce Lee7ace3952018-02-16 14:34:32 -08004199 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r
4200 + " in state " + r.getState()
Craig Mautnerd44711d2013-02-23 11:24:36 -08004201 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004202 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004203 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004204 activityRemoved = true;
4205 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004206 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004207 }
4208 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004209 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004210 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004211 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004212 }
4213
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004214 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
4215 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004216 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Bryce Lee7ace3952018-02-16 14:34:32 -08004217 "Destroying " + r + " in state " + r.getState() + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004218 + " pausing=" + mPausingActivity + " for reason " + reason);
4219 return destroyActivityLocked(r, true, reason);
4220 }
4221 return false;
4222 }
4223
4224 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
4225 String reason) {
4226 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004227 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004228 int maxTasks = tasks.size() / 4;
4229 if (maxTasks < 1) {
4230 maxTasks = 1;
4231 }
4232 int numReleased = 0;
4233 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
4234 final TaskRecord task = mTaskHistory.get(taskNdx);
4235 if (!tasks.contains(task)) {
4236 continue;
4237 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004238 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004239 int curNum = 0;
4240 final ArrayList<ActivityRecord> activities = task.mActivities;
4241 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
4242 final ActivityRecord activity = activities.get(actNdx);
4243 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004244 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Bryce Lee7ace3952018-02-16 14:34:32 -08004245 + " in state " + activity.getState() + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004246 + " pausing=" + mPausingActivity + " for reason " + reason);
4247 destroyActivityLocked(activity, true, reason);
4248 if (activities.get(actNdx) != activity) {
4249 // Was removed from list, back up so we don't miss the next one.
4250 actNdx--;
4251 }
4252 curNum++;
4253 }
4254 }
4255 if (curNum > 0) {
4256 numReleased += curNum;
4257 maxTasks--;
4258 if (mTaskHistory.get(taskNdx) != task) {
4259 // The entire task got removed, back up so we don't miss the next one.
4260 taskNdx--;
4261 }
4262 }
4263 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004264 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
4265 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004266 return numReleased;
4267 }
4268
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004269 /**
4270 * Destroy the current CLIENT SIDE instance of an activity. This may be
4271 * called both when actually finishing an activity, or when performing
4272 * a configuration switch where we destroy the current client-side object
4273 * but then create a new client-side object for this same HistoryRecord.
4274 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07004275 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004276 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
4277 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004278 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Bryce Leeb0f993f2018-03-02 15:38:01 -08004279
4280 if (r.isState(DESTROYING, DESTROYED)) {
Bryce Leec9406602018-03-09 11:02:10 -08004281 if (DEBUG_STATES) Slog.v(TAG_STATES, "activity " + r + " already destroying."
Bryce Leeb0f993f2018-03-02 15:38:01 -08004282 + "skipping request with reason:" + reason);
4283 return false;
4284 }
4285
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004286 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004287 r.userId, System.identityHashCode(r),
Bryce Leeaf691c02017-03-20 14:20:22 -07004288 r.getTask().taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004289
4290 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004291
Bryce Leec9406602018-03-09 11:02:10 -08004292 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004293
4294 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004295
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004296 if (hadApp) {
4297 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004298 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
4300 mService.mHeavyWeightProcess = null;
4301 mService.mHandler.sendEmptyMessage(
4302 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
4303 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004304 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07004305 // Update any services we are bound to that might care about whether
4306 // their client may have activities.
4307 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07004308 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07004309 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07004310 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004311 }
4312 }
4313
4314 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004315
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004316 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004317 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08004318 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Bryce Lee1d0d5142018-04-12 10:35:07 -07004319 DestroyActivityItem.obtain(r.finishing, r.configChangeFlags));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004320 } catch (Exception e) {
4321 // We can just ignore exceptions here... if the process
4322 // has crashed, our death notification will clean things
4323 // up.
4324 //Slog.w(TAG, "Exception thrown during finish", e);
4325 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004326 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004327 removedFromHistory = true;
4328 skipDestroy = true;
4329 }
4330 }
4331
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004332 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004333
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004334 // If the activity is finishing, we need to wait on removing it
4335 // from the list to give it a chance to do its cleanup. During
4336 // that time it may make calls back with its token so we need to
4337 // be able to find it on the list and so we don't want to remove
4338 // it from the list yet. Otherwise, we can just immediately put
4339 // it in the destroyed state since we are not removing it from the
4340 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004341 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004342 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004343 + " (destroy requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004344 r.setState(DESTROYING,
4345 "destroyActivityLocked. finishing and not skipping destroy");
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07004346 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004347 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4348 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004349 if (DEBUG_STATES) Slog.v(TAG_STATES,
4350 "Moving to DESTROYED: " + r + " (destroy skipped)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004351 r.setState(DESTROYED,
4352 "destroyActivityLocked. not finishing or skipping destroy");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004353 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004354 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004355 }
4356 } else {
4357 // remove this record from the history.
4358 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004359 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004360 removedFromHistory = true;
4361 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004362 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004363 r.setState(DESTROYED, "destroyActivityLocked. not finishing and had no app");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004364 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004365 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004366 }
4367 }
4368
4369 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004370
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004371 if (!mLRUActivities.remove(r) && hadApp) {
4372 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4373 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004374
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004375 return removedFromHistory;
4376 }
4377
Craig Mautner299f9602015-01-26 09:47:33 -08004378 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004379 final long origId = Binder.clearCallingIdentity();
4380 try {
Bryce Lee7ace3952018-02-16 14:34:32 -08004381 activityDestroyedLocked(ActivityRecord.forTokenLocked(token), reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004382 } finally {
4383 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004384 }
4385 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004386
Bryce Leec9406602018-03-09 11:02:10 -08004387 /**
4388 * This method is to only be called from the client via binder when the activity is destroyed
4389 * AND finished.
4390 */
Bryce Lee7ace3952018-02-16 14:34:32 -08004391 final void activityDestroyedLocked(ActivityRecord record, String reason) {
4392 if (record != null) {
4393 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, record);
4394 }
4395
4396 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + record);
4397
4398 if (isInStackLocked(record) != null) {
4399 if (record.isState(DESTROYING, DESTROYED)) {
Bryce Leec9406602018-03-09 11:02:10 -08004400 cleanUpActivityLocked(record, true, false);
Bryce Lee7ace3952018-02-16 14:34:32 -08004401 removeActivityFromHistoryLocked(record, reason);
4402 }
4403 }
4404
4405 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4406 }
4407
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004408 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4409 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004410 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004411 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4412 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004413 while (i > 0) {
4414 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004415 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004416 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004417 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004418 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004419 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004420 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004421 }
4422 }
4423 }
4424
Andrii Kulian21713ac2016-10-12 22:05:05 -07004425 private boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004426 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004427 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4428 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004429 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4430 "mGoingToSleepActivities");
Bryce Lee4a194382017-04-04 14:32:48 -07004431 removeHistoryRecordsForAppLocked(mStackSupervisor.mActivitiesWaitingForVisibleActivity, app,
4432 "mActivitiesWaitingForVisibleActivity");
Craig Mautnerf3333272013-04-22 10:55:53 -07004433 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4434 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004435
4436 boolean hasVisibleActivities = false;
4437
4438 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004439 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004440 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4441 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4443 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Bryce Leefbd263b42018-03-07 10:33:55 -08004444 mTmpActivities.clear();
4445 mTmpActivities.addAll(activities);
4446
4447 while (!mTmpActivities.isEmpty()) {
4448 final int targetIndex = mTmpActivities.size() - 1;
4449 final ActivityRecord r = mTmpActivities.remove(targetIndex);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004450 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
Bryce Leefbd263b42018-03-07 10:33:55 -08004451 "Record #" + targetIndex + " " + r + ": app=" + r.app);
4452
Craig Mautner0247fc82013-02-28 14:32:06 -08004453 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004454 if (r.visible) {
4455 hasVisibleActivities = true;
4456 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004457 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004458 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4459 // Don't currently have state for the activity, or
4460 // it is finishing -- always remove it.
4461 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004462 } else if (!r.visible && r.launchCount > 2 &&
4463 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004464 // We have launched this activity too many times since it was
4465 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004466 // (Note if the activity is visible, we don't remove the record.
4467 // We leave the dead window on the screen but the process will
4468 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004469 remove = true;
4470 } else {
4471 // The process may be gone, but the activity lives on!
4472 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004473 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004474 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004475 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4476 "Removing activity " + r + " from stack at " + i
4477 + ": haveState=" + r.haveState
4478 + " stateNotNeeded=" + r.stateNotNeeded
4479 + " finishing=" + r.finishing
Bryce Lee7ace3952018-02-16 14:34:32 -08004480 + " state=" + r.getState() + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004481 if (!r.finishing) {
4482 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4483 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4484 r.userId, System.identityHashCode(r),
Bryce Leeaf691c02017-03-20 14:20:22 -07004485 r.getTask().taskId, r.shortComponentName,
Craig Mautner0247fc82013-02-28 14:32:06 -08004486 "proc died without state saved");
Bryce Lee7ace3952018-02-16 14:34:32 -08004487 if (r.getState() == RESUMED) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07004488 mService.updateUsageStats(r, false);
4489 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004490 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004491 } else {
4492 // We have the current state for this activity, so
4493 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004494 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4495 if (DEBUG_APP) Slog.v(TAG_APP,
4496 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004497 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004498 // Set nowVisible to previous visible state. If the app was visible while
4499 // it died, we leave the dead window on screen so it's basically visible.
4500 // This is needed when user later tap on the dead window, we need to stop
4501 // other apps when user transfers focus to the restarted activity.
4502 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004503 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004504 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004505 "App died, clearing saved state of " + r);
4506 r.icicle = null;
4507 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004508 }
Bryce Leec9406602018-03-09 11:02:10 -08004509 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004510 if (remove) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004511 removeActivityFromHistoryLocked(r, "appDied");
Craig Mautneracebdc82015-02-24 10:53:03 -08004512 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004513 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004514 }
4515 }
4516
4517 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004518 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004519
chaviw0d562bf2018-03-15 14:24:14 -07004520 private void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004521 if (options != null) {
chaviw0d562bf2018-03-15 14:24:14 -07004522 ActivityRecord r = topRunningActivityLocked();
4523 if (r != null && !r.isState(RESUMED)) {
4524 r.updateOptionsLocked(options);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004525 } else {
4526 ActivityOptions.abort(options);
4527 }
4528 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004529 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004530 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004531
Andrii Kulian21713ac2016-10-12 22:05:05 -07004532 private void updateTaskMovement(TaskRecord task, boolean toFront) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004533 if (task.isPersistable) {
4534 task.mLastTimeMoved = System.currentTimeMillis();
4535 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4536 // recently will be most negative, tasks sent to the bottom before that will be less
4537 // negative. Similarly for recent tasks moved to the top which will be most positive.
4538 if (!toFront) {
4539 task.mLastTimeMoved *= -1;
4540 }
4541 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004542 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004543 }
4544
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004545 void moveHomeStackTaskToTop() {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004546 if (!isActivityTypeHome()) {
4547 throw new IllegalStateException("Calling moveHomeStackTaskToTop() on non-home stack: "
4548 + this);
4549 }
Craig Mautnera82aa092013-09-13 15:34:08 -07004550 final int top = mTaskHistory.size() - 1;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004551 if (top >= 0) {
4552 final TaskRecord task = mTaskHistory.get(top);
4553 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4554 "moveHomeStackTaskToTop: moving " + task);
4555 mTaskHistory.remove(top);
4556 mTaskHistory.add(top, task);
4557 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004558 }
4559 }
4560
chaviw0d562bf2018-03-15 14:24:14 -07004561 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
4562 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004563 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004564
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004565 final ActivityStack topStack = getDisplay().getTopStack();
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004566 final ActivityRecord topActivity = topStack != null ? topStack.getTopActivity() : null;
Craig Mautner11bf9a52013-02-19 14:08:51 -08004567 final int numTasks = mTaskHistory.size();
4568 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004569 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004570 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004571 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004572 ActivityOptions.abort(options);
4573 } else {
chaviw0d562bf2018-03-15 14:24:14 -07004574 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004575 }
4576 return;
4577 }
4578
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004579 if (timeTracker != null) {
4580 // The caller wants a time tracker associated with this task.
4581 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4582 tr.mActivities.get(i).appTimeTracker = timeTracker;
4583 }
4584 }
4585
Craig Mautner11bf9a52013-02-19 14:08:51 -08004586 // Shift all activities with this task up to the top
4587 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004588 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004589
Chong Zhang45c25ce2015-08-10 22:18:26 -07004590 // Don't refocus if invisible to current user
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004591 final ActivityRecord top = tr.getTopActivity();
Chong Zhang87761972016-08-22 13:53:24 -07004592 if (top == null || !top.okToShowLocked()) {
Winson Chung1dbc8112017-09-28 18:05:31 -07004593 if (top != null) {
4594 mStackSupervisor.mRecentTasks.add(top.getTask());
4595 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07004596 ActivityOptions.abort(options);
4597 return;
4598 }
4599
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004600 // Set focus to the top running activity of this stack.
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004601 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang6cda19c2016-06-14 19:07:56 -07004602 mStackSupervisor.moveFocusableActivityStackToFrontLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004603
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004604 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004605 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004606 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004607 if (r != null) {
Jorim Jaggifa9ed962018-01-25 00:16:49 +01004608 mStackSupervisor.mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004609 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004610 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004611 } else {
chaviw0d562bf2018-03-15 14:24:14 -07004612 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004613 }
Winson Chungc2baac02017-01-11 13:34:47 -08004614 // If a new task is moved to the front, then mark the existing top activity as supporting
Winson Chung942a85c2017-07-11 15:07:45 -07004615 // picture-in-picture while paused only if the task would not be considered an oerlay on top
4616 // of the current activity (eg. not fullscreen, or the assistant)
Winson Chungf7e03e12017-08-22 11:32:16 -07004617 if (canEnterPipOnTaskSwitch(topActivity, tr, null /* toFrontActivity */,
4618 options)) {
4619 topActivity.supportsEnterPipOnTaskSwitch = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08004620 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004621
Wale Ogunwaled046a012015-12-24 13:05:59 -08004622 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004623 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Yorke Leebd54c2a2016-10-25 13:49:23 -07004624
4625 mService.mTaskChangeNotificationController.notifyTaskMovedToFront(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004626 }
4627
4628 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004629 * Worker method for rearranging history stack. Implements the function of moving all
4630 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004631 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004632 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004633 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4634 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004635 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004636 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004637 * @return Returns true if the move completed, false if not.
4638 */
Craig Mautner299f9602015-01-26 09:47:33 -08004639 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004640 final TaskRecord tr = taskForIdLocked(taskId);
4641 if (tr == null) {
4642 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4643 return false;
4644 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004645 Slog.i(TAG, "moveTaskToBack: " + tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004646
Charles Heff9b4dff2017-09-22 10:18:37 +01004647 // In LockTask mode, moving a locked task to the back of the stack may expose unlocked
4648 // ones. Therefore we need to check if this operation is allowed.
Bryce Lee2b8e0372018-04-05 17:01:37 -07004649 if (!mService.getLockTaskController().canMoveTaskToBack(tr)) {
Winson Chung261c0f32017-03-07 16:54:31 -08004650 return false;
4651 }
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004652
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004653 // If we have a watcher, preflight the move before committing to it. First check
4654 // for *other* available tasks, but if none are available, then try again allowing the
4655 // current task to be selected.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004656 if (isTopStackOnDisplay() && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004657 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004658 if (next == null) {
4659 next = topRunningActivityLocked(null, 0);
4660 }
4661 if (next != null) {
4662 // ask watcher if this is allowed
4663 boolean moveOK = true;
4664 try {
4665 moveOK = mService.mController.activityResuming(next.packageName);
4666 } catch (RemoteException e) {
4667 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004668 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004669 }
4670 if (!moveOK) {
4671 return false;
4672 }
4673 }
4674 }
4675
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004676 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004677
Wale Ogunwale66e16852017-10-19 13:35:52 -07004678 mTaskHistory.remove(tr);
4679 mTaskHistory.add(0, tr);
4680 updateTaskMovement(tr, false);
Wale Ogunwale42709242015-08-11 13:54:42 -07004681
Wale Ogunwale66e16852017-10-19 13:35:52 -07004682 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Evan Rosky9c448172017-11-02 14:19:27 -07004683 moveToBack("moveTaskToBackLocked", tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004684
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004685 if (inPinnedWindowingMode()) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004686 mStackSupervisor.removeStack(this);
Winson Chung261c0f32017-03-07 16:54:31 -08004687 return true;
4688 }
4689
Wale Ogunwaled046a012015-12-24 13:05:59 -08004690 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004691 return true;
4692 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004693
Andrii Kulian21713ac2016-10-12 22:05:05 -07004694 static void logStartActivity(int tag, ActivityRecord r, TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004695 final Uri data = r.intent.getData();
4696 final String strData = data != null ? data.toSafeString() : null;
4697
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004698 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004699 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004700 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004701 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004702 }
4703
4704 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004705 * Ensures all visible activities at or below the input activity have the right configuration.
4706 */
4707 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4708 if (start == null || !start.visible) {
4709 return;
4710 }
4711
Bryce Leeaf691c02017-03-20 14:20:22 -07004712 final TaskRecord startTask = start.getTask();
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004713 boolean behindFullscreen = false;
4714 boolean updatedConfig = false;
4715
4716 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4717 final TaskRecord task = mTaskHistory.get(taskIndex);
4718 final ArrayList<ActivityRecord> activities = task.mActivities;
4719 int activityIndex =
Bryce Leeaf691c02017-03-20 14:20:22 -07004720 (start.getTask() == task) ? activities.indexOf(start) : activities.size() - 1;
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004721 for (; activityIndex >= 0; --activityIndex) {
4722 final ActivityRecord r = activities.get(activityIndex);
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08004723 updatedConfig |= r.ensureActivityConfiguration(0 /* globalChanges */,
Andrii Kulian21713ac2016-10-12 22:05:05 -07004724 preserveWindow);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004725 if (r.fullscreen) {
4726 behindFullscreen = true;
4727 break;
4728 }
4729 }
4730 if (behindFullscreen) {
4731 break;
4732 }
4733 }
4734 if (updatedConfig) {
Wale Ogunwale089586f2016-06-20 14:16:22 -07004735 // Ensure the resumed state of the focus activity if we updated the configuration of
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004736 // any activity.
4737 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4738 }
4739 }
4740
Wale Ogunwale1666e312016-12-16 11:27:18 -08004741 // TODO: Figure-out a way to consolidate with resize() method below.
4742 @Override
4743 public void requestResize(Rect bounds) {
4744 mService.resizeStack(mStackId, bounds, true /* allowResizeInDockedMode */,
4745 false /* preserveWindows */, false /* animate */, -1 /* animationDuration */);
4746 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07004747
Wale Ogunwale1666e312016-12-16 11:27:18 -08004748 // TODO: Can only be called from special methods in ActivityStackSupervisor.
4749 // Need to consolidate those calls points into this resize method so anyone can call directly.
4750 void resize(Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004751 if (!updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
4752 return;
4753 }
4754
4755 // Update override configurations of all tasks in the stack.
4756 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
Andrii Kulian1e32e022016-09-16 15:29:34 -07004757 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
4758
4759 mTmpBounds.clear();
Andrii Kulian1e32e022016-09-16 15:29:34 -07004760 mTmpInsetBounds.clear();
4761
chaviwbe43ac82018-04-04 15:14:49 -07004762 synchronized (mWindowManager.getWindowManagerLock()) {
4763 for (int i = mTaskHistory.size() - 1; i >= 0; i--) {
4764 final TaskRecord task = mTaskHistory.get(i);
4765 if (task.isResizeable()) {
4766 if (inFreeformWindowingMode()) {
4767 // TODO: Can be removed now since each freeform task is in its own stack.
4768 // For freeform stack we don't adjust the size of the tasks to match that
4769 // of the stack, but we do try to make sure the tasks are still contained
4770 // with the bounds of the stack.
4771 mTmpRect2.set(task.getOverrideBounds());
4772 fitWithinBounds(mTmpRect2, bounds);
4773 task.updateOverrideConfiguration(mTmpRect2);
4774 } else {
4775 task.updateOverrideConfiguration(taskBounds, insetBounds);
4776 }
4777 }
4778
4779 mTmpBounds.put(task.taskId, task.getOverrideBounds());
4780 if (tempTaskInsetBounds != null) {
4781 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004782 }
4783 }
4784
chaviwbe43ac82018-04-04 15:14:49 -07004785 mWindowContainerController.resize(bounds, mTmpBounds, mTmpInsetBounds);
4786 setBounds(bounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004787 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07004788 }
4789
4790
4791 /**
4792 * Adjust bounds to stay within stack bounds.
4793 *
4794 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4795 * that keep them unchanged, but be contained within the stack bounds.
4796 *
4797 * @param bounds Bounds to be adjusted.
4798 * @param stackBounds Bounds within which the other bounds should remain.
4799 */
4800 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08004801 if (stackBounds == null || stackBounds.isEmpty() || stackBounds.contains(bounds)) {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004802 return;
4803 }
4804
4805 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4806 final int maxRight = stackBounds.right
4807 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4808 int horizontalDiff = stackBounds.left - bounds.left;
4809 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4810 || (bounds.left + horizontalDiff >= maxRight)) {
4811 horizontalDiff = maxRight - bounds.left;
4812 }
4813 bounds.left += horizontalDiff;
4814 bounds.right += horizontalDiff;
4815 }
4816
4817 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4818 final int maxBottom = stackBounds.bottom
4819 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4820 int verticalDiff = stackBounds.top - bounds.top;
4821 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4822 || (bounds.top + verticalDiff >= maxBottom)) {
4823 verticalDiff = maxBottom - bounds.top;
4824 }
4825 bounds.top += verticalDiff;
4826 bounds.bottom += verticalDiff;
4827 }
4828 }
4829
Craig Mautnercae015f2013-02-08 14:31:27 -08004830 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004831 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4832 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4833 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4834 final ActivityRecord r = activities.get(activityNdx);
4835 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004836 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004837 }
4838 if (r.fullscreen && !r.finishing) {
4839 return false;
4840 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004841 }
4842 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004843 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004844 if (r == null) {
4845 return false;
4846 }
4847 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4848 + " would have returned true for r=" + r);
4849 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004850 }
4851
4852 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004853 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4854 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4855 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4856 final ActivityRecord r = activities.get(activityNdx);
4857 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004858 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004859 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004860 }
4861 }
4862 }
4863
Wale Ogunwale540e1232015-05-01 15:35:39 -07004864 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4865 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004866 boolean didSomething = false;
4867 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004868 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004869 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4870 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Bryce Leefbd263b42018-03-07 10:33:55 -08004871 mTmpActivities.clear();
4872 mTmpActivities.addAll(activities);
4873
4874 while (!mTmpActivities.isEmpty()) {
4875 ActivityRecord r = mTmpActivities.remove(0);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004876 final boolean sameComponent =
4877 (r.packageName.equals(packageName) && (filterByClasses == null
4878 || filterByClasses.contains(r.realActivity.getClassName())))
4879 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004880 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Bryce Leeaf691c02017-03-20 14:20:22 -07004881 && (sameComponent || r.getTask() == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004882 && (r.app == null || evenPersistent || !r.app.persistent)) {
4883 if (!doit) {
4884 if (r.finishing) {
4885 // If this activity is just finishing, then it is not
4886 // interesting as far as something to stop.
4887 continue;
4888 }
4889 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004890 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004891 if (r.isActivityTypeHome()) {
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004892 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4893 Slog.i(TAG, "Skip force-stop again " + r);
4894 continue;
4895 } else {
4896 homeActivity = r.realActivity;
4897 }
4898 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004899 didSomething = true;
4900 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004901 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004902 if (r.app != null) {
4903 r.app.removed = true;
4904 }
4905 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004906 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004907 lastTask = r.getTask();
Bryce Leefbd263b42018-03-07 10:33:55 -08004908 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4909 true);
Craig Mautnercae015f2013-02-08 14:31:27 -08004910 }
4911 }
4912 }
4913 return didSomething;
4914 }
4915
Winson Chung61c9e5a2017-10-11 10:39:32 -07004916 /**
4917 * @return The set of running tasks through {@param tasksOut} that are available to the caller.
4918 * If {@param ignoreActivityType} or {@param ignoreWindowingMode} are not undefined,
4919 * then skip running tasks that match those types.
4920 */
4921 void getRunningTasks(List<TaskRecord> tasksOut, @ActivityType int ignoreActivityType,
4922 @WindowingMode int ignoreWindowingMode, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004923 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4924 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004925 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004926 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004927 if (task.getTopActivity() == null) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004928 // Skip if there are no activities in the task
riddle_hsuddc74152015-04-07 11:30:09 +08004929 continue;
4930 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004931 if (!allowed && !task.isActivityTypeHome() && task.effectiveUid != callingUid) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004932 // Skip if the caller can't fetch this task
Dianne Hackborn09233282014-04-30 11:33:59 -07004933 continue;
4934 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004935 if (ignoreActivityType != ACTIVITY_TYPE_UNDEFINED
4936 && task.getActivityType() == ignoreActivityType) {
4937 // Skip ignored activity type
4938 continue;
Craig Mautneraab647e2013-02-28 16:31:36 -08004939 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004940 if (ignoreWindowingMode != WINDOWING_MODE_UNDEFINED
4941 && task.getWindowingMode() == ignoreWindowingMode) {
4942 // Skip ignored windowing mode
4943 continue;
4944 }
riddle_hsuddc74152015-04-07 11:30:09 +08004945 if (focusedStack && topTask) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004946 // For the focused stack top task, update the last stack active time so that it can
4947 // be used to determine the order of the tasks (it may not be set for newly created
4948 // tasks)
4949 task.lastActiveTime = SystemClock.elapsedRealtime();
riddle_hsuddc74152015-04-07 11:30:09 +08004950 topTask = false;
4951 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004952 tasksOut.add(task);
Craig Mautnercae015f2013-02-08 14:31:27 -08004953 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004954 }
4955
Andrii Kulian21713ac2016-10-12 22:05:05 -07004956 void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004957 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004958 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004959 if (top >= 0) {
4960 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4961 int activityTop = activities.size() - 1;
Dianne Hackborn354736e2016-08-22 17:00:05 -07004962 if (activityTop >= 0) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004963 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4964 "unhandled-back", true);
4965 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004966 }
4967 }
4968
Craig Mautner6b74cb52013-09-27 17:02:21 -07004969 /**
4970 * Reset local parameters because an app's activity died.
4971 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004972 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004973 */
4974 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004975 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004976 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004977 "App died while pausing: " + mPausingActivity);
4978 mPausingActivity = null;
4979 }
4980 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4981 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004982 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004983 }
4984
Craig Mautner19091252013-10-05 00:03:53 -07004985 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004986 }
4987
Craig Mautnercae015f2013-02-08 14:31:27 -08004988 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004989 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4990 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4991 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4992 final ActivityRecord r = activities.get(activityNdx);
4993 if (r.app == app) {
4994 Slog.w(TAG, " Force finishing activity "
4995 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004996 // Force the destroy to skip right to removal.
4997 r.app = null;
Adrian Roos93577212018-04-10 14:12:10 -07004998 mWindowManager.prepareAppTransition(TRANSIT_CRASHING_ACTIVITY_CLOSE,
4999 false /* TODO */, 0, true /* forceOverride */);
Bryce Leef52974c2018-02-14 15:12:01 -08005000 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false,
5001 "handleAppCrashedLocked");
Craig Mautnerd44711d2013-02-23 11:24:36 -08005002 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005003 }
5004 }
5005 }
5006
Dianne Hackborn390517b2013-05-30 15:03:32 -07005007 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005008 boolean dumpClient, String dumpPackage, boolean needSep) {
5009
5010 if (mTaskHistory.isEmpty()) {
5011 return false;
5012 }
5013 final String prefix = " ";
Craig Mautneraab647e2013-02-28 16:31:36 -08005014 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5015 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005016 if (needSep) {
5017 pw.println("");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005018 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005019 pw.println(prefix + "Task id #" + task.taskId);
Bryce Leef3c6a472017-11-14 14:53:06 -08005020 pw.println(prefix + "mBounds=" + task.getOverrideBounds());
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005021 pw.println(prefix + "mMinWidth=" + task.mMinWidth);
5022 pw.println(prefix + "mMinHeight=" + task.mMinHeight);
5023 pw.println(prefix + "mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
5024 pw.println(prefix + "* " + task);
5025 task.dump(pw, prefix + " ");
5026 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
5027 prefix, "Hist", true, !dumpAll, dumpClient, dumpPackage, false, null, task);
Craig Mautneraab647e2013-02-28 16:31:36 -08005028 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005029 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08005030 }
5031
5032 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Winson Chung6998bc42017-02-28 17:07:05 -08005033 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautnercae015f2013-02-08 14:31:27 -08005034
5035 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005036 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5037 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08005038 }
5039 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005040 final int top = mTaskHistory.size() - 1;
5041 if (top >= 0) {
5042 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
5043 int listTop = list.size() - 1;
5044 if (listTop >= 0) {
5045 activities.add(list.get(listTop));
5046 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005047 }
5048 } else {
5049 ItemMatcher matcher = new ItemMatcher();
5050 matcher.build(name);
5051
Craig Mautneraab647e2013-02-28 16:31:36 -08005052 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5053 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
5054 if (matcher.match(r1, r1.intent.getComponent())) {
5055 activities.add(r1);
5056 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005057 }
5058 }
5059 }
5060
5061 return activities;
5062 }
5063
5064 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07005065 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08005066
5067 // All activities that came from the package must be
5068 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08005069 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5070 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5071 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5072 final ActivityRecord a = activities.get(activityNdx);
5073 if (a.info.packageName.equals(packageName)) {
5074 a.forceNewConfig = true;
5075 if (starting != null && a == starting && a.visible) {
5076 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08005077 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08005078 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005079 }
5080 }
5081 }
5082
5083 return starting;
5084 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005085
Wale Ogunwale000957c2015-04-03 08:19:12 -07005086 /**
5087 * Removes the input task from this stack.
5088 * @param task to remove.
5089 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07005090 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
5091 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07005092 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07005093 void removeTask(TaskRecord task, String reason, int mode) {
Bryce Leeaf691c02017-03-20 14:20:22 -07005094 for (ActivityRecord record : task.mActivities) {
5095 onActivityRemovedFromStack(record);
Craig Mautner04a0ea62014-01-13 12:51:26 -08005096 }
5097
Bryce Lee2b8e0372018-04-05 17:01:37 -07005098 final boolean removed = mTaskHistory.remove(task);
5099
5100 if (removed) {
5101 EventLog.writeEvent(EventLogTags.AM_REMOVE_TASK, task.taskId, getStackId());
5102 }
5103
Winson Chungabb433b2017-03-24 09:35:42 -07005104 removeActivitiesFromLRUListLocked(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005105 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07005106
Wale Ogunwale06579d62016-04-30 15:29:06 -07005107 if (mode == REMOVE_TASK_MODE_DESTROYING && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005108 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07005109 final boolean isVoiceSession = task.voiceSession != null;
5110 if (isVoiceSession) {
5111 try {
5112 task.voiceSession.taskFinished(task.intent, task.taskId);
5113 } catch (RemoteException e) {
5114 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07005115 }
Craig Mautner41db4a72014-05-07 17:20:56 -07005116 if (task.autoRemoveFromRecents() || isVoiceSession) {
5117 // Task creator asked to remove this when done, or this task was a voice
5118 // interaction, so it should not remain on the recent tasks list.
Winson Chung1dbc8112017-09-28 18:05:31 -07005119 mStackSupervisor.mRecentTasks.remove(task);
Craig Mautner41db4a72014-05-07 17:20:56 -07005120 }
Bryce Lee840c5662017-04-13 10:02:51 -07005121
5122 task.removeWindowContainer();
Dianne Hackborn91097de2014-04-04 18:02:06 -07005123 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005124
5125 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005126 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07005127 // We only need to adjust focused stack if this stack is in focus and we are not in the
5128 // process of moving the task to the top of the stack that will be focused.
5129 if (isOnHomeDisplay() && mode != REMOVE_TASK_MODE_MOVING_TO_TOP
5130 && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005131 String myReason = reason + " leftTaskHistoryEmpty";
Bryce Leef3c6a472017-11-14 14:53:06 -08005132 if (!inMultiWindowMode() || !adjustFocusToNextFocusableStack(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005133 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005134 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005135 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07005136 if (isAttached()) {
5137 getDisplay().positionChildAtBottom(this);
Craig Mautner593a4e62014-01-15 17:55:51 -08005138 }
Wale Ogunwale388945c2017-10-04 12:13:46 -07005139 if (!isActivityTypeHome()) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07005140 remove();
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005141 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005142 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005143
Andrii Kulian02b7a832016-10-06 23:11:56 -07005144 task.setStack(null);
Winson Chungc81c0ce2017-03-17 12:27:30 -07005145
5146 // Notify if a task from the pinned stack is being removed (or moved depending on the mode)
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005147 if (inPinnedWindowingMode()) {
Winson Chungc81c0ce2017-03-17 12:27:30 -07005148 mService.mTaskChangeNotificationController.notifyActivityUnpinned();
5149 }
Craig Mautner0247fc82013-02-28 14:32:06 -08005150 }
5151
Dianne Hackborn91097de2014-04-04 18:02:06 -07005152 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5153 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07005154 boolean toTop) {
Bryce Leeb802ea12017-11-15 21:25:03 -08005155 return createTaskRecord(taskId, info, intent, voiceSession, voiceInteractor, toTop,
5156 null /*activity*/, null /*source*/, null /*options*/);
5157 }
5158
5159 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5160 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
5161 boolean toTop, ActivityRecord activity, ActivityRecord source,
5162 ActivityOptions options) {
Garfield Tan9b1efea2017-12-05 16:43:46 -08005163 final TaskRecord task = TaskRecord.create(
5164 mService, taskId, info, intent, voiceSession, voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005165 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005166 addTask(task, toTop, "createTaskRecord");
Lucas Dupin47a65c72018-02-15 14:16:18 -08005167 final int displayId = mDisplayId != INVALID_DISPLAY ? mDisplayId : DEFAULT_DISPLAY;
Bryce Lee2a3cc462017-10-27 10:57:35 -07005168 final boolean isLockscreenShown = mService.mStackSupervisor.getKeyguardController()
Lucas Dupin47a65c72018-02-15 14:16:18 -08005169 .isKeyguardOrAodShowing(displayId);
Bryce Leeec55eb02017-12-05 20:51:27 -08005170 if (!mStackSupervisor.getLaunchParamsController()
Bryce Leeb802ea12017-11-15 21:25:03 -08005171 .layoutTask(task, info.windowLayout, activity, source, options)
Bryce Leef3c6a472017-11-14 14:53:06 -08005172 && !matchParentBounds() && task.isResizeable() && !isLockscreenShown) {
5173 task.updateOverrideConfiguration(getOverrideBounds());
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005174 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005175 task.createWindowContainer(toTop, (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005176 return task;
5177 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005178
5179 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005180 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005181 }
5182
Wale Ogunwale5f986092015-12-04 15:35:38 -08005183 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Winson Chung5af42fc2017-03-24 17:11:33 -07005184 addTask(task, toTop ? MAX_VALUE : 0, true /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005185 if (toTop) {
5186 // TODO: figure-out a way to remove this call.
Wale Ogunwale1666e312016-12-16 11:27:18 -08005187 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
5188 true /* includingParents */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005189 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005190 }
5191
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005192 // TODO: This shouldn't allow automatic reparenting. Remove the call to preAddTask and deal
5193 // with the fall-out...
Winson Chung5af42fc2017-03-24 17:11:33 -07005194 void addTask(final TaskRecord task, int position, boolean schedulePictureInPictureModeChange,
5195 String reason) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005196 // TODO: Is this remove really needed? Need to look into the call path for the other addTask
5197 mTaskHistory.remove(task);
5198 position = getAdjustedPositionForTask(task, position, null /* starting */);
5199 final boolean toTop = position >= mTaskHistory.size();
Wale Ogunwale06579d62016-04-30 15:29:06 -07005200 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005201
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005202 mTaskHistory.add(position, task);
Andrii Kulian02b7a832016-10-06 23:11:56 -07005203 task.setStack(this);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005204
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005205 updateTaskMovement(task, toTop);
5206
Winson Chung5af42fc2017-03-24 17:11:33 -07005207 postAddTask(task, prevStack, schedulePictureInPictureModeChange);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005208 }
5209
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005210 void positionChildAt(TaskRecord task, int index) {
5211
5212 if (task.getStack() != this) {
5213 throw new IllegalArgumentException("AS.positionChildAt: task=" + task
5214 + " is not a child of stack=" + this + " current parent=" + task.getStack());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005215 }
5216
5217 task.updateOverrideConfigurationForStack(this);
5218
Jorim Jaggi023da532016-04-20 22:42:32 -07005219 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07005220 final boolean wasResumed = topRunningActivity == task.getStack().mResumedActivity;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005221 insertTaskAtPosition(task, index);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005222 task.setStack(this);
Winson Chung5af42fc2017-03-24 17:11:33 -07005223 postAddTask(task, null /* prevStack */, true /* schedulePictureInPictureModeChange */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005224
Jorim Jaggi023da532016-04-20 22:42:32 -07005225 if (wasResumed) {
5226 if (mResumedActivity != null) {
5227 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5228 + " other stack to this stack mResumedActivity=" + mResumedActivity
5229 + " other mResumedActivity=" + topRunningActivity);
5230 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08005231 topRunningActivity.setState(RESUMED, "positionChildAt");
Jorim Jaggi023da532016-04-20 22:42:32 -07005232 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005233
5234 // The task might have already been running and its visibility needs to be synchronized with
5235 // the visibility of the stack / windows.
5236 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
5237 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005238 }
5239
Wale Ogunwale06579d62016-04-30 15:29:06 -07005240 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005241 final ActivityStack prevStack = task.getStack();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005242 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005243 prevStack.removeTask(task, reason,
5244 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005245 }
5246 return prevStack;
5247 }
5248
Winson Chung5af42fc2017-03-24 17:11:33 -07005249 /**
5250 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
5251 * change. Callers may set this to false if they are explicitly scheduling PiP mode
5252 * changes themselves, like during the PiP animation
5253 */
5254 private void postAddTask(TaskRecord task, ActivityStack prevStack,
5255 boolean schedulePictureInPictureModeChange) {
5256 if (schedulePictureInPictureModeChange && prevStack != null) {
5257 mStackSupervisor.scheduleUpdatePictureInPictureModeIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005258 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005259 try {
5260 task.voiceSession.taskStarted(task.intent, task.taskId);
5261 } catch (RemoteException e) {
5262 }
5263 }
5264 }
5265
Winson Chungc2baac02017-01-11 13:34:47 -08005266 void moveToFrontAndResumeStateIfNeeded(ActivityRecord r, boolean moveToFront, boolean setResume,
5267 boolean setPause, String reason) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08005268 if (!moveToFront) {
5269 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005270 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005271
5272 // If the activity owns the last resumed activity, transfer that together,
5273 // so that we don't resume the same activity again in the new stack.
5274 // Apps may depend on onResume()/onPause() being called in pairs.
5275 if (setResume) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08005276 r.setState(RESUMED, "moveToFrontAndResumeStateIfNeeded");
Winson Chungabb433b2017-03-24 09:35:42 -07005277 updateLRUListLocked(r);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005278 }
Winson Chungc2baac02017-01-11 13:34:47 -08005279 // If the activity was previously pausing, then ensure we transfer that as well
5280 if (setPause) {
5281 mPausingActivity = r;
Winson Chung4dabf232017-01-25 13:25:22 -08005282 schedulePauseTimeout(r);
Winson Chungc2baac02017-01-11 13:34:47 -08005283 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005284 // Move the stack in which we are placing the activity to the front. The call will also
5285 // make sure the activity focus is set.
5286 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005287 }
5288
Craig Mautnerc00204b2013-03-05 15:02:14 -08005289 public int getStackId() {
5290 return mStackId;
5291 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005292
5293 @Override
5294 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005295 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005296 + " stackId=" + mStackId + " type=" + activityTypeToString(getActivityType())
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07005297 + " mode=" + windowingModeToString(getWindowingMode())
Wale Ogunwale8e923af2018-03-23 08:48:11 -07005298 + " visible=" + shouldBeVisible(null /* starting */)
5299 + " translucent=" + isStackTranslucent(null /* starting */)
5300 + ", "
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005301 + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005302 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005303
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07005304 void onLockTaskPackagesUpdated() {
Craig Mautner15df08a2015-04-01 12:17:18 -07005305 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5306 mTaskHistory.get(taskNdx).setLockTaskAuth();
5307 }
5308 }
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005309
5310 void executeAppTransition(ActivityOptions options) {
5311 mWindowManager.executeAppTransition();
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005312 ActivityOptions.abort(options);
5313 }
David Stevens9440dc82017-03-16 19:00:20 -07005314
5315 boolean shouldSleepActivities() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07005316 final ActivityDisplay display = getDisplay();
Bryce Lee271617a2018-03-15 10:39:12 -07005317
5318 // Do not sleep activities in this stack if we're marked as focused and the keyguard
5319 // is in the process of going away.
5320 if (mStackSupervisor.getFocusedStack() == this
5321 && mStackSupervisor.getKeyguardController().isKeyguardGoingAway()) {
5322 return false;
5323 }
5324
David Stevens9440dc82017-03-16 19:00:20 -07005325 return display != null ? display.isSleeping() : mService.isSleepingLocked();
5326 }
5327
5328 boolean shouldSleepOrShutDownActivities() {
5329 return shouldSleepActivities() || mService.isShuttingDownLocked();
5330 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07005331
5332 public void writeToProto(ProtoOutputStream proto, long fieldId) {
5333 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02005334 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005335 proto.write(ID, mStackId);
5336 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5337 final TaskRecord task = mTaskHistory.get(taskNdx);
5338 task.writeToProto(proto, TASKS);
5339 }
5340 if (mResumedActivity != null) {
5341 mResumedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
5342 }
5343 proto.write(DISPLAY_ID, mDisplayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08005344 if (!matchParentBounds()) {
5345 final Rect bounds = getOverrideBounds();
5346 bounds.writeToProto(proto, BOUNDS);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005347 }
Bryce Leef3c6a472017-11-14 14:53:06 -08005348
5349 // TODO: Remove, no longer needed with windowingMode.
5350 proto.write(FULLSCREEN, matchParentBounds());
Steven Timotius4346f0a2017-09-12 11:07:21 -07005351 proto.end(token);
5352 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005353}