blob: eb482c1b14e279f256551d921e8010108798540d [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;
94import static android.view.WindowManager.TRANSIT_NONE;
95import static android.view.WindowManager.TRANSIT_TASK_CLOSE;
96import static android.view.WindowManager.TRANSIT_TASK_OPEN;
97import static android.view.WindowManager.TRANSIT_TASK_OPEN_BEHIND;
98import static android.view.WindowManager.TRANSIT_TASK_TO_BACK;
99import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700100
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800101import static java.lang.Integer.MAX_VALUE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700102
103import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700104import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700105import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700106import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800107import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108import android.app.ResultInfo;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700109import android.app.WindowConfiguration.ActivityType;
110import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian446e8242017-10-26 15:17:29 -0700111import android.app.servertransaction.ActivityResultItem;
Andrii Kulian9956d892018-02-14 13:48:56 -0800112import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700113import android.app.servertransaction.NewIntentItem;
114import android.app.servertransaction.WindowVisibilityItem;
115import android.app.servertransaction.DestroyActivityItem;
116import android.app.servertransaction.PauseActivityItem;
117import android.app.servertransaction.ResumeActivityItem;
118import android.app.servertransaction.StopActivityItem;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800122import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700123import android.content.res.Configuration;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700124import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -0800125import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700126import android.os.Binder;
Craig Mautner329f4122013-11-07 09:10:42 -0800127import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128import android.os.Handler;
129import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +0900130import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700131import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132import android.os.RemoteException;
133import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -0800134import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700135import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700136import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700137import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700138import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800139import android.util.IntArray;
Jorim Jaggi023da532016-04-20 22:42:32 -0700140import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700141import android.util.Slog;
Andrii Kulian1e32e022016-09-16 15:29:34 -0700142import android.util.SparseArray;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700143import android.util.proto.ProtoOutputStream;
Craig Mautner59c00972012-07-30 12:10:24 -0700144import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700145
Andreas Gampea36dc622018-02-05 17:19:22 -0800146import com.android.internal.annotations.GuardedBy;
Bryce Lee840c5662017-04-13 10:02:51 -0700147import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700148import com.android.internal.app.IVoiceInteractor;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700149import com.android.internal.os.BatteryStatsImpl;
150import com.android.server.Watchdog;
151import com.android.server.am.ActivityManagerService.ItemMatcher;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700152import com.android.server.wm.ConfigurationContainer;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800153import com.android.server.wm.StackWindowController;
154import com.android.server.wm.StackWindowListener;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700155import com.android.server.wm.WindowManagerService;
156
Craig Mautnercae015f2013-02-08 14:31:27 -0800157import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800158import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159import java.lang.ref.WeakReference;
160import java.util.ArrayList;
161import java.util.Iterator;
162import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800163import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700164import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165
166/**
167 * State and management of a single stack of activities.
168 */
Winson Chung55893332017-02-17 17:13:10 -0800169class ActivityStack<T extends StackWindowController> extends ConfigurationContainer
170 implements StackWindowListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800171 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700172 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
173 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800174 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700175 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700176 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700177 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700178 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700179 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700180 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700181 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700182 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
183 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
184 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
185 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
186 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700187
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700188 // Ticks during which we check progress while waiting for an app to launch.
189 static final int LAUNCH_TICK = 500;
190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700191 // How long we wait until giving up on the last activity to pause. This
192 // is short because it directly impacts the responsiveness of starting the
193 // next activity.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700194 private static final int PAUSE_TIMEOUT = 500;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700195
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700196 // How long we wait for the activity to tell us it has stopped before
197 // giving up. This is a good amount of time because we really need this
Robert Carr3406d462018-03-15 16:19:07 -0700198 // from the application in order to get its saved state. Once the stop
199 // is complete we may start destroying client resources triggering
200 // crashes if the UI thread was hung. We put this timeout one second behind
201 // the ANR timeout so these situations will generate ANR instead of
202 // Surface lost or other errors.
203 private static final int STOP_TIMEOUT = 11 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700204
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700205 // How long we wait until giving up on an activity telling us it has
206 // finished destroying itself.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700207 private static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800208
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 // Set to false to disable the preview that is shown while a new activity
210 // is being started.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700211 private static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800212
Craig Mautner5eda9b32013-07-02 11:58:16 -0700213 // How long to wait for all background Activities to redraw following a call to
214 // convertToTranslucent().
Andrii Kulian21713ac2016-10-12 22:05:05 -0700215 private static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700216
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800217 // How many activities have to be scheduled to stop to force a stop pass.
218 private static final int MAX_STOPPING_TO_FORCE = 3;
219
Andrii Kulian1779e612016-10-12 21:58:25 -0700220 @Override
221 protected int getChildCount() {
222 return mTaskHistory.size();
223 }
224
225 @Override
226 protected ConfigurationContainer getChildAt(int index) {
227 return mTaskHistory.get(index);
228 }
229
230 @Override
231 protected ConfigurationContainer getParent() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700232 return getDisplay();
Andrii Kulian1779e612016-10-12 21:58:25 -0700233 }
234
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800235 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700236 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800237 super.onParentChanged();
238 mStackSupervisor.updateUIDsPresentOnDisplay();
239 }
240
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700241 enum ActivityState {
242 INITIALIZING,
243 RESUMED,
244 PAUSING,
245 PAUSED,
246 STOPPING,
247 STOPPED,
248 FINISHING,
249 DESTROYING,
250 DESTROYED
251 }
252
Bryce Lee840c5662017-04-13 10:02:51 -0700253 @VisibleForTesting
Wale Ogunwale06579d62016-04-30 15:29:06 -0700254 /* The various modes for the method {@link #removeTask}. */
255 // Task is being completely removed from all stacks in the system.
Bryce Lee840c5662017-04-13 10:02:51 -0700256 protected static final int REMOVE_TASK_MODE_DESTROYING = 0;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700257 // Task is being removed from this stack so we can add it to another stack. In the case we are
258 // moving we don't want to perform some operations on the task like removing it from window
259 // manager or recents.
260 static final int REMOVE_TASK_MODE_MOVING = 1;
261 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
262 // stack and the new stack will be on top of all stacks.
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700263 static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700264
Andrii Kulian1e32e022016-09-16 15:29:34 -0700265 // The height/width divide used when fitting a task within a bounds with method
266 // {@link #fitWithinBounds}.
267 // We always want the task to to be visible in the bounds without affecting its size when
268 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
269 // the input bounds right or bottom side minus the width or height divided by this value.
270 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700272 final ActivityManagerService mService;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700273 private final WindowManagerService mWindowManager;
Winson Chung55893332017-02-17 17:13:10 -0800274 T mWindowContainerController;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800275
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276 /**
277 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800278 * running) activities. It contains #TaskRecord objects.
279 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800280 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800281
282 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700283 * List of running activities, sorted by recent usage.
284 * The first entry in the list is the least recently used.
285 * It contains HistoryRecord objects.
286 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800287 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288
289 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700290 * When we are in the process of pausing an activity, before starting the
291 * next one, this variable holds the activity that is currently being paused.
292 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800293 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700294
295 /**
296 * This is the last activity that we put into the paused state. This is
297 * used to determine if we need to do an activity transition while sleeping,
298 * when we normally hold the top activity paused.
299 */
300 ActivityRecord mLastPausedActivity = null;
301
302 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700303 * Activities that specify No History must be removed once the user navigates away from them.
304 * If the device goes to sleep with such an activity in the paused state then we save it here
305 * and finish it later if another activity replaces it on wakeup.
306 */
307 ActivityRecord mLastNoHistoryActivity = null;
308
309 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700310 * Current activity that is resumed, or null if there is none.
311 */
312 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800313
Craig Mautner5eda9b32013-07-02 11:58:16 -0700314 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
315 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
316 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
317 // Activity in mTranslucentActivityWaiting is notified via
318 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
319 // background activity being drawn then the same call will be made with a true value.
320 ActivityRecord mTranslucentActivityWaiting = null;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700321 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700322
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700323 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700324 * Set when we know we are going to be calling updateConfiguration()
325 * soon, so want to skip intermediate config checks.
326 */
327 boolean mConfigWillChange;
328
Winson Chung47900652017-04-06 18:44:25 -0700329 /**
330 * When set, will force the stack to report as invisible.
331 */
332 boolean mForceHidden = false;
333
Andrii Kulian21713ac2016-10-12 22:05:05 -0700334 private boolean mUpdateBoundsDeferred;
335 private boolean mUpdateBoundsDeferredCalled;
336 private final Rect mDeferredBounds = new Rect();
337 private final Rect mDeferredTaskBounds = new Rect();
338 private final Rect mDeferredTaskInsetBounds = new Rect();
Jorim Jaggi192086e2016-03-11 17:17:03 +0100339
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700340 long mLaunchStartTime = 0;
341 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800342
Craig Mautner858d8a62013-04-23 17:08:34 -0700343 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700344
Craig Mautnerc00204b2013-03-05 15:02:14 -0800345 final int mStackId;
Craig Mautnere0a38842013-12-16 16:14:02 -0800346 /** The attached Display's unique identifier, or -1 if detached */
347 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800348
Andrii Kulian1e32e022016-09-16 15:29:34 -0700349 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
350 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800351 private final Rect mTmpRect2 = new Rect();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800352 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Andrii Kulian1e32e022016-09-16 15:29:34 -0700353
Bryce Leefbd263b42018-03-07 10:33:55 -0800354 /** List for processing through a set of activities */
355 private final ArrayList<ActivityRecord> mTmpActivities = new ArrayList<>();
356
Craig Mautner27084302013-03-25 08:05:25 -0700357 /** Run all ActivityStacks through this */
Winson Chung5af42fc2017-03-24 17:11:33 -0700358 protected final ActivityStackSupervisor mStackSupervisor;
Craig Mautner27084302013-03-25 08:05:25 -0700359
Jorim Jaggife762342016-10-13 14:33:27 +0200360 private boolean mTopActivityOccludesKeyguard;
361 private ActivityRecord mTopDismissingKeyguardActivity;
362
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700363 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700364 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
365 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
366 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
367 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700368 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700369
Andrii Kulian21713ac2016-10-12 22:05:05 -0700370 private static class ScheduleDestroyArgs {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700371 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700372 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700373 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700374 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700375 mReason = reason;
376 }
377 }
378
Zoran Marcetaf958b322012-08-09 20:27:12 +0900379 final Handler mHandler;
380
Andrii Kulian21713ac2016-10-12 22:05:05 -0700381 private class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800382
Craig Mautnerc8143c62013-09-03 12:15:57 -0700383 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900384 super(looper);
385 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700386
Zoran Marcetaf958b322012-08-09 20:27:12 +0900387 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700388 public void handleMessage(Message msg) {
389 switch (msg.what) {
390 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800391 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700392 // We don't at this point know if the activity is fullscreen,
393 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800394 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700395 synchronized (mService) {
396 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700397 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700398 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700399 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700402 case LAUNCH_TICK_MSG: {
403 ActivityRecord r = (ActivityRecord)msg.obj;
404 synchronized (mService) {
405 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700406 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700407 }
408 }
409 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700410 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800411 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700412 // We don't at this point know if the activity is fullscreen,
413 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800414 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800415 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800416 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800417 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700418 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700419 case STOP_TIMEOUT_MSG: {
420 ActivityRecord r = (ActivityRecord)msg.obj;
421 // We don't at this point know if the activity is fullscreen,
422 // so we need to be conservative and assume it isn't.
423 Slog.w(TAG, "Activity stop timeout for " + r);
424 synchronized (mService) {
425 if (r.isInHistory()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -0700426 r.activityStoppedLocked(null /* icicle */,
427 null /* persistentState */, null /* description */);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700428 }
429 }
430 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700431 case DESTROY_ACTIVITIES_MSG: {
432 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
433 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700434 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700435 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700436 } break;
437 case TRANSLUCENT_TIMEOUT_MSG: {
438 synchronized (mService) {
439 notifyActivityDrawnLocked(null);
440 }
441 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700442 }
443 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800444 }
445
Craig Mautner34b73df2014-01-12 21:11:08 -0800446 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800447 int count = 0;
448 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
449 count += mTaskHistory.get(taskNdx).mActivities.size();
450 }
451 return count;
452 }
453
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700454 ActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700455 int windowingMode, int activityType, boolean onTop) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700456 mStackSupervisor = supervisor;
457 mService = supervisor.mService;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800458 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
459 mWindowManager = mService.mWindowManager;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700460 mStackId = stackId;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -0700461 mCurrentUser = mService.mUserController.getCurrentUserId();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800462 mTmpRect2.setEmpty();
Andrii Kulian2fcc4512018-01-25 16:39:27 -0800463 // Set display id before setting activity and window type to make sure it won't affect
464 // stacks on a wrong display.
465 mDisplayId = display.mDisplayId;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700466 setActivityType(activityType);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800467 setWindowingMode(windowingMode);
Winson Chung55893332017-02-17 17:13:10 -0800468 mWindowContainerController = createStackWindowController(display.mDisplayId, onTop,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700469 mTmpRect2);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800470 postAddToDisplay(display, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
471 }
472
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700473 T createStackWindowController(int displayId, boolean onTop, Rect outBounds) {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700474 return (T) new StackWindowController(mStackId, this, displayId, onTop, outBounds,
475 mStackSupervisor.mWindowManager);
Winson Chung55893332017-02-17 17:13:10 -0800476 }
477
478 T getWindowContainerController() {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800479 return mWindowContainerController;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700480 }
481
Bryce Leec4ab62a2018-03-05 14:19:26 -0800482 /**
483 * This should be called when an activity in a child task changes state. This should only
484 * be called from
485 * {@link TaskRecord#onActivityStateChanged(ActivityRecord, ActivityState, String)}.
486 * @param record The {@link ActivityRecord} whose state has changed.
487 * @param state The new state.
488 * @param reason The reason for the change.
489 */
490 void onActivityStateChanged(ActivityRecord record, ActivityState state, String reason) {
491 if (record == mResumedActivity && state != RESUMED) {
Bryce Lee84730a02018-04-03 14:10:04 -0700492 setResumedActivity(null, reason + " - onActivityStateChanged");
Bryce Leec4ab62a2018-03-05 14:19:26 -0800493 }
494
495 if (state == RESUMED) {
496 if (DEBUG_STACK) Slog.v(TAG_STACK, "set resumed activity to:" + record + " reason:"
497 + reason);
Bryce Lee84730a02018-04-03 14:10:04 -0700498 setResumedActivity(record, reason + " - onActivityStateChanged");
Bryce Leec4ab62a2018-03-05 14:19:26 -0800499 mService.setResumedActivityUncheckLocked(record, reason);
500 mStackSupervisor.mRecentTasks.add(record.getTask());
501 }
502 }
503
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700504 @Override
505 public void onConfigurationChanged(Configuration newParentConfig) {
506 final int prevWindowingMode = getWindowingMode();
507 super.onConfigurationChanged(newParentConfig);
508 final ActivityDisplay display = getDisplay();
509 if (display != null && prevWindowingMode != getWindowingMode()) {
510 display.onStackWindowingModeChanged(this);
511 }
512 }
513
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700514 @Override
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800515 public void setWindowingMode(int windowingMode) {
Winson Chung3e36f822018-01-16 12:06:04 -0800516 setWindowingMode(windowingMode, false /* animate */, false /* showRecents */,
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800517 false /* enteringSplitScreenMode */);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800518 }
519
Wale Ogunwaledf262f52017-12-07 18:17:12 -0800520 void setWindowingMode(int preferredWindowingMode, boolean animate, boolean showRecents,
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800521 boolean enteringSplitScreenMode) {
522 final boolean creating = mWindowContainerController == null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800523 final int currentMode = getWindowingMode();
524 final ActivityDisplay display = getDisplay();
525 final TaskRecord topTask = topTask();
526 final ActivityStack splitScreenStack = display.getSplitScreenPrimaryStack();
527 mTmpOptions.setLaunchWindowingMode(preferredWindowingMode);
528
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800529 // Need to make sure windowing mode is supported. If we in the process of creating the stack
530 // no need to resolve the windowing mode again as it is already resolved to the right mode.
531 int windowingMode = creating
532 ? preferredWindowingMode
533 : display.resolveWindowingMode(
534 null /* ActivityRecord */, mTmpOptions, topTask, getActivityType());
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800535 if (splitScreenStack == this && windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
536 // Resolution to split-screen secondary for the primary split-screen stack means we want
537 // to go fullscreen.
538 windowingMode = WINDOWING_MODE_FULLSCREEN;
539 }
540
Wale Ogunwaleac36e4d2017-11-29 13:30:26 -0800541 final boolean alreadyInSplitScreenMode = display.hasSplitScreenPrimaryStack();
542
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800543 // Don't send non-resizeable notifications if the windowing mode changed was a side effect
544 // of us entering split-screen mode.
545 final boolean sendNonResizeableNotification = !enteringSplitScreenMode;
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800546 // Take any required action due to us not supporting the preferred windowing mode.
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800547 if (alreadyInSplitScreenMode && windowingMode == WINDOWING_MODE_FULLSCREEN
548 && sendNonResizeableNotification && isActivityTypeStandardOrUndefined()) {
549 final boolean preferredSplitScreen =
550 preferredWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
551 || preferredWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
552 if (preferredSplitScreen || creating) {
553 // Looks like we can't launch in split screen mode or the stack we are launching
554 // doesn't support split-screen mode, go ahead an dismiss split-screen and display a
555 // warning toast about it.
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800556 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
557 display.getSplitScreenPrimaryStack().setWindowingMode(WINDOWING_MODE_FULLSCREEN);
558 }
559 }
560
561 if (currentMode == windowingMode) {
562 // You are already in the window mode silly...
563 return;
564 }
565
566 final WindowManagerService wm = mService.mWindowManager;
567 final ActivityRecord topActivity = getTopActivity();
568
Wale Ogunwaledf262f52017-12-07 18:17:12 -0800569 if (sendNonResizeableNotification && windowingMode != WINDOWING_MODE_FULLSCREEN
570 && topActivity != null && topActivity.isNonResizableOrForcedResizable()
571 && !topActivity.noDisplay) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800572 // Inform the user that they are starting an app that may not work correctly in
573 // multi-window mode.
574 final String packageName = topActivity.appInfo.packageName;
575 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
576 topTask.taskId, FORCED_RESIZEABLE_REASON_SPLIT_SCREEN, packageName);
577 }
578
579 wm.deferSurfaceLayout();
580 try {
581 if (!animate && topActivity != null) {
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100582 mStackSupervisor.mNoAnimActivities.add(topActivity);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800583 }
584 super.setWindowingMode(windowingMode);
585
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800586 if (creating) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800587 // Nothing else to do if we don't have a window container yet. E.g. call from ctor.
588 return;
589 }
590
591 if (windowingMode == WINDOWING_MODE_PINNED || currentMode == WINDOWING_MODE_PINNED) {
592 // TODO: Need to remove use of PinnedActivityStack for this to be supported.
593 // NOTE: Need to ASS.scheduleUpdatePictureInPictureModeIfNeeded() in
594 // setWindowModeUnchecked() when this support is added. See TaskRecord.reparent()
595 throw new IllegalArgumentException(
596 "Changing pinned windowing mode not currently supported");
597 }
598
599 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && splitScreenStack != null) {
600 // We already have a split-screen stack in this display, so just move the tasks over.
601 // TODO: Figure-out how to do all the stuff in
602 // AMS.setTaskWindowingModeSplitScreenPrimary
603 throw new IllegalArgumentException("Setting primary split-screen windowing mode"
604 + " while there is already one isn't currently supported");
605 //return;
606 }
607
608 mTmpRect2.setEmpty();
609 if (windowingMode != WINDOWING_MODE_FULLSCREEN) {
610 mWindowContainerController.getRawBounds(mTmpRect2);
611 if (windowingMode == WINDOWING_MODE_FREEFORM) {
612 if (topTask != null) {
613 // TODO: Can we consolidate this and other sites that call this methods?
614 Rect bounds = topTask().getLaunchBounds();
615 if (bounds != null) {
616 mTmpRect2.set(bounds);
617 }
618 }
619 }
620 }
621
Bryce Leef3c6a472017-11-14 14:53:06 -0800622 if (!Objects.equals(getOverrideBounds(), mTmpRect2)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800623 resize(mTmpRect2, null /* tempTaskBounds */, null /* tempTaskInsetBounds */);
624 }
625 } finally {
Wale Ogunwalec59b4f62017-11-30 11:05:43 -0800626 if (showRecents && !alreadyInSplitScreenMode && mDisplayId == DEFAULT_DISPLAY
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800627 && windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
628 // Make sure recents stack exist when creating a dock stack as it normally needs to
629 // be on the other side of the docked stack and we make visibility decisions based
630 // on that.
631 // TODO: This is only here to help out with the case where recents stack doesn't
632 // exist yet. For that case the initial size of the split-screen stack will be the
633 // the one where the home stack is visible since recents isn't visible yet, but the
634 // divider will be off. I think we should just make the initial bounds that of home
635 // so that the divider matches and remove this logic.
Winson Chungc1674272018-02-21 10:15:17 -0800636 // TODO: This is currently only called when entering split-screen while in another
637 // task, and from the tests
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800638 final ActivityStack recentStack = display.getOrCreateStack(
639 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, ACTIVITY_TYPE_RECENTS,
640 true /* onTop */);
641 recentStack.moveToFront("setWindowingMode");
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800642 // If task moved to docked stack - show recents if needed.
Winson Chungdff7a732017-12-11 12:17:06 -0800643 mService.mWindowManager.showRecentApps();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800644 }
645 wm.continueSurfaceLayout();
646 }
647
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800648 // Don't ensure visible activities if the windowing mode change was a side effect of us
649 // entering split-screen mode.
650 if (!enteringSplitScreenMode) {
651 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
652 mStackSupervisor.resumeFocusedStackTopActivityLocked();
653 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800654 }
655
656 @Override
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700657 public boolean isCompatible(int windowingMode, int activityType) {
658 // TODO: Should we just move this to ConfigurationContainer?
659 if (activityType == ACTIVITY_TYPE_UNDEFINED) {
660 // Undefined activity types end up in a standard stack once the stack is created on a
661 // display, so they should be considered compatible.
662 activityType = ACTIVITY_TYPE_STANDARD;
663 }
664 final ActivityDisplay display = getDisplay();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700665 if (display != null && activityType == ACTIVITY_TYPE_STANDARD
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700666 && windowingMode == WINDOWING_MODE_UNDEFINED) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700667 // Standard activity types will mostly take on the windowing mode of the display if one
668 // isn't specified, so look-up a compatible stack based on the display's windowing mode.
669 windowingMode = display.getWindowingMode();
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700670 }
671 return super.isCompatible(windowingMode, activityType);
672 }
673
Andrii Kulian839def92016-11-02 10:58:58 -0700674 /** Adds the stack to specified display and calls WindowManager to do the same. */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700675 void reparent(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700676 // TODO: We should probably resolve the windowing mode for the stack on the new display here
677 // 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 -0800678 removeFromDisplay();
Winson Chungf34c6022018-01-25 09:44:30 -0800679 // Reparent the window container before we try to update the position when adding it to
680 // the new display below
Wale Ogunwale1666e312016-12-16 11:27:18 -0800681 mTmpRect2.setEmpty();
Winson Chungf34c6022018-01-25 09:44:30 -0800682 mWindowContainerController.reparent(activityDisplay.mDisplayId, mTmpRect2, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800683 postAddToDisplay(activityDisplay, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
Wale Ogunwale66e16852017-10-19 13:35:52 -0700684 adjustFocusToNextFocusableStack("reparent", true /* allowFocusSelf */);
Andrii Kulian250d6532017-02-08 23:30:45 -0800685 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Andrii Kulian51c1b672017-04-07 18:39:32 -0700686 // Update visibility of activities before notifying WM. This way it won't try to resize
687 // windows that are no longer visible.
688 mStackSupervisor.ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
689 !PRESERVE_WINDOWS);
Andrii Kulian839def92016-11-02 10:58:58 -0700690 }
691
692 /**
693 * Updates internal state after adding to new display.
694 * @param activityDisplay New display to which this stack was attached.
695 * @param bounds Updated bounds.
696 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700697 private void postAddToDisplay(ActivityDisplay activityDisplay, Rect bounds, boolean onTop) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700698 mDisplayId = activityDisplay.mDisplayId;
Bryce Leef3c6a472017-11-14 14:53:06 -0800699 setBounds(bounds);
Andrii Kulian1779e612016-10-12 21:58:25 -0700700 onParentChanged();
Wale Ogunwale961f4852016-02-01 20:25:54 -0800701
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700702 activityDisplay.addChild(this, onTop ? POSITION_TOP : POSITION_BOTTOM);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700703 if (inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale961f4852016-02-01 20:25:54 -0800704 // If we created a docked stack we want to resize it so it resizes all other stacks
705 // in the system.
706 mStackSupervisor.resizeDockedStackLocked(
Bryce Leef3c6a472017-11-14 14:53:06 -0800707 getOverrideBounds(), null, null, null, null, PRESERVE_WINDOWS);
Wale Ogunwale961f4852016-02-01 20:25:54 -0800708 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700709 }
710
Andrii Kulian839def92016-11-02 10:58:58 -0700711 /**
Andrii Kulian839def92016-11-02 10:58:58 -0700712 * Updates the inner state of the stack to remove it from its current parent, so it can be
713 * either destroyed completely or re-parented.
714 */
715 private void removeFromDisplay() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700716 final ActivityDisplay display = getDisplay();
717 if (display != null) {
718 display.removeChild(this);
719 }
720 mDisplayId = INVALID_DISPLAY;
Andrii Kulian839def92016-11-02 10:58:58 -0700721 }
722
723 /** Removes the stack completely. Also calls WindowManager to do the same on its side. */
724 void remove() {
725 removeFromDisplay();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800726 mWindowContainerController.removeContainer();
727 mWindowContainerController = null;
Andrii Kulian6d6fb402016-10-26 16:15:25 -0700728 onParentChanged();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700729 }
730
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700731 ActivityDisplay getDisplay() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700732 return mStackSupervisor.getActivityDisplay(mDisplayId);
733 }
734
Matthew Ngaa2b6202017-02-10 14:48:21 -0800735 /**
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700736 * @see #getStackDockedModeBounds(Rect, Rect, Rect, boolean)
Matthew Ngaa2b6202017-02-10 14:48:21 -0800737 */
738 void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
739 Rect outTempTaskBounds, boolean ignoreVisibility) {
740 mWindowContainerController.getStackDockedModeBounds(currentTempTaskBounds,
741 outStackBounds, outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800742 }
743
744 void prepareFreezingTaskBounds() {
745 mWindowContainerController.prepareFreezingTaskBounds();
746 }
747
Wale Ogunwale1666e312016-12-16 11:27:18 -0800748 void getWindowContainerBounds(Rect outBounds) {
749 if (mWindowContainerController != null) {
750 mWindowContainerController.getBounds(outBounds);
Winson Chungb00dc5e2017-01-25 09:44:25 -0800751 return;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800752 }
753 outBounds.setEmpty();
754 }
755
Matthew Ngaa2b6202017-02-10 14:48:21 -0800756 void getBoundsForNewConfiguration(Rect outBounds) {
757 mWindowContainerController.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800758 }
759
760 void positionChildWindowContainerAtTop(TaskRecord child) {
761 mWindowContainerController.positionChildAtTop(child.getWindowContainerController(),
762 true /* includingParents */);
763 }
764
Jorim Jaggi192086e2016-03-11 17:17:03 +0100765 /**
Winson Chung8bca9e42017-04-16 15:59:43 -0700766 * Returns whether to defer the scheduling of the multi-window mode.
767 */
768 boolean deferScheduleMultiWindowModeChanged() {
769 return false;
770 }
771
772 /**
Jorim Jaggi192086e2016-03-11 17:17:03 +0100773 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
774 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
775 */
776 void deferUpdateBounds() {
777 if (!mUpdateBoundsDeferred) {
778 mUpdateBoundsDeferred = true;
779 mUpdateBoundsDeferredCalled = false;
780 }
781 }
782
783 /**
784 * Continues updating bounds after updates have been deferred. If there was a resize attempt
785 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
786 * be resized to that bounds.
787 */
788 void continueUpdateBounds() {
789 final boolean wasDeferred = mUpdateBoundsDeferred;
790 mUpdateBoundsDeferred = false;
791 if (wasDeferred && mUpdateBoundsDeferredCalled) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800792 resize(mDeferredBounds.isEmpty() ? null : mDeferredBounds,
Jorim Jaggi192086e2016-03-11 17:17:03 +0100793 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
794 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
795 }
796 }
797
798 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
799 Rect tempTaskInsetBounds) {
800 if (!mUpdateBoundsDeferred) {
801 return true;
802 }
803 if (bounds != null) {
804 mDeferredBounds.set(bounds);
805 } else {
806 mDeferredBounds.setEmpty();
807 }
808 if (tempTaskBounds != null) {
809 mDeferredTaskBounds.set(tempTaskBounds);
810 } else {
811 mDeferredTaskBounds.setEmpty();
812 }
813 if (tempTaskInsetBounds != null) {
814 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
815 } else {
816 mDeferredTaskInsetBounds.setEmpty();
817 }
818 mUpdateBoundsDeferredCalled = true;
819 return false;
820 }
821
Bryce Leef3c6a472017-11-14 14:53:06 -0800822 @Override
823 public int setBounds(Rect bounds) {
824 return super.setBounds(!inMultiWindowMode() ? null : bounds);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700825 }
Craig Mautner5962b122012-10-05 14:45:52 -0700826
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700827 ActivityRecord topRunningActivityLocked() {
Winson Chung3f103eb2017-04-12 21:53:59 -0700828 return topRunningActivityLocked(false /* focusableOnly */);
829 }
830
Jorim Jaggiea039a82017-08-02 14:37:49 +0200831 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
832 outActivities.clear();
833 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
834 mTaskHistory.get(taskNdx).getAllRunningVisibleActivitiesLocked(outActivities);
835 }
836 }
837
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700838 private ActivityRecord topRunningActivityLocked(boolean focusableOnly) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800839 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700840 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Winson Chung3f103eb2017-04-12 21:53:59 -0700841 if (r != null && (!focusableOnly || r.isFocusable())) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700842 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800843 }
844 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845 return null;
846 }
847
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700848 ActivityRecord topRunningNonOverlayTaskActivity() {
849 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
850 final TaskRecord task = mTaskHistory.get(taskNdx);
851 final ArrayList<ActivityRecord> activities = task.mActivities;
852 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
853 final ActivityRecord r = activities.get(activityNdx);
854 if (!r.finishing && !r.mTaskOverlay) {
855 return r;
856 }
857 }
858 }
859 return null;
860 }
861
862 ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800863 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
864 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800865 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800866 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
867 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -0700868 if (!r.finishing && !r.delayedResume && r != notTop && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800869 return r;
870 }
871 }
872 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700873 return null;
874 }
875
876 /**
877 * This is a simplified version of topRunningActivityLocked that provides a number of
878 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800879 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 * @param token If non-null, any history records matching this token will be skipped.
881 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800882 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883 * @return Returns the HistoryRecord of the next activity on the stack.
884 */
885 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800886 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
887 TaskRecord task = mTaskHistory.get(taskNdx);
888 if (task.taskId == taskId) {
889 continue;
890 }
891 ArrayList<ActivityRecord> activities = task.mActivities;
892 for (int i = activities.size() - 1; i >= 0; --i) {
893 final ActivityRecord r = activities.get(i);
894 // Note: the taskId check depends on real taskId fields being non-zero
Chong Zhang87761972016-08-22 13:53:24 -0700895 if (!r.finishing && (token != r.appToken) && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800896 return r;
897 }
898 }
899 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 return null;
901 }
902
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800903 ActivityRecord getTopActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700904 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800905 final ActivityRecord r = mTaskHistory.get(taskNdx).getTopActivity();
906 if (r != null) {
907 return r;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700908 }
909 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700910 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700911 }
912
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700913 final TaskRecord topTask() {
914 final int size = mTaskHistory.size();
915 if (size > 0) {
916 return mTaskHistory.get(size - 1);
917 }
918 return null;
919 }
920
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800921 private TaskRecord bottomTask() {
Winson Chung1cebea62017-06-26 17:22:27 -0700922 if (mTaskHistory.isEmpty()) {
923 return null;
924 }
925 return mTaskHistory.get(0);
926 }
927
Craig Mautnerd2328952013-03-05 12:46:26 -0800928 TaskRecord taskForIdLocked(int id) {
929 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
930 final TaskRecord task = mTaskHistory.get(taskNdx);
931 if (task.taskId == id) {
932 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800933 }
934 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700935 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 }
937
Craig Mautnerd2328952013-03-05 12:46:26 -0800938 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700939 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800940 return isInStackLocked(r);
941 }
942
943 ActivityRecord isInStackLocked(ActivityRecord r) {
944 if (r == null) {
945 return null;
946 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700947 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700948 final ActivityStack stack = r.getStack();
949 if (stack != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
950 if (stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800951 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800952 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800953 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800954 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800955 }
956
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800957 boolean isInStackLocked(TaskRecord task) {
958 return mTaskHistory.contains(task);
959 }
960
961 /** Checks if there are tasks with specific UID in the stack. */
962 boolean isUidPresent(int uid) {
963 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -0800964 for (ActivityRecord r : task.mActivities) {
965 if (r.getUid() == uid) {
966 return true;
967 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800968 }
969 }
970 return false;
971 }
972
973 /** Get all UIDs that are present in the stack. */
974 void getPresentUIDs(IntArray presentUIDs) {
975 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -0800976 for (ActivityRecord r : task.mActivities) {
977 presentUIDs.add(r.getUid());
978 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800979 }
980 }
981
Winson Chungabb433b2017-03-24 09:35:42 -0700982 final void removeActivitiesFromLRUListLocked(TaskRecord task) {
983 for (ActivityRecord r : task.mActivities) {
984 mLRUActivities.remove(r);
985 }
986 }
987
Craig Mautner2420ead2013-04-01 17:13:20 -0700988 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989 final boolean hadit = mLRUActivities.remove(r);
990 mLRUActivities.add(r);
991 return hadit;
992 }
993
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800994 final boolean isHomeOrRecentsStack() {
Wale Ogunwale68278562017-09-23 17:13:55 -0700995 return isActivityTypeHome() || isActivityTypeRecents();
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800996 }
997
Craig Mautnere0a38842013-12-16 16:14:02 -0800998 final boolean isOnHomeDisplay() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700999 return mDisplayId == DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08001000 }
1001
Wale Ogunwale66e16852017-10-19 13:35:52 -07001002 private boolean returnsToHomeStack() {
1003 return !inMultiWindowMode()
1004 && !mTaskHistory.isEmpty()
1005 && mTaskHistory.get(0).returnsToHomeStack();
1006 }
1007
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001008 void moveToFront(String reason) {
1009 moveToFront(reason, null);
1010 }
1011
1012 /**
1013 * @param reason The reason for moving the stack to the front.
1014 * @param task If non-null, the task will be moved to the top of the stack.
1015 * */
1016 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001017 if (!isAttached()) {
1018 return;
1019 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07001020
Wale Ogunwalebb285872018-03-01 13:05:30 -08001021 final ActivityDisplay display = getDisplay();
1022
1023 if (inSplitScreenSecondaryWindowingMode()) {
1024 // If the stack is in split-screen seconardy mode, we need to make sure we move the
1025 // primary split-screen stack forward in the case it is currently behind a fullscreen
1026 // stack so both halves of the split-screen appear on-top and the fullscreen stack isn't
1027 // cutting between them.
1028 // TODO(b/70677280): This is a workaround until we can fix as part of b/70677280.
1029 final ActivityStack topFullScreenStack =
1030 display.getTopStackInWindowingMode(WINDOWING_MODE_FULLSCREEN);
1031 if (topFullScreenStack != null) {
1032 final ActivityStack primarySplitScreenStack = display.getSplitScreenPrimaryStack();
1033 if (display.getIndexOf(topFullScreenStack)
1034 > display.getIndexOf(primarySplitScreenStack)) {
1035 primarySplitScreenStack.moveToFront(reason + " splitScreenToTop");
1036 }
1037 }
1038 }
1039
Wale Ogunwale66e16852017-10-19 13:35:52 -07001040 if (!isActivityTypeHome() && returnsToHomeStack()) {
1041 // Make sure the home stack is behind this stack since that is where we should return to
1042 // when this stack is no longer visible.
1043 mStackSupervisor.moveHomeStackToFront(reason + " returnToHome");
1044 }
1045
Wale Ogunwalebb285872018-03-01 13:05:30 -08001046 display.positionChildAtTop(this);
Andrii Kulian839def92016-11-02 10:58:58 -07001047 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001048 if (task != null) {
1049 insertTaskAtTop(task, null);
Wale Ogunwaleaa47c122016-09-23 16:39:53 -07001050 return;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001051 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001052 }
1053
Evan Rosky9c448172017-11-02 14:19:27 -07001054 /**
1055 * @param reason The reason for moving the stack to the back.
1056 * @param task If non-null, the task will be moved to the bottom of the stack.
1057 **/
1058 void moveToBack(String reason, TaskRecord task) {
1059 if (!isAttached()) {
1060 return;
1061 }
1062
Bryce Lee8cab4a02018-01-05 09:00:49 -08001063 /**
1064 * The intent behind moving a primary split screen stack to the back is usually to hide
1065 * behind the home stack. Exit split screen in this case.
1066 */
1067 if (getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
1068 setWindowingMode(WINDOWING_MODE_FULLSCREEN);
1069 }
1070
Evan Rosky9c448172017-11-02 14:19:27 -07001071 getDisplay().positionChildAtBottom(this);
1072 mStackSupervisor.setFocusStackUnchecked(reason, getDisplay().getTopStack());
1073 if (task != null) {
1074 insertTaskAtBottom(task);
1075 return;
Evan Rosky9c448172017-11-02 14:19:27 -07001076 }
1077 }
1078
Wale Ogunwaled046a012015-12-24 13:05:59 -08001079 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001080 final ActivityRecord r = topRunningActivityLocked();
Bryce Lee7b851cc2018-04-10 14:53:13 -07001081 return mStackSupervisor.isFocusable(this, r != null && r.isFocusable());
Wale Ogunwaled046a012015-12-24 13:05:59 -08001082 }
1083
Craig Mautnere0a38842013-12-16 16:14:02 -08001084 final boolean isAttached() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001085 return getParent() != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001086 }
1087
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001088 /**
Wale Ogunwale39381972015-12-17 17:15:29 -08001089 * Returns the top activity in any existing task matching the given Intent in the input result.
1090 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001091 */
Wale Ogunwale39381972015-12-17 17:15:29 -08001092 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001093 Intent intent = target.intent;
1094 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001095 ComponentName cls = intent.getComponent();
1096 if (info.targetActivity != null) {
1097 cls = new ComponentName(info.packageName, info.targetActivity);
1098 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001099 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -07001100 boolean isDocument = intent != null & intent.isDocument();
1101 // If documentData is non-null then it must match the existing task data.
1102 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -08001103
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001104 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -08001105 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1106 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001107 if (task.voiceSession != null) {
1108 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001109 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -07001110 continue;
1111 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001112 if (task.userId != userId) {
1113 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001114 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -07001115 continue;
1116 }
Bryce Lee9f6affd2017-09-01 09:18:35 -07001117
1118 // Overlays should not be considered as the task's logical top activity.
1119 final ActivityRecord r = task.getTopActivity(false /* includeOverlays */);
Craig Mautner000f0022013-02-26 15:04:29 -08001120 if (r == null || r.finishing || r.userId != userId ||
1121 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001122 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -08001123 continue;
1124 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001125 if (!r.hasCompatibleActivityType(target)) {
Chong Zhangb546f7e2015-08-05 14:21:36 -07001126 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
1127 continue;
1128 }
Craig Mautner000f0022013-02-26 15:04:29 -08001129
Craig Mautnerd00f4742014-03-12 14:17:26 -07001130 final Intent taskIntent = task.intent;
1131 final Intent affinityIntent = task.affinityIntent;
1132 final boolean taskIsDocument;
1133 final Uri taskDocumentData;
1134 if (taskIntent != null && taskIntent.isDocument()) {
1135 taskIsDocument = true;
1136 taskDocumentData = taskIntent.getData();
1137 } else if (affinityIntent != null && affinityIntent.isDocument()) {
1138 taskIsDocument = true;
1139 taskDocumentData = affinityIntent.getData();
1140 } else {
1141 taskIsDocument = false;
1142 taskDocumentData = null;
1143 }
1144
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001145 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -07001146 + taskIntent.getComponent().flattenToShortString()
Bryce Leeaf691c02017-03-20 14:20:22 -07001147 + "/aff=" + r.getTask().rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001148 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001149 // TODO Refactor to remove duplications. Check if logic can be simplified.
1150 if (taskIntent != null && taskIntent.getComponent() != null &&
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001151 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001152 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001153 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001154 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001155 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1156 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -08001157 result.r = r;
1158 result.matchedByRootAffinity = false;
1159 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001160 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
1161 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001162 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001163 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001164 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001165 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1166 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -08001167 result.r = r;
1168 result.matchedByRootAffinity = false;
1169 break;
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001170 } else if (!isDocument && !taskIsDocument
Winson Chung5b895b72017-05-01 13:46:25 -07001171 && result.r == null && task.rootAffinity != null) {
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001172 if (task.rootAffinity.equals(target.taskAffinity)) {
1173 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
1174 // It is possible for multiple tasks to have the same root affinity especially
1175 // if they are in separate stacks. We save off this candidate, but keep looking
1176 // to see if there is a better candidate.
1177 result.r = r;
1178 result.matchedByRootAffinity = true;
1179 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001180 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001181 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001182 }
1183
1184 /**
1185 * Returns the first activity (starting from the top of the stack) that
1186 * is the same as the given activity. Returns null if no such activity
1187 * is found.
1188 */
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001189 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
1190 boolean compareIntentFilters) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 ComponentName cls = intent.getComponent();
1192 if (info.targetActivity != null) {
1193 cls = new ComponentName(info.packageName, info.targetActivity);
1194 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001195 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001196
Craig Mautner000f0022013-02-26 15:04:29 -08001197 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001198 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001199 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001200
Craig Mautner000f0022013-02-26 15:04:29 -08001201 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1202 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001203 if (!r.okToShowLocked()) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -07001204 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001205 }
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001206 if (!r.finishing && r.userId == userId) {
1207 if (compareIntentFilters) {
1208 if (r.intent.filterEquals(intent)) {
1209 return r;
1210 }
1211 } else {
1212 if (r.intent.getComponent().equals(cls)) {
1213 return r;
1214 }
1215 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001216 }
1217 }
1218 }
1219
1220 return null;
1221 }
1222
Amith Yamasani742a6712011-05-04 14:49:28 -07001223 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -07001224 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -07001225 */
Craig Mautner93529a42013-10-04 15:03:13 -07001226 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001227 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -07001228 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001229 }
1230 mCurrentUser = userId;
1231
1232 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001233 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001234 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001235 final TaskRecord task = mTaskHistory.get(i);
1236
Chong Zhang87761972016-08-22 13:53:24 -07001237 if (task.okToShowLocked()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001238 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001239 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001240 mTaskHistory.remove(i);
1241 mTaskHistory.add(task);
1242 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001243 // Use same value for i.
1244 } else {
1245 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001246 }
1247 }
Amith Yamasani742a6712011-05-04 14:49:28 -07001248 }
1249
Craig Mautner2420ead2013-04-01 17:13:20 -07001250 void minimalResumeActivityLocked(ActivityRecord r) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001251 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
1252 + " callers=" + Debug.getCallers(5));
Bryce Leec4ab62a2018-03-05 14:19:26 -08001253 r.setState(RESUMED, "minimalResumeActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001254 r.completeResumeLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -07001255 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001256 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
1257 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001258 }
1259
Narayan Kamath7829c812015-06-08 17:39:43 +01001260 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001261 if (mFullyDrawnStartTime != 0) {
1262 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1263 }
Narayan Kamath7829c812015-06-08 17:39:43 +01001264 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -07001265 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1266 }
1267
1268 private void stopFullyDrawnTraceIfNeeded() {
1269 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
1270 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1271 mFullyDrawnStartTime = 0;
1272 }
1273 }
1274
Craig Mautnere79d42682013-04-01 19:01:53 -07001275 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001276 if (r.displayStartTime == 0) {
1277 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
1278 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001279 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001280 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001281 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001282 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001283 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001284 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001287
Andrii Kulian21713ac2016-10-12 22:05:05 -07001288 private void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -07001289 // Make sure that there is no activity waiting for this to launch.
1290 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
1291 r.displayStartTime = r.fullyDrawnStartTime = 0;
1292 } else {
1293 mStackSupervisor.removeTimeoutsForActivityLocked(r);
1294 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1295 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001296 }
1297
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001298 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001299 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001300 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1301 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1302 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1303 activities.get(activityNdx).setSleeping(false);
1304 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001305 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -08001306 if (mPausingActivity != null) {
1307 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
1308 activityPausedLocked(mPausingActivity.appToken, true);
1309 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001310 }
1311
Todd Kennedy39bfee52016-02-24 10:28:21 -08001312 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1313 final String packageName = aInfo.packageName;
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001314 final int userId = UserHandle.getUserId(aInfo.uid);
1315
Todd Kennedy39bfee52016-02-24 10:28:21 -08001316 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1317 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1318 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001319 final ActivityRecord ar = activities.get(activityNdx);
1320
1321 if ((userId == ar.userId) && packageName.equals(ar.packageName)) {
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -07001322 ar.updateApplicationInfo(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08001323 }
1324 }
1325 }
1326 }
1327
David Stevens9440dc82017-03-16 19:00:20 -07001328 void checkReadyForSleep() {
1329 if (shouldSleepActivities() && goToSleepIfPossible(false /* shuttingDown */)) {
1330 mStackSupervisor.checkReadyForSleepLocked(true /* allowDelay */);
1331 }
1332 }
1333
Craig Mautner0eea92c2013-05-16 13:35:39 -07001334 /**
David Stevens9440dc82017-03-16 19:00:20 -07001335 * Tries to put the activities in the stack to sleep.
1336 *
1337 * If the stack is not in a state where its activities can be put to sleep, this function will
1338 * start any necessary actions to move the stack into such a state. It is expected that this
1339 * function get called again when those actions complete.
1340 *
1341 * @param shuttingDown true when the called because the device is shutting down.
David Stevens18abd0e2017-08-17 14:55:47 -07001342 * @return true if the stack finished going to sleep, false if the stack only started the
1343 * process of going to sleep (checkReadyForSleep will be called when that process finishes).
Craig Mautner0eea92c2013-05-16 13:35:39 -07001344 */
David Stevens9440dc82017-03-16 19:00:20 -07001345 boolean goToSleepIfPossible(boolean shuttingDown) {
1346 boolean shouldSleep = true;
1347
Craig Mautner0eea92c2013-05-16 13:35:39 -07001348 if (mResumedActivity != null) {
1349 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001350 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
1351 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1352 "Sleep => pause with userLeaving=false");
Bryce Lee5daa3122017-04-19 10:40:42 -07001353
chaviwa1d68532018-03-16 15:33:43 -07001354 startPausingLocked(false, true, null, false);
David Stevens9440dc82017-03-16 19:00:20 -07001355 shouldSleep = false ;
1356 } else if (mPausingActivity != null) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001357 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001358 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
David Stevens9440dc82017-03-16 19:00:20 -07001359 shouldSleep = false;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001360 }
David Stevens9440dc82017-03-16 19:00:20 -07001361
1362 if (!shuttingDown) {
1363 if (containsActivityFromStack(mStackSupervisor.mStoppingActivities)) {
1364 // Still need to tell some activities to stop; can't sleep yet.
1365 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
1366 + mStackSupervisor.mStoppingActivities.size() + " activities");
1367
1368 mStackSupervisor.scheduleIdleLocked();
1369 shouldSleep = false;
1370 }
1371
1372 if (containsActivityFromStack(mStackSupervisor.mGoingToSleepActivities)) {
1373 // Still need to tell some activities to sleep; can't sleep yet.
1374 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
1375 + mStackSupervisor.mGoingToSleepActivities.size() + " activities");
1376 shouldSleep = false;
1377 }
1378 }
1379
1380 if (shouldSleep) {
1381 goToSleep();
1382 }
1383
David Stevens18abd0e2017-08-17 14:55:47 -07001384 return shouldSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -07001385 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001386
David Stevens18abd0e2017-08-17 14:55:47 -07001387 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001388 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001389
Andrii Kulianf9949d52016-05-06 12:53:25 -07001390 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001391 // This ensures that the activity's onStop() is called.
1392 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1393 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1394 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1395 final ActivityRecord r = activities.get(activityNdx);
Bryce Lee7ace3952018-02-16 14:34:32 -08001396 if (r.isState(STOPPING, STOPPED, PAUSED, PAUSING)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001397 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001398 }
1399 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 }
Craig Mautner59c00972012-07-30 12:10:24 -07001402
David Stevens9440dc82017-03-16 19:00:20 -07001403 private boolean containsActivityFromStack(List<ActivityRecord> rs) {
1404 for (ActivityRecord r : rs) {
1405 if (r.getStack() == this) {
1406 return true;
1407 }
1408 }
1409 return false;
1410 }
1411
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001412 /**
Winson Chung4dabf232017-01-25 13:25:22 -08001413 * Schedule a pause timeout in case the app doesn't respond. We don't give it much time because
1414 * this directly impacts the responsiveness seen by the user.
1415 */
1416 private void schedulePauseTimeout(ActivityRecord r) {
1417 final Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1418 msg.obj = r;
1419 r.pauseTime = SystemClock.uptimeMillis();
1420 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1421 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
1422 }
1423
1424 /**
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001425 * Start pausing the currently resumed activity. It is an error to call this if there
1426 * is already an activity being paused or there is no resumed activity.
1427 *
1428 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1429 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1430 * screen turning off).
Wale Ogunwale950faff2016-08-08 09:51:04 -07001431 * @param resuming The activity we are currently trying to resume or null if this is not being
1432 * called as part of resuming the top activity, so we shouldn't try to instigate
1433 * a resume here if not null.
Winson Chung6954fc92017-03-24 16:22:12 -07001434 * @param pauseImmediately True if the caller does not want to wait for the activity callback to
1435 * complete pausing.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001436 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1437 * it to tell us when it is done.
1438 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001439 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
Winson Chung6954fc92017-03-24 16:22:12 -07001440 ActivityRecord resuming, boolean pauseImmediately) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001441 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001442 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
Bryce Lee7ace3952018-02-16 14:34:32 -08001443 + " state=" + mPausingActivity.getState());
David Stevens9440dc82017-03-16 19:00:20 -07001444 if (!shouldSleepActivities()) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001445 // Avoid recursion among check for sleep and complete pause during sleeping.
1446 // Because activity will be paused immediately after resume, just let pause
1447 // be completed by the order of activity paused from clients.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001448 completePauseLocked(false, resuming);
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001449 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001450 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 ActivityRecord prev = mResumedActivity;
Bryce Lee5daa3122017-04-19 10:40:42 -07001452
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001453 if (prev == null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07001454 if (resuming == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001455 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001456 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001457 }
1458 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001460
Andrii Kulianb1888aa2018-02-16 14:40:42 -08001461 if (prev == resuming) {
1462 Slog.wtf(TAG, "Trying to pause activity that is in process of being resumed");
1463 return false;
1464 }
1465
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001466 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001467 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001468 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001470 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1471 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Bryce Lee7ace3952018-02-16 14:34:32 -08001472 prev.setState(PAUSING, "startPausingLocked");
Bryce Leeaf691c02017-03-20 14:20:22 -07001473 prev.getTask().touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001474 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001475 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001476
Dianne Hackborncee04b52013-07-03 17:01:28 -07001477 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478
1479 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001480
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001482 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 try {
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08001484 EventLogTags.writeAmPauseActivity(prev.userId, System.identityHashCode(prev),
1485 prev.shortComponentName, "userLeaving=" + userLeaving);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001486 mService.updateUsageStats(prev, false);
Andrii Kulian446e8242017-10-26 15:17:29 -07001487
Bryce Leeb0f993f2018-03-02 15:38:01 -08001488 mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001489 PauseActivityItem.obtain(prev.finishing, userLeaving,
Bryce Leef52974c2018-02-14 15:12:01 -08001490 prev.configChangeFlags, pauseImmediately).setDescription(
1491 prev.getLifecycleDescription("startPausingLocked")));
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
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001746 /**
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001747 * Returns true if the stack should be visible.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001748 *
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001749 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001750 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001751 boolean shouldBeVisible(ActivityRecord starting) {
Winson Chung47900652017-04-06 18:44:25 -07001752 if (!isAttached() || mForceHidden) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001753 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001754 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001755 if (mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001756 return true;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001757 }
1758
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001759 final ActivityRecord top = topRunningActivityLocked();
1760 if (top == null && isInStackLocked(starting) == null && !isTopStackOnDisplay()) {
1761 // Shouldn't be visible if you don't have any running activities, not starting one, and
1762 // not the top stack on display.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001763 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001764 }
1765
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001766 final ActivityDisplay display = getDisplay();
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001767 boolean gotSplitScreenStack = false;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001768 boolean gotOpaqueSplitScreenPrimary = false;
1769 boolean gotOpaqueSplitScreenSecondary = false;
Wale Ogunwale68278562017-09-23 17:13:55 -07001770 final int windowingMode = getWindowingMode();
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001771 final boolean isAssistantType = isActivityTypeAssistant();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001772 for (int i = display.getChildCount() - 1; i >= 0; --i) {
1773 final ActivityStack other = display.getChildAt(i);
1774 if (other == this) {
1775 // Should be visible if there is no other stack occluding it.
1776 return true;
1777 }
Wale Ogunwale68278562017-09-23 17:13:55 -07001778
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001779 final int otherWindowingMode = other.getWindowingMode();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001780
1781 if (otherWindowingMode == WINDOWING_MODE_FULLSCREEN) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07001782 if (other.isStackTranslucent(starting)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001783 // Can be visible behind a translucent fullscreen stack.
1784 continue;
1785 }
1786 return false;
1787 } else if (otherWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
1788 && !gotOpaqueSplitScreenPrimary) {
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001789 gotSplitScreenStack = true;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001790 gotOpaqueSplitScreenPrimary =
Wale Ogunwale66e16852017-10-19 13:35:52 -07001791 !other.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001792 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
1793 && gotOpaqueSplitScreenPrimary) {
1794 // Can not be visible behind another opaque stack in split-screen-primary mode.
1795 return false;
1796 }
1797 } else if (otherWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
1798 && !gotOpaqueSplitScreenSecondary) {
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001799 gotSplitScreenStack = true;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001800 gotOpaqueSplitScreenSecondary =
Wale Ogunwale66e16852017-10-19 13:35:52 -07001801 !other.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001802 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
1803 && gotOpaqueSplitScreenSecondary) {
1804 // Can not be visible behind another opaque stack in split-screen-secondary mode.
1805 return false;
1806 }
1807 }
1808 if (gotOpaqueSplitScreenPrimary && gotOpaqueSplitScreenSecondary) {
1809 // Can not be visible if we are in split-screen windowing mode and both halves of
1810 // the screen are opaque.
1811 return false;
1812 }
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001813 if (isAssistantType && gotSplitScreenStack) {
1814 // Assistant stack can't be visible behind split-screen. In addition to this not
1815 // making sense, it also works around an issue here we boost the z-order of the
1816 // assistant window surfaces in window manager whenever it is visible.
1817 return false;
1818 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001819 }
1820
1821 // Well, nothing is stopping you from being visible...
1822 return true;
Wale Ogunwale68278562017-09-23 17:13:55 -07001823 }
1824
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001825 final int rankTaskLayers(int baseLayer) {
1826 int layer = 0;
1827 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1828 final TaskRecord task = mTaskHistory.get(taskNdx);
1829 ActivityRecord r = task.topRunningActivityLocked();
1830 if (r == null || r.finishing || !r.visible) {
1831 task.mLayerRank = -1;
1832 } else {
1833 task.mLayerRank = baseLayer + layer++;
1834 }
1835 }
1836 return layer;
1837 }
1838
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 /**
1840 * Make sure that all activities that need to be visible (that is, they
1841 * currently can be seen by the user) actually are.
1842 */
Wale Ogunwale66e16852017-10-19 13:35:52 -07001843 // 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 -07001844 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1845 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02001846 mTopActivityOccludesKeyguard = false;
1847 mTopDismissingKeyguardActivity = null;
Bryce Lee2a3cc462017-10-27 10:57:35 -07001848 mStackSupervisor.getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02001849 try {
1850 ActivityRecord top = topRunningActivityLocked();
1851 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
1852 + " configChanges=0x" + Integer.toHexString(configChanges));
1853 if (top != null) {
1854 checkTranslucentActivityWaiting(top);
1855 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001856
Jorim Jaggife762342016-10-13 14:33:27 +02001857 // If the top activity is not fullscreen, then we need to
1858 // make sure any activities under it are now visible.
1859 boolean aboveTop = top != null;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001860 final boolean stackShouldBeVisible = shouldBeVisible(starting);
1861 boolean behindFullscreenActivity = !stackShouldBeVisible;
Jorim Jaggife762342016-10-13 14:33:27 +02001862 boolean resumeNextActivity = mStackSupervisor.isFocusedStack(this)
1863 && (isInStackLocked(starting) == null);
Wale Ogunwale86dde2b2018-01-18 06:15:45 -08001864 final boolean isTopNotPinnedStack =
1865 isAttached() && getDisplay().isTopNotPinnedStack(this);
Jorim Jaggife762342016-10-13 14:33:27 +02001866 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1867 final TaskRecord task = mTaskHistory.get(taskNdx);
1868 final ArrayList<ActivityRecord> activities = task.mActivities;
1869 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1870 final ActivityRecord r = activities.get(activityNdx);
1871 if (r.finishing) {
Jorim Jaggife762342016-10-13 14:33:27 +02001872 continue;
Chong Zhang22bc8512016-04-07 13:47:18 -07001873 }
Jorim Jaggife762342016-10-13 14:33:27 +02001874 final boolean isTop = r == top;
1875 if (aboveTop && !isTop) {
1876 continue;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001877 }
Jorim Jaggife762342016-10-13 14:33:27 +02001878 aboveTop = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001879
Jorim Jaggife762342016-10-13 14:33:27 +02001880 // Check whether activity should be visible without Keyguard influence
Jorim Jaggi241ae102016-11-02 21:57:33 -07001881 final boolean visibleIgnoringKeyguard = r.shouldBeVisibleIgnoringKeyguard(
Jorim Jaggi241ae102016-11-02 21:57:33 -07001882 behindFullscreenActivity);
1883 r.visibleIgnoringKeyguard = visibleIgnoringKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +02001884
1885 // Now check whether it's really visible depending on Keyguard state.
Jorim Jaggi241ae102016-11-02 21:57:33 -07001886 final boolean reallyVisible = checkKeyguardVisibility(r,
chaviw2c500982018-01-04 17:05:05 -08001887 visibleIgnoringKeyguard, isTop && isTopNotPinnedStack);
Jorim Jaggi241ae102016-11-02 21:57:33 -07001888 if (visibleIgnoringKeyguard) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001889 behindFullscreenActivity = updateBehindFullscreen(!stackShouldBeVisible,
Wale Ogunwale66e16852017-10-19 13:35:52 -07001890 behindFullscreenActivity, r);
Jorim Jaggife762342016-10-13 14:33:27 +02001891 }
1892 if (reallyVisible) {
1893 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
Bryce Lee7ace3952018-02-16 14:34:32 -08001894 + " finishing=" + r.finishing + " state=" + r.getState());
Jorim Jaggife762342016-10-13 14:33:27 +02001895 // First: if this is not the current activity being started, make
1896 // sure it matches the current configuration.
1897 if (r != starting) {
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08001898 // Ensure activity configuration ignoring stop state since we are
1899 // becoming visible.
1900 r.ensureActivityConfiguration(0 /* globalChanges */, preserveWindows,
1901 true /* ignoreStopState */);
Jorim Jaggife762342016-10-13 14:33:27 +02001902 }
1903
1904 if (r.app == null || r.app.thread == null) {
1905 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
1906 resumeNextActivity, r)) {
1907 if (activityNdx >= activities.size()) {
1908 // Record may be removed if its process needs to restart.
1909 activityNdx = activities.size() - 1;
1910 } else {
1911 resumeNextActivity = false;
1912 }
1913 }
1914 } else if (r.visible) {
1915 // If this activity is already visible, then there is nothing to do here.
1916 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1917 "Skipping: already visible at " + r);
1918
1919 if (r.handleAlreadyVisible()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001920 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001921 }
Jorim Jaggife762342016-10-13 14:33:27 +02001922 } else {
1923 r.makeVisibleIfNeeded(starting);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001924 }
Jorim Jaggife762342016-10-13 14:33:27 +02001925 // Aggregate current change flags.
1926 configChanges |= r.configChangeFlags;
Craig Mautnerbb742462014-07-07 15:28:55 -07001927 } else {
Jorim Jaggife762342016-10-13 14:33:27 +02001928 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Bryce Lee7ace3952018-02-16 14:34:32 -08001929 + " finishing=" + r.finishing + " state=" + r.getState()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001930 + " stackShouldBeVisible=" + stackShouldBeVisible
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001931 + " behindFullscreenActivity=" + behindFullscreenActivity
1932 + " mLaunchTaskBehind=" + r.mLaunchTaskBehind);
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001933 makeInvisible(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001934 }
Jorim Jaggife762342016-10-13 14:33:27 +02001935 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001936 final int windowingMode = getWindowingMode();
1937 if (windowingMode == WINDOWING_MODE_FREEFORM) {
Jorim Jaggife762342016-10-13 14:33:27 +02001938 // The visibility of tasks and the activities they contain in freeform stack are
1939 // determined individually unlike other stacks where the visibility or fullscreen
1940 // status of an activity in a previous task affects other.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001941 behindFullscreenActivity = !stackShouldBeVisible;
Wale Ogunwale68278562017-09-23 17:13:55 -07001942 } else if (isActivityTypeHome()) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001943 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001944 + " stackShouldBeVisible=" + stackShouldBeVisible
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001945 + " behindFullscreenActivity=" + behindFullscreenActivity);
1946 // No other task in the home stack should be visible behind the home activity.
1947 // Home activities is usually a translucent activity with the wallpaper behind
1948 // them. However, when they don't have the wallpaper behind them, we want to
1949 // show activities in the next application stack behind them vs. another
1950 // task in the home stack like recents.
1951 behindFullscreenActivity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001952 }
1953 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001954
Jorim Jaggife762342016-10-13 14:33:27 +02001955 if (mTranslucentActivityWaiting != null &&
1956 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1957 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1958 notifyActivityDrawnLocked(null);
1959 }
1960 } finally {
Bryce Lee2a3cc462017-10-27 10:57:35 -07001961 mStackSupervisor.getKeyguardController().endActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02001962 }
1963 }
1964
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01001965 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
1966 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1967 mTaskHistory.get(taskNdx).addStartingWindowsForVisibleActivities(taskSwitch);
1968 }
1969 }
1970
Jorim Jaggife762342016-10-13 14:33:27 +02001971 /**
1972 * @return true if the top visible activity wants to occlude the Keyguard, false otherwise
1973 */
1974 boolean topActivityOccludesKeyguard() {
1975 return mTopActivityOccludesKeyguard;
1976 }
1977
1978 /**
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001979 * Returns true if this stack should be resized to match the bounds specified by
1980 * {@link ActivityOptions#setLaunchBounds} when launching an activity into the stack.
1981 */
1982 boolean resizeStackWithLaunchBounds() {
1983 return inPinnedWindowingMode();
1984 }
1985
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001986 @Override
1987 public boolean supportsSplitScreenWindowingMode() {
1988 final TaskRecord topTask = topTask();
1989 return super.supportsSplitScreenWindowingMode()
1990 && (topTask == null || topTask.supportsSplitScreenWindowingMode());
1991 }
1992
Wale Ogunwale2b07da82017-11-08 14:52:40 -08001993 /** @return True if the resizing of the primary-split-screen stack affects this stack size. */
1994 boolean affectedBySplitScreenResize() {
1995 if (!supportsSplitScreenWindowingMode()) {
1996 return false;
1997 }
1998 final int windowingMode = getWindowingMode();
1999 return windowingMode != WINDOWING_MODE_FREEFORM && windowingMode != WINDOWING_MODE_PINNED;
2000 }
2001
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002002 /**
Jorim Jaggife762342016-10-13 14:33:27 +02002003 * @return the top most visible activity that wants to dismiss Keyguard
2004 */
2005 ActivityRecord getTopDismissingKeyguardActivity() {
2006 return mTopDismissingKeyguardActivity;
2007 }
2008
2009 /**
2010 * Checks whether {@param r} should be visible depending on Keyguard state and updates
2011 * {@link #mTopActivityOccludesKeyguard} and {@link #mTopDismissingKeyguardActivity} if
2012 * necessary.
2013 *
2014 * @return true if {@param r} is visible taken Keyguard state into account, false otherwise
2015 */
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002016 boolean checkKeyguardVisibility(ActivityRecord r, boolean shouldBeVisible, boolean isTop) {
Lucas Dupin47a65c72018-02-15 14:16:18 -08002017 final int displayId = mDisplayId != INVALID_DISPLAY ? mDisplayId : DEFAULT_DISPLAY;
2018 final boolean keyguardOrAodShowing = mStackSupervisor.getKeyguardController()
2019 .isKeyguardOrAodShowing(displayId);
Bryce Lee2a3cc462017-10-27 10:57:35 -07002020 final boolean keyguardLocked = mStackSupervisor.getKeyguardController().isKeyguardLocked();
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002021 final boolean showWhenLocked = r.canShowWhenLocked();
Jorim Jaggie69c9312016-10-31 18:24:38 -07002022 final boolean dismissKeyguard = r.hasDismissKeyguardWindows();
Jorim Jaggife762342016-10-13 14:33:27 +02002023 if (shouldBeVisible) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002024 if (dismissKeyguard && mTopDismissingKeyguardActivity == null) {
Jorim Jaggife762342016-10-13 14:33:27 +02002025 mTopDismissingKeyguardActivity = r;
2026 }
2027
2028 // Only the top activity may control occluded, as we can't occlude the Keyguard if the
2029 // top app doesn't want to occlude it.
2030 if (isTop) {
2031 mTopActivityOccludesKeyguard |= showWhenLocked;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07002032 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002033
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002034 final boolean canShowWithKeyguard = canShowWithInsecureKeyguard()
Bryce Lee2a3cc462017-10-27 10:57:35 -07002035 && mStackSupervisor.getKeyguardController().canDismissKeyguard();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002036 if (canShowWithKeyguard) {
2037 return true;
2038 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002039 }
Lucas Dupin47a65c72018-02-15 14:16:18 -08002040 if (keyguardOrAodShowing) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002041 // If keyguard is showing, nothing is visible, except if we are able to dismiss Keyguard
Lucas Dupin47a65c72018-02-15 14:16:18 -08002042 // right away and AOD isn't visible.
Bryce Lee2a3cc462017-10-27 10:57:35 -07002043 return shouldBeVisible && mStackSupervisor.getKeyguardController()
Jorim Jaggi07961872016-11-23 11:28:57 +01002044 .canShowActivityWhileKeyguardShowing(r, dismissKeyguard);
Jorim Jaggife762342016-10-13 14:33:27 +02002045 } else if (keyguardLocked) {
Bryce Lee2a3cc462017-10-27 10:57:35 -07002046 return shouldBeVisible && mStackSupervisor.getKeyguardController().canShowWhileOccluded(
Jorim Jaggi07961872016-11-23 11:28:57 +01002047 dismissKeyguard, showWhenLocked);
Jorim Jaggife762342016-10-13 14:33:27 +02002048 } else {
2049 return shouldBeVisible;
Craig Mautnereb8abf72014-07-02 15:04:09 -07002050 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051 }
Craig Mautner58547802013-03-05 08:23:53 -08002052
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002053 /**
2054 * Check if the display to which this stack is attached has
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002055 * {@link Display#FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD} applied.
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002056 */
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002057 private boolean canShowWithInsecureKeyguard() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002058 final ActivityDisplay activityDisplay = getDisplay();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002059 if (activityDisplay == null) {
2060 throw new IllegalStateException("Stack is not attached to any display, stackId="
2061 + mStackId);
2062 }
2063
2064 final int flags = activityDisplay.mDisplay.getFlags();
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002065 return (flags & FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002066 }
2067
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002068 private void checkTranslucentActivityWaiting(ActivityRecord top) {
2069 if (mTranslucentActivityWaiting != top) {
2070 mUndrawnActivitiesBelowTopTranslucent.clear();
2071 if (mTranslucentActivityWaiting != null) {
2072 // Call the callback with a timeout indication.
2073 notifyActivityDrawnLocked(null);
2074 mTranslucentActivityWaiting = null;
2075 }
2076 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2077 }
2078 }
2079
2080 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08002081 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002082 // We need to make sure the app is running if it's the top, or it is just made visible from
2083 // invisible. If the app is already visible, it must have died while it was visible. In this
2084 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
2085 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002086 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002087 // This activity needs to be visible, but isn't even running...
2088 // get it started and resume if no other stack in this stack is resumed.
2089 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
2090 if (r != starting) {
2091 r.startFreezingScreenLocked(r.app, configChanges);
2092 }
2093 if (!r.visible || r.mLaunchTaskBehind) {
2094 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002095 r.setVisible(true);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002096 }
2097 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002098 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002099 return true;
2100 }
2101 }
2102 return false;
2103 }
2104
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002105 // TODO: Should probably be moved into ActivityRecord.
2106 private void makeInvisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002107 if (!r.visible) {
2108 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
2109 return;
2110 }
2111 // Now for any activities that aren't visible to the user, make sure they no longer are
2112 // keeping the screen frozen.
Bryce Lee7ace3952018-02-16 14:34:32 -08002113 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.getState());
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002114 try {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002115 final boolean canEnterPictureInPicture = r.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002116 "makeInvisible", true /* beforeStopping */);
Wale Ogunwale08488a62017-09-05 12:37:03 -07002117 // Defer telling the client it is hidden if it can enter Pip and isn't current paused,
2118 // stopped or stopping. This gives it a chance to enter Pip in onPause().
2119 // TODO: There is still a question surrounding activities in multi-window mode that want
2120 // to enter Pip after they are paused, but are still visible. I they should be okay to
2121 // enter Pip in those cases, but not "auto-Pip" which is what this condition covers and
2122 // the current contract for "auto-Pip" is that the app should enter it before onPause
2123 // returns. Just need to confirm this reasoning makes sense.
Wale Ogunwale89973222017-04-23 18:39:45 -07002124 final boolean deferHidingClient = canEnterPictureInPicture
Bryce Lee7ace3952018-02-16 14:34:32 -08002125 && !r.isState(STOPPING, STOPPED, PAUSED);
Wale Ogunwaleec950642017-04-25 07:44:21 -07002126 r.setDeferHidingClient(deferHidingClient);
2127 r.setVisible(false);
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002128
Bryce Lee7ace3952018-02-16 14:34:32 -08002129 switch (r.getState()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002130 case STOPPING:
2131 case STOPPED:
2132 if (r.app != null && r.app.thread != null) {
2133 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2134 "Scheduling invisibility: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08002135 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08002136 WindowVisibilityItem.obtain(false /* showWindow */));
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002137 }
Winson Chungc2baac02017-01-11 13:34:47 -08002138
2139 // Reset the flag indicating that an app can enter picture-in-picture once the
2140 // activity is hidden
Winson Chungf7e03e12017-08-22 11:32:16 -07002141 r.supportsEnterPipOnTaskSwitch = false;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002142 break;
2143
2144 case INITIALIZING:
2145 case RESUMED:
2146 case PAUSING:
2147 case PAUSED:
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002148 addToStopping(r, true /* scheduleIdle */,
2149 canEnterPictureInPicture /* idleDelayed */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002150 break;
2151
2152 default:
2153 break;
2154 }
2155 } catch (Exception e) {
2156 // Just skip on any failure; we'll make it visible when it next restarts.
2157 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
2158 }
2159 }
2160
2161 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002162 ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002163 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002164 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08002165 + " stackInvisible=" + stackInvisible
2166 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08002167 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002168 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002169 }
2170 return behindFullscreenActivity;
2171 }
2172
Todd Kennedyaab56db2015-01-30 09:39:53 -08002173 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002174 mTranslucentActivityWaiting = r;
2175 mUndrawnActivitiesBelowTopTranslucent.clear();
2176 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
2177 }
2178
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002179 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2180 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2181 final TaskRecord task = mTaskHistory.get(taskNdx);
2182 final ArrayList<ActivityRecord> activities = task.mActivities;
2183 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2184 final ActivityRecord r = activities.get(activityNdx);
2185 if ( r.appTimeTracker != except) {
2186 r.appTimeTracker = null;
2187 }
2188 }
2189 }
2190 }
2191
Craig Mautner5eda9b32013-07-02 11:58:16 -07002192 /**
2193 * Called as activities below the top translucent activity are redrawn. When the last one is
2194 * redrawn notify the top activity by calling
2195 * {@link Activity#onTranslucentConversionComplete}.
2196 *
2197 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2198 * occurred and the activity will be notified immediately.
2199 */
2200 void notifyActivityDrawnLocked(ActivityRecord r) {
2201 if ((r == null)
2202 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2203 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2204 // The last undrawn activity below the top has just been drawn. If there is an
2205 // opaque activity at the top, notify it that it can become translucent safely now.
2206 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2207 mTranslucentActivityWaiting = null;
2208 mUndrawnActivitiesBelowTopTranslucent.clear();
2209 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2210
Craig Mautner71dd1b62014-02-18 15:48:52 -08002211 if (waitingActivity != null) {
2212 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
2213 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
2214 try {
2215 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
2216 waitingActivity.appToken, r != null);
2217 } catch (RemoteException e) {
2218 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002219 }
2220 }
2221 }
2222 }
2223
Craig Mautnera61bc652013-10-28 15:43:18 -07002224 /** If any activities below the top running one are in the INITIALIZING state and they have a
2225 * starting window displayed then remove that starting window. It is possible that the activity
2226 * in this state will never resumed in which case that starting window will be orphaned. */
2227 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002228 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002229 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002230 // We don't want to clear starting window for activities that aren't behind fullscreen
2231 // activities as we need to display their starting window until they are done initializing.
2232 boolean behindFullscreenActivity = false;
Wale Ogunwale98742a52016-07-12 10:29:12 -07002233
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002234 if (!shouldBeVisible(null)) {
Wale Ogunwale98742a52016-07-12 10:29:12 -07002235 // The stack is not visible, so no activity in it should be displaying a starting
2236 // window. Mark all activities below top and behind fullscreen.
2237 aboveTop = false;
2238 behindFullscreenActivity = true;
2239 }
2240
Craig Mautnera61bc652013-10-28 15:43:18 -07002241 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2242 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2243 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2244 final ActivityRecord r = activities.get(activityNdx);
2245 if (aboveTop) {
2246 if (r == topActivity) {
2247 aboveTop = false;
2248 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002249 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002250 continue;
2251 }
2252
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002253 r.removeOrphanedStartingWindow(behindFullscreenActivity);
Wale Ogunwale68741142016-05-17 09:40:02 -07002254 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002255 }
2256 }
2257 }
2258
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002259 /**
2260 * Ensure that the top activity in the stack is resumed.
2261 *
2262 * @param prev The previously resumed activity, for when in the process
2263 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002264 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002265 *
2266 * @return Returns true if something is being resumed, or false if
2267 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002268 *
2269 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2270 * non-focused stack to be resumed.
2271 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2272 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002273 */
Andreas Gampea36dc622018-02-05 17:19:22 -08002274 @GuardedBy("mService")
Wale Ogunwaled046a012015-12-24 13:05:59 -08002275 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002276 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002277 // Don't even start recursing.
2278 return false;
2279 }
2280
2281 boolean result = false;
2282 try {
2283 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002284 mStackSupervisor.inResumeTopActivity = true;
Bryce Lee69ad8182017-09-28 10:01:36 -07002285 result = resumeTopActivityInnerLocked(prev, options);
Bryce Leefc7cedd2018-03-01 15:38:07 -08002286
2287 // When resuming the top activity, it may be necessary to pause the top activity (for
2288 // example, returning to the lock screen. We suppress the normal pause logic in
2289 // {@link #resumeTopActivityUncheckedLocked}, since the top activity is resumed at the
2290 // end. We call the {@link ActivityStackSupervisor#checkReadyForSleepLocked} again here
2291 // to ensure any necessary pause logic occurs. In the case where the Activity will be
2292 // shown regardless of the lock screen, the call to
2293 // {@link ActivityStackSupervisor#checkReadyForSleepLocked} is skipped.
2294 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
2295 if (next == null || !next.canTurnScreenOn()) {
2296 checkReadyForSleep();
2297 }
Craig Mautner544efa72014-09-04 16:41:20 -07002298 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002299 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002300 }
chaviw59b98852017-06-13 12:05:44 -07002301
Craig Mautner544efa72014-09-04 16:41:20 -07002302 return result;
2303 }
2304
Bryce Leec4ab62a2018-03-05 14:19:26 -08002305 /**
2306 * Returns the currently resumed activity.
2307 */
2308 protected ActivityRecord getResumedActivity() {
2309 return mResumedActivity;
2310 }
2311
Bryce Lee84730a02018-04-03 14:10:04 -07002312 private void setResumedActivity(ActivityRecord r, String reason) {
2313 if (mResumedActivity == r) {
2314 return;
2315 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08002316
Bryce Lee84730a02018-04-03 14:10:04 -07002317 if (DEBUG_STACK) Slog.d(TAG_STACK, "setResumedActivity stack:" + this + " + from: "
2318 + mResumedActivity + " to:" + r + " reason:" + reason);
2319 mResumedActivity = r;
Chong Zhang6cda19c2016-06-14 19:07:56 -07002320 }
2321
Andreas Gampea36dc622018-02-05 17:19:22 -08002322 @GuardedBy("mService")
Chong Zhang280d3322015-11-03 17:27:26 -08002323 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002324 if (!mService.mBooting && !mService.mBooted) {
2325 // Not ready yet!
2326 return false;
2327 }
2328
Winson Chung3f103eb2017-04-12 21:53:59 -07002329 // Find the next top-most activity to resume in this stack that is not finishing and is
2330 // focusable. If it is not focusable, we will fall into the case below to resume the
2331 // top activity in the next focusable task.
2332 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
Andrii Kulian7fc22812016-12-28 13:04:11 -08002333
2334 final boolean hasRunningActivity = next != null;
2335
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002336 // TODO: Maybe this entire condition can get removed?
Wale Ogunwale66e16852017-10-19 13:35:52 -07002337 if (hasRunningActivity && !isAttached()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002338 return false;
2339 }
2340
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002341 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002342
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 // Remember how we'll process this pause/resume situation, and ensure
2344 // that the state is reset however we wind up proceeding.
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08002345 boolean userLeaving = mStackSupervisor.mUserLeaving;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002346 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347
Andrii Kulian7fc22812016-12-28 13:04:11 -08002348 if (!hasRunningActivity) {
2349 // There are no activities left in the stack, let's look somewhere else.
2350 return resumeTopActivityInNextFocusableStack(prev, options, "noMoreActivities");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 }
2352
2353 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002354
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 // If the top activity is the resumed one, nothing to do.
Bryce Lee7ace3952018-02-16 14:34:32 -08002356 if (mResumedActivity == next && next.isState(RESUMED)
2357 && mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 // Make sure we have executed any pending transitions, since there
2359 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002360 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002361 if (DEBUG_STATES) Slog.d(TAG_STATES,
2362 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002363 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 return false;
2365 }
2366
2367 // If we are sleeping, and there is no resumed activity, and the top
2368 // activity is paused, well that is the state we want.
David Stevens9440dc82017-03-16 19:00:20 -07002369 if (shouldSleepOrShutDownActivities()
p13451dbad2872012-04-18 11:39:23 +09002370 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002371 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 // Make sure we have executed any pending transitions, since there
2373 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002374 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002375 if (DEBUG_STATES) Slog.d(TAG_STATES,
2376 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002377 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002378 return false;
2379 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002380
2381 // Make sure that the user who owns this activity is started. If not,
2382 // we will just leave it as is because someone should be bringing
2383 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002384 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002385 Slog.w(TAG, "Skipping resume of top activity " + next
2386 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002387 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002388 return false;
2389 }
2390
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 // The activity may be waiting for stop, but that is no longer
2392 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002393 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002394 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002395 next.sleeping = false;
Bryce Lee4a194382017-04-04 14:32:48 -07002396 mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002398 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002400 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002401 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002402 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002403 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002404 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002405 return false;
2406 }
2407
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002408 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2409
Wale Ogunwale89973222017-04-23 18:39:45 -07002410 boolean lastResumedCanPip = false;
2411 final ActivityStack lastFocusedStack = mStackSupervisor.getLastStack();
2412 if (lastFocusedStack != null && lastFocusedStack != this) {
2413 // So, why aren't we using prev here??? See the param comment on the method. prev doesn't
2414 // represent the last resumed activity. However, the last focus stack does if it isn't null.
2415 final ActivityRecord lastResumed = lastFocusedStack.mResumedActivity;
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08002416 if (userLeaving && inMultiWindowMode() && lastFocusedStack.shouldBeVisible(next)) {
2417 // The user isn't leaving if this stack is the multi-window mode and the last
2418 // focused stack should still be visible.
2419 if(DEBUG_USER_LEAVING) Slog.i(TAG_USER_LEAVING, "Overriding userLeaving to false"
2420 + " next=" + next + " lastResumed=" + lastResumed);
2421 userLeaving = false;
2422 }
Wale Ogunwale89973222017-04-23 18:39:45 -07002423 lastResumedCanPip = lastResumed != null && lastResumed.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002424 "resumeTopActivity", userLeaving /* beforeStopping */);
Wale Ogunwale89973222017-04-23 18:39:45 -07002425 }
Winson Chungc2baac02017-01-11 13:34:47 -08002426 // If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002427 // to be paused, while at the same time resuming the new resume activity only if the
2428 // previous activity can't go into Pip since we want to give Pip activities a chance to
2429 // enter Pip before resuming the next activity.
Wale Ogunwale89973222017-04-23 18:39:45 -07002430 final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0
2431 && !lastResumedCanPip;
2432
Winson Chungc2baac02017-01-11 13:34:47 -08002433 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002434 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002435 if (DEBUG_STATES) Slog.d(TAG_STATES,
2436 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Winson Chungc2baac02017-01-11 13:34:47 -08002437 pausing |= startPausingLocked(userLeaving, false, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002438 }
Winson Chungc2baac02017-01-11 13:34:47 -08002439 if (pausing && !resumeWhilePausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002440 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002441 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002442 // At this point we want to put the upcoming activity's process
2443 // at the top of the LRU list, since we know we will be needing it
2444 // very soon and it would be a waste to let it get killed if it
2445 // happens to be sitting towards the end.
2446 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002447 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002448 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002449 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002450 return true;
Bryce Lee7ace3952018-02-16 14:34:32 -08002451 } else if (mResumedActivity == next && next.isState(RESUMED)
2452 && mStackSupervisor.allResumedActivitiesComplete()) {
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002453 // It is possible for the activity to be resumed when we paused back stacks above if the
2454 // next activity doesn't have to wait for pause to complete.
2455 // So, nothing else to-do except:
2456 // Make sure we have executed any pending transitions, since there
2457 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002458 executeAppTransition(options);
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002459 if (DEBUG_STATES) Slog.d(TAG_STATES,
2460 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
2461 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2462 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 }
2464
Christopher Tated3f175c2012-06-14 14:16:54 -07002465 // If the most recent activity was noHistory but was only stopped rather
2466 // than stopped+finished because the device went to sleep, we need to make
2467 // sure to finish it as we're making a new activity topmost.
David Stevens9440dc82017-03-16 19:00:20 -07002468 if (shouldSleepActivities() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002469 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002470 if (DEBUG_STATES) Slog.d(TAG_STATES,
2471 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002472 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002473 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002474 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002475 }
2476
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002477 if (prev != null && prev != next) {
Bryce Lee4a194382017-04-04 14:32:48 -07002478 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Craig Mautner8c14c152015-01-15 17:32:07 -08002479 && next != null && !next.nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07002480 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002481 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2482 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 } else {
2484 // The next activity is already visible, so hide the previous
2485 // activity's windows right now so we can show the new one ASAP.
2486 // We only do this if the previous is finishing, which should mean
2487 // it is on top of the one being resumed so hiding it quickly
2488 // is good. Otherwise, we want to do the normal route of allowing
2489 // the resumed activity to be shown so we can decide if the
2490 // previous should actually be hidden depending on whether the
2491 // new one is found to be full-screen or not.
2492 if (prev.finishing) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002493 prev.setVisibility(false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002494 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2495 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Bryce Lee4a194382017-04-04 14:32:48 -07002496 + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 + ", nowVisible=" + next.nowVisible);
2498 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002499 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002500 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002501 + ", waitingVisible="
Bryce Lee4a194382017-04-04 14:32:48 -07002502 + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002503 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002504 }
2505 }
2506 }
2507
Dianne Hackborne7f97212011-02-24 14:40:20 -08002508 // Launching this app's activity, make sure the app is no longer
2509 // considered stopped.
2510 try {
2511 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002512 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002513 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002514 } catch (IllegalArgumentException e) {
2515 Slog.w(TAG, "Failed trying to unstop package "
2516 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002517 }
2518
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002519 // We are starting up the next activity, so tell the window manager
2520 // that the previous one will be hidden soon. This way it can know
2521 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002522 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002523 if (prev != null) {
2524 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002525 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002526 "Prepare close transition: prev=" + prev);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002527 if (mStackSupervisor.mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002528 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002529 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002530 } else {
Bryce Leeaf691c02017-03-20 14:20:22 -07002531 mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002532 ? TRANSIT_ACTIVITY_CLOSE
2533 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002535 prev.setVisibility(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002536 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002537 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2538 "Prepare open transition: prev=" + prev);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002539 if (mStackSupervisor.mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002540 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002541 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 } else {
Bryce Leeaf691c02017-03-20 14:20:22 -07002543 mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002544 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002545 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002546 ? TRANSIT_TASK_OPEN_BEHIND
2547 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002548 }
2549 }
Craig Mautner967212c2013-04-13 21:10:58 -07002550 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002551 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002552 if (mStackSupervisor.mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002553 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002554 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002556 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002557 }
2558 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002559
Craig Mautner525f3d92013-05-07 14:01:50 -07002560 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002561 next.applyOptionsLocked();
2562 } else {
2563 next.clearOptionsLocked();
2564 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002565
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002566 mStackSupervisor.mNoAnimActivities.clear();
2567
Craig Mautnercf910b02013-04-23 11:23:27 -07002568 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002570 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2571 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002572
Chong Zhangd127c6d2016-05-02 16:36:41 -07002573 // If the previous activity is translucent, force a visibility update of
2574 // the next activity, so that it's added to WM's opening app list, and
2575 // transition animation can be set up properly.
2576 // For example, pressing Home button with a translucent activity in focus.
2577 // Launcher is already visible in this case. If we don't add it to opening
2578 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2579 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2580 final boolean lastActivityTranslucent = lastStack != null
Bryce Leef3c6a472017-11-14 14:53:06 -08002581 && (lastStack.inMultiWindowMode()
Chong Zhangd127c6d2016-05-02 16:36:41 -07002582 || (lastStack.mLastPausedActivity != null
2583 && !lastStack.mLastPausedActivity.fullscreen));
2584
Bryce Lee69ad8182017-09-28 10:01:36 -07002585 // The contained logic must be synchronized, since we are both changing the visibility
2586 // and updating the {@link Configuration}. {@link ActivityRecord#setVisibility} will
2587 // ultimately cause the client code to schedule a layout. Since layouts retrieve the
2588 // current {@link Configuration}, we must ensure that the below code updates it before
2589 // the layout can occur.
2590 synchronized(mWindowManager.getWindowManagerLock()) {
2591 // This activity is now becoming visible.
2592 if (!next.visible || next.stopped || lastActivityTranslucent) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002593 next.setVisibility(true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002594 }
Craig Mautner58547802013-03-05 08:23:53 -08002595
Bryce Lee69ad8182017-09-28 10:01:36 -07002596 // schedule launch ticks to collect information about slow apps.
2597 next.startLaunchTickingLocked();
2598
2599 ActivityRecord lastResumedActivity =
2600 lastStack == null ? null :lastStack.mResumedActivity;
Bryce Lee7ace3952018-02-16 14:34:32 -08002601 final ActivityState lastState = next.getState();
Bryce Lee69ad8182017-09-28 10:01:36 -07002602
2603 mService.updateCpuStats();
2604
2605 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next
2606 + " (in existing)");
2607
Bryce Leec4ab62a2018-03-05 14:19:26 -08002608 next.setState(RESUMED, "resumeTopActivityInnerLocked");
Bryce Lee69ad8182017-09-28 10:01:36 -07002609
2610 mService.updateLruProcessLocked(next.app, true, null);
2611 updateLRUListLocked(next);
2612 mService.updateOomAdjLocked();
2613
2614 // Have the window manager re-evaluate the orientation of
2615 // the screen based on the new activity order.
2616 boolean notUpdated = true;
2617
2618 if (mStackSupervisor.isFocusedStack(this)) {
2619
2620 // We have special rotation behavior when Keyguard is locked. Make sure all
2621 // activity visibilities are set correctly as well as the transition is updated
2622 // if needed to get the correct rotation behavior.
2623 // TODO: Remove this once visibilities are set correctly immediately when
2624 // starting an activity.
Bryce Lee2a3cc462017-10-27 10:57:35 -07002625 if (mStackSupervisor.getKeyguardController().isKeyguardLocked()) {
Bryce Lee69ad8182017-09-28 10:01:36 -07002626 mStackSupervisor.ensureActivitiesVisibleLocked(null /* starting */,
2627 0 /* configChanges */, false /* preserveWindows */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002628 }
Bryce Lee69ad8182017-09-28 10:01:36 -07002629 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
2630 mStackSupervisor.getDisplayOverrideConfiguration(mDisplayId),
2631 next.mayFreezeScreenLocked(next.app) ? next.appToken : null,
2632 mDisplayId);
2633 if (config != null) {
2634 next.frozenBeforeDestroy = true;
2635 }
2636 notUpdated = !mService.updateDisplayOverrideConfigurationLocked(config, next,
2637 false /* deferResume */, mDisplayId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002638 }
2639
Bryce Lee69ad8182017-09-28 10:01:36 -07002640 if (notUpdated) {
2641 // The configuration update wasn't able to keep the existing
2642 // instance of the activity, and instead started a new one.
2643 // We should be all done, but let's just make sure our activity
2644 // is still at the top and schedule another run if something
2645 // weird happened.
2646 ActivityRecord nextNext = topRunningActivityLocked();
2647 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
2648 "Activity config changed during resume: " + next
2649 + ", new next: " + nextNext);
2650 if (nextNext != next) {
2651 // Do over!
2652 mStackSupervisor.scheduleResumeTopActivities();
2653 }
2654 if (!next.visible || next.stopped) {
2655 next.setVisibility(true);
2656 }
2657 next.completeResumeLocked();
2658 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2659 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002660 }
2661
Bryce Lee69ad8182017-09-28 10:01:36 -07002662 try {
Andrii Kulian9956d892018-02-14 13:48:56 -08002663 final ClientTransaction transaction = ClientTransaction.obtain(next.app.thread,
2664 next.appToken);
Bryce Lee69ad8182017-09-28 10:01:36 -07002665 // Deliver all pending results.
2666 ArrayList<ResultInfo> a = next.results;
2667 if (a != null) {
2668 final int N = a.size();
2669 if (!next.finishing && N > 0) {
2670 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2671 "Delivering results to " + next + ": " + a);
Andrii Kulian9956d892018-02-14 13:48:56 -08002672 transaction.addCallback(ActivityResultItem.obtain(a));
Bryce Lee69ad8182017-09-28 10:01:36 -07002673 }
2674 }
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002675
Bryce Lee69ad8182017-09-28 10:01:36 -07002676 if (next.newIntents != null) {
Andrii Kulian9956d892018-02-14 13:48:56 -08002677 transaction.addCallback(NewIntentItem.obtain(next.newIntents,
2678 false /* andPause */));
Bryce Lee69ad8182017-09-28 10:01:36 -07002679 }
Craig Mautner58547802013-03-05 08:23:53 -08002680
Bryce Lee69ad8182017-09-28 10:01:36 -07002681 // Well the app will no longer be stopped.
2682 // Clear app token stopped state in window manager if needed.
2683 next.notifyAppResumed(next.stopped);
Craig Mautner58547802013-03-05 08:23:53 -08002684
Bryce Lee69ad8182017-09-28 10:01:36 -07002685 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2686 System.identityHashCode(next), next.getTask().taskId,
2687 next.shortComponentName);
2688
2689 next.sleeping = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05002690 mService.getAppWarningsLocked().onResumeActivity(next);
Bryce Lee69ad8182017-09-28 10:01:36 -07002691 mService.showAskCompatModeDialogLocked(next);
2692 next.app.pendingUiClean = true;
2693 next.app.forceProcessStateUpTo(mService.mTopProcessState);
2694 next.clearOptionsLocked();
Andrii Kulian9956d892018-02-14 13:48:56 -08002695 transaction.setLifecycleStateRequest(
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08002696 ResumeActivityItem.obtain(next.app.repProcState,
Bryce Leed946f862018-01-16 15:59:47 -08002697 mService.isNextTransitionForward())
2698 .setDescription(next.getLifecycleDescription(
2699 "resumeTopActivityInnerLocked")));
Bryce Leeb0f993f2018-03-02 15:38:01 -08002700 mService.getLifecycleManager().scheduleTransaction(transaction);
Bryce Lee69ad8182017-09-28 10:01:36 -07002701
2702 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed "
2703 + next);
2704 } catch (Exception e) {
2705 // Whoops, need to restart this activity!
2706 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
2707 + lastState + ": " + next);
Bryce Lee7ace3952018-02-16 14:34:32 -08002708 next.setState(lastState, "resumeTopActivityInnerLocked");
Bryce Lee4c401382018-03-13 19:23:03 -07002709
2710 // lastResumedActivity being non-null implies there is a lastStack present.
2711 if (lastResumedActivity != null) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08002712 lastResumedActivity.setState(RESUMED, "resumeTopActivityInnerLocked");
Bryce Lee69ad8182017-09-28 10:01:36 -07002713 }
Bryce Lee4c401382018-03-13 19:23:03 -07002714
Bryce Lee69ad8182017-09-28 10:01:36 -07002715 Slog.i(TAG, "Restarting because process died: " + next);
2716 if (!next.hasBeenLaunched) {
2717 next.hasBeenLaunched = true;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002718 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null
2719 && lastStack.isTopStackOnDisplay()) {
Bryce Lee69ad8182017-09-28 10:01:36 -07002720 next.showStartingWindow(null /* prev */, false /* newTask */,
2721 false /* taskSwitch */);
2722 }
2723 mStackSupervisor.startSpecificActivityLocked(next, true, false);
2724 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2725 return true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002726 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002727 }
2728
2729 // From this point on, if something goes wrong there is no way
2730 // to recover the activity.
2731 try {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002732 next.completeResumeLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002733 } catch (Exception e) {
2734 // If any exception gets thrown, toss away this
2735 // activity and try the next one.
2736 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002737 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002738 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002739 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002740 return true;
2741 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 } else {
2743 // Whoops, need to restart this activity!
2744 if (!next.hasBeenLaunched) {
2745 next.hasBeenLaunched = true;
2746 } else {
2747 if (SHOW_APP_STARTING_PREVIEW) {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002748 next.showStartingWindow(null /* prev */, false /* newTask */,
2749 false /* taskSwich */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002750 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002751 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002752 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002753 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002754 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 }
2756
Craig Mautnercf910b02013-04-23 11:23:27 -07002757 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002758 return true;
2759 }
2760
Andrii Kulian7fc22812016-12-28 13:04:11 -08002761 private boolean resumeTopActivityInNextFocusableStack(ActivityRecord prev,
2762 ActivityOptions options, String reason) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002763 if (adjustFocusToNextFocusableStack(reason)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002764 // Try to move focus to the next visible stack with a running activity if this
2765 // stack is not covering the entire screen or is on a secondary display (with no home
2766 // stack).
2767 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2768 mStackSupervisor.getFocusedStack(), prev, null);
2769 }
2770
2771 // Let's just start up the Launcher...
2772 ActivityOptions.abort(options);
2773 if (DEBUG_STATES) Slog.d(TAG_STATES,
2774 "resumeTopActivityInNextFocusableStack: " + reason + ", go home");
2775 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2776 // Only resume home if on home display
2777 return isOnHomeDisplay() &&
2778 mStackSupervisor.resumeHomeStackTask(prev, reason);
2779 }
2780
riddle_hsuc215a4f2014-12-27 12:10:45 +08002781 private TaskRecord getNextTask(TaskRecord targetTask) {
2782 final int index = mTaskHistory.indexOf(targetTask);
2783 if (index >= 0) {
2784 final int numTasks = mTaskHistory.size();
2785 for (int i = index + 1; i < numTasks; ++i) {
2786 TaskRecord task = mTaskHistory.get(i);
2787 if (task.userId == targetTask.userId) {
2788 return task;
2789 }
2790 }
2791 }
2792 return null;
2793 }
2794
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002795 /** Returns the position the input task should be placed in this stack. */
2796 int getAdjustedPositionForTask(TaskRecord task, int suggestedPosition,
2797 ActivityRecord starting) {
2798
2799 int maxPosition = mTaskHistory.size();
2800 if ((starting != null && starting.okToShowLocked())
2801 || (starting == null && task.okToShowLocked())) {
2802 // If the task or starting activity can be shown, then whatever position is okay.
2803 return Math.min(suggestedPosition, maxPosition);
2804 }
2805
2806 // The task can't be shown, put non-current user tasks below current user tasks.
2807 while (maxPosition > 0) {
2808 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2809 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2810 || tmpTask.topRunningActivityLocked() == null) {
2811 break;
2812 }
2813 maxPosition--;
2814 }
2815
2816 return Math.min(suggestedPosition, maxPosition);
2817 }
2818
Andrii Kuliand2765632016-12-12 22:26:34 -08002819 /**
2820 * Used from {@link ActivityStack#positionTask(TaskRecord, int)}.
2821 * @see ActivityManagerService#positionTaskInStack(int, int, int).
2822 */
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002823 private void insertTaskAtPosition(TaskRecord task, int position) {
2824 if (position >= mTaskHistory.size()) {
2825 insertTaskAtTop(task, null);
2826 return;
Evan Rosky9c448172017-11-02 14:19:27 -07002827 } else if (position <= 0) {
2828 insertTaskAtBottom(task);
2829 return;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002830 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002831 position = getAdjustedPositionForTask(task, position, null /* starting */);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002832 mTaskHistory.remove(task);
2833 mTaskHistory.add(position, task);
Bryce Leef3c6a472017-11-14 14:53:06 -08002834 mWindowContainerController.positionChildAt(task.getWindowContainerController(), position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002835 updateTaskMovement(task, true);
2836 }
2837
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002838 private void insertTaskAtTop(TaskRecord task, ActivityRecord starting) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002839 // TODO: Better place to put all the code below...may be addTask...
2840 mTaskHistory.remove(task);
2841 // Now put task at top.
2842 final int position = getAdjustedPositionForTask(task, mTaskHistory.size(), starting);
2843 mTaskHistory.add(position, task);
2844 updateTaskMovement(task, true);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002845 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
2846 true /* includingParents */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002847 }
2848
Evan Rosky9c448172017-11-02 14:19:27 -07002849 private void insertTaskAtBottom(TaskRecord task) {
2850 // Unlike insertTaskAtPosition, this will also position parents of the windowcontroller.
2851 mTaskHistory.remove(task);
2852 final int position = getAdjustedPositionForTask(task, 0, null);
2853 mTaskHistory.add(position, task);
2854 updateTaskMovement(task, true);
2855 mWindowContainerController.positionChildAtBottom(task.getWindowContainerController(),
2856 true /* includingParents */);
2857 }
2858
Bryce Leead5b8322018-03-08 14:28:52 -08002859 void startActivityLocked(ActivityRecord r, ActivityRecord focusedTopActivity,
Winson Chungb5c41b72016-12-07 15:00:47 -08002860 boolean newTask, boolean keepCurTransition, ActivityOptions options) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002861 TaskRecord rTask = r.getTask();
Craig Mautnerd2328952013-03-05 12:46:26 -08002862 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002863 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2864 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002865 // Last activity in task had been removed or ActivityManagerService is reusing task.
2866 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002867 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002868 insertTaskAtTop(rTask, r);
Craig Mautner77878772013-03-04 19:46:24 -08002869 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002870 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002871 if (!newTask) {
2872 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002873 boolean startIt = true;
2874 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2875 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002876 if (task.getTopActivity() == null) {
2877 // All activities in task are finishing.
2878 continue;
2879 }
Bryce Leeaf691c02017-03-20 14:20:22 -07002880 if (task == rTask) {
Craig Mautner70a86932013-02-28 22:37:44 -08002881 // Here it is! Now, if this is not yet visible to the
2882 // user, then just add it without starting; it will
2883 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002884 if (!startIt) {
2885 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2886 + task, new RuntimeException("here").fillInStackTrace());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002887 r.createWindowContainer();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002888 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 return;
2890 }
2891 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002892 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002893 startIt = false;
2894 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002895 }
2896 }
2897
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002898 // Place a new activity at top of stack, so it is next to interact with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002899
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002900 // If we are not placing the new activity frontmost, we do not want to deliver the
2901 // onUserLeaving callback to the actual frontmost activity
Bryce Leeaf691c02017-03-20 14:20:22 -07002902 final TaskRecord activityTask = r.getTask();
2903 if (task == activityTask && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002904 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002905 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002906 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002907 }
Craig Mautner70a86932013-02-28 22:37:44 -08002908
Bryce Leeaf691c02017-03-20 14:20:22 -07002909 task = activityTask;
Craig Mautner70a86932013-02-28 22:37:44 -08002910
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002912 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002913 new RuntimeException("here").fillInStackTrace());
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002914 // TODO: Need to investigate if it is okay for the controller to already be created by the
2915 // time we get to this point. I think it is, but need to double check.
2916 // Use test in b/34179495 to trace the call path.
2917 if (r.getWindowContainerController() == null) {
2918 r.createWindowContainer();
2919 }
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002920 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002921
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002922 if (!isHomeOrRecentsStack() || numActivities() > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002923 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002925 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002926 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002927 mStackSupervisor.mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002928 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002929 int transit = TRANSIT_ACTIVITY_OPEN;
2930 if (newTask) {
2931 if (r.mLaunchTaskBehind) {
2932 transit = TRANSIT_TASK_OPEN_BEHIND;
2933 } else {
Winson Chungc2baac02017-01-11 13:34:47 -08002934 // If a new task is being launched, then mark the existing top activity as
Winson Chung942a85c2017-07-11 15:07:45 -07002935 // supporting picture-in-picture while pausing only if the starting activity
2936 // would not be considered an overlay on top of the current activity
2937 // (eg. not fullscreen, or the assistant)
Winson Chungf7e03e12017-08-22 11:32:16 -07002938 if (canEnterPipOnTaskSwitch(focusedTopActivity,
2939 null /* toFrontTask */, r, options)) {
2940 focusedTopActivity.supportsEnterPipOnTaskSwitch = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08002941 }
2942 transit = TRANSIT_TASK_OPEN;
2943 }
2944 }
2945 mWindowManager.prepareAppTransition(transit, keepCurTransition);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002946 mStackSupervisor.mNoAnimActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948 boolean doShow = true;
2949 if (newTask) {
2950 // Even though this activity is starting fresh, we still need
2951 // to reset it to make sure we apply affinities to move any
2952 // existing activities from other tasks in to it.
2953 // If the caller has requested that the target task be
2954 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002955 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002956 resetTaskIfNeededLocked(r, r);
2957 doShow = topRunningNonDelayedActivityLocked(null) == r;
2958 }
Chong Zhang280d3322015-11-03 17:27:26 -08002959 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002960 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2961 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002962 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002963 if (r.mLaunchTaskBehind) {
2964 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2965 // tell WindowManager that r is visible even though it is at the back of the stack.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002966 r.setVisibility(true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002967 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002968 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 // Figure out if we are transitioning from another activity that is
2970 // "has the same starting icon" as the next one. This allows the
2971 // window manager to keep the previous window it had previously
2972 // created, if it still had one.
Bryce Leeaf691c02017-03-20 14:20:22 -07002973 TaskRecord prevTask = r.getTask();
2974 ActivityRecord prev = prevTask.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 if (prev != null) {
2976 // We don't want to reuse the previous starting preview if:
2977 // (1) The current activity is in a different task.
Bryce Leeaf691c02017-03-20 14:20:22 -07002978 if (prev.getTask() != prevTask) {
Craig Mautner29219d92013-04-16 20:19:12 -07002979 prev = null;
2980 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002981 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002982 else if (prev.nowVisible) {
2983 prev = null;
2984 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002986 r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002987 }
2988 } else {
2989 // If this is the first activity, don't do any fancy animations,
2990 // because there is nothing for it to animate on top of.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002991 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002993 }
2994
Winson Chungf7e03e12017-08-22 11:32:16 -07002995 /**
2996 * @return Whether the switch to another task can trigger the currently running activity to
2997 * enter PiP while it is pausing (if supported). Only one of {@param toFrontTask} or
2998 * {@param toFrontActivity} should be set.
2999 */
3000 private boolean canEnterPipOnTaskSwitch(ActivityRecord pipCandidate,
3001 TaskRecord toFrontTask, ActivityRecord toFrontActivity, ActivityOptions opts) {
3002 if (opts != null && opts.disallowEnterPictureInPictureWhileLaunching()) {
3003 // Ensure the caller has requested not to trigger auto-enter PiP
3004 return false;
3005 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003006 if (pipCandidate == null || pipCandidate.inPinnedWindowingMode()) {
Winson Chungf7e03e12017-08-22 11:32:16 -07003007 // Ensure that we do not trigger entering PiP an activity on the pinned stack
3008 return false;
3009 }
Wale Ogunwale68278562017-09-23 17:13:55 -07003010 final ActivityStack targetStack = toFrontTask != null
3011 ? toFrontTask.getStack() : toFrontActivity.getStack();
3012 if (targetStack != null && targetStack.isActivityTypeAssistant()) {
Winson Chungf7e03e12017-08-22 11:32:16 -07003013 // Ensure the task/activity being brought forward is not the assistant
3014 return false;
3015 }
Winson Chungf7e03e12017-08-22 11:32:16 -07003016 return true;
3017 }
3018
Jorim Jaggi02886a82016-12-06 09:10:06 -08003019 private boolean isTaskSwitch(ActivityRecord r,
3020 ActivityRecord topFocusedActivity) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003021 return topFocusedActivity != null && r.getTask() != topFocusedActivity.getTask();
Jorim Jaggi02886a82016-12-06 09:10:06 -08003022 }
3023
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003024 /**
3025 * Perform a reset of the given task, if needed as part of launching it.
3026 * Returns the new HistoryRecord at the top of the task.
3027 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08003028 /**
3029 * Helper method for #resetTaskIfNeededLocked.
3030 * We are inside of the task being reset... we'll either finish this activity, push it out
3031 * for another task, or leave it as-is.
3032 * @param task The task containing the Activity (taskTop) that might be reset.
3033 * @param forceReset
3034 * @return An ActivityOptions that needs to be processed.
3035 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07003036 private ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003037 ActivityOptions topOptions = null;
3038
3039 int replyChainEnd = -1;
3040 boolean canMoveOptions = true;
3041
3042 // We only do this for activities that are not the root of the task (since if we finish
3043 // the root, we may no longer have the task!).
3044 final ArrayList<ActivityRecord> activities = task.mActivities;
3045 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003046 final int rootActivityNdx = task.findEffectiveRootIndex();
3047 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003048 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003049 if (target.frontOfTask)
3050 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003051
3052 final int flags = target.info.flags;
3053 final boolean finishOnTaskLaunch =
3054 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3055 final boolean allowTaskReparenting =
3056 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3057 final boolean clearWhenTaskReset =
3058 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
3059
3060 if (!finishOnTaskLaunch
3061 && !clearWhenTaskReset
3062 && target.resultTo != null) {
3063 // If this activity is sending a reply to a previous
3064 // activity, we can't do anything with it now until
3065 // we reach the start of the reply chain.
3066 // XXX note that we are assuming the result is always
3067 // to the previous activity, which is almost always
3068 // the case but we really shouldn't count on.
3069 if (replyChainEnd < 0) {
3070 replyChainEnd = i;
3071 }
3072 } else if (!finishOnTaskLaunch
3073 && !clearWhenTaskReset
3074 && allowTaskReparenting
3075 && target.taskAffinity != null
3076 && !target.taskAffinity.equals(task.affinity)) {
3077 // If this activity has an affinity for another
3078 // task, then we need to move it out of here. We will
3079 // move it as far out of the way as possible, to the
3080 // bottom of the activity stack. This also keeps it
3081 // correctly ordered with any activities we previously
3082 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08003083 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003084 final ActivityRecord bottom =
3085 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08003086 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003087 if (bottom != null && target.taskAffinity != null
Bryce Leeaf691c02017-03-20 14:20:22 -07003088 && target.taskAffinity.equals(bottom.getTask().affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003089 // If the activity currently at the bottom has the
3090 // same task affinity as the one we are moving,
3091 // then merge it into the same task.
Bryce Leeaf691c02017-03-20 14:20:22 -07003092 targetTask = bottom.getTask();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003093 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003094 + " out to bottom task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003095 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003096 targetTask = createTaskRecord(
3097 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003098 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08003099 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003100 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003101 + " out to new task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003102 }
3103
Craig Mautner525f3d92013-05-07 14:01:50 -07003104 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003105 final int start = replyChainEnd < 0 ? i : replyChainEnd;
3106 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07003107 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003108 if (p.finishing) {
3109 continue;
3110 }
3111
Craig Mautnere3a74d52013-02-22 14:14:58 -08003112 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003113 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003114 topOptions = p.takeOptionsLocked();
3115 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003116 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003117 }
3118 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003119 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3120 "Removing activity " + p + " from task=" + task + " adding to task="
3121 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003122 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Bryce Leeaf691c02017-03-20 14:20:22 -07003123 "Pushing next activity " + p + " out to target's task " + target);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08003124 p.reparent(targetTask, 0 /* position - bottom */, "resetTargetTaskIfNeeded");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003125 }
3126
Wale Ogunwale1666e312016-12-16 11:27:18 -08003127 mWindowContainerController.positionChildAtBottom(
Wale Ogunwale66e16852017-10-19 13:35:52 -07003128 targetTask.getWindowContainerController(), false /* includingParents */);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003129 replyChainEnd = -1;
3130 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
3131 // If the activity should just be removed -- either
3132 // because it asks for it, or the task should be
3133 // cleared -- then finish it and anything that is
3134 // part of its reply chain.
3135 int end;
3136 if (clearWhenTaskReset) {
3137 // In this case, we want to finish this activity
3138 // and everything above it, so be sneaky and pretend
3139 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08003140 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003141 } else if (replyChainEnd < 0) {
3142 end = i;
3143 } else {
3144 end = replyChainEnd;
3145 }
Craig Mautner525f3d92013-05-07 14:01:50 -07003146 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003147 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003148 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003149 if (p.finishing) {
3150 continue;
3151 }
3152 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003153 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003154 topOptions = p.takeOptionsLocked();
3155 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003156 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003157 }
3158 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003159 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08003160 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08003161 if (finishActivityLocked(
3162 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003163 end--;
3164 srcPos--;
3165 }
3166 }
3167 replyChainEnd = -1;
3168 } else {
3169 // If we were in the middle of a chain, well the
3170 // activity that started it all doesn't want anything
3171 // special, so leave it all as-is.
3172 replyChainEnd = -1;
3173 }
3174 }
3175
3176 return topOptions;
3177 }
3178
3179 /**
3180 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
3181 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
3182 * @param affinityTask The task we are looking for an affinity to.
3183 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
3184 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
3185 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
3186 */
Craig Mautner525f3d92013-05-07 14:01:50 -07003187 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08003188 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003189 int replyChainEnd = -1;
3190 final int taskId = task.taskId;
3191 final String taskAffinity = task.affinity;
3192
3193 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
3194 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003195 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
3196
3197 // Do not operate on or below the effective root Activity.
3198 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003199 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003200 if (target.frontOfTask)
3201 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003202
3203 final int flags = target.info.flags;
3204 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3205 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3206
3207 if (target.resultTo != null) {
3208 // If this activity is sending a reply to a previous
3209 // activity, we can't do anything with it now until
3210 // we reach the start of the reply chain.
3211 // XXX note that we are assuming the result is always
3212 // to the previous activity, which is almost always
3213 // the case but we really shouldn't count on.
3214 if (replyChainEnd < 0) {
3215 replyChainEnd = i;
3216 }
3217 } else if (topTaskIsHigher
3218 && allowTaskReparenting
3219 && taskAffinity != null
3220 && taskAffinity.equals(target.taskAffinity)) {
3221 // This activity has an affinity for our task. Either remove it if we are
3222 // clearing or move it over to our task. Note that
3223 // we currently punt on the case where we are resetting a
3224 // task that is not at the top but who has activities above
3225 // with an affinity to it... this is really not a normal
3226 // case, and we will need to later pull that task to the front
3227 // and usually at that point we will do the reset and pick
3228 // up those remaining activities. (This only happens if
3229 // someone starts an activity in a new task from an activity
3230 // in a task that is not currently on top.)
3231 if (forceReset || finishOnTaskLaunch) {
3232 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003233 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3234 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003235 for (int srcPos = start; srcPos >= i; --srcPos) {
3236 final ActivityRecord p = activities.get(srcPos);
3237 if (p.finishing) {
3238 continue;
3239 }
Todd Kennedy539db512014-12-15 09:57:55 -08003240 finishActivityLocked(
3241 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003242 }
3243 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003244 if (taskInsertionPoint < 0) {
3245 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003246
Craig Mautner77878772013-03-04 19:46:24 -08003247 }
Craig Mautner77878772013-03-04 19:46:24 -08003248
3249 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003250 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3251 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3252 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003253 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003254 final ActivityRecord p = activities.get(srcPos);
Winson Chung30480042017-01-26 10:55:34 -08003255 p.reparent(task, taskInsertionPoint, "resetAffinityTaskIfNeededLocked");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003256
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003257 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3258 "Removing and adding activity " + p + " to stack at " + task
3259 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003260 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3261 + " from " + srcPos + " in to resetting task " + task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003262 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08003263 mWindowContainerController.positionChildAtTop(
3264 task.getWindowContainerController(), true /* includingParents */);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003265
3266 // Now we've moved it in to place... but what if this is
3267 // a singleTop activity and we have put it on top of another
3268 // instance of the same activity? Then we drop the instance
3269 // below so it remains singleTop.
3270 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3271 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003272 int targetNdx = taskActivities.indexOf(target);
3273 if (targetNdx > 0) {
3274 ActivityRecord p = taskActivities.get(targetNdx - 1);
3275 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003276 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3277 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003278 }
3279 }
3280 }
3281 }
3282
3283 replyChainEnd = -1;
3284 }
3285 }
Craig Mautner77878772013-03-04 19:46:24 -08003286 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003287 }
3288
Craig Mautner8849a5e2013-04-02 16:41:03 -07003289 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003290 ActivityRecord newActivity) {
Winson Chung5fa39752017-10-04 14:50:15 -07003291 final boolean forceReset =
Craig Mautnere3a74d52013-02-22 14:14:58 -08003292 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
Bryce Leeaf691c02017-03-20 14:20:22 -07003293 final TaskRecord task = taskTop.getTask();
Craig Mautnere3a74d52013-02-22 14:14:58 -08003294
3295 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3296 * for remaining tasks. Used for later tasks to reparent to task. */
3297 boolean taskFound = false;
3298
3299 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3300 ActivityOptions topOptions = null;
3301
Craig Mautner77878772013-03-04 19:46:24 -08003302 // Preserve the location for reparenting in the new task.
3303 int reparentInsertionPoint = -1;
3304
Craig Mautnere3a74d52013-02-22 14:14:58 -08003305 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3306 final TaskRecord targetTask = mTaskHistory.get(i);
3307
3308 if (targetTask == task) {
3309 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3310 taskFound = true;
3311 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003312 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3313 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003314 }
3315 }
3316
Craig Mautner70a86932013-02-28 22:37:44 -08003317 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003318 if (taskNdx >= 0) {
3319 do {
3320 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3321 } while (taskTop == null && taskNdx >= 0);
3322 }
Craig Mautner70a86932013-02-28 22:37:44 -08003323
Craig Mautnere3a74d52013-02-22 14:14:58 -08003324 if (topOptions != null) {
3325 // If we got some ActivityOptions from an activity on top that
3326 // was removed from the task, propagate them to the new real top.
3327 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003328 taskTop.updateOptionsLocked(topOptions);
3329 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003330 topOptions.abort();
3331 }
3332 }
3333
3334 return taskTop;
3335 }
3336
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003337 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3338 String resultWho, int requestCode, int resultCode, Intent data) {
3339
3340 if (callingUid > 0) {
3341 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003342 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003343 }
3344
3345 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3346 + " : who=" + resultWho + " req=" + requestCode
3347 + " res=" + resultCode + " data=" + data);
3348 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3349 try {
3350 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3351 list.add(new ResultInfo(resultWho, requestCode,
3352 resultCode, data));
Bryce Leeb0f993f2018-03-02 15:38:01 -08003353 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08003354 ActivityResultItem.obtain(list));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 return;
3356 } catch (Exception e) {
3357 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3358 }
3359 }
3360
3361 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3362 }
3363
Shunta Sato63b8ee32016-07-11 13:32:52 +09003364 /** 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 -07003365 private boolean isATopFinishingTask(TaskRecord task) {
Shunta Sato63b8ee32016-07-11 13:32:52 +09003366 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3367 final TaskRecord current = mTaskHistory.get(i);
3368 final ActivityRecord r = current.topRunningActivityLocked();
3369 if (r != null) {
3370 // We got a top running activity, so there isn't a top finishing task...
3371 return false;
3372 }
3373 if (current == task) {
3374 return true;
3375 }
3376 }
3377 return false;
3378 }
3379
Wale Ogunwale66e16852017-10-19 13:35:52 -07003380 private void adjustFocusedActivityStack(ActivityRecord r, String reason) {
Shunta Sato63b8ee32016-07-11 13:32:52 +09003381 if (!mStackSupervisor.isFocusedStack(this) ||
3382 ((mResumedActivity != r) && (mResumedActivity != null))) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003383 return;
3384 }
3385
3386 final ActivityRecord next = topRunningActivityLocked();
3387 final String myReason = reason + " adjustFocus";
Bryce Lee3345c4e2017-04-25 07:40:41 -07003388
Wale Ogunwale66e16852017-10-19 13:35:52 -07003389 if (next == r) {
3390 mStackSupervisor.moveFocusableActivityStackToFrontLocked(
3391 mStackSupervisor.topRunningActivityLocked(), myReason);
3392 return;
Wale Ogunwaled045c822015-12-02 09:14:28 -08003393 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003394
Wale Ogunwale66e16852017-10-19 13:35:52 -07003395 if (next != null && isFocusable()) {
3396 // Keep focus in stack if we have a top running activity and are focusable.
3397 return;
3398 }
3399
3400 // Task is not guaranteed to be non-null. For example, destroying the
3401 // {@link ActivityRecord} will disassociate the task from the activity.
3402 final TaskRecord task = r.getTask();
3403
3404 if (task == null) {
3405 throw new IllegalStateException("activity no longer associated with task:" + r);
3406 }
3407
3408 // Move focus to next focusable stack if possible.
3409 if (adjustFocusToNextFocusableStack(myReason)) {
3410 return;
3411 }
3412
3413 // Whatever...go home.
3414 mStackSupervisor.moveHomeStackTaskToTop(myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003415 }
3416
Andrii Kulian250d6532017-02-08 23:30:45 -08003417 /** Find next proper focusable stack and make it focused. */
Wale Ogunwale17696de2018-03-15 16:41:05 -07003418 boolean adjustFocusToNextFocusableStack(String reason) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07003419 return adjustFocusToNextFocusableStack(reason, false /* allowFocusSelf */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003420 }
3421
3422 /**
3423 * Find next proper focusable stack and make it focused.
3424 * @param allowFocusSelf Is the focus allowed to remain on the same stack.
3425 */
Wale Ogunwale66e16852017-10-19 13:35:52 -07003426 private boolean adjustFocusToNextFocusableStack(String reason, boolean allowFocusSelf) {
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08003427 final ActivityStack stack =
3428 mStackSupervisor.getNextFocusableStackLocked(this, !allowFocusSelf);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003429 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003430 if (stack == null) {
3431 return false;
3432 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003433
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003434 final ActivityRecord top = stack.topRunningActivityLocked();
3435
Wale Ogunwale66e16852017-10-19 13:35:52 -07003436 if (stack.isActivityTypeHome() && (top == null || !top.visible)) {
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003437 // If we will be focusing on the home stack next and its current top activity isn't
Wale Ogunwale66e16852017-10-19 13:35:52 -07003438 // visible, then use the move the home stack task to top to make the activity visible.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003439 return mStackSupervisor.moveHomeStackTaskToTop(reason);
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003440 }
Chong Zhang6cda19c2016-06-14 19:07:56 -07003441
3442 stack.moveToFront(myReason);
3443 return true;
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003444 }
3445
Craig Mautnerf3333272013-04-22 10:55:53 -07003446 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003447 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003448 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3449 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3450 if (!r.finishing) {
David Stevens9440dc82017-03-16 19:00:20 -07003451 if (!shouldSleepActivities()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003452 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003453 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3454 "stop-no-history", false)) {
Bryce Lee3345c4e2017-04-25 07:40:41 -07003455 // If {@link requestFinishActivityLocked} returns {@code true},
Wale Ogunwale66e16852017-10-19 13:35:52 -07003456 // {@link adjustFocusedActivityStack} would have been already called.
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003457 r.resumeKeyDispatchingLocked();
3458 return;
3459 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003460 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003461 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003462 + " on stop because we're just sleeping");
3463 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003464 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003465 }
3466
3467 if (r.app != null && r.app.thread != null) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07003468 adjustFocusedActivityStack(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003469 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003470 try {
3471 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003472 if (DEBUG_STATES) Slog.v(TAG_STATES,
3473 "Moving to STOPPING: " + r + " (stop requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08003474 r.setState(STOPPING, "stopActivityLocked");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003475 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3476 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003477 if (!r.visible) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003478 r.setVisible(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003479 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003480 EventLogTags.writeAmStopActivity(
3481 r.userId, System.identityHashCode(r), r.shortComponentName);
Bryce Leeb0f993f2018-03-02 15:38:01 -08003482 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Bryce Leed946f862018-01-16 15:59:47 -08003483 StopActivityItem.obtain(r.visible, r.configChangeFlags)
3484 .setDescription(r.getLifecycleDescription("stopActivityLocked")));
David Stevens9440dc82017-03-16 19:00:20 -07003485 if (shouldSleepOrShutDownActivities()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003486 r.setSleeping(true);
3487 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003488 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003489 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003490 } catch (Exception e) {
3491 // Maybe just ignore exceptions here... if the process
3492 // has crashed, our death notification will clean things
3493 // up.
3494 Slog.w(TAG, "Exception thrown during pause", e);
3495 // Just in case, assume it to be stopped.
3496 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003497 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Bryce Lee7ace3952018-02-16 14:34:32 -08003498 r.setState(STOPPED, "stopActivityLocked");
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003499 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003500 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003501 }
3502 }
3503 }
3504 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003506 /**
3507 * @return Returns true if the activity is being finished, false if for
3508 * some reason it is being left as-is.
3509 */
3510 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003511 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003512 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003513 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3514 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003515 + ", result=" + resultCode + ", data=" + resultData
3516 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003517 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003518 return false;
3519 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003520
Craig Mautnerd44711d2013-02-23 11:24:36 -08003521 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 return true;
3523 }
3524
Craig Mautnerd2328952013-03-05 12:46:26 -08003525 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003526 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3527 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3528 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3529 ActivityRecord r = activities.get(activityNdx);
3530 if (r.resultTo == self && r.requestCode == requestCode) {
3531 if ((r.resultWho == null && resultWho == null) ||
3532 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3533 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3534 false);
3535 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003536 }
3537 }
3538 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003539 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003540 }
3541
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07003542 /**
3543 * Finish the topmost activity that belongs to the crashed app. We may also finish the activity
3544 * that requested launch of the crashed one to prevent launch-crash loop.
3545 * @param app The app that crashed.
3546 * @param reason Reason to perform this action.
3547 * @return The task that was finished in this stack, {@code null} if top running activity does
3548 * not belong to the crashed app.
3549 */
3550 final TaskRecord finishTopCrashedActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003551 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003552 TaskRecord finishedTask = null;
3553 if (r == null || r.app != app) {
3554 return null;
3555 }
3556 Slog.w(TAG, " Force finishing activity "
3557 + r.intent.getComponent().flattenToShortString());
Bryce Leeaf691c02017-03-20 14:20:22 -07003558 finishedTask = r.getTask();
3559 int taskNdx = mTaskHistory.indexOf(finishedTask);
3560 final TaskRecord task = finishedTask;
3561 int activityNdx = task.mActivities.indexOf(r);
Adrian Roos20d7df32016-01-12 18:59:43 +01003562 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Bryce Leeaf691c02017-03-20 14:20:22 -07003563 finishedTask = task;
Adrian Roos20d7df32016-01-12 18:59:43 +01003564 // Also terminate any activities below it that aren't yet
3565 // stopped, to avoid a situation where one will get
3566 // re-start our crashing activity once it gets resumed again.
3567 --activityNdx;
3568 if (activityNdx < 0) {
3569 do {
3570 --taskNdx;
3571 if (taskNdx < 0) {
3572 break;
3573 }
3574 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3575 } while (activityNdx < 0);
3576 }
3577 if (activityNdx >= 0) {
3578 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Bryce Lee7ace3952018-02-16 14:34:32 -08003579 if (r.isState(RESUMED, PAUSING, PAUSED)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003580 if (!r.isActivityTypeHome() || mService.mHomeProcess != r.app) {
Adrian Roos20d7df32016-01-12 18:59:43 +01003581 Slog.w(TAG, " Force finishing activity "
3582 + r.intent.getComponent().flattenToShortString());
3583 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003584 }
3585 }
3586 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003587 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003588 }
3589
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003590 final void finishVoiceTask(IVoiceInteractionSession session) {
3591 IBinder sessionBinder = session.asBinder();
3592 boolean didOne = false;
3593 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3594 TaskRecord tr = mTaskHistory.get(taskNdx);
3595 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3596 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3597 ActivityRecord r = tr.mActivities.get(activityNdx);
3598 if (!r.finishing) {
3599 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3600 false);
3601 didOne = true;
3602 }
3603 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003604 } else {
3605 // Check if any of the activities are using voice
3606 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3607 ActivityRecord r = tr.mActivities.get(activityNdx);
3608 if (r.voiceSession != null
3609 && r.voiceSession.asBinder() == sessionBinder) {
3610 // Inform of cancellation
3611 r.clearVoiceSessionLocked();
3612 try {
3613 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3614 null);
3615 } catch (RemoteException re) {
3616 // Ok
3617 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003618 mService.finishRunningVoiceLocked();
3619 break;
3620 }
3621 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003622 }
3623 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003624
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003625 if (didOne) {
3626 mService.updateOomAdjLocked();
3627 }
3628 }
3629
Craig Mautnerd2328952013-03-05 12:46:26 -08003630 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003631 ArrayList<ActivityRecord> activities = r.getTask().mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003632 for (int index = activities.indexOf(r); index >= 0; --index) {
3633 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003634 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003635 break;
3636 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003637 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003638 }
3639 return true;
3640 }
3641
Andrii Kulian21713ac2016-10-12 22:05:05 -07003642 private void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
Dianne Hackborn5c607432012-02-28 14:44:19 -08003643 // send the result
3644 ActivityRecord resultTo = r.resultTo;
3645 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003646 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003647 + " who=" + r.resultWho + " req=" + r.requestCode
3648 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003649 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003650 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003651 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003652 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003653 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003654 if (r.info.applicationInfo.uid > 0) {
3655 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3656 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003657 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003658 }
3659 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3660 resultData);
3661 r.resultTo = null;
3662 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003663 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003664
3665 // Make sure this HistoryRecord is not holding on to other resources,
3666 // because clients have remote IPC references to this object so we
3667 // can't assume that will go away and want to avoid circular IPC refs.
3668 r.results = null;
3669 r.pendingResults = null;
3670 r.newIntents = null;
3671 r.icicle = null;
3672 }
3673
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003674 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003675 * See {@link #finishActivityLocked(ActivityRecord, int, Intent, String, boolean, boolean)}
3676 */
3677 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3678 String reason, boolean oomAdj) {
3679 return finishActivityLocked(r, resultCode, resultData, reason, oomAdj, !PAUSE_IMMEDIATELY);
3680 }
3681
3682 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003683 * @return Returns true if this activity has been removed from the history
3684 * list, or false if it is still in the list and will be removed later.
3685 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003686 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
Winson Chung6954fc92017-03-24 16:22:12 -07003687 String reason, boolean oomAdj, boolean pauseImmediately) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003688 if (r.finishing) {
3689 Slog.w(TAG, "Duplicate finish request for " + r);
3690 return false;
3691 }
3692
Jorim Jaggife762342016-10-13 14:33:27 +02003693 mWindowManager.deferSurfaceLayout();
3694 try {
3695 r.makeFinishingLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07003696 final TaskRecord task = r.getTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003697 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3698 r.userId, System.identityHashCode(r),
3699 task.taskId, r.shortComponentName, reason);
3700 final ArrayList<ActivityRecord> activities = task.mActivities;
3701 final int index = activities.indexOf(r);
3702 if (index < (activities.size() - 1)) {
3703 task.setFrontOfTask();
3704 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3705 // If the caller asked that this activity (and all above it)
3706 // be cleared when the task is reset, don't lose that information,
3707 // but propagate it up to the next activity.
3708 ActivityRecord next = activities.get(index+1);
3709 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3710 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003711 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003712
Jorim Jaggife762342016-10-13 14:33:27 +02003713 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003714
Wale Ogunwale66e16852017-10-19 13:35:52 -07003715 adjustFocusedActivityStack(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003716
Jorim Jaggife762342016-10-13 14:33:27 +02003717 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003718
Yorke Leebdef5372017-04-10 16:38:51 -07003719 final boolean endTask = index <= 0 && !task.isClearingToReuseTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003720 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
3721 if (mResumedActivity == r) {
3722 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
3723 "Prepare close transition: finishing " + r);
Winson Chung6954fc92017-03-24 16:22:12 -07003724 if (endTask) {
3725 mService.mTaskChangeNotificationController.notifyTaskRemovalStarted(
3726 task.taskId);
3727 }
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003728 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003729
Jorim Jaggife762342016-10-13 14:33:27 +02003730 // Tell window manager to prepare for this one to be removed.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003731 r.setVisibility(false);
Jorim Jaggife762342016-10-13 14:33:27 +02003732
3733 if (mPausingActivity == null) {
3734 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3735 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3736 "finish() => pause with userLeaving=false");
Winson Chung6954fc92017-03-24 16:22:12 -07003737 startPausingLocked(false, false, null, pauseImmediately);
Jorim Jaggife762342016-10-13 14:33:27 +02003738 }
3739
3740 if (endTask) {
Bryce Lee2b8e0372018-04-05 17:01:37 -07003741 mService.getLockTaskController().clearLockedTask(task);
Jorim Jaggife762342016-10-13 14:33:27 +02003742 }
Bryce Lee7ace3952018-02-16 14:34:32 -08003743 } else if (!r.isState(PAUSING)) {
Jorim Jaggife762342016-10-13 14:33:27 +02003744 // If the activity is PAUSING, we will complete the finish once
3745 // it is done pausing; else we can just directly finish it here.
3746 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
3747 if (r.visible) {
Winson Chung6954fc92017-03-24 16:22:12 -07003748 prepareActivityHideTransitionAnimation(r, transit);
3749 }
3750
3751 final int finishMode = (r.visible || r.nowVisible) ? FINISH_AFTER_VISIBLE
3752 : FINISH_AFTER_PAUSE;
Bryce Leef52974c2018-02-14 15:12:01 -08003753 final boolean removedActivity = finishCurrentActivityLocked(r, finishMode, oomAdj,
3754 "finishActivityLocked") == null;
Winson Chung6954fc92017-03-24 16:22:12 -07003755
3756 // The following code is an optimization. When the last non-task overlay activity
3757 // is removed from the task, we remove the entire task from the stack. However,
3758 // since that is done after the scheduled destroy callback from the activity, that
3759 // call to change the visibility of the task overlay activities would be out of
3760 // sync with the activitiy visibility being set for this finishing activity above.
3761 // In this case, we can set the visibility of all the task overlay activities when
3762 // we detect the last one is finishing to keep them in sync.
3763 if (task.onlyHasTaskOverlayActivities(true /* excludeFinishing */)) {
3764 for (ActivityRecord taskOverlay : task.mActivities) {
3765 if (!taskOverlay.mTaskOverlay) {
3766 continue;
3767 }
3768 prepareActivityHideTransitionAnimation(taskOverlay, transit);
Jorim Jaggife762342016-10-13 14:33:27 +02003769 }
3770 }
Winson Chung6954fc92017-03-24 16:22:12 -07003771 return removedActivity;
Jorim Jaggife762342016-10-13 14:33:27 +02003772 } else {
3773 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
3774 }
3775
3776 return false;
3777 } finally {
3778 mWindowManager.continueSurfaceLayout();
3779 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003780 }
3781
Winson Chung6954fc92017-03-24 16:22:12 -07003782 private void prepareActivityHideTransitionAnimation(ActivityRecord r, int transit) {
3783 mWindowManager.prepareAppTransition(transit, false);
3784 r.setVisibility(false);
3785 mWindowManager.executeAppTransition();
Bryce Lee4a194382017-04-04 14:32:48 -07003786 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(r)) {
3787 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(r);
Winson Chung6954fc92017-03-24 16:22:12 -07003788 }
3789 }
3790
Craig Mautnerf3333272013-04-22 10:55:53 -07003791 static final int FINISH_IMMEDIATELY = 0;
3792 static final int FINISH_AFTER_PAUSE = 1;
3793 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003794
Bryce Leef52974c2018-02-14 15:12:01 -08003795 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj,
3796 String reason) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003797 // First things first: if this activity is currently visible,
3798 // and the resumed activity is not yet visible, then hold off on
3799 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003800
3801 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
3802
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003803 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003804 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003805 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Winson Chung4dabf232017-01-25 13:25:22 -08003806 addToStopping(r, false /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003807 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003808 if (DEBUG_STATES) Slog.v(TAG_STATES,
3809 "Moving to STOPPING: "+ r + " (finish requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08003810 r.setState(STOPPING, "finishCurrentActivityLocked");
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003811 if (oomAdj) {
3812 mService.updateOomAdjLocked();
3813 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003814 return r;
3815 }
3816
3817 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003818 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003819 mStackSupervisor.mGoingToSleepActivities.remove(r);
Bryce Lee4a194382017-04-04 14:32:48 -07003820 mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(r);
Bryce Lee7ace3952018-02-16 14:34:32 -08003821 final ActivityState prevState = r.getState();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003822 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08003823
Bryce Lee7ace3952018-02-16 14:34:32 -08003824 r.setState(FINISHING, "finishCurrentActivityLocked");
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003825 final boolean finishingActivityInNonFocusedStack
Andrii Kulian02b7a832016-10-06 23:11:56 -07003826 = r.getStack() != mStackSupervisor.getFocusedStack()
Bryce Lee7ace3952018-02-16 14:34:32 -08003827 && prevState == PAUSED && mode == FINISH_AFTER_VISIBLE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003828
3829 if (mode == FINISH_IMMEDIATELY
Bryce Lee7ace3952018-02-16 14:34:32 -08003830 || (prevState == PAUSED
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003831 && (mode == FINISH_AFTER_PAUSE || inPinnedWindowingMode()))
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003832 || finishingActivityInNonFocusedStack
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003833 || prevState == STOPPING
3834 || prevState == STOPPED
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003835 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003836 r.makeFinishingLocked();
Bryce Leef52974c2018-02-14 15:12:01 -08003837 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm:" + reason);
Andrii Kulian7318d632016-07-20 18:59:28 -07003838
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003839 if (finishingActivityInNonFocusedStack) {
3840 // Finishing activity that was in paused state and it was in not currently focused
3841 // stack, need to make something visible in its place.
Andrii Kulian7318d632016-07-20 18:59:28 -07003842 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3843 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003844 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003845 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003846 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003847 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003848 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3849 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003850 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003851 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003852
3853 // Need to go through the full pause cycle to get this
3854 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003855 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003856 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003857 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003858 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003859 return r;
3860 }
3861
Craig Mautneree36c772014-07-16 14:56:05 -07003862 void finishAllActivitiesLocked(boolean immediately) {
3863 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003864 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3865 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3866 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3867 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003868 noActivitiesInStack = false;
3869 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003870 continue;
3871 }
Craig Mautneree36c772014-07-16 14:56:05 -07003872 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Bryce Leef52974c2018-02-14 15:12:01 -08003873 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false,
3874 "finishAllActivitiesLocked");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003875 }
3876 }
Craig Mautneree36c772014-07-16 14:56:05 -07003877 if (noActivitiesInStack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003878 remove();
Craig Mautneree36c772014-07-16 14:56:05 -07003879 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003880 }
3881
Wale Ogunwale66e16852017-10-19 13:35:52 -07003882 /** @return true if the stack behind this one is a standard activity type. */
3883 boolean inFrontOfStandardStack() {
3884 final ActivityDisplay display = getDisplay();
3885 if (display == null) {
3886 return false;
3887 }
3888 final int index = display.getIndexOf(this);
3889 if (index == 0) {
3890 return false;
3891 }
3892 final ActivityStack stackBehind = display.getChildAt(index - 1);
3893 return stackBehind.isActivityTypeStandard();
3894 }
3895
3896 boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003897 // Basic case: for simple app-centric recents, we need to recreate
3898 // the task if the affinity has changed.
Bryce Leeaf691c02017-03-20 14:20:22 -07003899 if (srec == null || srec.getTask().affinity == null ||
3900 !srec.getTask().affinity.equals(destAffinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003901 return true;
3902 }
3903 // Document-centric case: an app may be split in to multiple documents;
3904 // they need to re-create their task if this current activity is the root
3905 // of a document, unless simply finishing it will return them to the the
3906 // correct app behind.
Bryce Leeaf691c02017-03-20 14:20:22 -07003907 final TaskRecord task = srec.getTask();
Wale Ogunwale66e16852017-10-19 13:35:52 -07003908 if (srec.frontOfTask && task.getBaseIntent() != null && task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003909 // Okay, this activity is at the root of its task. What to do, what to do...
Wale Ogunwale66e16852017-10-19 13:35:52 -07003910 if (!inFrontOfStandardStack()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003911 // Finishing won't return to an application, so we need to recreate.
3912 return true;
3913 }
3914 // We now need to get the task below it to determine what to do.
Bryce Leeaf691c02017-03-20 14:20:22 -07003915 int taskIdx = mTaskHistory.indexOf(task);
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003916 if (taskIdx <= 0) {
3917 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3918 return false;
3919 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003920 final TaskRecord prevTask = mTaskHistory.get(taskIdx);
Bryce Leeaf691c02017-03-20 14:20:22 -07003921 if (!task.affinity.equals(prevTask.affinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003922 // These are different apps, so need to recreate.
3923 return true;
3924 }
3925 }
3926 return false;
3927 }
3928
Wale Ogunwale7d701172015-03-11 15:36:30 -07003929 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003930 Intent resultData) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003931 final TaskRecord task = srec.getTask();
Craig Mautner0247fc82013-02-28 14:32:06 -08003932 final ArrayList<ActivityRecord> activities = task.mActivities;
3933 final int start = activities.indexOf(srec);
3934 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003935 return false;
3936 }
3937 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003938 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003939 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003940 final ComponentName dest = destIntent.getComponent();
3941 if (start > 0 && dest != null) {
3942 for (int i = finishTo; i >= 0; i--) {
3943 ActivityRecord r = activities.get(i);
3944 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003945 r.info.name.equals(dest.getClassName())) {
3946 finishTo = i;
3947 parent = r;
3948 foundParentInTask = true;
3949 break;
3950 }
3951 }
3952 }
3953
3954 IActivityController controller = mService.mController;
3955 if (controller != null) {
3956 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3957 if (next != null) {
3958 // ask watcher if this is allowed
3959 boolean resumeOK = true;
3960 try {
3961 resumeOK = controller.activityResuming(next.packageName);
3962 } catch (RemoteException e) {
3963 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003964 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003965 }
3966
3967 if (!resumeOK) {
3968 return false;
3969 }
3970 }
3971 }
3972 final long origId = Binder.clearCallingIdentity();
3973 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003974 ActivityRecord r = activities.get(i);
3975 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003976 // Only return the supplied result for the first activity finished
3977 resultCode = Activity.RESULT_CANCELED;
3978 resultData = null;
3979 }
3980
3981 if (parent != null && foundParentInTask) {
3982 final int parentLaunchMode = parent.info.launchMode;
3983 final int destIntentFlags = destIntent.getFlags();
3984 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3985 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3986 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3987 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003988 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3989 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003990 } else {
3991 try {
3992 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -07003993 destIntent.getComponent(), ActivityManagerService.STOCK_PM_FLAGS,
3994 srec.userId);
Bryce Lee4c9a5972017-12-01 22:14:24 -08003995 // TODO(b/64750076): Check if calling pid should really be -1.
3996 final int res = mService.getActivityStartController()
3997 .obtainStarter(destIntent, "navigateUpTo")
3998 .setCaller(srec.app.thread)
3999 .setActivityInfo(aInfo)
4000 .setResultTo(parent.appToken)
4001 .setCallingPid(-1)
4002 .setCallingUid(parent.launchedFromUid)
4003 .setCallingPackage(parent.launchedFromPackage)
4004 .setRealCallingPid(-1)
4005 .setRealCallingUid(parent.launchedFromUid)
4006 .setComponentSpecified(true)
4007 .execute();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004008 foundParentInTask = res == ActivityManager.START_SUCCESS;
4009 } catch (RemoteException e) {
4010 foundParentInTask = false;
4011 }
4012 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08004013 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004014 }
4015 }
4016 Binder.restoreCallingIdentity(origId);
4017 return foundParentInTask;
4018 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004019
4020 /**
4021 * Remove any state associated with the {@link ActivityRecord}. This should be called whenever
4022 * an activity moves away from the stack.
4023 */
4024 void onActivityRemovedFromStack(ActivityRecord r) {
Bryce Lee84730a02018-04-03 14:10:04 -07004025 removeTimeoutsForActivityLocked(r);
4026
4027 if (mResumedActivity != null && mResumedActivity == r) {
4028 setResumedActivity(null, "onActivityRemovedFromStack");
Bryce Leeaf691c02017-03-20 14:20:22 -07004029 }
Bryce Lee84730a02018-04-03 14:10:04 -07004030 if (mPausingActivity != null && mPausingActivity == r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004031 mPausingActivity = null;
4032 }
Bryce Lee84730a02018-04-03 14:10:04 -07004033 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004034
Bryce Lee84730a02018-04-03 14:10:04 -07004035 void onActivityAddedToStack(ActivityRecord r) {
4036 if(r.getState() == RESUMED) {
4037 setResumedActivity(r, "onActivityAddedToStack");
4038 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004039 }
4040
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004041 /**
4042 * Perform the common clean-up of an activity record. This is called both
4043 * as part of destroyActivityLocked() (when destroying the client-side
4044 * representation) and cleaning things up as a result of its hosting
4045 * processing going away, in which case there is no remaining client-side
4046 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08004047 *
4048 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004049 */
Bryce Leec9406602018-03-09 11:02:10 -08004050 private void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices, boolean setState) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004051 onActivityRemovedFromStack(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004052
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004053 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004054 r.frozenBeforeDestroy = false;
4055
Bryce Leec9406602018-03-09 11:02:10 -08004056 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004057 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004058 r.setState(DESTROYED, "cleanupActivityLocked");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004059 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004060 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004061 }
4062
Bryce Lee4a194382017-04-04 14:32:48 -07004063 // Inform supervisor the activity has been removed.
4064 mStackSupervisor.cleanupActivity(r);
4065
Craig Mautner2420ead2013-04-01 17:13:20 -07004066
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004067 // Remove any pending results.
4068 if (r.finishing && r.pendingResults != null) {
4069 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
4070 PendingIntentRecord rec = apr.get();
4071 if (rec != null) {
4072 mService.cancelIntentSenderLocked(rec, false);
4073 }
4074 }
4075 r.pendingResults = null;
4076 }
4077
4078 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07004079 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004080 }
4081
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004082 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004083 removeTimeoutsForActivityLocked(r);
Wale Ogunwale8fd75422016-06-24 14:20:37 -07004084 // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
4085 // manager so it can update its bookkeeping.
4086 mWindowManager.notifyAppRelaunchesCleared(r.appToken);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004087 }
4088
Winson Chung4dabf232017-01-25 13:25:22 -08004089 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004090 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004091 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004092 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004093 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07004094 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004095 }
4096
Chong Zhangbffd8892016-08-08 11:16:06 -07004097 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004098 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07004099 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004100 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
4101 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
4102
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004103 r.takeFromHistory();
4104 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004105 if (DEBUG_STATES) Slog.v(TAG_STATES,
4106 "Moving to DESTROYED: " + r + " (removed from history)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004107 r.setState(DESTROYED, "removeActivityFromHistoryLocked");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004108 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004109 r.app = null;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08004110 r.removeWindowContainer();
Bryce Leeaf691c02017-03-20 14:20:22 -07004111 final TaskRecord task = r.getTask();
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004112 final boolean lastActivity = task != null ? task.removeActivity(r) : false;
Winson Chung6954fc92017-03-24 16:22:12 -07004113 // If we are removing the last activity in the task, not including task overlay activities,
4114 // then fall through into the block below to remove the entire task itself
4115 final boolean onlyHasTaskOverlays = task != null
4116 ? task.onlyHasTaskOverlayActivities(false /* excludingFinishing */) : false;
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004117
Winson Chung6954fc92017-03-24 16:22:12 -07004118 if (lastActivity || onlyHasTaskOverlays) {
4119 if (DEBUG_STACK) {
4120 Slog.i(TAG_STACK,
4121 "removeActivityFromHistoryLocked: last activity removed from " + this
4122 + " onlyHasTaskOverlays=" + onlyHasTaskOverlays);
4123 }
4124
Bryce Leed6d26752017-05-25 13:57:46 -07004125 // The following block can be executed multiple times if there is more than one overlay.
4126 // {@link ActivityStackSupervisor#removeTaskByIdLocked} handles this by reverse lookup
4127 // of the task by id and exiting early if not found.
Winson Chunge3c21e02017-04-11 18:02:17 -07004128 if (onlyHasTaskOverlays) {
4129 // When destroying a task, tell the supervisor to remove it so that any activity it
4130 // has can be cleaned up correctly. This is currently the only place where we remove
4131 // a task with the DESTROYING mode, so instead of passing the onlyHasTaskOverlays
4132 // state into removeTask(), we just clear the task here before the other residual
4133 // work.
4134 // TODO: If the callers to removeTask() changes such that we have multiple places
4135 // where we are destroying the task, move this back into removeTask()
4136 mStackSupervisor.removeTaskByIdLocked(task.taskId, false /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07004137 !REMOVE_FROM_RECENTS, PAUSE_IMMEDIATELY, reason);
Winson Chunge3c21e02017-04-11 18:02:17 -07004138 }
Bryce Lee1d930582017-05-01 08:35:24 -07004139
Bryce Leed6d26752017-05-25 13:57:46 -07004140 // We must keep the task around until all activities are destroyed. The following
4141 // statement will only execute once since overlays are also considered activities.
Bryce Lee1d930582017-05-01 08:35:24 -07004142 if (lastActivity) {
4143 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
4144 }
Craig Mautner312ba862014-02-10 17:55:01 -08004145 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004146 cleanUpActivityServicesLocked(r);
4147 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004148 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004149
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004150 /**
4151 * Perform clean-up of service connections in an activity record.
4152 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07004153 private void cleanUpActivityServicesLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004154 // Throw away any services that have been bound by this activity.
4155 if (r.connections != null) {
4156 Iterator<ConnectionRecord> it = r.connections.iterator();
4157 while (it.hasNext()) {
4158 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004159 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004160 }
4161 r.connections = null;
4162 }
4163 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004164
Craig Mautneree2e45a2014-06-27 12:10:03 -07004165 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004166 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004167 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004168 mHandler.sendMessage(msg);
4169 }
4170
Andrii Kulian21713ac2016-10-12 22:05:05 -07004171 private void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004172 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004173 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004174 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4175 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4176 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4177 final ActivityRecord r = activities.get(activityNdx);
4178 if (r.finishing) {
4179 continue;
4180 }
4181 if (r.fullscreen) {
4182 lastIsOpaque = true;
4183 }
4184 if (owner != null && r.app != owner) {
4185 continue;
4186 }
4187 if (!lastIsOpaque) {
4188 continue;
4189 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004190 if (r.isDestroyable()) {
Bryce Lee7ace3952018-02-16 14:34:32 -08004191 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r
4192 + " in state " + r.getState()
Craig Mautnerd44711d2013-02-23 11:24:36 -08004193 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004194 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004195 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004196 activityRemoved = true;
4197 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004198 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004199 }
4200 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004201 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004202 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004203 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004204 }
4205
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004206 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
4207 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004208 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Bryce Lee7ace3952018-02-16 14:34:32 -08004209 "Destroying " + r + " in state " + r.getState() + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004210 + " pausing=" + mPausingActivity + " for reason " + reason);
4211 return destroyActivityLocked(r, true, reason);
4212 }
4213 return false;
4214 }
4215
4216 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
4217 String reason) {
4218 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004219 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004220 int maxTasks = tasks.size() / 4;
4221 if (maxTasks < 1) {
4222 maxTasks = 1;
4223 }
4224 int numReleased = 0;
4225 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
4226 final TaskRecord task = mTaskHistory.get(taskNdx);
4227 if (!tasks.contains(task)) {
4228 continue;
4229 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004230 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004231 int curNum = 0;
4232 final ArrayList<ActivityRecord> activities = task.mActivities;
4233 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
4234 final ActivityRecord activity = activities.get(actNdx);
4235 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004236 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Bryce Lee7ace3952018-02-16 14:34:32 -08004237 + " in state " + activity.getState() + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004238 + " pausing=" + mPausingActivity + " for reason " + reason);
4239 destroyActivityLocked(activity, true, reason);
4240 if (activities.get(actNdx) != activity) {
4241 // Was removed from list, back up so we don't miss the next one.
4242 actNdx--;
4243 }
4244 curNum++;
4245 }
4246 }
4247 if (curNum > 0) {
4248 numReleased += curNum;
4249 maxTasks--;
4250 if (mTaskHistory.get(taskNdx) != task) {
4251 // The entire task got removed, back up so we don't miss the next one.
4252 taskNdx--;
4253 }
4254 }
4255 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004256 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
4257 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004258 return numReleased;
4259 }
4260
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004261 /**
4262 * Destroy the current CLIENT SIDE instance of an activity. This may be
4263 * called both when actually finishing an activity, or when performing
4264 * a configuration switch where we destroy the current client-side object
4265 * but then create a new client-side object for this same HistoryRecord.
4266 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07004267 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004268 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
4269 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004270 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Bryce Leeb0f993f2018-03-02 15:38:01 -08004271
4272 if (r.isState(DESTROYING, DESTROYED)) {
Bryce Leec9406602018-03-09 11:02:10 -08004273 if (DEBUG_STATES) Slog.v(TAG_STATES, "activity " + r + " already destroying."
Bryce Leeb0f993f2018-03-02 15:38:01 -08004274 + "skipping request with reason:" + reason);
4275 return false;
4276 }
4277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004278 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004279 r.userId, System.identityHashCode(r),
Bryce Leeaf691c02017-03-20 14:20:22 -07004280 r.getTask().taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004281
4282 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004283
Bryce Leec9406602018-03-09 11:02:10 -08004284 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004285
4286 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004287
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004288 if (hadApp) {
4289 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004290 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004291 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
4292 mService.mHeavyWeightProcess = null;
4293 mService.mHandler.sendEmptyMessage(
4294 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
4295 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004296 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07004297 // Update any services we are bound to that might care about whether
4298 // their client may have activities.
4299 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07004300 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07004301 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07004302 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004303 }
4304 }
4305
4306 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004307
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004308 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004309 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08004310 mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken,
Bryce Leed946f862018-01-16 15:59:47 -08004311 DestroyActivityItem.obtain(r.finishing, r.configChangeFlags)
Bryce Leea33c13d2018-02-08 14:37:06 -08004312 .setDescription(
4313 r.getLifecycleDescription("destroyActivityLocked:" + reason)));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004314 } catch (Exception e) {
4315 // We can just ignore exceptions here... if the process
4316 // has crashed, our death notification will clean things
4317 // up.
4318 //Slog.w(TAG, "Exception thrown during finish", e);
4319 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004320 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004321 removedFromHistory = true;
4322 skipDestroy = true;
4323 }
4324 }
4325
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004326 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004327
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004328 // If the activity is finishing, we need to wait on removing it
4329 // from the list to give it a chance to do its cleanup. During
4330 // that time it may make calls back with its token so we need to
4331 // be able to find it on the list and so we don't want to remove
4332 // it from the list yet. Otherwise, we can just immediately put
4333 // it in the destroyed state since we are not removing it from the
4334 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004335 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004336 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004337 + " (destroy requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004338 r.setState(DESTROYING,
4339 "destroyActivityLocked. finishing and not skipping destroy");
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07004340 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004341 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4342 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004343 if (DEBUG_STATES) Slog.v(TAG_STATES,
4344 "Moving to DESTROYED: " + r + " (destroy skipped)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004345 r.setState(DESTROYED,
4346 "destroyActivityLocked. not finishing or skipping destroy");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004347 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004348 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004349 }
4350 } else {
4351 // remove this record from the history.
4352 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004353 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004354 removedFromHistory = true;
4355 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004356 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004357 r.setState(DESTROYED, "destroyActivityLocked. not finishing and had no app");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004358 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004359 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004360 }
4361 }
4362
4363 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004365 if (!mLRUActivities.remove(r) && hadApp) {
4366 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4367 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004368
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004369 return removedFromHistory;
4370 }
4371
Craig Mautner299f9602015-01-26 09:47:33 -08004372 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004373 final long origId = Binder.clearCallingIdentity();
4374 try {
Bryce Lee7ace3952018-02-16 14:34:32 -08004375 activityDestroyedLocked(ActivityRecord.forTokenLocked(token), reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004376 } finally {
4377 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004378 }
4379 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004380
Bryce Leec9406602018-03-09 11:02:10 -08004381 /**
4382 * This method is to only be called from the client via binder when the activity is destroyed
4383 * AND finished.
4384 */
Bryce Lee7ace3952018-02-16 14:34:32 -08004385 final void activityDestroyedLocked(ActivityRecord record, String reason) {
4386 if (record != null) {
4387 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, record);
4388 }
4389
4390 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + record);
4391
4392 if (isInStackLocked(record) != null) {
4393 if (record.isState(DESTROYING, DESTROYED)) {
Bryce Leec9406602018-03-09 11:02:10 -08004394 cleanUpActivityLocked(record, true, false);
Bryce Lee7ace3952018-02-16 14:34:32 -08004395 removeActivityFromHistoryLocked(record, reason);
4396 }
4397 }
4398
4399 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4400 }
4401
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004402 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4403 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004404 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004405 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4406 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004407 while (i > 0) {
4408 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004409 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004410 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004411 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004412 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004413 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004414 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004415 }
4416 }
4417 }
4418
Andrii Kulian21713ac2016-10-12 22:05:05 -07004419 private boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004420 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004421 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4422 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004423 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4424 "mGoingToSleepActivities");
Bryce Lee4a194382017-04-04 14:32:48 -07004425 removeHistoryRecordsForAppLocked(mStackSupervisor.mActivitiesWaitingForVisibleActivity, app,
4426 "mActivitiesWaitingForVisibleActivity");
Craig Mautnerf3333272013-04-22 10:55:53 -07004427 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4428 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004429
4430 boolean hasVisibleActivities = false;
4431
4432 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004433 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004434 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4435 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004436 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4437 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Bryce Leefbd263b42018-03-07 10:33:55 -08004438 mTmpActivities.clear();
4439 mTmpActivities.addAll(activities);
4440
4441 while (!mTmpActivities.isEmpty()) {
4442 final int targetIndex = mTmpActivities.size() - 1;
4443 final ActivityRecord r = mTmpActivities.remove(targetIndex);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004444 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
Bryce Leefbd263b42018-03-07 10:33:55 -08004445 "Record #" + targetIndex + " " + r + ": app=" + r.app);
4446
Craig Mautner0247fc82013-02-28 14:32:06 -08004447 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004448 if (r.visible) {
4449 hasVisibleActivities = true;
4450 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004451 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004452 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4453 // Don't currently have state for the activity, or
4454 // it is finishing -- always remove it.
4455 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004456 } else if (!r.visible && r.launchCount > 2 &&
4457 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004458 // We have launched this activity too many times since it was
4459 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004460 // (Note if the activity is visible, we don't remove the record.
4461 // We leave the dead window on the screen but the process will
4462 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004463 remove = true;
4464 } else {
4465 // The process may be gone, but the activity lives on!
4466 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004467 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004468 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004469 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4470 "Removing activity " + r + " from stack at " + i
4471 + ": haveState=" + r.haveState
4472 + " stateNotNeeded=" + r.stateNotNeeded
4473 + " finishing=" + r.finishing
Bryce Lee7ace3952018-02-16 14:34:32 -08004474 + " state=" + r.getState() + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004475 if (!r.finishing) {
4476 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4477 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4478 r.userId, System.identityHashCode(r),
Bryce Leeaf691c02017-03-20 14:20:22 -07004479 r.getTask().taskId, r.shortComponentName,
Craig Mautner0247fc82013-02-28 14:32:06 -08004480 "proc died without state saved");
Bryce Lee7ace3952018-02-16 14:34:32 -08004481 if (r.getState() == RESUMED) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07004482 mService.updateUsageStats(r, false);
4483 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004484 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004485 } else {
4486 // We have the current state for this activity, so
4487 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004488 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4489 if (DEBUG_APP) Slog.v(TAG_APP,
4490 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004491 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004492 // Set nowVisible to previous visible state. If the app was visible while
4493 // it died, we leave the dead window on screen so it's basically visible.
4494 // This is needed when user later tap on the dead window, we need to stop
4495 // other apps when user transfers focus to the restarted activity.
4496 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004497 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004498 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004499 "App died, clearing saved state of " + r);
4500 r.icicle = null;
4501 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004502 }
Bryce Leec9406602018-03-09 11:02:10 -08004503 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004504 if (remove) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004505 removeActivityFromHistoryLocked(r, "appDied");
Craig Mautneracebdc82015-02-24 10:53:03 -08004506 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004507 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004508 }
4509 }
4510
4511 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004512 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004513
chaviw0d562bf2018-03-15 14:24:14 -07004514 private void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004515 if (options != null) {
chaviw0d562bf2018-03-15 14:24:14 -07004516 ActivityRecord r = topRunningActivityLocked();
4517 if (r != null && !r.isState(RESUMED)) {
4518 r.updateOptionsLocked(options);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004519 } else {
4520 ActivityOptions.abort(options);
4521 }
4522 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004523 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004524 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004525
Andrii Kulian21713ac2016-10-12 22:05:05 -07004526 private void updateTaskMovement(TaskRecord task, boolean toFront) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004527 if (task.isPersistable) {
4528 task.mLastTimeMoved = System.currentTimeMillis();
4529 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4530 // recently will be most negative, tasks sent to the bottom before that will be less
4531 // negative. Similarly for recent tasks moved to the top which will be most positive.
4532 if (!toFront) {
4533 task.mLastTimeMoved *= -1;
4534 }
4535 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004536 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004537 }
4538
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004539 void moveHomeStackTaskToTop() {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004540 if (!isActivityTypeHome()) {
4541 throw new IllegalStateException("Calling moveHomeStackTaskToTop() on non-home stack: "
4542 + this);
4543 }
Craig Mautnera82aa092013-09-13 15:34:08 -07004544 final int top = mTaskHistory.size() - 1;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004545 if (top >= 0) {
4546 final TaskRecord task = mTaskHistory.get(top);
4547 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4548 "moveHomeStackTaskToTop: moving " + task);
4549 mTaskHistory.remove(top);
4550 mTaskHistory.add(top, task);
4551 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004552 }
4553 }
4554
chaviw0d562bf2018-03-15 14:24:14 -07004555 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
4556 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004557 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004558
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004559 final ActivityStack topStack = getDisplay().getTopStack();
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004560 final ActivityRecord topActivity = topStack != null ? topStack.getTopActivity() : null;
Craig Mautner11bf9a52013-02-19 14:08:51 -08004561 final int numTasks = mTaskHistory.size();
4562 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004563 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004564 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004565 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004566 ActivityOptions.abort(options);
4567 } else {
chaviw0d562bf2018-03-15 14:24:14 -07004568 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004569 }
4570 return;
4571 }
4572
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004573 if (timeTracker != null) {
4574 // The caller wants a time tracker associated with this task.
4575 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4576 tr.mActivities.get(i).appTimeTracker = timeTracker;
4577 }
4578 }
4579
Craig Mautner11bf9a52013-02-19 14:08:51 -08004580 // Shift all activities with this task up to the top
4581 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004582 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004583
Chong Zhang45c25ce2015-08-10 22:18:26 -07004584 // Don't refocus if invisible to current user
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004585 final ActivityRecord top = tr.getTopActivity();
Chong Zhang87761972016-08-22 13:53:24 -07004586 if (top == null || !top.okToShowLocked()) {
Winson Chung1dbc8112017-09-28 18:05:31 -07004587 if (top != null) {
4588 mStackSupervisor.mRecentTasks.add(top.getTask());
4589 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07004590 ActivityOptions.abort(options);
4591 return;
4592 }
4593
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004594 // Set focus to the top running activity of this stack.
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004595 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang6cda19c2016-06-14 19:07:56 -07004596 mStackSupervisor.moveFocusableActivityStackToFrontLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004597
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004598 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004599 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004600 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004601 if (r != null) {
Jorim Jaggifa9ed962018-01-25 00:16:49 +01004602 mStackSupervisor.mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004603 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004604 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004605 } else {
chaviw0d562bf2018-03-15 14:24:14 -07004606 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004607 }
Winson Chungc2baac02017-01-11 13:34:47 -08004608 // If a new task is moved to the front, then mark the existing top activity as supporting
Winson Chung942a85c2017-07-11 15:07:45 -07004609 // picture-in-picture while paused only if the task would not be considered an oerlay on top
4610 // of the current activity (eg. not fullscreen, or the assistant)
Winson Chungf7e03e12017-08-22 11:32:16 -07004611 if (canEnterPipOnTaskSwitch(topActivity, tr, null /* toFrontActivity */,
4612 options)) {
4613 topActivity.supportsEnterPipOnTaskSwitch = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08004614 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004615
Wale Ogunwaled046a012015-12-24 13:05:59 -08004616 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004617 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Yorke Leebd54c2a2016-10-25 13:49:23 -07004618
4619 mService.mTaskChangeNotificationController.notifyTaskMovedToFront(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004620 }
4621
4622 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004623 * Worker method for rearranging history stack. Implements the function of moving all
4624 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004625 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004626 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004627 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4628 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004629 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004630 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004631 * @return Returns true if the move completed, false if not.
4632 */
Craig Mautner299f9602015-01-26 09:47:33 -08004633 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004634 final TaskRecord tr = taskForIdLocked(taskId);
4635 if (tr == null) {
4636 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4637 return false;
4638 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004639 Slog.i(TAG, "moveTaskToBack: " + tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004640
Charles Heff9b4dff2017-09-22 10:18:37 +01004641 // In LockTask mode, moving a locked task to the back of the stack may expose unlocked
4642 // ones. Therefore we need to check if this operation is allowed.
Bryce Lee2b8e0372018-04-05 17:01:37 -07004643 if (!mService.getLockTaskController().canMoveTaskToBack(tr)) {
Winson Chung261c0f32017-03-07 16:54:31 -08004644 return false;
4645 }
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004646
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004647 // If we have a watcher, preflight the move before committing to it. First check
4648 // for *other* available tasks, but if none are available, then try again allowing the
4649 // current task to be selected.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004650 if (isTopStackOnDisplay() && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004651 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004652 if (next == null) {
4653 next = topRunningActivityLocked(null, 0);
4654 }
4655 if (next != null) {
4656 // ask watcher if this is allowed
4657 boolean moveOK = true;
4658 try {
4659 moveOK = mService.mController.activityResuming(next.packageName);
4660 } catch (RemoteException e) {
4661 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004662 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004663 }
4664 if (!moveOK) {
4665 return false;
4666 }
4667 }
4668 }
4669
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004670 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004671
Wale Ogunwale66e16852017-10-19 13:35:52 -07004672 mTaskHistory.remove(tr);
4673 mTaskHistory.add(0, tr);
4674 updateTaskMovement(tr, false);
Wale Ogunwale42709242015-08-11 13:54:42 -07004675
Wale Ogunwale66e16852017-10-19 13:35:52 -07004676 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Evan Rosky9c448172017-11-02 14:19:27 -07004677 moveToBack("moveTaskToBackLocked", tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004678
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004679 if (inPinnedWindowingMode()) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004680 mStackSupervisor.removeStack(this);
Winson Chung261c0f32017-03-07 16:54:31 -08004681 return true;
4682 }
4683
Wale Ogunwaled046a012015-12-24 13:05:59 -08004684 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004685 return true;
4686 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004687
Andrii Kulian21713ac2016-10-12 22:05:05 -07004688 static void logStartActivity(int tag, ActivityRecord r, TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004689 final Uri data = r.intent.getData();
4690 final String strData = data != null ? data.toSafeString() : null;
4691
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004692 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004693 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004694 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004695 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004696 }
4697
4698 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004699 * Ensures all visible activities at or below the input activity have the right configuration.
4700 */
4701 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4702 if (start == null || !start.visible) {
4703 return;
4704 }
4705
Bryce Leeaf691c02017-03-20 14:20:22 -07004706 final TaskRecord startTask = start.getTask();
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004707 boolean behindFullscreen = false;
4708 boolean updatedConfig = false;
4709
4710 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4711 final TaskRecord task = mTaskHistory.get(taskIndex);
4712 final ArrayList<ActivityRecord> activities = task.mActivities;
4713 int activityIndex =
Bryce Leeaf691c02017-03-20 14:20:22 -07004714 (start.getTask() == task) ? activities.indexOf(start) : activities.size() - 1;
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004715 for (; activityIndex >= 0; --activityIndex) {
4716 final ActivityRecord r = activities.get(activityIndex);
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08004717 updatedConfig |= r.ensureActivityConfiguration(0 /* globalChanges */,
Andrii Kulian21713ac2016-10-12 22:05:05 -07004718 preserveWindow);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004719 if (r.fullscreen) {
4720 behindFullscreen = true;
4721 break;
4722 }
4723 }
4724 if (behindFullscreen) {
4725 break;
4726 }
4727 }
4728 if (updatedConfig) {
Wale Ogunwale089586f2016-06-20 14:16:22 -07004729 // Ensure the resumed state of the focus activity if we updated the configuration of
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004730 // any activity.
4731 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4732 }
4733 }
4734
Wale Ogunwale1666e312016-12-16 11:27:18 -08004735 // TODO: Figure-out a way to consolidate with resize() method below.
4736 @Override
4737 public void requestResize(Rect bounds) {
4738 mService.resizeStack(mStackId, bounds, true /* allowResizeInDockedMode */,
4739 false /* preserveWindows */, false /* animate */, -1 /* animationDuration */);
4740 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07004741
Wale Ogunwale1666e312016-12-16 11:27:18 -08004742 // TODO: Can only be called from special methods in ActivityStackSupervisor.
4743 // Need to consolidate those calls points into this resize method so anyone can call directly.
4744 void resize(Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004745 if (!updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
4746 return;
4747 }
4748
4749 // Update override configurations of all tasks in the stack.
4750 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
Andrii Kulian1e32e022016-09-16 15:29:34 -07004751 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
4752
4753 mTmpBounds.clear();
Andrii Kulian1e32e022016-09-16 15:29:34 -07004754 mTmpInsetBounds.clear();
4755
chaviwbe43ac82018-04-04 15:14:49 -07004756 synchronized (mWindowManager.getWindowManagerLock()) {
4757 for (int i = mTaskHistory.size() - 1; i >= 0; i--) {
4758 final TaskRecord task = mTaskHistory.get(i);
4759 if (task.isResizeable()) {
4760 if (inFreeformWindowingMode()) {
4761 // TODO: Can be removed now since each freeform task is in its own stack.
4762 // For freeform stack we don't adjust the size of the tasks to match that
4763 // of the stack, but we do try to make sure the tasks are still contained
4764 // with the bounds of the stack.
4765 mTmpRect2.set(task.getOverrideBounds());
4766 fitWithinBounds(mTmpRect2, bounds);
4767 task.updateOverrideConfiguration(mTmpRect2);
4768 } else {
4769 task.updateOverrideConfiguration(taskBounds, insetBounds);
4770 }
4771 }
4772
4773 mTmpBounds.put(task.taskId, task.getOverrideBounds());
4774 if (tempTaskInsetBounds != null) {
4775 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004776 }
4777 }
4778
chaviwbe43ac82018-04-04 15:14:49 -07004779 mWindowContainerController.resize(bounds, mTmpBounds, mTmpInsetBounds);
4780 setBounds(bounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004781 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07004782 }
4783
4784
4785 /**
4786 * Adjust bounds to stay within stack bounds.
4787 *
4788 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4789 * that keep them unchanged, but be contained within the stack bounds.
4790 *
4791 * @param bounds Bounds to be adjusted.
4792 * @param stackBounds Bounds within which the other bounds should remain.
4793 */
4794 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08004795 if (stackBounds == null || stackBounds.isEmpty() || stackBounds.contains(bounds)) {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004796 return;
4797 }
4798
4799 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4800 final int maxRight = stackBounds.right
4801 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4802 int horizontalDiff = stackBounds.left - bounds.left;
4803 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4804 || (bounds.left + horizontalDiff >= maxRight)) {
4805 horizontalDiff = maxRight - bounds.left;
4806 }
4807 bounds.left += horizontalDiff;
4808 bounds.right += horizontalDiff;
4809 }
4810
4811 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4812 final int maxBottom = stackBounds.bottom
4813 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4814 int verticalDiff = stackBounds.top - bounds.top;
4815 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4816 || (bounds.top + verticalDiff >= maxBottom)) {
4817 verticalDiff = maxBottom - bounds.top;
4818 }
4819 bounds.top += verticalDiff;
4820 bounds.bottom += verticalDiff;
4821 }
4822 }
4823
Craig Mautnercae015f2013-02-08 14:31:27 -08004824 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004825 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4826 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4827 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4828 final ActivityRecord r = activities.get(activityNdx);
4829 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004830 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004831 }
4832 if (r.fullscreen && !r.finishing) {
4833 return false;
4834 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004835 }
4836 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004837 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004838 if (r == null) {
4839 return false;
4840 }
4841 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4842 + " would have returned true for r=" + r);
4843 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004844 }
4845
4846 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004847 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4848 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4849 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4850 final ActivityRecord r = activities.get(activityNdx);
4851 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004852 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004853 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004854 }
4855 }
4856 }
4857
Wale Ogunwale540e1232015-05-01 15:35:39 -07004858 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4859 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004860 boolean didSomething = false;
4861 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004862 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004863 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4864 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Bryce Leefbd263b42018-03-07 10:33:55 -08004865 mTmpActivities.clear();
4866 mTmpActivities.addAll(activities);
4867
4868 while (!mTmpActivities.isEmpty()) {
4869 ActivityRecord r = mTmpActivities.remove(0);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004870 final boolean sameComponent =
4871 (r.packageName.equals(packageName) && (filterByClasses == null
4872 || filterByClasses.contains(r.realActivity.getClassName())))
4873 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004874 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Bryce Leeaf691c02017-03-20 14:20:22 -07004875 && (sameComponent || r.getTask() == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004876 && (r.app == null || evenPersistent || !r.app.persistent)) {
4877 if (!doit) {
4878 if (r.finishing) {
4879 // If this activity is just finishing, then it is not
4880 // interesting as far as something to stop.
4881 continue;
4882 }
4883 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004884 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004885 if (r.isActivityTypeHome()) {
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004886 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4887 Slog.i(TAG, "Skip force-stop again " + r);
4888 continue;
4889 } else {
4890 homeActivity = r.realActivity;
4891 }
4892 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004893 didSomething = true;
4894 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004895 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004896 if (r.app != null) {
4897 r.app.removed = true;
4898 }
4899 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004900 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004901 lastTask = r.getTask();
Bryce Leefbd263b42018-03-07 10:33:55 -08004902 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4903 true);
Craig Mautnercae015f2013-02-08 14:31:27 -08004904 }
4905 }
4906 }
4907 return didSomething;
4908 }
4909
Winson Chung61c9e5a2017-10-11 10:39:32 -07004910 /**
4911 * @return The set of running tasks through {@param tasksOut} that are available to the caller.
4912 * If {@param ignoreActivityType} or {@param ignoreWindowingMode} are not undefined,
4913 * then skip running tasks that match those types.
4914 */
4915 void getRunningTasks(List<TaskRecord> tasksOut, @ActivityType int ignoreActivityType,
4916 @WindowingMode int ignoreWindowingMode, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004917 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4918 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004919 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004920 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004921 if (task.getTopActivity() == null) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004922 // Skip if there are no activities in the task
riddle_hsuddc74152015-04-07 11:30:09 +08004923 continue;
4924 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004925 if (!allowed && !task.isActivityTypeHome() && task.effectiveUid != callingUid) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004926 // Skip if the caller can't fetch this task
Dianne Hackborn09233282014-04-30 11:33:59 -07004927 continue;
4928 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004929 if (ignoreActivityType != ACTIVITY_TYPE_UNDEFINED
4930 && task.getActivityType() == ignoreActivityType) {
4931 // Skip ignored activity type
4932 continue;
Craig Mautneraab647e2013-02-28 16:31:36 -08004933 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004934 if (ignoreWindowingMode != WINDOWING_MODE_UNDEFINED
4935 && task.getWindowingMode() == ignoreWindowingMode) {
4936 // Skip ignored windowing mode
4937 continue;
4938 }
riddle_hsuddc74152015-04-07 11:30:09 +08004939 if (focusedStack && topTask) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07004940 // For the focused stack top task, update the last stack active time so that it can
4941 // be used to determine the order of the tasks (it may not be set for newly created
4942 // tasks)
4943 task.lastActiveTime = SystemClock.elapsedRealtime();
riddle_hsuddc74152015-04-07 11:30:09 +08004944 topTask = false;
4945 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07004946 tasksOut.add(task);
Craig Mautnercae015f2013-02-08 14:31:27 -08004947 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004948 }
4949
Andrii Kulian21713ac2016-10-12 22:05:05 -07004950 void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004951 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004952 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004953 if (top >= 0) {
4954 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4955 int activityTop = activities.size() - 1;
Dianne Hackborn354736e2016-08-22 17:00:05 -07004956 if (activityTop >= 0) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004957 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4958 "unhandled-back", true);
4959 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004960 }
4961 }
4962
Craig Mautner6b74cb52013-09-27 17:02:21 -07004963 /**
4964 * Reset local parameters because an app's activity died.
4965 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004966 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004967 */
4968 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004969 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004970 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004971 "App died while pausing: " + mPausingActivity);
4972 mPausingActivity = null;
4973 }
4974 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4975 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004976 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004977 }
4978
Craig Mautner19091252013-10-05 00:03:53 -07004979 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004980 }
4981
Craig Mautnercae015f2013-02-08 14:31:27 -08004982 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004983 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4984 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4985 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4986 final ActivityRecord r = activities.get(activityNdx);
4987 if (r.app == app) {
4988 Slog.w(TAG, " Force finishing activity "
4989 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004990 // Force the destroy to skip right to removal.
4991 r.app = null;
Bryce Leef52974c2018-02-14 15:12:01 -08004992 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false,
4993 "handleAppCrashedLocked");
Craig Mautnerd44711d2013-02-23 11:24:36 -08004994 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004995 }
4996 }
4997 }
4998
Dianne Hackborn390517b2013-05-30 15:03:32 -07004999 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005000 boolean dumpClient, String dumpPackage, boolean needSep) {
5001
5002 if (mTaskHistory.isEmpty()) {
5003 return false;
5004 }
5005 final String prefix = " ";
Craig Mautneraab647e2013-02-28 16:31:36 -08005006 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5007 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005008 if (needSep) {
5009 pw.println("");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005010 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005011 pw.println(prefix + "Task id #" + task.taskId);
Bryce Leef3c6a472017-11-14 14:53:06 -08005012 pw.println(prefix + "mBounds=" + task.getOverrideBounds());
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005013 pw.println(prefix + "mMinWidth=" + task.mMinWidth);
5014 pw.println(prefix + "mMinHeight=" + task.mMinHeight);
5015 pw.println(prefix + "mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
5016 pw.println(prefix + "* " + task);
5017 task.dump(pw, prefix + " ");
5018 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
5019 prefix, "Hist", true, !dumpAll, dumpClient, dumpPackage, false, null, task);
Craig Mautneraab647e2013-02-28 16:31:36 -08005020 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005021 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08005022 }
5023
5024 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Winson Chung6998bc42017-02-28 17:07:05 -08005025 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautnercae015f2013-02-08 14:31:27 -08005026
5027 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005028 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5029 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08005030 }
5031 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005032 final int top = mTaskHistory.size() - 1;
5033 if (top >= 0) {
5034 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
5035 int listTop = list.size() - 1;
5036 if (listTop >= 0) {
5037 activities.add(list.get(listTop));
5038 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005039 }
5040 } else {
5041 ItemMatcher matcher = new ItemMatcher();
5042 matcher.build(name);
5043
Craig Mautneraab647e2013-02-28 16:31:36 -08005044 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5045 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
5046 if (matcher.match(r1, r1.intent.getComponent())) {
5047 activities.add(r1);
5048 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005049 }
5050 }
5051 }
5052
5053 return activities;
5054 }
5055
5056 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07005057 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08005058
5059 // All activities that came from the package must be
5060 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08005061 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5062 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5063 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5064 final ActivityRecord a = activities.get(activityNdx);
5065 if (a.info.packageName.equals(packageName)) {
5066 a.forceNewConfig = true;
5067 if (starting != null && a == starting && a.visible) {
5068 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08005069 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08005070 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005071 }
5072 }
5073 }
5074
5075 return starting;
5076 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005077
Wale Ogunwale000957c2015-04-03 08:19:12 -07005078 /**
5079 * Removes the input task from this stack.
5080 * @param task to remove.
5081 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07005082 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
5083 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07005084 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07005085 void removeTask(TaskRecord task, String reason, int mode) {
Bryce Leeaf691c02017-03-20 14:20:22 -07005086 for (ActivityRecord record : task.mActivities) {
5087 onActivityRemovedFromStack(record);
Craig Mautner04a0ea62014-01-13 12:51:26 -08005088 }
5089
Bryce Lee2b8e0372018-04-05 17:01:37 -07005090 final boolean removed = mTaskHistory.remove(task);
5091
5092 if (removed) {
5093 EventLog.writeEvent(EventLogTags.AM_REMOVE_TASK, task.taskId, getStackId());
5094 }
5095
Winson Chungabb433b2017-03-24 09:35:42 -07005096 removeActivitiesFromLRUListLocked(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005097 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07005098
Wale Ogunwale06579d62016-04-30 15:29:06 -07005099 if (mode == REMOVE_TASK_MODE_DESTROYING && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005100 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07005101 final boolean isVoiceSession = task.voiceSession != null;
5102 if (isVoiceSession) {
5103 try {
5104 task.voiceSession.taskFinished(task.intent, task.taskId);
5105 } catch (RemoteException e) {
5106 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07005107 }
Craig Mautner41db4a72014-05-07 17:20:56 -07005108 if (task.autoRemoveFromRecents() || isVoiceSession) {
5109 // Task creator asked to remove this when done, or this task was a voice
5110 // interaction, so it should not remain on the recent tasks list.
Winson Chung1dbc8112017-09-28 18:05:31 -07005111 mStackSupervisor.mRecentTasks.remove(task);
Craig Mautner41db4a72014-05-07 17:20:56 -07005112 }
Bryce Lee840c5662017-04-13 10:02:51 -07005113
5114 task.removeWindowContainer();
Dianne Hackborn91097de2014-04-04 18:02:06 -07005115 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005116
5117 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005118 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07005119 // We only need to adjust focused stack if this stack is in focus and we are not in the
5120 // process of moving the task to the top of the stack that will be focused.
5121 if (isOnHomeDisplay() && mode != REMOVE_TASK_MODE_MOVING_TO_TOP
5122 && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005123 String myReason = reason + " leftTaskHistoryEmpty";
Bryce Leef3c6a472017-11-14 14:53:06 -08005124 if (!inMultiWindowMode() || !adjustFocusToNextFocusableStack(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005125 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005126 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005127 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07005128 if (isAttached()) {
5129 getDisplay().positionChildAtBottom(this);
Craig Mautner593a4e62014-01-15 17:55:51 -08005130 }
Wale Ogunwale388945c2017-10-04 12:13:46 -07005131 if (!isActivityTypeHome()) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07005132 remove();
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005133 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005134 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005135
Andrii Kulian02b7a832016-10-06 23:11:56 -07005136 task.setStack(null);
Winson Chungc81c0ce2017-03-17 12:27:30 -07005137
5138 // Notify if a task from the pinned stack is being removed (or moved depending on the mode)
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005139 if (inPinnedWindowingMode()) {
Winson Chungc81c0ce2017-03-17 12:27:30 -07005140 mService.mTaskChangeNotificationController.notifyActivityUnpinned();
5141 }
Craig Mautner0247fc82013-02-28 14:32:06 -08005142 }
5143
Dianne Hackborn91097de2014-04-04 18:02:06 -07005144 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5145 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07005146 boolean toTop) {
Bryce Leeb802ea12017-11-15 21:25:03 -08005147 return createTaskRecord(taskId, info, intent, voiceSession, voiceInteractor, toTop,
5148 null /*activity*/, null /*source*/, null /*options*/);
5149 }
5150
5151 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5152 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
5153 boolean toTop, ActivityRecord activity, ActivityRecord source,
5154 ActivityOptions options) {
Garfield Tan9b1efea2017-12-05 16:43:46 -08005155 final TaskRecord task = TaskRecord.create(
5156 mService, taskId, info, intent, voiceSession, voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005157 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005158 addTask(task, toTop, "createTaskRecord");
Lucas Dupin47a65c72018-02-15 14:16:18 -08005159 final int displayId = mDisplayId != INVALID_DISPLAY ? mDisplayId : DEFAULT_DISPLAY;
Bryce Lee2a3cc462017-10-27 10:57:35 -07005160 final boolean isLockscreenShown = mService.mStackSupervisor.getKeyguardController()
Lucas Dupin47a65c72018-02-15 14:16:18 -08005161 .isKeyguardOrAodShowing(displayId);
Bryce Leeec55eb02017-12-05 20:51:27 -08005162 if (!mStackSupervisor.getLaunchParamsController()
Bryce Leeb802ea12017-11-15 21:25:03 -08005163 .layoutTask(task, info.windowLayout, activity, source, options)
Bryce Leef3c6a472017-11-14 14:53:06 -08005164 && !matchParentBounds() && task.isResizeable() && !isLockscreenShown) {
5165 task.updateOverrideConfiguration(getOverrideBounds());
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005166 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005167 task.createWindowContainer(toTop, (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005168 return task;
5169 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005170
5171 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005172 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005173 }
5174
Wale Ogunwale5f986092015-12-04 15:35:38 -08005175 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Winson Chung5af42fc2017-03-24 17:11:33 -07005176 addTask(task, toTop ? MAX_VALUE : 0, true /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005177 if (toTop) {
5178 // TODO: figure-out a way to remove this call.
Wale Ogunwale1666e312016-12-16 11:27:18 -08005179 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
5180 true /* includingParents */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005181 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005182 }
5183
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005184 // TODO: This shouldn't allow automatic reparenting. Remove the call to preAddTask and deal
5185 // with the fall-out...
Winson Chung5af42fc2017-03-24 17:11:33 -07005186 void addTask(final TaskRecord task, int position, boolean schedulePictureInPictureModeChange,
5187 String reason) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005188 // TODO: Is this remove really needed? Need to look into the call path for the other addTask
5189 mTaskHistory.remove(task);
5190 position = getAdjustedPositionForTask(task, position, null /* starting */);
5191 final boolean toTop = position >= mTaskHistory.size();
Wale Ogunwale06579d62016-04-30 15:29:06 -07005192 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005193
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005194 mTaskHistory.add(position, task);
Andrii Kulian02b7a832016-10-06 23:11:56 -07005195 task.setStack(this);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005196
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005197 updateTaskMovement(task, toTop);
5198
Winson Chung5af42fc2017-03-24 17:11:33 -07005199 postAddTask(task, prevStack, schedulePictureInPictureModeChange);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005200 }
5201
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005202 void positionChildAt(TaskRecord task, int index) {
5203
5204 if (task.getStack() != this) {
5205 throw new IllegalArgumentException("AS.positionChildAt: task=" + task
5206 + " is not a child of stack=" + this + " current parent=" + task.getStack());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005207 }
5208
5209 task.updateOverrideConfigurationForStack(this);
5210
Jorim Jaggi023da532016-04-20 22:42:32 -07005211 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07005212 final boolean wasResumed = topRunningActivity == task.getStack().mResumedActivity;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005213 insertTaskAtPosition(task, index);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005214 task.setStack(this);
Winson Chung5af42fc2017-03-24 17:11:33 -07005215 postAddTask(task, null /* prevStack */, true /* schedulePictureInPictureModeChange */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005216
Jorim Jaggi023da532016-04-20 22:42:32 -07005217 if (wasResumed) {
5218 if (mResumedActivity != null) {
5219 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5220 + " other stack to this stack mResumedActivity=" + mResumedActivity
5221 + " other mResumedActivity=" + topRunningActivity);
5222 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08005223 topRunningActivity.setState(RESUMED, "positionChildAt");
Jorim Jaggi023da532016-04-20 22:42:32 -07005224 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005225
5226 // The task might have already been running and its visibility needs to be synchronized with
5227 // the visibility of the stack / windows.
5228 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
5229 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005230 }
5231
Wale Ogunwale06579d62016-04-30 15:29:06 -07005232 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005233 final ActivityStack prevStack = task.getStack();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005234 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005235 prevStack.removeTask(task, reason,
5236 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005237 }
5238 return prevStack;
5239 }
5240
Winson Chung5af42fc2017-03-24 17:11:33 -07005241 /**
5242 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
5243 * change. Callers may set this to false if they are explicitly scheduling PiP mode
5244 * changes themselves, like during the PiP animation
5245 */
5246 private void postAddTask(TaskRecord task, ActivityStack prevStack,
5247 boolean schedulePictureInPictureModeChange) {
5248 if (schedulePictureInPictureModeChange && prevStack != null) {
5249 mStackSupervisor.scheduleUpdatePictureInPictureModeIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005250 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005251 try {
5252 task.voiceSession.taskStarted(task.intent, task.taskId);
5253 } catch (RemoteException e) {
5254 }
5255 }
5256 }
5257
Winson Chungc2baac02017-01-11 13:34:47 -08005258 void moveToFrontAndResumeStateIfNeeded(ActivityRecord r, boolean moveToFront, boolean setResume,
5259 boolean setPause, String reason) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08005260 if (!moveToFront) {
5261 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005262 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005263
5264 // If the activity owns the last resumed activity, transfer that together,
5265 // so that we don't resume the same activity again in the new stack.
5266 // Apps may depend on onResume()/onPause() being called in pairs.
5267 if (setResume) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08005268 r.setState(RESUMED, "moveToFrontAndResumeStateIfNeeded");
Winson Chungabb433b2017-03-24 09:35:42 -07005269 updateLRUListLocked(r);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005270 }
Winson Chungc2baac02017-01-11 13:34:47 -08005271 // If the activity was previously pausing, then ensure we transfer that as well
5272 if (setPause) {
5273 mPausingActivity = r;
Winson Chung4dabf232017-01-25 13:25:22 -08005274 schedulePauseTimeout(r);
Winson Chungc2baac02017-01-11 13:34:47 -08005275 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005276 // Move the stack in which we are placing the activity to the front. The call will also
5277 // make sure the activity focus is set.
5278 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005279 }
5280
Craig Mautnerc00204b2013-03-05 15:02:14 -08005281 public int getStackId() {
5282 return mStackId;
5283 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005284
5285 @Override
5286 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005287 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005288 + " stackId=" + mStackId + " type=" + activityTypeToString(getActivityType())
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07005289 + " mode=" + windowingModeToString(getWindowingMode())
Wale Ogunwale8e923af2018-03-23 08:48:11 -07005290 + " visible=" + shouldBeVisible(null /* starting */)
5291 + " translucent=" + isStackTranslucent(null /* starting */)
5292 + ", "
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005293 + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005294 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005295
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07005296 void onLockTaskPackagesUpdated() {
Craig Mautner15df08a2015-04-01 12:17:18 -07005297 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5298 mTaskHistory.get(taskNdx).setLockTaskAuth();
5299 }
5300 }
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005301
5302 void executeAppTransition(ActivityOptions options) {
5303 mWindowManager.executeAppTransition();
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005304 ActivityOptions.abort(options);
5305 }
David Stevens9440dc82017-03-16 19:00:20 -07005306
5307 boolean shouldSleepActivities() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07005308 final ActivityDisplay display = getDisplay();
Bryce Lee271617a2018-03-15 10:39:12 -07005309
5310 // Do not sleep activities in this stack if we're marked as focused and the keyguard
5311 // is in the process of going away.
5312 if (mStackSupervisor.getFocusedStack() == this
5313 && mStackSupervisor.getKeyguardController().isKeyguardGoingAway()) {
5314 return false;
5315 }
5316
David Stevens9440dc82017-03-16 19:00:20 -07005317 return display != null ? display.isSleeping() : mService.isSleepingLocked();
5318 }
5319
5320 boolean shouldSleepOrShutDownActivities() {
5321 return shouldSleepActivities() || mService.isShuttingDownLocked();
5322 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07005323
5324 public void writeToProto(ProtoOutputStream proto, long fieldId) {
5325 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02005326 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005327 proto.write(ID, mStackId);
5328 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5329 final TaskRecord task = mTaskHistory.get(taskNdx);
5330 task.writeToProto(proto, TASKS);
5331 }
5332 if (mResumedActivity != null) {
5333 mResumedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
5334 }
5335 proto.write(DISPLAY_ID, mDisplayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08005336 if (!matchParentBounds()) {
5337 final Rect bounds = getOverrideBounds();
5338 bounds.writeToProto(proto, BOUNDS);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005339 }
Bryce Leef3c6a472017-11-14 14:53:06 -08005340
5341 // TODO: Remove, no longer needed with windowingMode.
5342 proto.write(FULLSCREEN, matchParentBounds());
Steven Timotius4346f0a2017-09-12 11:07:21 -07005343 proto.end(token);
5344 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005345}