blob: 64bd667e5548f175cc12b24df1b58c116ef93431 [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
Winson Chung83471632016-12-13 11:02:12 -080019import static android.app.ActivityManager.StackId.ASSISTANT_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070020import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
21import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
23import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale56d75cf2016-03-09 15:14:47 -080024import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070025import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Winson Chung91e5c882017-04-21 14:44:38 -070026import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Wale Ogunwalef75962a2017-08-23 14:58:04 -070027import static android.app.ActivityManager.StackId.getWindowingModeForStackId;
28import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080029import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080030import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070031import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwalef5d1e352016-09-22 08:34:42 -070032import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian7211d2e2017-01-27 15:58:05 -080033import static android.view.Display.FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080034
Andrii Kulian94e82d9b02017-07-13 15:33:06 -070035import static android.view.Display.INVALID_DISPLAY;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070036import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
37import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
38import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_APP;
39import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CLEANUP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070040import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070041import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RESULTS;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070045import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TRANSITION;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_USER_LEAVING;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
52import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
53import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_APP;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CLEANUP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070055import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RESULTS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070060import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TRANSITION;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_USER_LEAVING;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
67import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
68import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Craig Mautner84984fa2014-06-19 11:19:20 -070069import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Winson Chung83471632016-12-13 11:02:12 -080070import static com.android.server.am.ActivityRecord.ASSISTANT_ACTIVITY_TYPE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070071import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -070072import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
73import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale39381972015-12-17 17:15:29 -080074import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Winson Chung7c3ede32017-03-14 14:18:34 -070075import static com.android.server.am.ActivityStackSupervisor.ON_TOP;
Winson Chung6954fc92017-03-24 16:22:12 -070076import static com.android.server.am.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070077import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Winson Chung6954fc92017-03-24 16:22:12 -070078import static com.android.server.am.ActivityStackSupervisor.REMOVE_FROM_RECENTS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070079import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_CLOSE;
80import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_OPEN;
81import static com.android.server.wm.AppTransition.TRANSIT_NONE;
82import static com.android.server.wm.AppTransition.TRANSIT_TASK_CLOSE;
83import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN;
84import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN_BEHIND;
85import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_BACK;
86import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_FRONT;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080087import static java.lang.Integer.MAX_VALUE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088
89import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070090import android.app.ActivityManager;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070091import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070092import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070093import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080095import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096import android.app.ResultInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070097import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070098import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070099import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800100import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700101import android.content.res.Configuration;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700102import android.graphics.Point;
103import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -0800104import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700106import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -0800107import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108import android.os.Handler;
109import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +0900110import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700111import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112import android.os.RemoteException;
113import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -0800114import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700115import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700116import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700117import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800119import android.util.IntArray;
Jorim Jaggi023da532016-04-20 22:42:32 -0700120import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121import android.util.Slog;
Andrii Kulian1e32e022016-09-16 15:29:34 -0700122import android.util.SparseArray;
Craig Mautner59c00972012-07-30 12:10:24 -0700123import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124
Bryce Lee840c5662017-04-13 10:02:51 -0700125import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700126import com.android.internal.app.IVoiceInteractor;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700127import com.android.internal.os.BatteryStatsImpl;
128import com.android.server.Watchdog;
129import com.android.server.am.ActivityManagerService.ItemMatcher;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700130import com.android.server.wm.ConfigurationContainer;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800131import com.android.server.wm.StackWindowController;
132import com.android.server.wm.StackWindowListener;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700133import com.android.server.wm.WindowManagerService;
134
Craig Mautnercae015f2013-02-08 14:31:27 -0800135import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800136import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700137import java.lang.ref.WeakReference;
138import java.util.ArrayList;
139import java.util.Iterator;
140import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800141import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700142import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700143
144/**
145 * State and management of a single stack of activities.
146 */
Winson Chung55893332017-02-17 17:13:10 -0800147class ActivityStack<T extends StackWindowController> extends ConfigurationContainer
148 implements StackWindowListener {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800149
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800150 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
152 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800153 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700155 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700156 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700157 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700160 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700161 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
162 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
163 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
164 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
165 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700166
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700167 // Ticks during which we check progress while waiting for an app to launch.
168 static final int LAUNCH_TICK = 500;
169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 // How long we wait until giving up on the last activity to pause. This
171 // is short because it directly impacts the responsiveness of starting the
172 // next activity.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700173 private static final int PAUSE_TIMEOUT = 500;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700175 // How long we wait for the activity to tell us it has stopped before
176 // giving up. This is a good amount of time because we really need this
177 // from the application in order to get its saved state.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700178 private static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180 // How long we wait until giving up on an activity telling us it has
181 // finished destroying itself.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700182 private static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800185 // disabled.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700186 private static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800187
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700188 // Set to false to disable the preview that is shown while a new activity
189 // is being started.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700190 private static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800191
Craig Mautner5eda9b32013-07-02 11:58:16 -0700192 // How long to wait for all background Activities to redraw following a call to
193 // convertToTranslucent().
Andrii Kulian21713ac2016-10-12 22:05:05 -0700194 private static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700195
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800196 // How many activities have to be scheduled to stop to force a stop pass.
197 private static final int MAX_STOPPING_TO_FORCE = 3;
198
Andrii Kulian1779e612016-10-12 21:58:25 -0700199 @Override
200 protected int getChildCount() {
201 return mTaskHistory.size();
202 }
203
204 @Override
205 protected ConfigurationContainer getChildAt(int index) {
206 return mTaskHistory.get(index);
207 }
208
209 @Override
210 protected ConfigurationContainer getParent() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700211 return getDisplay();
Andrii Kulian1779e612016-10-12 21:58:25 -0700212 }
213
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800214 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700215 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800216 super.onParentChanged();
217 mStackSupervisor.updateUIDsPresentOnDisplay();
218 }
219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700220 enum ActivityState {
221 INITIALIZING,
222 RESUMED,
223 PAUSING,
224 PAUSED,
225 STOPPING,
226 STOPPED,
227 FINISHING,
228 DESTROYING,
229 DESTROYED
230 }
231
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800232 // Stack is not considered visible.
233 static final int STACK_INVISIBLE = 0;
234 // Stack is considered visible
235 static final int STACK_VISIBLE = 1;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800236
Bryce Lee840c5662017-04-13 10:02:51 -0700237 @VisibleForTesting
Wale Ogunwale06579d62016-04-30 15:29:06 -0700238 /* The various modes for the method {@link #removeTask}. */
239 // Task is being completely removed from all stacks in the system.
Bryce Lee840c5662017-04-13 10:02:51 -0700240 protected static final int REMOVE_TASK_MODE_DESTROYING = 0;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700241 // Task is being removed from this stack so we can add it to another stack. In the case we are
242 // moving we don't want to perform some operations on the task like removing it from window
243 // manager or recents.
244 static final int REMOVE_TASK_MODE_MOVING = 1;
245 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
246 // stack and the new stack will be on top of all stacks.
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700247 static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700248
Andrii Kulian1e32e022016-09-16 15:29:34 -0700249 // The height/width divide used when fitting a task within a bounds with method
250 // {@link #fitWithinBounds}.
251 // We always want the task to to be visible in the bounds without affecting its size when
252 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
253 // the input bounds right or bottom side minus the width or height divided by this value.
254 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
255
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700256 final ActivityManagerService mService;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700257 private final WindowManagerService mWindowManager;
Winson Chung55893332017-02-17 17:13:10 -0800258 T mWindowContainerController;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800259 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800260
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700261 /**
262 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800263 * running) activities. It contains #TaskRecord objects.
264 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800265 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800266
267 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268 * List of running activities, sorted by recent usage.
269 * The first entry in the list is the least recently used.
270 * It contains HistoryRecord objects.
271 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800272 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273
274 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 * Animations that for the current transition have requested not to
276 * be considered for the transition animation.
277 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800278 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700279
280 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700281 * When we are in the process of pausing an activity, before starting the
282 * next one, this variable holds the activity that is currently being paused.
283 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800284 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700285
286 /**
287 * This is the last activity that we put into the paused state. This is
288 * used to determine if we need to do an activity transition while sleeping,
289 * when we normally hold the top activity paused.
290 */
291 ActivityRecord mLastPausedActivity = null;
292
293 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700294 * Activities that specify No History must be removed once the user navigates away from them.
295 * If the device goes to sleep with such an activity in the paused state then we save it here
296 * and finish it later if another activity replaces it on wakeup.
297 */
298 ActivityRecord mLastNoHistoryActivity = null;
299
300 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700301 * Current activity that is resumed, or null if there is none.
302 */
303 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800304
Craig Mautner5eda9b32013-07-02 11:58:16 -0700305 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
306 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
307 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
308 // Activity in mTranslucentActivityWaiting is notified via
309 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
310 // background activity being drawn then the same call will be made with a true value.
311 ActivityRecord mTranslucentActivityWaiting = null;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700312 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700313
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700314 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700315 * Set when we know we are going to be calling updateConfiguration()
316 * soon, so want to skip intermediate config checks.
317 */
318 boolean mConfigWillChange;
319
Winson Chung47900652017-04-06 18:44:25 -0700320 /**
321 * When set, will force the stack to report as invisible.
322 */
323 boolean mForceHidden = false;
324
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700325 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800326 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700327 // Current bounds of the stack or null if fullscreen.
328 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800329
Andrii Kulian21713ac2016-10-12 22:05:05 -0700330 private boolean mUpdateBoundsDeferred;
331 private boolean mUpdateBoundsDeferredCalled;
332 private final Rect mDeferredBounds = new Rect();
333 private final Rect mDeferredTaskBounds = new Rect();
334 private final Rect mDeferredTaskInsetBounds = new Rect();
Jorim Jaggi192086e2016-03-11 17:17:03 +0100335
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700336 long mLaunchStartTime = 0;
337 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800338
Craig Mautner858d8a62013-04-23 17:08:34 -0700339 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700340
Craig Mautnerc00204b2013-03-05 15:02:14 -0800341 final int mStackId;
Craig Mautnere0a38842013-12-16 16:14:02 -0800342 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700343 // TODO: This list doesn't belong here...
Craig Mautnere0a38842013-12-16 16:14:02 -0800344 ArrayList<ActivityStack> mStacks;
345 /** The attached Display's unique identifier, or -1 if detached */
346 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800347
Andrii Kulian1e32e022016-09-16 15:29:34 -0700348 /** Temp variables used during override configuration update. */
349 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
350 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
351 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800352 private final Rect mTmpRect2 = new Rect();
Andrii Kulian1e32e022016-09-16 15:29:34 -0700353
Craig Mautner27084302013-03-25 08:05:25 -0700354 /** Run all ActivityStacks through this */
Winson Chung5af42fc2017-03-24 17:11:33 -0700355 protected final ActivityStackSupervisor mStackSupervisor;
Craig Mautner27084302013-03-25 08:05:25 -0700356
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700357 private final LaunchingTaskPositioner mTaskPositioner;
358
Jorim Jaggife762342016-10-13 14:33:27 +0200359 private boolean mTopActivityOccludesKeyguard;
360 private ActivityRecord mTopDismissingKeyguardActivity;
361
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700362 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
364 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
365 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
366 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700367 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700368
Andrii Kulian21713ac2016-10-12 22:05:05 -0700369 private static class ScheduleDestroyArgs {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700370 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700371 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700372 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700373 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700374 mReason = reason;
375 }
376 }
377
Zoran Marcetaf958b322012-08-09 20:27:12 +0900378 final Handler mHandler;
379
Andrii Kulian21713ac2016-10-12 22:05:05 -0700380 private class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800381
Craig Mautnerc8143c62013-09-03 12:15:57 -0700382 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900383 super(looper);
384 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700385
Zoran Marcetaf958b322012-08-09 20:27:12 +0900386 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 public void handleMessage(Message msg) {
388 switch (msg.what) {
389 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800390 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700391 // We don't at this point know if the activity is fullscreen,
392 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800393 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700394 synchronized (mService) {
395 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700396 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700397 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700398 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800399 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700401 case LAUNCH_TICK_MSG: {
402 ActivityRecord r = (ActivityRecord)msg.obj;
403 synchronized (mService) {
404 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700405 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700406 }
407 }
408 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700409 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800410 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700411 // We don't at this point know if the activity is fullscreen,
412 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800413 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800414 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800415 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700417 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700418 case STOP_TIMEOUT_MSG: {
419 ActivityRecord r = (ActivityRecord)msg.obj;
420 // We don't at this point know if the activity is fullscreen,
421 // so we need to be conservative and assume it isn't.
422 Slog.w(TAG, "Activity stop timeout for " + r);
423 synchronized (mService) {
424 if (r.isInHistory()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -0700425 r.activityStoppedLocked(null /* icicle */,
426 null /* persistentState */, null /* description */);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700427 }
428 }
429 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700430 case DESTROY_ACTIVITIES_MSG: {
431 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
432 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700433 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700434 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700435 } break;
436 case TRANSLUCENT_TIMEOUT_MSG: {
437 synchronized (mService) {
438 notifyActivityDrawnLocked(null);
439 }
440 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700441 }
442 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800443 }
444
Craig Mautner34b73df2014-01-12 21:11:08 -0800445 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800446 int count = 0;
447 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
448 count += mTaskHistory.get(taskNdx).mActivities.size();
449 }
450 return count;
451 }
452
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700453 ActivityStack(ActivityStackSupervisor.ActivityDisplay display, int stackId,
454 ActivityStackSupervisor supervisor, RecentTasks recentTasks, boolean onTop) {
455 mStackSupervisor = supervisor;
456 mService = supervisor.mService;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800457 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
458 mWindowManager = mService.mWindowManager;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700459 mStackId = stackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700460 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800461 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700462 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
463 ? new LaunchingTaskPositioner() : null;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800464 mTmpRect2.setEmpty();
Wale Ogunwalef75962a2017-08-23 14:58:04 -0700465 updateOverrideConfiguration();
Winson Chung55893332017-02-17 17:13:10 -0800466 mWindowContainerController = createStackWindowController(display.mDisplayId, onTop,
Wale Ogunwalef75962a2017-08-23 14:58:04 -0700467 mTmpRect2, getOverrideConfiguration());
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700468 mStackSupervisor.mStacks.put(mStackId, this);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800469 postAddToDisplay(display, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
470 }
471
Wale Ogunwale687b4272017-07-27 02:56:23 -0700472 T createStackWindowController(int displayId, boolean onTop, Rect outBounds,
Wale Ogunwalef75962a2017-08-23 14:58:04 -0700473 Configuration overrideConfig) {
Wale Ogunwale687b4272017-07-27 02:56:23 -0700474 return (T) new StackWindowController(mStackId, this, displayId, onTop, outBounds,
Wale Ogunwalef75962a2017-08-23 14:58:04 -0700475 overrideConfig);
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
Wale Ogunwalef75962a2017-08-23 14:58:04 -0700482 // TODO: Not needed once we are no longer using stack ids as the override config. can be passed
483 // in.
484 private void updateOverrideConfiguration() {
485 final int windowingMode = getWindowingModeForStackId(mStackId);
486 if (windowingMode != WINDOWING_MODE_UNDEFINED) {
487 setWindowingMode(windowingMode);
488 }
489 }
490
Andrii Kulian839def92016-11-02 10:58:58 -0700491 /** Adds the stack to specified display and calls WindowManager to do the same. */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800492 void reparent(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
493 removeFromDisplay();
494 mTmpRect2.setEmpty();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800495 postAddToDisplay(activityDisplay, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
Andrii Kulian250d6532017-02-08 23:30:45 -0800496 adjustFocusToNextFocusableStackLocked("reparent", true /* allowFocusSelf */);
497 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Andrii Kulian51c1b672017-04-07 18:39:32 -0700498 // Update visibility of activities before notifying WM. This way it won't try to resize
499 // windows that are no longer visible.
500 mStackSupervisor.ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
501 !PRESERVE_WINDOWS);
502 mWindowContainerController.reparent(activityDisplay.mDisplayId, mTmpRect2, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -0700503 }
504
505 /**
506 * Updates internal state after adding to new display.
507 * @param activityDisplay New display to which this stack was attached.
508 * @param bounds Updated bounds.
509 */
510 private void postAddToDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay,
Wale Ogunwale1666e312016-12-16 11:27:18 -0800511 Rect bounds, boolean onTop) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700512 mDisplayId = activityDisplay.mDisplayId;
513 mStacks = activityDisplay.mStacks;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800514 mBounds = bounds != null ? new Rect(bounds) : null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700515 mFullscreen = mBounds == null;
516 if (mTaskPositioner != null) {
517 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
518 mTaskPositioner.configure(mBounds);
519 }
Andrii Kulian1779e612016-10-12 21:58:25 -0700520 onParentChanged();
Wale Ogunwale961f4852016-02-01 20:25:54 -0800521
Winson Chung7c3ede32017-03-14 14:18:34 -0700522 activityDisplay.attachStack(this, findStackInsertIndex(onTop));
Wale Ogunwale961f4852016-02-01 20:25:54 -0800523 if (mStackId == DOCKED_STACK_ID) {
524 // If we created a docked stack we want to resize it so it resizes all other stacks
525 // in the system.
526 mStackSupervisor.resizeDockedStackLocked(
527 mBounds, null, null, null, null, PRESERVE_WINDOWS);
528 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700529 }
530
Andrii Kulian839def92016-11-02 10:58:58 -0700531 /**
Andrii Kulian839def92016-11-02 10:58:58 -0700532 * Updates the inner state of the stack to remove it from its current parent, so it can be
533 * either destroyed completely or re-parented.
534 */
535 private void removeFromDisplay() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700536 final ActivityStackSupervisor.ActivityDisplay display = getDisplay();
537 if (display != null) {
538 display.detachStack(this);
539 }
540 mDisplayId = INVALID_DISPLAY;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700541 mStacks = null;
542 if (mTaskPositioner != null) {
543 mTaskPositioner.reset();
544 }
Jorim Jaggi899327f2016-02-25 20:44:18 -0500545 if (mStackId == DOCKED_STACK_ID) {
546 // If we removed a docked stack we want to resize it so it resizes all other stacks
547 // in the system to fullscreen.
548 mStackSupervisor.resizeDockedStackLocked(
549 null, null, null, null, null, PRESERVE_WINDOWS);
550 }
Andrii Kulian839def92016-11-02 10:58:58 -0700551 }
552
553 /** Removes the stack completely. Also calls WindowManager to do the same on its side. */
554 void remove() {
555 removeFromDisplay();
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700556 mStackSupervisor.mStacks.remove(mStackId);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800557 mWindowContainerController.removeContainer();
558 mWindowContainerController = null;
Andrii Kulian6d6fb402016-10-26 16:15:25 -0700559 onParentChanged();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700560 }
561
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700562 ActivityStackSupervisor.ActivityDisplay getDisplay() {
563 return mStackSupervisor.getActivityDisplay(mDisplayId);
564 }
565
Andrii Kulian21713ac2016-10-12 22:05:05 -0700566 void getDisplaySize(Point out) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700567 getDisplay().mDisplay.getSize(out);
Winsonc809cbb2015-11-02 12:06:15 -0800568 }
569
Matthew Ngaa2b6202017-02-10 14:48:21 -0800570 /**
571 * @see ActivityStack.getStackDockedModeBounds(Rect, Rect, Rect, boolean)
572 */
573 void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
574 Rect outTempTaskBounds, boolean ignoreVisibility) {
575 mWindowContainerController.getStackDockedModeBounds(currentTempTaskBounds,
576 outStackBounds, outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800577 }
578
579 void prepareFreezingTaskBounds() {
580 mWindowContainerController.prepareFreezingTaskBounds();
581 }
582
Wale Ogunwale1666e312016-12-16 11:27:18 -0800583 void getWindowContainerBounds(Rect outBounds) {
584 if (mWindowContainerController != null) {
585 mWindowContainerController.getBounds(outBounds);
Winson Chungb00dc5e2017-01-25 09:44:25 -0800586 return;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800587 }
588 outBounds.setEmpty();
589 }
590
Matthew Ngaa2b6202017-02-10 14:48:21 -0800591 void getBoundsForNewConfiguration(Rect outBounds) {
592 mWindowContainerController.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800593 }
594
595 void positionChildWindowContainerAtTop(TaskRecord child) {
596 mWindowContainerController.positionChildAtTop(child.getWindowContainerController(),
597 true /* includingParents */);
598 }
599
Jorim Jaggi192086e2016-03-11 17:17:03 +0100600 /**
Winson Chung8bca9e42017-04-16 15:59:43 -0700601 * Returns whether to defer the scheduling of the multi-window mode.
602 */
603 boolean deferScheduleMultiWindowModeChanged() {
604 return false;
605 }
606
607 /**
Jorim Jaggi192086e2016-03-11 17:17:03 +0100608 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
609 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
610 */
611 void deferUpdateBounds() {
612 if (!mUpdateBoundsDeferred) {
613 mUpdateBoundsDeferred = true;
614 mUpdateBoundsDeferredCalled = false;
615 }
616 }
617
618 /**
619 * Continues updating bounds after updates have been deferred. If there was a resize attempt
620 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
621 * be resized to that bounds.
622 */
623 void continueUpdateBounds() {
624 final boolean wasDeferred = mUpdateBoundsDeferred;
625 mUpdateBoundsDeferred = false;
626 if (wasDeferred && mUpdateBoundsDeferredCalled) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800627 resize(mDeferredBounds.isEmpty() ? null : mDeferredBounds,
Jorim Jaggi192086e2016-03-11 17:17:03 +0100628 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
629 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
630 }
631 }
632
633 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
634 Rect tempTaskInsetBounds) {
635 if (!mUpdateBoundsDeferred) {
636 return true;
637 }
638 if (bounds != null) {
639 mDeferredBounds.set(bounds);
640 } else {
641 mDeferredBounds.setEmpty();
642 }
643 if (tempTaskBounds != null) {
644 mDeferredTaskBounds.set(tempTaskBounds);
645 } else {
646 mDeferredTaskBounds.setEmpty();
647 }
648 if (tempTaskInsetBounds != null) {
649 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
650 } else {
651 mDeferredTaskInsetBounds.setEmpty();
652 }
653 mUpdateBoundsDeferredCalled = true;
654 return false;
655 }
656
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700657 void setBounds(Rect bounds) {
658 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700659 if (mTaskPositioner != null) {
660 mTaskPositioner.configure(bounds);
661 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700662 }
Craig Mautner5962b122012-10-05 14:45:52 -0700663
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700664 ActivityRecord topRunningActivityLocked() {
Winson Chung3f103eb2017-04-12 21:53:59 -0700665 return topRunningActivityLocked(false /* focusableOnly */);
666 }
667
Jorim Jaggiea039a82017-08-02 14:37:49 +0200668 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
669 outActivities.clear();
670 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
671 mTaskHistory.get(taskNdx).getAllRunningVisibleActivitiesLocked(outActivities);
672 }
673 }
674
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700675 private ActivityRecord topRunningActivityLocked(boolean focusableOnly) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800676 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700677 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Winson Chung3f103eb2017-04-12 21:53:59 -0700678 if (r != null && (!focusableOnly || r.isFocusable())) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700679 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800680 }
681 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 return null;
683 }
684
Wale Ogunwale42f07d92017-05-01 21:32:58 -0700685 ActivityRecord topRunningNonOverlayTaskActivity() {
686 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
687 final TaskRecord task = mTaskHistory.get(taskNdx);
688 final ArrayList<ActivityRecord> activities = task.mActivities;
689 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
690 final ActivityRecord r = activities.get(activityNdx);
691 if (!r.finishing && !r.mTaskOverlay) {
692 return r;
693 }
694 }
695 }
696 return null;
697 }
698
699 ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800700 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
701 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800702 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800703 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
704 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -0700705 if (!r.finishing && !r.delayedResume && r != notTop && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800706 return r;
707 }
708 }
709 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700710 return null;
711 }
712
713 /**
714 * This is a simplified version of topRunningActivityLocked that provides a number of
715 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800716 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700717 * @param token If non-null, any history records matching this token will be skipped.
718 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800719 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700720 * @return Returns the HistoryRecord of the next activity on the stack.
721 */
722 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800723 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
724 TaskRecord task = mTaskHistory.get(taskNdx);
725 if (task.taskId == taskId) {
726 continue;
727 }
728 ArrayList<ActivityRecord> activities = task.mActivities;
729 for (int i = activities.size() - 1; i >= 0; --i) {
730 final ActivityRecord r = activities.get(i);
731 // Note: the taskId check depends on real taskId fields being non-zero
Chong Zhang87761972016-08-22 13:53:24 -0700732 if (!r.finishing && (token != r.appToken) && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800733 return r;
734 }
735 }
736 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700737 return null;
738 }
739
Craig Mautner8849a5e2013-04-02 16:41:03 -0700740 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700741 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
742 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700743 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
744 final ActivityRecord r = activities.get(activityNdx);
745 if (!r.finishing) {
746 return r;
747 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700748 }
749 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700750 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700751 }
752
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700753 final TaskRecord topTask() {
754 final int size = mTaskHistory.size();
755 if (size > 0) {
756 return mTaskHistory.get(size - 1);
757 }
758 return null;
759 }
760
Winson Chung1cebea62017-06-26 17:22:27 -0700761 final TaskRecord bottomTask() {
762 if (mTaskHistory.isEmpty()) {
763 return null;
764 }
765 return mTaskHistory.get(0);
766 }
767
Craig Mautnerd2328952013-03-05 12:46:26 -0800768 TaskRecord taskForIdLocked(int id) {
769 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
770 final TaskRecord task = mTaskHistory.get(taskNdx);
771 if (task.taskId == id) {
772 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800773 }
774 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700775 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700776 }
777
Craig Mautnerd2328952013-03-05 12:46:26 -0800778 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700779 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800780 return isInStackLocked(r);
781 }
782
783 ActivityRecord isInStackLocked(ActivityRecord r) {
784 if (r == null) {
785 return null;
786 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700787 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700788 final ActivityStack stack = r.getStack();
789 if (stack != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
790 if (stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800791 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800792 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800793 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800794 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800795 }
796
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800797 boolean isInStackLocked(TaskRecord task) {
798 return mTaskHistory.contains(task);
799 }
800
801 /** Checks if there are tasks with specific UID in the stack. */
802 boolean isUidPresent(int uid) {
803 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -0800804 for (ActivityRecord r : task.mActivities) {
805 if (r.getUid() == uid) {
806 return true;
807 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800808 }
809 }
810 return false;
811 }
812
813 /** Get all UIDs that are present in the stack. */
814 void getPresentUIDs(IntArray presentUIDs) {
815 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -0800816 for (ActivityRecord r : task.mActivities) {
817 presentUIDs.add(r.getUid());
818 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800819 }
820 }
821
Winson Chungabb433b2017-03-24 09:35:42 -0700822 final void removeActivitiesFromLRUListLocked(TaskRecord task) {
823 for (ActivityRecord r : task.mActivities) {
824 mLRUActivities.remove(r);
825 }
826 }
827
Craig Mautner2420ead2013-04-01 17:13:20 -0700828 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829 final boolean hadit = mLRUActivities.remove(r);
830 mLRUActivities.add(r);
831 return hadit;
832 }
833
Craig Mautnerde4ef022013-04-07 19:01:33 -0700834 final boolean isHomeStack() {
835 return mStackId == HOME_STACK_ID;
836 }
837
Winson Chung91e5c882017-04-21 14:44:38 -0700838 final boolean isRecentsStack() {
839 return mStackId == RECENTS_STACK_ID;
840 }
841
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800842 final boolean isHomeOrRecentsStack() {
843 return StackId.isHomeOrRecentsStack(mStackId);
844 }
845
Winson Chung0583d3d2015-12-18 10:03:12 -0500846 final boolean isDockedStack() {
847 return mStackId == DOCKED_STACK_ID;
848 }
849
850 final boolean isPinnedStack() {
851 return mStackId == PINNED_STACK_ID;
852 }
853
Winson Chung83471632016-12-13 11:02:12 -0800854 final boolean isAssistantStack() {
855 return mStackId == ASSISTANT_STACK_ID;
856 }
857
Craig Mautnere0a38842013-12-16 16:14:02 -0800858 final boolean isOnHomeDisplay() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700859 return isAttached() && mDisplayId == DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -0800860 }
861
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700862 void moveToFront(String reason) {
863 moveToFront(reason, null);
864 }
865
866 /**
867 * @param reason The reason for moving the stack to the front.
868 * @param task If non-null, the task will be moved to the top of the stack.
869 * */
870 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700871 if (!isAttached()) {
872 return;
873 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700874
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700875 mStacks.remove(this);
Winson Chung7c3ede32017-03-14 14:18:34 -0700876 mStacks.add(findStackInsertIndex(ON_TOP), this);
Andrii Kulian839def92016-11-02 10:58:58 -0700877 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700878 if (task != null) {
879 insertTaskAtTop(task, null);
Wale Ogunwaleaa47c122016-09-23 16:39:53 -0700880 return;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700881 }
Wale Ogunwaleaa47c122016-09-23 16:39:53 -0700882
883 task = topTask();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700884 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800885 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
886 true /* includingParents */);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800888 }
889
Jiaquan Hee13e9642016-06-15 15:16:13 -0700890 /**
891 * @param task If non-null, the task will be moved to the back of the stack.
892 * */
Andrii Kulian21713ac2016-10-12 22:05:05 -0700893 private void moveToBack(TaskRecord task) {
Jiaquan Hee13e9642016-06-15 15:16:13 -0700894 if (!isAttached()) {
895 return;
896 }
897
898 mStacks.remove(this);
899 mStacks.add(0, this);
900
901 if (task != null) {
902 mTaskHistory.remove(task);
903 mTaskHistory.add(0, task);
904 updateTaskMovement(task, false);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800905 mWindowContainerController.positionChildAtBottom(task.getWindowContainerController());
Jiaquan Hee13e9642016-06-15 15:16:13 -0700906 }
907 }
908
Winson Chung7c3ede32017-03-14 14:18:34 -0700909 /**
910 * @return the index to insert a new stack into, taking the always-on-top stacks into account.
911 */
912 private int findStackInsertIndex(boolean onTop) {
913 if (onTop) {
914 int addIndex = mStacks.size();
915 if (addIndex > 0) {
916 final ActivityStack topStack = mStacks.get(addIndex - 1);
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700917 if (topStack.getWindowConfiguration().isAlwaysOnTop()
918 && topStack != this) {
Winson Chung7c3ede32017-03-14 14:18:34 -0700919 // If the top stack is always on top, we move this stack just below it.
920 addIndex--;
921 }
922 }
923 return addIndex;
924 } else {
925 return 0;
926 }
927 }
928
Wale Ogunwaled046a012015-12-24 13:05:59 -0800929 boolean isFocusable() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700930 if (getWindowConfiguration().canReceiveKeys()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800931 return true;
932 }
933 // The stack isn't focusable. See if its top activity is focusable to force focus on the
934 // stack.
935 final ActivityRecord r = topRunningActivityLocked();
936 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800937 }
938
Craig Mautnere0a38842013-12-16 16:14:02 -0800939 final boolean isAttached() {
940 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800941 }
942
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700943 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800944 * Returns the top activity in any existing task matching the given Intent in the input result.
945 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700946 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800947 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700948 Intent intent = target.intent;
949 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700950 ComponentName cls = intent.getComponent();
951 if (info.targetActivity != null) {
952 cls = new ComponentName(info.packageName, info.targetActivity);
953 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700954 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700955 boolean isDocument = intent != null & intent.isDocument();
956 // If documentData is non-null then it must match the existing task data.
957 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800958
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700959 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800960 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
961 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700962 if (task.voiceSession != null) {
963 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700964 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700965 continue;
966 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700967 if (task.userId != userId) {
968 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700969 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700970 continue;
971 }
Craig Mautner000f0022013-02-26 15:04:29 -0800972 final ActivityRecord r = task.getTopActivity();
973 if (r == null || r.finishing || r.userId != userId ||
974 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700975 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800976 continue;
977 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700978 if (r.mActivityType != target.mActivityType) {
979 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
980 continue;
981 }
Craig Mautner000f0022013-02-26 15:04:29 -0800982
Craig Mautnerd00f4742014-03-12 14:17:26 -0700983 final Intent taskIntent = task.intent;
984 final Intent affinityIntent = task.affinityIntent;
985 final boolean taskIsDocument;
986 final Uri taskDocumentData;
987 if (taskIntent != null && taskIntent.isDocument()) {
988 taskIsDocument = true;
989 taskDocumentData = taskIntent.getData();
990 } else if (affinityIntent != null && affinityIntent.isDocument()) {
991 taskIsDocument = true;
992 taskDocumentData = affinityIntent.getData();
993 } else {
994 taskIsDocument = false;
995 taskDocumentData = null;
996 }
997
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700998 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700999 + taskIntent.getComponent().flattenToShortString()
Bryce Leeaf691c02017-03-20 14:20:22 -07001000 + "/aff=" + r.getTask().rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001001 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001002 // TODO Refactor to remove duplications. Check if logic can be simplified.
1003 if (taskIntent != null && taskIntent.getComponent() != null &&
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001004 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001005 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001006 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001007 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001008 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1009 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -08001010 result.r = r;
1011 result.matchedByRootAffinity = false;
1012 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001013 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
1014 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001015 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001016 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001017 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001018 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1019 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -08001020 result.r = r;
1021 result.matchedByRootAffinity = false;
1022 break;
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001023 } else if (!isDocument && !taskIsDocument
Winson Chung5b895b72017-05-01 13:46:25 -07001024 && result.r == null && task.rootAffinity != null) {
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001025 if (task.rootAffinity.equals(target.taskAffinity)) {
1026 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
1027 // It is possible for multiple tasks to have the same root affinity especially
1028 // if they are in separate stacks. We save off this candidate, but keep looking
1029 // to see if there is a better candidate.
1030 result.r = r;
1031 result.matchedByRootAffinity = true;
1032 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001033 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001034 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001035 }
1036
1037 /**
1038 * Returns the first activity (starting from the top of the stack) that
1039 * is the same as the given activity. Returns null if no such activity
1040 * is found.
1041 */
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001042 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
1043 boolean compareIntentFilters) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001044 ComponentName cls = intent.getComponent();
1045 if (info.targetActivity != null) {
1046 cls = new ComponentName(info.packageName, info.targetActivity);
1047 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001048 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001049
Craig Mautner000f0022013-02-26 15:04:29 -08001050 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001051 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001052 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001053
Craig Mautner000f0022013-02-26 15:04:29 -08001054 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1055 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001056 if (!r.okToShowLocked()) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -07001057 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001058 }
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001059 if (!r.finishing && r.userId == userId) {
1060 if (compareIntentFilters) {
1061 if (r.intent.filterEquals(intent)) {
1062 return r;
1063 }
1064 } else {
1065 if (r.intent.getComponent().equals(cls)) {
1066 return r;
1067 }
1068 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001069 }
1070 }
1071 }
1072
1073 return null;
1074 }
1075
Amith Yamasani742a6712011-05-04 14:49:28 -07001076 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -07001077 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -07001078 */
Craig Mautner93529a42013-10-04 15:03:13 -07001079 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001080 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -07001081 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001082 }
1083 mCurrentUser = userId;
1084
1085 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001086 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001087 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001088 final TaskRecord task = mTaskHistory.get(i);
1089
Chong Zhang87761972016-08-22 13:53:24 -07001090 if (task.okToShowLocked()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001091 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001092 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001093 mTaskHistory.remove(i);
1094 mTaskHistory.add(task);
1095 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001096 // Use same value for i.
1097 } else {
1098 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001099 }
1100 }
Amith Yamasani742a6712011-05-04 14:49:28 -07001101 }
1102
Craig Mautner2420ead2013-04-01 17:13:20 -07001103 void minimalResumeActivityLocked(ActivityRecord r) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001104 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
1105 + " callers=" + Debug.getCallers(5));
Chong Zhang6cda19c2016-06-14 19:07:56 -07001106 setResumedActivityLocked(r, "minimalResumeActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001107 r.completeResumeLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -07001108 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001109 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
1110 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001111 }
1112
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08001113 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -07001114 if (r != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001115 final TaskRecord task = r.getTask();
1116 mRecentTasks.addLocked(task);
1117 task.touchActiveTime();
Chong Zhang45c25ce2015-08-10 22:18:26 -07001118 }
1119 }
1120
Narayan Kamath7829c812015-06-08 17:39:43 +01001121 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001122 if (mFullyDrawnStartTime != 0) {
1123 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1124 }
Narayan Kamath7829c812015-06-08 17:39:43 +01001125 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -07001126 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1127 }
1128
1129 private void stopFullyDrawnTraceIfNeeded() {
1130 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
1131 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1132 mFullyDrawnStartTime = 0;
1133 }
1134 }
1135
Craig Mautnere79d42682013-04-01 19:01:53 -07001136 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001137 if (r.displayStartTime == 0) {
1138 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
1139 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001140 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001141 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001142 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001143 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001144 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001145 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001147 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001148
Andrii Kulian21713ac2016-10-12 22:05:05 -07001149 private void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -07001150 // Make sure that there is no activity waiting for this to launch.
1151 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
1152 r.displayStartTime = r.fullyDrawnStartTime = 0;
1153 } else {
1154 mStackSupervisor.removeTimeoutsForActivityLocked(r);
1155 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1156 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001157 }
1158
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001159 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001160 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001161 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1162 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1163 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1164 activities.get(activityNdx).setSleeping(false);
1165 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001166 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -08001167 if (mPausingActivity != null) {
1168 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
1169 activityPausedLocked(mPausingActivity.appToken, true);
1170 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001171 }
1172
Todd Kennedy39bfee52016-02-24 10:28:21 -08001173 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1174 final String packageName = aInfo.packageName;
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001175 final int userId = UserHandle.getUserId(aInfo.uid);
1176
Todd Kennedy39bfee52016-02-24 10:28:21 -08001177 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1178 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1179 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001180 final ActivityRecord ar = activities.get(activityNdx);
1181
1182 if ((userId == ar.userId) && packageName.equals(ar.packageName)) {
1183 ar.info.applicationInfo = aInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -08001184 }
1185 }
1186 }
1187 }
1188
David Stevens9440dc82017-03-16 19:00:20 -07001189 void checkReadyForSleep() {
1190 if (shouldSleepActivities() && goToSleepIfPossible(false /* shuttingDown */)) {
1191 mStackSupervisor.checkReadyForSleepLocked(true /* allowDelay */);
1192 }
1193 }
1194
Craig Mautner0eea92c2013-05-16 13:35:39 -07001195 /**
David Stevens9440dc82017-03-16 19:00:20 -07001196 * Tries to put the activities in the stack to sleep.
1197 *
1198 * If the stack is not in a state where its activities can be put to sleep, this function will
1199 * start any necessary actions to move the stack into such a state. It is expected that this
1200 * function get called again when those actions complete.
1201 *
1202 * @param shuttingDown true when the called because the device is shutting down.
David Stevens18abd0e2017-08-17 14:55:47 -07001203 * @return true if the stack finished going to sleep, false if the stack only started the
1204 * process of going to sleep (checkReadyForSleep will be called when that process finishes).
Craig Mautner0eea92c2013-05-16 13:35:39 -07001205 */
David Stevens9440dc82017-03-16 19:00:20 -07001206 boolean goToSleepIfPossible(boolean shuttingDown) {
1207 boolean shouldSleep = true;
1208
Craig Mautner0eea92c2013-05-16 13:35:39 -07001209 if (mResumedActivity != null) {
1210 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001211 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
1212 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1213 "Sleep => pause with userLeaving=false");
Bryce Lee5daa3122017-04-19 10:40:42 -07001214
1215 // If we are in the middle of resuming the top activity in
1216 // {@link #resumeTopActivityUncheckedLocked}, mResumedActivity will be set but not
1217 // resumed yet. We must not proceed pausing the activity here. This method will be
David Stevens9440dc82017-03-16 19:00:20 -07001218 // called again if necessary as part of {@link #checkReadyForSleep} or
Bryce Lee5daa3122017-04-19 10:40:42 -07001219 // {@link ActivityStackSupervisor#checkReadyForSleepLocked}.
1220 if (mStackSupervisor.inResumeTopActivity) {
1221 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "In the middle of resuming top activity "
1222 + mResumedActivity);
David Stevens9440dc82017-03-16 19:00:20 -07001223 } else {
1224 startPausingLocked(false, true, null, false);
Bryce Lee5daa3122017-04-19 10:40:42 -07001225 }
David Stevens9440dc82017-03-16 19:00:20 -07001226 shouldSleep = false ;
1227 } else if (mPausingActivity != null) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001228 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001229 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
David Stevens9440dc82017-03-16 19:00:20 -07001230 shouldSleep = false;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001231 }
David Stevens9440dc82017-03-16 19:00:20 -07001232
1233 if (!shuttingDown) {
1234 if (containsActivityFromStack(mStackSupervisor.mStoppingActivities)) {
1235 // Still need to tell some activities to stop; can't sleep yet.
1236 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
1237 + mStackSupervisor.mStoppingActivities.size() + " activities");
1238
1239 mStackSupervisor.scheduleIdleLocked();
1240 shouldSleep = false;
1241 }
1242
1243 if (containsActivityFromStack(mStackSupervisor.mGoingToSleepActivities)) {
1244 // Still need to tell some activities to sleep; can't sleep yet.
1245 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
1246 + mStackSupervisor.mGoingToSleepActivities.size() + " activities");
1247 shouldSleep = false;
1248 }
1249 }
1250
1251 if (shouldSleep) {
1252 goToSleep();
1253 }
1254
David Stevens18abd0e2017-08-17 14:55:47 -07001255 return shouldSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -07001256 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001257
David Stevens18abd0e2017-08-17 14:55:47 -07001258 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001259 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001260
Andrii Kulianf9949d52016-05-06 12:53:25 -07001261 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001262 // This ensures that the activity's onStop() is called.
1263 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1264 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1265 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1266 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07001267 if (r.state == STOPPING || r.state == STOPPED
Andrii Kulianf9949d52016-05-06 12:53:25 -07001268 || r.state == ActivityState.PAUSED || r.state == ActivityState.PAUSING) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001269 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001270 }
1271 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001272 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001273 }
Craig Mautner59c00972012-07-30 12:10:24 -07001274
David Stevens9440dc82017-03-16 19:00:20 -07001275 private boolean containsActivityFromStack(List<ActivityRecord> rs) {
1276 for (ActivityRecord r : rs) {
1277 if (r.getStack() == this) {
1278 return true;
1279 }
1280 }
1281 return false;
1282 }
1283
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001284 /**
Winson Chung4dabf232017-01-25 13:25:22 -08001285 * Schedule a pause timeout in case the app doesn't respond. We don't give it much time because
1286 * this directly impacts the responsiveness seen by the user.
1287 */
1288 private void schedulePauseTimeout(ActivityRecord r) {
1289 final Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1290 msg.obj = r;
1291 r.pauseTime = SystemClock.uptimeMillis();
1292 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1293 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
1294 }
1295
1296 /**
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001297 * Start pausing the currently resumed activity. It is an error to call this if there
1298 * is already an activity being paused or there is no resumed activity.
1299 *
1300 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1301 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1302 * screen turning off).
Wale Ogunwale950faff2016-08-08 09:51:04 -07001303 * @param resuming The activity we are currently trying to resume or null if this is not being
1304 * called as part of resuming the top activity, so we shouldn't try to instigate
1305 * a resume here if not null.
Winson Chung6954fc92017-03-24 16:22:12 -07001306 * @param pauseImmediately True if the caller does not want to wait for the activity callback to
1307 * complete pausing.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001308 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1309 * it to tell us when it is done.
1310 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001311 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
Winson Chung6954fc92017-03-24 16:22:12 -07001312 ActivityRecord resuming, boolean pauseImmediately) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001313 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001314 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1315 + " state=" + mPausingActivity.state);
David Stevens9440dc82017-03-16 19:00:20 -07001316 if (!shouldSleepActivities()) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001317 // Avoid recursion among check for sleep and complete pause during sleeping.
1318 // Because activity will be paused immediately after resume, just let pause
1319 // be completed by the order of activity paused from clients.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001320 completePauseLocked(false, resuming);
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001321 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001322 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001323 ActivityRecord prev = mResumedActivity;
Bryce Lee5daa3122017-04-19 10:40:42 -07001324
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001325 if (prev == null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07001326 if (resuming == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001327 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001328 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001329 }
1330 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001331 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001332
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001333 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001334 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001335 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001336 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001337 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001338 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1339 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001340 prev.state = ActivityState.PAUSING;
Bryce Leeaf691c02017-03-20 14:20:22 -07001341 prev.getTask().touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001342 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001343 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -07001344 if (mService.mHasRecents
Bryce Leeaf691c02017-03-20 14:20:22 -07001345 && (next == null || next.noDisplay || next.getTask() != prev.getTask()
1346 || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001347 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001348 }
Dianne Hackborncee04b52013-07-03 17:01:28 -07001349 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001350
1351 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001352
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001353 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001354 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001355 try {
1356 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001357 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001358 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001359 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001360 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Winson Chung6954fc92017-03-24 16:22:12 -07001361 userLeaving, prev.configChangeFlags, pauseImmediately);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001362 } catch (Exception e) {
1363 // Ignore exception, if process died other code will cleanup.
1364 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001365 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001367 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001368 }
1369 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001370 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001371 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001372 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001373 }
1374
1375 // If we are not going to sleep, we want to ensure the device is
1376 // awake until the next activity is started.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001377 if (!uiSleeping && !mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001378 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001379 }
1380
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001381 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001382 // Have the window manager pause its key dispatching until the new
1383 // activity has started. If we're pausing the activity just because
1384 // the screen is being turned off and the UI is sleeping, don't interrupt
1385 // key dispatch; the same activity will pick it up again on wakeup.
1386 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001387 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001388 } else if (DEBUG_PAUSE) {
1389 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 }
1391
Winson Chung6954fc92017-03-24 16:22:12 -07001392 if (pauseImmediately) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001393 // If the caller said they don't want to wait for the pause, then complete
1394 // the pause now.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001395 completePauseLocked(false, resuming);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001396 return false;
1397
1398 } else {
Winson Chung4dabf232017-01-25 13:25:22 -08001399 schedulePauseTimeout(prev);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001400 return true;
1401 }
1402
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001403 } else {
1404 // This activity failed to schedule the
1405 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001406 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Wale Ogunwale950faff2016-08-08 09:51:04 -07001407 if (resuming == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001408 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001409 }
1410 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001411 }
1412 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001413
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001414 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001415 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1416 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417
Craig Mautnerd2328952013-03-05 12:46:26 -08001418 final ActivityRecord r = isInStackLocked(token);
1419 if (r != null) {
1420 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1421 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001422 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001423 + (timeout ? " (due to timeout)" : " (pause complete)"));
Jorim Jaggife762342016-10-13 14:33:27 +02001424 mService.mWindowManager.deferSurfaceLayout();
1425 try {
Winson Chung3f103eb2017-04-12 21:53:59 -07001426 completePauseLocked(true /* resumeNext */, null /* resumingActivity */);
Jorim Jaggife762342016-10-13 14:33:27 +02001427 } finally {
1428 mService.mWindowManager.continueSurfaceLayout();
1429 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001430 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001431 } else {
1432 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1433 r.userId, System.identityHashCode(r), r.shortComponentName,
1434 mPausingActivity != null
1435 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001436 if (r.state == ActivityState.PAUSING) {
1437 r.state = ActivityState.PAUSED;
1438 if (r.finishing) {
1439 if (DEBUG_PAUSE) Slog.v(TAG,
1440 "Executing finish of failed to pause activity: " + r);
1441 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1442 }
louis_chang047dfd42015-04-08 16:35:55 +08001443 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444 }
1445 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001446 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001447 }
1448
Wale Ogunwale950faff2016-08-08 09:51:04 -07001449 private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001450 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001451 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001452
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001453 if (prev != null) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07001454 final boolean wasStopping = prev.state == STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001455 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001456 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001457 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001458 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001459 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001460 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1461 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Bryce Lee4a194382017-04-04 14:32:48 -07001462 if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001463 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1464 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001465 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001466 if (prev.deferRelaunchUntilPaused) {
1467 // Complete the deferred relaunch that was waiting for pause to complete.
1468 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001469 prev.relaunchActivityLocked(false /* andResume */,
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001470 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001471 } else if (wasStopping) {
1472 // We are also stopping, the stop request must have gone soon after the pause.
1473 // We can't clobber it, because the stop confirmation will not be handled.
1474 // We don't need to schedule another stop, we only need to let it happen.
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07001475 prev.state = STOPPING;
David Stevens9440dc82017-03-16 19:00:20 -07001476 } else if (!prev.visible || shouldSleepOrShutDownActivities()) {
Wale Ogunwaleec950642017-04-25 07:44:21 -07001477 // Clear out any deferred client hide we might currently have.
1478 prev.setDeferHidingClient(false);
Jose Lima4b6c6692014-08-12 17:41:12 -07001479 // If we were visible then resumeTopActivities will release resources before
Wale Ogunwaleec950642017-04-25 07:44:21 -07001480 // stopping.
Winson Chung4dabf232017-01-25 13:25:22 -08001481 addToStopping(prev, true /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001482 }
1483 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001484 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001485 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001486 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001487 // It is possible the activity was freezing the screen before it was paused.
1488 // In that case go ahead and remove the freeze this activity has on the screen
1489 // since it is no longer visible.
Wale Ogunwalee8524002016-09-13 16:34:57 -07001490 if (prev != null) {
1491 prev.stopFreezingScreenLocked(true /*force*/);
1492 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001493 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001495
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001496 if (resumeNext) {
1497 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
David Stevens9440dc82017-03-16 19:00:20 -07001498 if (!topStack.shouldSleepOrShutDownActivities()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001499 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001500 } else {
David Stevens9440dc82017-03-16 19:00:20 -07001501 checkReadyForSleep();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001502 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001503 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001504 // If there are no more activities available to run, do resume anyway to start
1505 // something. Also if the top activity on the stack is not the just paused
1506 // activity, we need to go ahead and resume it to ensure we complete an
1507 // in-flight app switch.
1508 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001509 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001510 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001512
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001513 if (prev != null) {
1514 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001515
Craig Mautner525f3d92013-05-07 14:01:50 -07001516 if (prev.app != null && prev.cpuTimeAtResume > 0
1517 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001518 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1519 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001520 if (diff > 0) {
1521 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1522 synchronized (bsi) {
1523 BatteryStatsImpl.Uid.Proc ps =
1524 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001525 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001526 if (ps != null) {
1527 ps.addForegroundTimeLocked(diff);
1528 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 }
1530 }
1531 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001532 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001534
Andrii Kulian8290f8f2016-06-30 17:51:32 -07001535 // Notify when the task stack has changed, but only if visibilities changed (not just
1536 // focus). Also if there is an active pinned stack - we always want to notify it about
1537 // task stack changes, because its positioning may depend on it.
1538 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause
1539 || mService.mStackSupervisor.getStack(PINNED_STACK_ID) != null) {
Yorke Leebd54c2a2016-10-25 13:49:23 -07001540 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001541 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1542 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001543
Wale Ogunwale950faff2016-08-08 09:51:04 -07001544 mStackSupervisor.ensureActivitiesVisibleLocked(resuming, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 }
1546
Winson Chung4dabf232017-01-25 13:25:22 -08001547 void addToStopping(ActivityRecord r, boolean scheduleIdle, boolean idleDelayed) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001548 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1549 mStackSupervisor.mStoppingActivities.add(r);
1550 }
1551
1552 // If we already have a few activities waiting to stop, then give up
1553 // on things going idle and start clearing them out. Or if r is the
1554 // last of activity of the last task the stack will be empty and must
1555 // be cleared immediately.
1556 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1557 || (r.frontOfTask && mTaskHistory.size() <= 1);
Winson Chung4dabf232017-01-25 13:25:22 -08001558 if (scheduleIdle || forceIdle) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001559 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
Winson Chung4dabf232017-01-25 13:25:22 -08001560 + forceIdle + "immediate=" + !idleDelayed);
1561 if (!idleDelayed) {
1562 mStackSupervisor.scheduleIdleLocked();
1563 } else {
1564 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1565 }
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001566 } else {
David Stevens9440dc82017-03-16 19:00:20 -07001567 checkReadyForSleep();
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001568 }
1569 }
1570
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001571 // Find the first visible activity above the passed activity and if it is translucent return it
1572 // otherwise return null;
1573 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001574 TaskRecord task = r.getTask();
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001575 if (task == null) {
1576 return null;
1577 }
1578
Andrii Kulian02b7a832016-10-06 23:11:56 -07001579 final ActivityStack stack = task.getStack();
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001580 if (stack == null) {
1581 return null;
1582 }
1583
1584 int stackNdx = mStacks.indexOf(stack);
1585
1586 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1587 int taskNdx = tasks.indexOf(task);
1588
1589 ArrayList<ActivityRecord> activities = task.mActivities;
1590 int activityNdx = activities.indexOf(r) + 1;
1591
1592 final int numStacks = mStacks.size();
1593 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001594 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001595 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001596 final int numTasks = tasks.size();
1597 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001598 final TaskRecord currentTask = tasks.get(taskNdx);
1599 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001600 final int numActivities = activities.size();
1601 while (activityNdx < numActivities) {
1602 final ActivityRecord activity = activities.get(activityNdx);
1603 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001604 return historyStack.mFullscreen
1605 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001606 }
1607 ++activityNdx;
1608 }
1609 activityNdx = 0;
1610 ++taskNdx;
1611 }
1612 taskNdx = 0;
1613 ++stackNdx;
1614 }
1615
1616 return null;
1617 }
1618
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001619 /** Returns true if the stack contains a fullscreen task. */
1620 private boolean hasFullscreenTask() {
1621 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1622 final TaskRecord task = mTaskHistory.get(i);
1623 if (task.mFullscreen) {
1624 return true;
1625 }
1626 }
1627 return false;
1628 }
1629
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001630 /**
1631 * Returns true if the stack is translucent and can have other contents visible behind it if
1632 * needed. A stack is considered translucent if it don't contain a visible or
1633 * starting (about to be visible) activity that is fullscreen (opaque).
1634 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001635 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001636 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001637 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001638 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1639 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001640 final ArrayList<ActivityRecord> activities = task.mActivities;
1641 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1642 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001643
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001644 if (r.finishing) {
1645 // We don't factor in finishing activities when determining translucency since
1646 // they will be gone soon.
1647 continue;
1648 }
1649
1650 if (!r.visible && r != starting) {
1651 // Also ignore invisible activities that are not the currently starting
1652 // activity (about to be visible).
1653 continue;
1654 }
1655
1656 if (r.fullscreen) {
1657 // Stack isn't translucent if it has at least one fullscreen activity
1658 // that is visible.
1659 return false;
1660 }
1661
Winson Chunge74ad8d2017-02-17 15:12:25 -08001662 if (!isHomeOrRecentsStack() && r.frontOfTask && task.isOverHomeStack()
1663 && !StackId.isHomeOrRecentsStack(stackBehindId) && !isAssistantStack()) {
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001664 // Stack isn't translucent if it's top activity should have the home stack
Winson Chunge74ad8d2017-02-17 15:12:25 -08001665 // behind it and the stack currently behind it isn't the home or recents stack
1666 // or the assistant stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001667 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001668 }
1669 }
1670 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001671 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001672 }
1673
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001674 /** Returns true if the stack is currently considered visible. */
1675 boolean isVisible() {
Winson Chung47900652017-04-06 18:44:25 -07001676 return mWindowContainerController != null && mWindowContainerController.isVisible()
1677 && !mForceHidden;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001678 }
1679
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001680 /**
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001681 * Returns what the stack visibility should be: {@link #STACK_INVISIBLE} or
1682 * {@link #STACK_VISIBLE}.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001683 *
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001684 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001685 */
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001686 int shouldBeVisible(ActivityRecord starting) {
Winson Chung47900652017-04-06 18:44:25 -07001687 if (!isAttached() || mForceHidden) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001688 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001689 }
1690
Andrii Kuliane8d928a2016-11-14 18:38:14 -08001691 if (mStackSupervisor.isFrontStackOnDisplay(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001692 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001693 }
1694
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001695 final int stackIndex = mStacks.indexOf(this);
1696
1697 if (stackIndex == mStacks.size() - 1) {
1698 Slog.wtf(TAG,
1699 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001700 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001701 }
1702
Andrii Kulian0864bbb2017-02-16 15:45:58 -08001703 // Check position and visibility of this stack relative to the front stack on its display.
1704 final ActivityStack topStack = getTopStackOnDisplay();
1705 final int topStackId = topStack.mStackId;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001706
1707 if (mStackId == DOCKED_STACK_ID) {
Winson Chunge74ad8d2017-02-17 15:12:25 -08001708 // If the assistant stack is focused and translucent, then the docked stack is always
1709 // visible
Winson Chung521f0112017-03-13 16:14:45 -07001710 if (topStack.isAssistantStack()) {
1711 return (topStack.isStackTranslucent(starting, DOCKED_STACK_ID)) ? STACK_VISIBLE
1712 : STACK_INVISIBLE;
Winson Chunge74ad8d2017-02-17 15:12:25 -08001713 }
Matthew Ng6875d472017-05-01 13:59:50 -07001714 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001715 }
1716
Matthew Ng330757d2017-02-28 14:19:17 -08001717 // Set home stack to invisible when it is below but not immediately below the docked stack
1718 // A case would be if recents stack exists but has no tasks and is below the docked stack
1719 // and home stack is below recents
1720 if (mStackId == HOME_STACK_ID) {
1721 int dockedStackIndex = mStacks.indexOf(mStackSupervisor.getStack(DOCKED_STACK_ID));
1722 if (dockedStackIndex > stackIndex && stackIndex != dockedStackIndex - 1) {
1723 return STACK_INVISIBLE;
1724 }
1725 }
1726
Andrii Kulian0864bbb2017-02-16 15:45:58 -08001727 // Find the first stack behind front stack that actually got something visible.
1728 int stackBehindTopIndex = mStacks.indexOf(topStack) - 1;
1729 while (stackBehindTopIndex >= 0 &&
1730 mStacks.get(stackBehindTopIndex).topRunningActivityLocked() == null) {
1731 stackBehindTopIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001732 }
Andrii Kulian0864bbb2017-02-16 15:45:58 -08001733 final int stackBehindTopId = (stackBehindTopIndex >= 0)
1734 ? mStacks.get(stackBehindTopIndex).mStackId : INVALID_STACK_ID;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001735 final boolean alwaysOnTop = topStack.getWindowConfiguration().isAlwaysOnTop();
1736 if (topStackId == DOCKED_STACK_ID || alwaysOnTop) {
Winson Chungdd7593a2017-07-05 18:02:36 -07001737 if (stackIndex == stackBehindTopIndex) {
1738 // Stacks directly behind the docked or pinned stack are always visible.
1739 return STACK_VISIBLE;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001740 } else if (alwaysOnTop && stackIndex == stackBehindTopIndex - 1) {
Winson Chungdd7593a2017-07-05 18:02:36 -07001741 // Otherwise, this stack can also be visible if it is directly behind a docked stack
1742 // or translucent assistant stack behind an always-on-top top-most stack
1743 if (stackBehindTopId == DOCKED_STACK_ID) {
1744 return STACK_VISIBLE;
1745 } else if (stackBehindTopId == ASSISTANT_STACK_ID) {
1746 return mStacks.get(stackBehindTopIndex).isStackTranslucent(starting, mStackId)
1747 ? STACK_VISIBLE : STACK_INVISIBLE;
1748 }
1749 }
Matthew Ng6875d472017-05-01 13:59:50 -07001750 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001751
Andrii Kulian0864bbb2017-02-16 15:45:58 -08001752 if (StackId.isBackdropToTranslucentActivity(topStackId)
1753 && topStack.isStackTranslucent(starting, stackBehindTopId)) {
Winson Chung83471632016-12-13 11:02:12 -08001754 // Stacks behind the fullscreen or assistant stack with a translucent activity are
1755 // always visible so they can act as a backdrop to the translucent activity.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001756 // For example, dialog activities
Andrii Kulian0864bbb2017-02-16 15:45:58 -08001757 if (stackIndex == stackBehindTopIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001758 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001759 }
Andrii Kulian0864bbb2017-02-16 15:45:58 -08001760 if (stackBehindTopIndex >= 0) {
1761 if ((stackBehindTopId == DOCKED_STACK_ID
1762 || stackBehindTopId == PINNED_STACK_ID)
1763 && stackIndex == (stackBehindTopIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001764 // The stack behind the docked or pinned stack is also visible so we can have a
1765 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001766 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001767 }
1768 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001769 }
1770
Wale Ogunwale3797c222015-10-27 14:21:58 -07001771 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001772 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001773 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001774 }
1775
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001776 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001777 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001778
1779 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1780 continue;
1781 }
1782
Wale Ogunwale3797c222015-10-27 14:21:58 -07001783 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001784 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001785 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001786 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001787
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001788 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001789 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001790 }
1791 }
1792
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001793 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001794 }
1795
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001796 final int rankTaskLayers(int baseLayer) {
1797 int layer = 0;
1798 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1799 final TaskRecord task = mTaskHistory.get(taskNdx);
1800 ActivityRecord r = task.topRunningActivityLocked();
1801 if (r == null || r.finishing || !r.visible) {
1802 task.mLayerRank = -1;
1803 } else {
1804 task.mLayerRank = baseLayer + layer++;
1805 }
1806 }
1807 return layer;
1808 }
1809
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 /**
1811 * Make sure that all activities that need to be visible (that is, they
1812 * currently can be seen by the user) actually are.
1813 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001814 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1815 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02001816 mTopActivityOccludesKeyguard = false;
1817 mTopDismissingKeyguardActivity = null;
1818 mStackSupervisor.mKeyguardController.beginActivityVisibilityUpdate();
1819 try {
1820 ActivityRecord top = topRunningActivityLocked();
1821 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
1822 + " configChanges=0x" + Integer.toHexString(configChanges));
1823 if (top != null) {
1824 checkTranslucentActivityWaiting(top);
1825 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001826
Jorim Jaggife762342016-10-13 14:33:27 +02001827 // If the top activity is not fullscreen, then we need to
1828 // make sure any activities under it are now visible.
1829 boolean aboveTop = top != null;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001830 final int stackVisibility = shouldBeVisible(starting);
Jorim Jaggife762342016-10-13 14:33:27 +02001831 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
Jorim Jaggife762342016-10-13 14:33:27 +02001832 boolean behindFullscreenActivity = stackInvisible;
1833 boolean resumeNextActivity = mStackSupervisor.isFocusedStack(this)
1834 && (isInStackLocked(starting) == null);
1835 boolean behindTranslucentActivity = false;
Jorim Jaggife762342016-10-13 14:33:27 +02001836 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1837 final TaskRecord task = mTaskHistory.get(taskNdx);
1838 final ArrayList<ActivityRecord> activities = task.mActivities;
1839 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1840 final ActivityRecord r = activities.get(activityNdx);
1841 if (r.finishing) {
1842 // Normally the screenshot will be taken in makeInvisible(). When an activity
1843 // is finishing, we no longer change its visibility, but we still need to take
1844 // the screenshots if startPausingLocked decided it should be taken.
1845 if (r.mUpdateTaskThumbnailWhenHidden) {
1846 r.updateThumbnailLocked(r.screenshotActivityLocked(),
1847 null /* description */);
1848 r.mUpdateTaskThumbnailWhenHidden = false;
1849 }
1850 continue;
Chong Zhang22bc8512016-04-07 13:47:18 -07001851 }
Jorim Jaggife762342016-10-13 14:33:27 +02001852 final boolean isTop = r == top;
1853 if (aboveTop && !isTop) {
1854 continue;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001855 }
Jorim Jaggife762342016-10-13 14:33:27 +02001856 aboveTop = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001857
Jorim Jaggife762342016-10-13 14:33:27 +02001858 // Check whether activity should be visible without Keyguard influence
Jorim Jaggi241ae102016-11-02 21:57:33 -07001859 final boolean visibleIgnoringKeyguard = r.shouldBeVisibleIgnoringKeyguard(
Jorim Jaggi241ae102016-11-02 21:57:33 -07001860 behindFullscreenActivity);
1861 r.visibleIgnoringKeyguard = visibleIgnoringKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +02001862
1863 // Now check whether it's really visible depending on Keyguard state.
Jorim Jaggi241ae102016-11-02 21:57:33 -07001864 final boolean reallyVisible = checkKeyguardVisibility(r,
1865 visibleIgnoringKeyguard, isTop);
1866 if (visibleIgnoringKeyguard) {
Jorim Jaggife762342016-10-13 14:33:27 +02001867 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1868 behindFullscreenActivity, task, r);
1869 if (behindFullscreenActivity && !r.fullscreen) {
1870 behindTranslucentActivity = true;
1871 }
1872 }
1873 if (reallyVisible) {
1874 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1875 + " finishing=" + r.finishing + " state=" + r.state);
1876 // First: if this is not the current activity being started, make
1877 // sure it matches the current configuration.
1878 if (r != starting) {
1879 r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindows);
1880 }
1881
1882 if (r.app == null || r.app.thread == null) {
1883 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
1884 resumeNextActivity, r)) {
1885 if (activityNdx >= activities.size()) {
1886 // Record may be removed if its process needs to restart.
1887 activityNdx = activities.size() - 1;
1888 } else {
1889 resumeNextActivity = false;
1890 }
1891 }
1892 } else if (r.visible) {
1893 // If this activity is already visible, then there is nothing to do here.
1894 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1895 "Skipping: already visible at " + r);
1896
1897 if (r.handleAlreadyVisible()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001898 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001899 }
Jorim Jaggife762342016-10-13 14:33:27 +02001900 } else {
1901 r.makeVisibleIfNeeded(starting);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001902 }
Jorim Jaggife762342016-10-13 14:33:27 +02001903 // Aggregate current change flags.
1904 configChanges |= r.configChangeFlags;
Craig Mautnerbb742462014-07-07 15:28:55 -07001905 } else {
Jorim Jaggife762342016-10-13 14:33:27 +02001906 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
1907 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1908 + stackInvisible + " behindFullscreenActivity="
1909 + behindFullscreenActivity + " mLaunchTaskBehind="
1910 + r.mLaunchTaskBehind);
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001911 makeInvisible(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001912 }
Jorim Jaggife762342016-10-13 14:33:27 +02001913 }
1914 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1915 // The visibility of tasks and the activities they contain in freeform stack are
1916 // determined individually unlike other stacks where the visibility or fullscreen
1917 // status of an activity in a previous task affects other.
1918 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
1919 } else if (mStackId == HOME_STACK_ID) {
Wale Ogunwale069bbd32017-02-03 07:58:14 -08001920 if (task.isHomeTask()) {
Jorim Jaggife762342016-10-13 14:33:27 +02001921 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1922 + " stackInvisible=" + stackInvisible
1923 + " behindFullscreenActivity=" + behindFullscreenActivity);
1924 // No other task in the home stack should be visible behind the home activity.
1925 // Home activities is usually a translucent activity with the wallpaper behind
1926 // them. However, when they don't have the wallpaper behind them, we want to
1927 // show activities in the next application stack behind them vs. another
1928 // task in the home stack like recents.
1929 behindFullscreenActivity = true;
1930 } else if (task.isRecentsTask()
1931 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1932 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1933 "Recents task returning to app: at " + task
1934 + " stackInvisible=" + stackInvisible
1935 + " behindFullscreenActivity=" + behindFullscreenActivity);
1936 // We don't want any other tasks in the home stack visible if the recents
1937 // activity is going to be returning to an application activity type.
1938 // We do this to preserve the visible order the user used to get into the
1939 // recents activity. The recents activity is normally translucent and if it
1940 // doesn't have the wallpaper behind it the next activity in the home stack
1941 // shouldn't be visible when the home stack is brought to the front to display
1942 // the recents activity from an app.
1943 behindFullscreenActivity = true;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001944 }
Winson Chung1cebea62017-06-26 17:22:27 -07001945 } else if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
1946 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping after task=" + task
1947 + " returning to non-application type=" + task.getTaskToReturnTo());
Winson Chung03157022017-07-19 11:58:59 -07001948 // Once we reach a fullscreen stack task that has a running activity and should
1949 // return to another stack task, then no other activities behind that one should
1950 // be visible.
1951 if (task.topRunningActivityLocked() != null &&
1952 task.getTaskToReturnTo() != APPLICATION_ACTIVITY_TYPE) {
Winson Chung1cebea62017-06-26 17:22:27 -07001953 behindFullscreenActivity = true;
1954 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 }
1956 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001957
Jorim Jaggife762342016-10-13 14:33:27 +02001958 if (mTranslucentActivityWaiting != null &&
1959 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1960 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1961 notifyActivityDrawnLocked(null);
1962 }
1963 } finally {
1964 mStackSupervisor.mKeyguardController.endActivityVisibilityUpdate();
1965 }
1966 }
1967
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01001968 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
1969 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1970 mTaskHistory.get(taskNdx).addStartingWindowsForVisibleActivities(taskSwitch);
1971 }
1972 }
1973
Jorim Jaggife762342016-10-13 14:33:27 +02001974 /**
1975 * @return true if the top visible activity wants to occlude the Keyguard, false otherwise
1976 */
1977 boolean topActivityOccludesKeyguard() {
1978 return mTopActivityOccludesKeyguard;
1979 }
1980
1981 /**
1982 * @return the top most visible activity that wants to dismiss Keyguard
1983 */
1984 ActivityRecord getTopDismissingKeyguardActivity() {
1985 return mTopDismissingKeyguardActivity;
1986 }
1987
1988 /**
1989 * Checks whether {@param r} should be visible depending on Keyguard state and updates
1990 * {@link #mTopActivityOccludesKeyguard} and {@link #mTopDismissingKeyguardActivity} if
1991 * necessary.
1992 *
1993 * @return true if {@param r} is visible taken Keyguard state into account, false otherwise
1994 */
Wale Ogunwaled334a052017-05-12 07:17:36 -07001995 boolean checkKeyguardVisibility(ActivityRecord r, boolean shouldBeVisible,
Jorim Jaggife762342016-10-13 14:33:27 +02001996 boolean isTop) {
Winson Chung0655da02016-12-16 17:05:01 -08001997 final boolean isInPinnedStack = r.getStack().getStackId() == PINNED_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +02001998 final boolean keyguardShowing = mStackSupervisor.mKeyguardController.isKeyguardShowing();
1999 final boolean keyguardLocked = mStackSupervisor.mKeyguardController.isKeyguardLocked();
chaviw59b98852017-06-13 12:05:44 -07002000 final boolean showWhenLocked = r.canShowWhenLocked() && !isInPinnedStack;
Jorim Jaggie69c9312016-10-31 18:24:38 -07002001 final boolean dismissKeyguard = r.hasDismissKeyguardWindows();
Jorim Jaggife762342016-10-13 14:33:27 +02002002 if (shouldBeVisible) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002003 if (dismissKeyguard && mTopDismissingKeyguardActivity == null) {
Jorim Jaggife762342016-10-13 14:33:27 +02002004 mTopDismissingKeyguardActivity = r;
2005 }
2006
2007 // Only the top activity may control occluded, as we can't occlude the Keyguard if the
2008 // top app doesn't want to occlude it.
2009 if (isTop) {
2010 mTopActivityOccludesKeyguard |= showWhenLocked;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07002011 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002012
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002013 final boolean canShowWithKeyguard = canShowWithInsecureKeyguard()
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002014 && mStackSupervisor.mKeyguardController.canDismissKeyguard();
2015 if (canShowWithKeyguard) {
2016 return true;
2017 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018 }
Jorim Jaggife762342016-10-13 14:33:27 +02002019 if (keyguardShowing) {
Craig Mautnereb8abf72014-07-02 15:04:09 -07002020
Jorim Jaggie69c9312016-10-31 18:24:38 -07002021 // If keyguard is showing, nothing is visible, except if we are able to dismiss Keyguard
2022 // right away.
2023 return shouldBeVisible && mStackSupervisor.mKeyguardController
Jorim Jaggi07961872016-11-23 11:28:57 +01002024 .canShowActivityWhileKeyguardShowing(r, dismissKeyguard);
Jorim Jaggife762342016-10-13 14:33:27 +02002025 } else if (keyguardLocked) {
Jorim Jaggi07961872016-11-23 11:28:57 +01002026 return shouldBeVisible && mStackSupervisor.mKeyguardController.canShowWhileOccluded(
2027 dismissKeyguard, showWhenLocked);
Jorim Jaggife762342016-10-13 14:33:27 +02002028 } else {
2029 return shouldBeVisible;
Craig Mautnereb8abf72014-07-02 15:04:09 -07002030 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002031 }
Craig Mautner58547802013-03-05 08:23:53 -08002032
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002033 /**
2034 * Check if the display to which this stack is attached has
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002035 * {@link Display#FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD} applied.
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002036 */
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002037 private boolean canShowWithInsecureKeyguard() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002038 final ActivityStackSupervisor.ActivityDisplay activityDisplay = getDisplay();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002039 if (activityDisplay == null) {
2040 throw new IllegalStateException("Stack is not attached to any display, stackId="
2041 + mStackId);
2042 }
2043
2044 final int flags = activityDisplay.mDisplay.getFlags();
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002045 return (flags & FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002046 }
2047
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002048 private void checkTranslucentActivityWaiting(ActivityRecord top) {
2049 if (mTranslucentActivityWaiting != top) {
2050 mUndrawnActivitiesBelowTopTranslucent.clear();
2051 if (mTranslucentActivityWaiting != null) {
2052 // Call the callback with a timeout indication.
2053 notifyActivityDrawnLocked(null);
2054 mTranslucentActivityWaiting = null;
2055 }
2056 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2057 }
2058 }
2059
2060 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08002061 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002062 // We need to make sure the app is running if it's the top, or it is just made visible from
2063 // invisible. If the app is already visible, it must have died while it was visible. In this
2064 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
2065 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002066 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002067 // This activity needs to be visible, but isn't even running...
2068 // get it started and resume if no other stack in this stack is resumed.
2069 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
2070 if (r != starting) {
2071 r.startFreezingScreenLocked(r.app, configChanges);
2072 }
2073 if (!r.visible || r.mLaunchTaskBehind) {
2074 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002075 r.setVisible(true);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002076 }
2077 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002078 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002079 return true;
2080 }
2081 }
2082 return false;
2083 }
2084
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002085 // TODO: Should probably be moved into ActivityRecord.
2086 private void makeInvisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002087 if (!r.visible) {
2088 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
2089 return;
2090 }
2091 // Now for any activities that aren't visible to the user, make sure they no longer are
2092 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08002093 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002094 try {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002095 final boolean canEnterPictureInPicture = r.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002096 "makeInvisible", true /* beforeStopping */);
Wale Ogunwale89973222017-04-23 18:39:45 -07002097 // Defer telling the client it is hidden if it can enter Pip and isn't current stopped
2098 // or stopping. This gives it a chance to enter Pip in onPause().
2099 final boolean deferHidingClient = canEnterPictureInPicture
2100 && r.state != STOPPING && r.state != STOPPED;
Wale Ogunwaleec950642017-04-25 07:44:21 -07002101 r.setDeferHidingClient(deferHidingClient);
2102 r.setVisible(false);
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002103
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002104 switch (r.state) {
2105 case STOPPING:
2106 case STOPPED:
2107 if (r.app != null && r.app.thread != null) {
2108 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2109 "Scheduling invisibility: " + r);
2110 r.app.thread.scheduleWindowVisibility(r.appToken, false);
2111 }
Winson Chungc2baac02017-01-11 13:34:47 -08002112
2113 // Reset the flag indicating that an app can enter picture-in-picture once the
2114 // activity is hidden
2115 r.supportsPictureInPictureWhilePausing = false;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002116 break;
2117
2118 case INITIALIZING:
2119 case RESUMED:
2120 case PAUSING:
2121 case PAUSED:
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002122 addToStopping(r, true /* scheduleIdle */,
2123 canEnterPictureInPicture /* idleDelayed */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002124 break;
2125
2126 default:
2127 break;
2128 }
2129 } catch (Exception e) {
2130 // Just skip on any failure; we'll make it visible when it next restarts.
2131 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
2132 }
2133 }
2134
2135 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
2136 TaskRecord task, ActivityRecord r) {
2137 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002138 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08002139 + " stackInvisible=" + stackInvisible
2140 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08002141 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002142 behindFullscreenActivity = true;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002143 } else if (!isHomeOrRecentsStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002144 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
2145 + " stackInvisible=" + stackInvisible
2146 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08002147 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002148 }
2149 return behindFullscreenActivity;
2150 }
2151
Todd Kennedyaab56db2015-01-30 09:39:53 -08002152 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002153 mTranslucentActivityWaiting = r;
2154 mUndrawnActivitiesBelowTopTranslucent.clear();
2155 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
2156 }
2157
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002158 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2159 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2160 final TaskRecord task = mTaskHistory.get(taskNdx);
2161 final ArrayList<ActivityRecord> activities = task.mActivities;
2162 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2163 final ActivityRecord r = activities.get(activityNdx);
2164 if ( r.appTimeTracker != except) {
2165 r.appTimeTracker = null;
2166 }
2167 }
2168 }
2169 }
2170
Craig Mautner5eda9b32013-07-02 11:58:16 -07002171 /**
2172 * Called as activities below the top translucent activity are redrawn. When the last one is
2173 * redrawn notify the top activity by calling
2174 * {@link Activity#onTranslucentConversionComplete}.
2175 *
2176 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2177 * occurred and the activity will be notified immediately.
2178 */
2179 void notifyActivityDrawnLocked(ActivityRecord r) {
2180 if ((r == null)
2181 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2182 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2183 // The last undrawn activity below the top has just been drawn. If there is an
2184 // opaque activity at the top, notify it that it can become translucent safely now.
2185 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2186 mTranslucentActivityWaiting = null;
2187 mUndrawnActivitiesBelowTopTranslucent.clear();
2188 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2189
Craig Mautner71dd1b62014-02-18 15:48:52 -08002190 if (waitingActivity != null) {
2191 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
2192 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
2193 try {
2194 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
2195 waitingActivity.appToken, r != null);
2196 } catch (RemoteException e) {
2197 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002198 }
2199 }
2200 }
2201 }
2202
Craig Mautnera61bc652013-10-28 15:43:18 -07002203 /** If any activities below the top running one are in the INITIALIZING state and they have a
2204 * starting window displayed then remove that starting window. It is possible that the activity
2205 * in this state will never resumed in which case that starting window will be orphaned. */
2206 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002207 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002208 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002209 // We don't want to clear starting window for activities that aren't behind fullscreen
2210 // activities as we need to display their starting window until they are done initializing.
2211 boolean behindFullscreenActivity = false;
Wale Ogunwale98742a52016-07-12 10:29:12 -07002212
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002213 if (shouldBeVisible(null) == STACK_INVISIBLE) {
Wale Ogunwale98742a52016-07-12 10:29:12 -07002214 // The stack is not visible, so no activity in it should be displaying a starting
2215 // window. Mark all activities below top and behind fullscreen.
2216 aboveTop = false;
2217 behindFullscreenActivity = true;
2218 }
2219
Craig Mautnera61bc652013-10-28 15:43:18 -07002220 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2221 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2222 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2223 final ActivityRecord r = activities.get(activityNdx);
2224 if (aboveTop) {
2225 if (r == topActivity) {
2226 aboveTop = false;
2227 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002228 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002229 continue;
2230 }
2231
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002232 r.removeOrphanedStartingWindow(behindFullscreenActivity);
Wale Ogunwale68741142016-05-17 09:40:02 -07002233 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002234 }
2235 }
2236 }
2237
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002238 /**
2239 * Ensure that the top activity in the stack is resumed.
2240 *
2241 * @param prev The previously resumed activity, for when in the process
2242 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002243 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002244 *
2245 * @return Returns true if something is being resumed, or false if
2246 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002247 *
2248 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2249 * non-focused stack to be resumed.
2250 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2251 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002252 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08002253 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002254 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002255 // Don't even start recursing.
2256 return false;
2257 }
2258
2259 boolean result = false;
2260 try {
2261 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002262 mStackSupervisor.inResumeTopActivity = true;
Craig Mautner544efa72014-09-04 16:41:20 -07002263 result = resumeTopActivityInnerLocked(prev, options);
2264 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002265 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002266 }
chaviw59b98852017-06-13 12:05:44 -07002267
Bryce Lee5daa3122017-04-19 10:40:42 -07002268 // When resuming the top activity, it may be necessary to pause the top activity (for
2269 // example, returning to the lock screen. We suppress the normal pause logic in
2270 // {@link #resumeTopActivityUncheckedLocked}, since the top activity is resumed at the end.
2271 // We call the {@link ActivityStackSupervisor#checkReadyForSleepLocked} again here to ensure
chaviw59b98852017-06-13 12:05:44 -07002272 // any necessary pause logic occurs. In the case where the Activity will be shown regardless
2273 // of the lock screen, the call to {@link ActivityStackSupervisor#checkReadyForSleepLocked}
2274 // is skipped.
2275 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
2276 if (next == null || !next.canTurnScreenOn()) {
David Stevens9440dc82017-03-16 19:00:20 -07002277 checkReadyForSleep();
chaviw59b98852017-06-13 12:05:44 -07002278 }
Bryce Lee5daa3122017-04-19 10:40:42 -07002279
Craig Mautner544efa72014-09-04 16:41:20 -07002280 return result;
2281 }
2282
Chong Zhang6cda19c2016-06-14 19:07:56 -07002283 void setResumedActivityLocked(ActivityRecord r, String reason) {
2284 // TODO: move mResumedActivity to stack supervisor,
2285 // there should only be 1 global copy of resumed activity.
2286 mResumedActivity = r;
Chong Zhangd2cb3112016-07-20 11:13:22 -07002287 r.state = ActivityState.RESUMED;
Chong Zhang6cda19c2016-06-14 19:07:56 -07002288 mService.setResumedActivityUncheckLocked(r, reason);
Bryce Leeaf691c02017-03-20 14:20:22 -07002289 final TaskRecord task = r.getTask();
2290 task.touchActiveTime();
2291 mRecentTasks.addLocked(task);
Chong Zhang6cda19c2016-06-14 19:07:56 -07002292 }
2293
Chong Zhang280d3322015-11-03 17:27:26 -08002294 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002295 if (!mService.mBooting && !mService.mBooted) {
2296 // Not ready yet!
2297 return false;
2298 }
2299
Winson Chung3f103eb2017-04-12 21:53:59 -07002300 // Find the next top-most activity to resume in this stack that is not finishing and is
2301 // focusable. If it is not focusable, we will fall into the case below to resume the
2302 // top activity in the next focusable task.
2303 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
Andrii Kulian7fc22812016-12-28 13:04:11 -08002304
2305 final boolean hasRunningActivity = next != null;
2306
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002307 // TODO: Maybe this entire condition can get removed?
2308 if (hasRunningActivity && getDisplay() == null) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002309 return false;
2310 }
2311
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002312 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002313
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314 // Remember how we'll process this pause/resume situation, and ensure
2315 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002316 final boolean userLeaving = mStackSupervisor.mUserLeaving;
2317 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002318
Andrii Kulian7fc22812016-12-28 13:04:11 -08002319 if (!hasRunningActivity) {
2320 // There are no activities left in the stack, let's look somewhere else.
2321 return resumeTopActivityInNextFocusableStack(prev, options, "noMoreActivities");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 }
2323
2324 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002325
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002327 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2328 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 // Make sure we have executed any pending transitions, since there
2330 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002331 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002332 if (DEBUG_STATES) Slog.d(TAG_STATES,
2333 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002334 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002335 return false;
2336 }
2337
Bryce Leeaf691c02017-03-20 14:20:22 -07002338 final TaskRecord nextTask = next.getTask();
2339 final TaskRecord prevTask = prev != null ? prev.getTask() : null;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002340 if (prevTask != null && prevTask.getStack() == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002341 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002342 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002343 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002344 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002345 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002346 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002347 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002348 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002349 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002350 } else if (!isOnHomeDisplay()) {
2351 return false;
2352 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002353 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002354 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002355 return isOnHomeDisplay() &&
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002356 mStackSupervisor.resumeHomeStackTask(prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002357 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002358 }
2359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 // If we are sleeping, and there is no resumed activity, and the top
2361 // activity is paused, well that is the state we want.
David Stevens9440dc82017-03-16 19:00:20 -07002362 if (shouldSleepOrShutDownActivities()
p13451dbad2872012-04-18 11:39:23 +09002363 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002364 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 // Make sure we have executed any pending transitions, since there
2366 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002367 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002368 if (DEBUG_STATES) Slog.d(TAG_STATES,
2369 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002370 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 return false;
2372 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002373
2374 // Make sure that the user who owns this activity is started. If not,
2375 // we will just leave it as is because someone should be bringing
2376 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002377 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002378 Slog.w(TAG, "Skipping resume of top activity " + next
2379 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002380 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002381 return false;
2382 }
2383
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 // The activity may be waiting for stop, but that is no longer
2385 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002386 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002387 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002388 next.sleeping = false;
Bryce Lee4a194382017-04-04 14:32:48 -07002389 mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002391 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002392
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002393 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002394 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002395 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002396 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002397 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002398 return false;
2399 }
2400
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002401 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2402
Wale Ogunwale89973222017-04-23 18:39:45 -07002403 boolean lastResumedCanPip = false;
2404 final ActivityStack lastFocusedStack = mStackSupervisor.getLastStack();
2405 if (lastFocusedStack != null && lastFocusedStack != this) {
2406 // So, why aren't we using prev here??? See the param comment on the method. prev doesn't
2407 // represent the last resumed activity. However, the last focus stack does if it isn't null.
2408 final ActivityRecord lastResumed = lastFocusedStack.mResumedActivity;
2409 lastResumedCanPip = lastResumed != null && lastResumed.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002410 "resumeTopActivity", userLeaving /* beforeStopping */);
Wale Ogunwale89973222017-04-23 18:39:45 -07002411 }
Winson Chungc2baac02017-01-11 13:34:47 -08002412 // If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002413 // to be paused, while at the same time resuming the new resume activity only if the
2414 // previous activity can't go into Pip since we want to give Pip activities a chance to
2415 // enter Pip before resuming the next activity.
Wale Ogunwale89973222017-04-23 18:39:45 -07002416 final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0
2417 && !lastResumedCanPip;
2418
Winson Chungc2baac02017-01-11 13:34:47 -08002419 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002420 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002421 if (DEBUG_STATES) Slog.d(TAG_STATES,
2422 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Winson Chungc2baac02017-01-11 13:34:47 -08002423 pausing |= startPausingLocked(userLeaving, false, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002424 }
Winson Chungc2baac02017-01-11 13:34:47 -08002425 if (pausing && !resumeWhilePausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002426 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002427 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002428 // At this point we want to put the upcoming activity's process
2429 // at the top of the LRU list, since we know we will be needing it
2430 // very soon and it would be a waste to let it get killed if it
2431 // happens to be sitting towards the end.
2432 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002433 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002434 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002435 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002436 return true;
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002437 } else if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2438 mStackSupervisor.allResumedActivitiesComplete()) {
2439 // It is possible for the activity to be resumed when we paused back stacks above if the
2440 // next activity doesn't have to wait for pause to complete.
2441 // So, nothing else to-do except:
2442 // Make sure we have executed any pending transitions, since there
2443 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002444 executeAppTransition(options);
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002445 if (DEBUG_STATES) Slog.d(TAG_STATES,
2446 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
2447 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2448 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 }
2450
Christopher Tated3f175c2012-06-14 14:16:54 -07002451 // If the most recent activity was noHistory but was only stopped rather
2452 // than stopped+finished because the device went to sleep, we need to make
2453 // sure to finish it as we're making a new activity topmost.
David Stevens9440dc82017-03-16 19:00:20 -07002454 if (shouldSleepActivities() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002455 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002456 if (DEBUG_STATES) Slog.d(TAG_STATES,
2457 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002458 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002459 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002460 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002461 }
2462
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 if (prev != null && prev != next) {
Bryce Lee4a194382017-04-04 14:32:48 -07002464 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Craig Mautner8c14c152015-01-15 17:32:07 -08002465 && next != null && !next.nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07002466 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002467 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2468 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 } else {
2470 // The next activity is already visible, so hide the previous
2471 // activity's windows right now so we can show the new one ASAP.
2472 // We only do this if the previous is finishing, which should mean
2473 // it is on top of the one being resumed so hiding it quickly
2474 // is good. Otherwise, we want to do the normal route of allowing
2475 // the resumed activity to be shown so we can decide if the
2476 // previous should actually be hidden depending on whether the
2477 // new one is found to be full-screen or not.
2478 if (prev.finishing) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002479 prev.setVisibility(false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002480 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2481 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Bryce Lee4a194382017-04-04 14:32:48 -07002482 + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 + ", nowVisible=" + next.nowVisible);
2484 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002485 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002486 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002487 + ", waitingVisible="
Bryce Lee4a194382017-04-04 14:32:48 -07002488 + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002489 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002490 }
2491 }
2492 }
2493
Dianne Hackborne7f97212011-02-24 14:40:20 -08002494 // Launching this app's activity, make sure the app is no longer
2495 // considered stopped.
2496 try {
2497 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002498 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002499 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002500 } catch (IllegalArgumentException e) {
2501 Slog.w(TAG, "Failed trying to unstop package "
2502 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002503 }
2504
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 // We are starting up the next activity, so tell the window manager
2506 // that the previous one will be hidden soon. This way it can know
2507 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002508 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002509 if (prev != null) {
2510 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002511 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002512 "Prepare close transition: prev=" + prev);
2513 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002514 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002515 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516 } else {
Bryce Leeaf691c02017-03-20 14:20:22 -07002517 mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002518 ? TRANSIT_ACTIVITY_CLOSE
2519 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002520 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002521 prev.setVisibility(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002522 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002523 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2524 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002525 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002526 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002527 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002528 } else {
Bryce Leeaf691c02017-03-20 14:20:22 -07002529 mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002530 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002531 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002532 ? TRANSIT_TASK_OPEN_BEHIND
2533 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 }
2535 }
Craig Mautner967212c2013-04-13 21:10:58 -07002536 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002537 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002539 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002540 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002541 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002542 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002543 }
2544 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002545
2546 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002547 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002548 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2549 if (opts != null) {
2550 resumeAnimOptions = opts.toBundle();
2551 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002552 next.applyOptionsLocked();
2553 } else {
2554 next.clearOptionsLocked();
2555 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002556
Craig Mautnercf910b02013-04-23 11:23:27 -07002557 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002559 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2560 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002561
Chong Zhangd127c6d2016-05-02 16:36:41 -07002562 // If the previous activity is translucent, force a visibility update of
2563 // the next activity, so that it's added to WM's opening app list, and
2564 // transition animation can be set up properly.
2565 // For example, pressing Home button with a translucent activity in focus.
2566 // Launcher is already visible in this case. If we don't add it to opening
2567 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2568 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2569 final boolean lastActivityTranslucent = lastStack != null
2570 && (!lastStack.mFullscreen
2571 || (lastStack.mLastPausedActivity != null
2572 && !lastStack.mLastPausedActivity.fullscreen));
2573
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002574 // This activity is now becoming visible.
Chong Zhangd127c6d2016-05-02 16:36:41 -07002575 if (!next.visible || next.stopped || lastActivityTranslucent) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002576 next.setVisibility(true);
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002577 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002579 // schedule launch ticks to collect information about slow apps.
2580 next.startLaunchTickingLocked();
2581
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002582 ActivityRecord lastResumedActivity =
2583 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002584 ActivityState lastState = next.state;
2585
2586 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002587
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002588 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Chong Zhang6cda19c2016-06-14 19:07:56 -07002589
2590 setResumedActivityLocked(next, "resumeTopActivityInnerLocked");
2591
Dianne Hackborndb926082013-10-31 16:32:44 -07002592 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002594 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002595
2596 // Have the window manager re-evaluate the orientation of
2597 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002598 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002599 if (mStackSupervisor.isFocusedStack(this)) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002600 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
2601 mStackSupervisor.getDisplayOverrideConfiguration(mDisplayId),
2602 next.mayFreezeScreenLocked(next.app) ? next.appToken : null, mDisplayId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002603 if (config != null) {
2604 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 }
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002606 notUpdated = !mService.updateDisplayOverrideConfigurationLocked(config, next,
2607 false /* deferResume */, mDisplayId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002608 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002609
Craig Mautner525f3d92013-05-07 14:01:50 -07002610 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002611 // The configuration update wasn't able to keep the existing
2612 // instance of the activity, and instead started a new one.
2613 // We should be all done, but let's just make sure our activity
2614 // is still at the top and schedule another run if something
2615 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002616 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002617 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002618 "Activity config changed during resume: " + next
2619 + ", new next: " + nextNext);
2620 if (nextNext != next) {
2621 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002622 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002623 }
Chong Zhangd2cb3112016-07-20 11:13:22 -07002624 if (!next.visible || next.stopped) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002625 next.setVisibility(true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002626 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002627 next.completeResumeLocked();
Craig Mautnercf910b02013-04-23 11:23:27 -07002628 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Chong Zhangd2cb3112016-07-20 11:13:22 -07002629 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002630 }
Craig Mautner58547802013-03-05 08:23:53 -08002631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002632 try {
2633 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002634 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002635 if (a != null) {
2636 final int N = a.size();
2637 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002638 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2639 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002640 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 }
2642 }
2643
2644 if (next.newIntents != null) {
Wale Ogunwale826c7062016-09-13 08:25:54 -07002645 next.app.thread.scheduleNewIntent(
2646 next.newIntents, next.appToken, false /* andPause */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647 }
2648
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002649 // Well the app will no longer be stopped.
2650 // Clear app token stopped state in window manager if needed.
Jorim Jaggibae01b12017-04-11 16:29:10 -07002651 next.notifyAppResumed(next.stopped);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002652
Craig Mautner299f9602015-01-26 09:47:33 -08002653 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
Bryce Leeaf691c02017-03-20 14:20:22 -07002654 System.identityHashCode(next), next.getTask().taskId,
2655 next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002656
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002657 next.sleeping = false;
Alan Viverette5f31b982016-06-21 14:46:14 -04002658 mService.showUnsupportedZoomDialogIfNeededLocked(next);
Craig Mautner2420ead2013-04-01 17:13:20 -07002659 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002660 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002661 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002662 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002663 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002664 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002665
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002666 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002667 } catch (Exception e) {
2668 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002669 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002670 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002672 if (lastStack != null) {
2673 lastStack.mResumedActivity = lastResumedActivity;
2674 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675 Slog.i(TAG, "Restarting because process died: " + next);
2676 if (!next.hasBeenLaunched) {
2677 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002678 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
Andrii Kulian7d95df42017-02-15 10:11:48 -08002679 mStackSupervisor.isFrontStackOnDisplay(lastStack)) {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002680 next.showStartingWindow(null /* prev */, false /* newTask */,
2681 false /* taskSwitch */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 }
George Mount2c92c972014-03-20 09:38:23 -07002683 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002684 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002685 return true;
2686 }
2687
2688 // From this point on, if something goes wrong there is no way
2689 // to recover the activity.
2690 try {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002691 next.completeResumeLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002692 } catch (Exception e) {
2693 // If any exception gets thrown, toss away this
2694 // activity and try the next one.
2695 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002696 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002697 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002698 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 return true;
2700 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 } else {
2702 // Whoops, need to restart this activity!
2703 if (!next.hasBeenLaunched) {
2704 next.hasBeenLaunched = true;
2705 } else {
2706 if (SHOW_APP_STARTING_PREVIEW) {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002707 next.showStartingWindow(null /* prev */, false /* newTask */,
2708 false /* taskSwich */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002710 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002712 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002713 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002714 }
2715
Craig Mautnercf910b02013-04-23 11:23:27 -07002716 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002717 return true;
2718 }
2719
Andrii Kulian7fc22812016-12-28 13:04:11 -08002720 private boolean resumeTopActivityInNextFocusableStack(ActivityRecord prev,
2721 ActivityOptions options, String reason) {
Winson Chung83471632016-12-13 11:02:12 -08002722 if ((!mFullscreen || !isOnHomeDisplay()) && adjustFocusToNextFocusableStackLocked(reason)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002723 // Try to move focus to the next visible stack with a running activity if this
2724 // stack is not covering the entire screen or is on a secondary display (with no home
2725 // stack).
2726 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2727 mStackSupervisor.getFocusedStack(), prev, null);
2728 }
2729
2730 // Let's just start up the Launcher...
2731 ActivityOptions.abort(options);
2732 if (DEBUG_STATES) Slog.d(TAG_STATES,
2733 "resumeTopActivityInNextFocusableStack: " + reason + ", go home");
2734 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2735 // Only resume home if on home display
2736 return isOnHomeDisplay() &&
2737 mStackSupervisor.resumeHomeStackTask(prev, reason);
2738 }
2739
riddle_hsuc215a4f2014-12-27 12:10:45 +08002740 private TaskRecord getNextTask(TaskRecord targetTask) {
2741 final int index = mTaskHistory.indexOf(targetTask);
2742 if (index >= 0) {
2743 final int numTasks = mTaskHistory.size();
2744 for (int i = index + 1; i < numTasks; ++i) {
2745 TaskRecord task = mTaskHistory.get(i);
2746 if (task.userId == targetTask.userId) {
2747 return task;
2748 }
2749 }
2750 }
2751 return null;
2752 }
2753
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002754 /** Returns the position the input task should be placed in this stack. */
2755 int getAdjustedPositionForTask(TaskRecord task, int suggestedPosition,
2756 ActivityRecord starting) {
2757
2758 int maxPosition = mTaskHistory.size();
2759 if ((starting != null && starting.okToShowLocked())
2760 || (starting == null && task.okToShowLocked())) {
2761 // If the task or starting activity can be shown, then whatever position is okay.
2762 return Math.min(suggestedPosition, maxPosition);
2763 }
2764
2765 // The task can't be shown, put non-current user tasks below current user tasks.
2766 while (maxPosition > 0) {
2767 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2768 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2769 || tmpTask.topRunningActivityLocked() == null) {
2770 break;
2771 }
2772 maxPosition--;
2773 }
2774
2775 return Math.min(suggestedPosition, maxPosition);
2776 }
2777
Andrii Kuliand2765632016-12-12 22:26:34 -08002778 /**
2779 * Used from {@link ActivityStack#positionTask(TaskRecord, int)}.
2780 * @see ActivityManagerService#positionTaskInStack(int, int, int).
2781 */
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002782 private void insertTaskAtPosition(TaskRecord task, int position) {
2783 if (position >= mTaskHistory.size()) {
2784 insertTaskAtTop(task, null);
2785 return;
2786 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002787 position = getAdjustedPositionForTask(task, position, null /* starting */);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002788 mTaskHistory.remove(task);
2789 mTaskHistory.add(position, task);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002790 mWindowContainerController.positionChildAt(task.getWindowContainerController(), position,
2791 task.mBounds, task.getOverrideConfiguration());
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002792 updateTaskMovement(task, true);
2793 }
2794
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002795 private void insertTaskAtTop(TaskRecord task, ActivityRecord starting) {
2796 updateTaskReturnToForTopInsertion(task);
2797 // TODO: Better place to put all the code below...may be addTask...
2798 mTaskHistory.remove(task);
2799 // Now put task at top.
2800 final int position = getAdjustedPositionForTask(task, mTaskHistory.size(), starting);
2801 mTaskHistory.add(position, task);
2802 updateTaskMovement(task, true);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002803 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
2804 true /* includingParents */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002805 }
2806
Winson Chung83471632016-12-13 11:02:12 -08002807 /**
2808 * Updates the {@param task}'s return type before it is moved to the top.
2809 */
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002810 private void updateTaskReturnToForTopInsertion(TaskRecord task) {
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002811 boolean isLastTaskOverHome = false;
Winson Chung83471632016-12-13 11:02:12 -08002812 // If the moving task is over the home or assistant stack, transfer its return type to next
2813 // task so that they return to the same stack
2814 if (task.isOverHomeStack() || task.isOverAssistantStack()) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002815 final TaskRecord nextTask = getNextTask(task);
2816 if (nextTask != null) {
2817 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002818 } else {
2819 isLastTaskOverHome = true;
riddle_hsuc215a4f2014-12-27 12:10:45 +08002820 }
2821 }
2822
Winson Chung83471632016-12-13 11:02:12 -08002823 // If this is not on the default display, then just set the return type to application
2824 if (!isOnHomeDisplay()) {
2825 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
2826 return;
2827 }
2828
Winson Chung83471632016-12-13 11:02:12 -08002829 final ActivityStack lastStack = mStackSupervisor.getLastStack();
Bryce Leeaf691c02017-03-20 14:20:22 -07002830
2831 // If there is no last task, do not set task to return to
2832 if (lastStack == null) {
2833 return;
2834 }
2835
2836 // If the task was launched from the assistant stack, set the return type to assistant
2837 if (lastStack.isAssistantStack()) {
Winson Chung83471632016-12-13 11:02:12 -08002838 task.setTaskToReturnTo(ASSISTANT_ACTIVITY_TYPE);
2839 return;
2840 }
2841
Craig Mautner9c85c202013-10-04 20:11:26 -07002842 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002843 // activity, set mTaskToReturnTo accordingly.
Winson Chung83471632016-12-13 11:02:12 -08002844 final boolean fromHomeOrRecents = lastStack.isHomeOrRecentsStack();
2845 final TaskRecord topTask = lastStack.topTask();
2846 if (!isHomeOrRecentsStack() && (fromHomeOrRecents || topTask() != task)) {
2847 // If it's a last task over home - we default to keep its return to type not to
2848 // make underlying task focused when this one will be finished.
2849 int returnToType = isLastTaskOverHome
2850 ? task.getTaskToReturnTo() : APPLICATION_ACTIVITY_TYPE;
2851 if (fromHomeOrRecents && StackId.allowTopTaskToReturnHome(mStackId)) {
2852 returnToType = topTask == null ? HOME_ACTIVITY_TYPE : topTask.taskType;
Craig Mautnere0a38842013-12-16 16:14:02 -08002853 }
Winson Chung83471632016-12-13 11:02:12 -08002854 task.setTaskToReturnTo(returnToType);
Craig Mautner9c85c202013-10-04 20:11:26 -07002855 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002856 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002857
Winson Chungb5c41b72016-12-07 15:00:47 -08002858 final void startActivityLocked(ActivityRecord r, ActivityRecord focusedTopActivity,
2859 boolean newTask, boolean keepCurTransition, ActivityOptions options) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002860 TaskRecord rTask = r.getTask();
Craig Mautnerd2328952013-03-05 12:46:26 -08002861 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002862 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2863 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002864 // Last activity in task had been removed or ActivityManagerService is reusing task.
2865 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002866 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002867 insertTaskAtTop(rTask, r);
Craig Mautner77878772013-03-04 19:46:24 -08002868 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002869 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002870 if (!newTask) {
2871 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002872 boolean startIt = true;
2873 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2874 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002875 if (task.getTopActivity() == null) {
2876 // All activities in task are finishing.
2877 continue;
2878 }
Bryce Leeaf691c02017-03-20 14:20:22 -07002879 if (task == rTask) {
Craig Mautner70a86932013-02-28 22:37:44 -08002880 // Here it is! Now, if this is not yet visible to the
2881 // user, then just add it without starting; it will
2882 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002883 if (!startIt) {
2884 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2885 + task, new RuntimeException("here").fillInStackTrace());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002886 r.createWindowContainer();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002887 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002888 return;
2889 }
2890 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002891 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002892 startIt = false;
2893 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002894 }
2895 }
2896
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002897 // Place a new activity at top of stack, so it is next to interact with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002898
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002899 // If we are not placing the new activity frontmost, we do not want to deliver the
2900 // onUserLeaving callback to the actual frontmost activity
Bryce Leeaf691c02017-03-20 14:20:22 -07002901 final TaskRecord activityTask = r.getTask();
2902 if (task == activityTask && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002903 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002904 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002905 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002906 }
Craig Mautner70a86932013-02-28 22:37:44 -08002907
Bryce Leeaf691c02017-03-20 14:20:22 -07002908 task = activityTask;
Craig Mautner70a86932013-02-28 22:37:44 -08002909
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002910 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002911 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002912 new RuntimeException("here").fillInStackTrace());
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08002913 // TODO: Need to investigate if it is okay for the controller to already be created by the
2914 // time we get to this point. I think it is, but need to double check.
2915 // Use test in b/34179495 to trace the call path.
2916 if (r.getWindowContainerController() == null) {
2917 r.createWindowContainer();
2918 }
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002919 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002920
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002921 if (!isHomeOrRecentsStack() || numActivities() > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002922 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002924 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002925 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002926 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002928 int transit = TRANSIT_ACTIVITY_OPEN;
2929 if (newTask) {
2930 if (r.mLaunchTaskBehind) {
2931 transit = TRANSIT_TASK_OPEN_BEHIND;
2932 } else {
Winson Chungc2baac02017-01-11 13:34:47 -08002933 // If a new task is being launched, then mark the existing top activity as
Winson Chung942a85c2017-07-11 15:07:45 -07002934 // supporting picture-in-picture while pausing only if the starting activity
2935 // would not be considered an overlay on top of the current activity
2936 // (eg. not fullscreen, or the assistant)
Winson Chung4aede8a2017-07-05 12:23:25 -07002937 if (focusedTopActivity != null
2938 && focusedTopActivity.getStackId() != PINNED_STACK_ID
Winson Chung942a85c2017-07-11 15:07:45 -07002939 && r.getStackId() != ASSISTANT_STACK_ID
2940 && r.fullscreen) {
Winson Chungc2baac02017-01-11 13:34:47 -08002941 focusedTopActivity.supportsPictureInPictureWhilePausing = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08002942 }
2943 transit = TRANSIT_TASK_OPEN;
2944 }
2945 }
2946 mWindowManager.prepareAppTransition(transit, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 mNoAnimActivities.remove(r);
2948 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949 boolean doShow = true;
2950 if (newTask) {
2951 // Even though this activity is starting fresh, we still need
2952 // to reset it to make sure we apply affinities to move any
2953 // existing activities from other tasks in to it.
2954 // If the caller has requested that the target task be
2955 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002956 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 resetTaskIfNeededLocked(r, r);
2958 doShow = topRunningNonDelayedActivityLocked(null) == r;
2959 }
Chong Zhang280d3322015-11-03 17:27:26 -08002960 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002961 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2962 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002963 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002964 if (r.mLaunchTaskBehind) {
2965 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2966 // tell WindowManager that r is visible even though it is at the back of the stack.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002967 r.setVisibility(true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002968 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002969 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 // Figure out if we are transitioning from another activity that is
2971 // "has the same starting icon" as the next one. This allows the
2972 // window manager to keep the previous window it had previously
2973 // created, if it still had one.
Bryce Leeaf691c02017-03-20 14:20:22 -07002974 TaskRecord prevTask = r.getTask();
2975 ActivityRecord prev = prevTask.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 if (prev != null) {
2977 // We don't want to reuse the previous starting preview if:
2978 // (1) The current activity is in a different task.
Bryce Leeaf691c02017-03-20 14:20:22 -07002979 if (prev.getTask() != prevTask) {
Craig Mautner29219d92013-04-16 20:19:12 -07002980 prev = null;
2981 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002982 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002983 else if (prev.nowVisible) {
2984 prev = null;
2985 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002986 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002987 r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 }
2989 } else {
2990 // If this is the first activity, don't do any fancy animations,
2991 // because there is nothing for it to animate on top of.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002992 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002994 }
2995
Jorim Jaggi02886a82016-12-06 09:10:06 -08002996 private boolean isTaskSwitch(ActivityRecord r,
2997 ActivityRecord topFocusedActivity) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002998 return topFocusedActivity != null && r.getTask() != topFocusedActivity.getTask();
Jorim Jaggi02886a82016-12-06 09:10:06 -08002999 }
3000
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003001 /**
3002 * Perform a reset of the given task, if needed as part of launching it.
3003 * Returns the new HistoryRecord at the top of the task.
3004 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08003005 /**
3006 * Helper method for #resetTaskIfNeededLocked.
3007 * We are inside of the task being reset... we'll either finish this activity, push it out
3008 * for another task, or leave it as-is.
3009 * @param task The task containing the Activity (taskTop) that might be reset.
3010 * @param forceReset
3011 * @return An ActivityOptions that needs to be processed.
3012 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07003013 private ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003014 ActivityOptions topOptions = null;
3015
3016 int replyChainEnd = -1;
3017 boolean canMoveOptions = true;
3018
3019 // We only do this for activities that are not the root of the task (since if we finish
3020 // the root, we may no longer have the task!).
3021 final ArrayList<ActivityRecord> activities = task.mActivities;
3022 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003023 final int rootActivityNdx = task.findEffectiveRootIndex();
3024 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003025 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003026 if (target.frontOfTask)
3027 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003028
3029 final int flags = target.info.flags;
3030 final boolean finishOnTaskLaunch =
3031 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3032 final boolean allowTaskReparenting =
3033 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3034 final boolean clearWhenTaskReset =
3035 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
3036
3037 if (!finishOnTaskLaunch
3038 && !clearWhenTaskReset
3039 && target.resultTo != null) {
3040 // If this activity is sending a reply to a previous
3041 // activity, we can't do anything with it now until
3042 // we reach the start of the reply chain.
3043 // XXX note that we are assuming the result is always
3044 // to the previous activity, which is almost always
3045 // the case but we really shouldn't count on.
3046 if (replyChainEnd < 0) {
3047 replyChainEnd = i;
3048 }
3049 } else if (!finishOnTaskLaunch
3050 && !clearWhenTaskReset
3051 && allowTaskReparenting
3052 && target.taskAffinity != null
3053 && !target.taskAffinity.equals(task.affinity)) {
3054 // If this activity has an affinity for another
3055 // task, then we need to move it out of here. We will
3056 // move it as far out of the way as possible, to the
3057 // bottom of the activity stack. This also keeps it
3058 // correctly ordered with any activities we previously
3059 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08003060 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003061 final ActivityRecord bottom =
3062 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08003063 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003064 if (bottom != null && target.taskAffinity != null
Bryce Leeaf691c02017-03-20 14:20:22 -07003065 && target.taskAffinity.equals(bottom.getTask().affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003066 // If the activity currently at the bottom has the
3067 // same task affinity as the one we are moving,
3068 // then merge it into the same task.
Bryce Leeaf691c02017-03-20 14:20:22 -07003069 targetTask = bottom.getTask();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003070 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003071 + " out to bottom task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003072 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003073 targetTask = createTaskRecord(
3074 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
Wale Ogunwale72919d22016-12-08 18:58:50 -08003075 target.info, null, null, null, false, target.mActivityType);
Craig Mautner329f4122013-11-07 09:10:42 -08003076 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003077 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003078 + " out to new task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003079 }
3080
Craig Mautner525f3d92013-05-07 14:01:50 -07003081 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003082 final int start = replyChainEnd < 0 ? i : replyChainEnd;
3083 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07003084 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003085 if (p.finishing) {
3086 continue;
3087 }
3088
Craig Mautnere3a74d52013-02-22 14:14:58 -08003089 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003090 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003091 topOptions = p.takeOptionsLocked();
3092 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003093 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003094 }
3095 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003096 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3097 "Removing activity " + p + " from task=" + task + " adding to task="
3098 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003099 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Bryce Leeaf691c02017-03-20 14:20:22 -07003100 "Pushing next activity " + p + " out to target's task " + target);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08003101 p.reparent(targetTask, 0 /* position - bottom */, "resetTargetTaskIfNeeded");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003102 }
3103
Wale Ogunwale1666e312016-12-16 11:27:18 -08003104 mWindowContainerController.positionChildAtBottom(
3105 targetTask.getWindowContainerController());
Craig Mautnere3a74d52013-02-22 14:14:58 -08003106 replyChainEnd = -1;
3107 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
3108 // If the activity should just be removed -- either
3109 // because it asks for it, or the task should be
3110 // cleared -- then finish it and anything that is
3111 // part of its reply chain.
3112 int end;
3113 if (clearWhenTaskReset) {
3114 // In this case, we want to finish this activity
3115 // and everything above it, so be sneaky and pretend
3116 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08003117 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003118 } else if (replyChainEnd < 0) {
3119 end = i;
3120 } else {
3121 end = replyChainEnd;
3122 }
Craig Mautner525f3d92013-05-07 14:01:50 -07003123 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003124 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003125 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003126 if (p.finishing) {
3127 continue;
3128 }
3129 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003130 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003131 topOptions = p.takeOptionsLocked();
3132 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003133 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003134 }
3135 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003136 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08003137 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08003138 if (finishActivityLocked(
3139 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003140 end--;
3141 srcPos--;
3142 }
3143 }
3144 replyChainEnd = -1;
3145 } else {
3146 // If we were in the middle of a chain, well the
3147 // activity that started it all doesn't want anything
3148 // special, so leave it all as-is.
3149 replyChainEnd = -1;
3150 }
3151 }
3152
3153 return topOptions;
3154 }
3155
3156 /**
3157 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
3158 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
3159 * @param affinityTask The task we are looking for an affinity to.
3160 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
3161 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
3162 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
3163 */
Craig Mautner525f3d92013-05-07 14:01:50 -07003164 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08003165 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003166 int replyChainEnd = -1;
3167 final int taskId = task.taskId;
3168 final String taskAffinity = task.affinity;
3169
3170 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
3171 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003172 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
3173
3174 // Do not operate on or below the effective root Activity.
3175 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003176 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003177 if (target.frontOfTask)
3178 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003179
3180 final int flags = target.info.flags;
3181 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3182 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3183
3184 if (target.resultTo != null) {
3185 // If this activity is sending a reply to a previous
3186 // activity, we can't do anything with it now until
3187 // we reach the start of the reply chain.
3188 // XXX note that we are assuming the result is always
3189 // to the previous activity, which is almost always
3190 // the case but we really shouldn't count on.
3191 if (replyChainEnd < 0) {
3192 replyChainEnd = i;
3193 }
3194 } else if (topTaskIsHigher
3195 && allowTaskReparenting
3196 && taskAffinity != null
3197 && taskAffinity.equals(target.taskAffinity)) {
3198 // This activity has an affinity for our task. Either remove it if we are
3199 // clearing or move it over to our task. Note that
3200 // we currently punt on the case where we are resetting a
3201 // task that is not at the top but who has activities above
3202 // with an affinity to it... this is really not a normal
3203 // case, and we will need to later pull that task to the front
3204 // and usually at that point we will do the reset and pick
3205 // up those remaining activities. (This only happens if
3206 // someone starts an activity in a new task from an activity
3207 // in a task that is not currently on top.)
3208 if (forceReset || finishOnTaskLaunch) {
3209 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003210 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3211 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003212 for (int srcPos = start; srcPos >= i; --srcPos) {
3213 final ActivityRecord p = activities.get(srcPos);
3214 if (p.finishing) {
3215 continue;
3216 }
Todd Kennedy539db512014-12-15 09:57:55 -08003217 finishActivityLocked(
3218 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003219 }
3220 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003221 if (taskInsertionPoint < 0) {
3222 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003223
Craig Mautner77878772013-03-04 19:46:24 -08003224 }
Craig Mautner77878772013-03-04 19:46:24 -08003225
3226 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003227 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3228 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3229 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003230 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003231 final ActivityRecord p = activities.get(srcPos);
Winson Chung30480042017-01-26 10:55:34 -08003232 p.reparent(task, taskInsertionPoint, "resetAffinityTaskIfNeededLocked");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003233
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003234 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3235 "Removing and adding activity " + p + " to stack at " + task
3236 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003237 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3238 + " from " + srcPos + " in to resetting task " + task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003239 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08003240 mWindowContainerController.positionChildAtTop(
3241 task.getWindowContainerController(), true /* includingParents */);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003242
3243 // Now we've moved it in to place... but what if this is
3244 // a singleTop activity and we have put it on top of another
3245 // instance of the same activity? Then we drop the instance
3246 // below so it remains singleTop.
3247 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3248 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003249 int targetNdx = taskActivities.indexOf(target);
3250 if (targetNdx > 0) {
3251 ActivityRecord p = taskActivities.get(targetNdx - 1);
3252 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003253 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3254 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003255 }
3256 }
3257 }
3258 }
3259
3260 replyChainEnd = -1;
3261 }
3262 }
Craig Mautner77878772013-03-04 19:46:24 -08003263 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003264 }
3265
Craig Mautner8849a5e2013-04-02 16:41:03 -07003266 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003267 ActivityRecord newActivity) {
3268 boolean forceReset =
3269 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
3270 if (ACTIVITY_INACTIVE_RESET_TIME > 0
Bryce Leeaf691c02017-03-20 14:20:22 -07003271 && taskTop.getTask().getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003272 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
3273 forceReset = true;
3274 }
3275 }
3276
Bryce Leeaf691c02017-03-20 14:20:22 -07003277 final TaskRecord task = taskTop.getTask();
Craig Mautnere3a74d52013-02-22 14:14:58 -08003278
3279 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3280 * for remaining tasks. Used for later tasks to reparent to task. */
3281 boolean taskFound = false;
3282
3283 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3284 ActivityOptions topOptions = null;
3285
Craig Mautner77878772013-03-04 19:46:24 -08003286 // Preserve the location for reparenting in the new task.
3287 int reparentInsertionPoint = -1;
3288
Craig Mautnere3a74d52013-02-22 14:14:58 -08003289 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3290 final TaskRecord targetTask = mTaskHistory.get(i);
3291
3292 if (targetTask == task) {
3293 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3294 taskFound = true;
3295 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003296 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3297 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003298 }
3299 }
3300
Craig Mautner70a86932013-02-28 22:37:44 -08003301 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003302 if (taskNdx >= 0) {
3303 do {
3304 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3305 } while (taskTop == null && taskNdx >= 0);
3306 }
Craig Mautner70a86932013-02-28 22:37:44 -08003307
Craig Mautnere3a74d52013-02-22 14:14:58 -08003308 if (topOptions != null) {
3309 // If we got some ActivityOptions from an activity on top that
3310 // was removed from the task, propagate them to the new real top.
3311 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003312 taskTop.updateOptionsLocked(topOptions);
3313 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003314 topOptions.abort();
3315 }
3316 }
3317
3318 return taskTop;
3319 }
3320
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003321 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3322 String resultWho, int requestCode, int resultCode, Intent data) {
3323
3324 if (callingUid > 0) {
3325 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003326 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003327 }
3328
3329 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3330 + " : who=" + resultWho + " req=" + requestCode
3331 + " res=" + resultCode + " data=" + data);
3332 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3333 try {
3334 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3335 list.add(new ResultInfo(resultWho, requestCode,
3336 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003337 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 return;
3339 } catch (Exception e) {
3340 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3341 }
3342 }
3343
3344 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3345 }
3346
Shunta Sato63b8ee32016-07-11 13:32:52 +09003347 /** Returns true if the task is one of the task finishing on-top of the top running task. */
3348 boolean isATopFinishingTask(TaskRecord task) {
3349 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3350 final TaskRecord current = mTaskHistory.get(i);
3351 final ActivityRecord r = current.topRunningActivityLocked();
3352 if (r != null) {
3353 // We got a top running activity, so there isn't a top finishing task...
3354 return false;
3355 }
3356 if (current == task) {
3357 return true;
3358 }
3359 }
3360 return false;
3361 }
3362
Chong Zhang6cda19c2016-06-14 19:07:56 -07003363 private void adjustFocusedActivityStackLocked(ActivityRecord r, String reason) {
Shunta Sato63b8ee32016-07-11 13:32:52 +09003364 if (!mStackSupervisor.isFocusedStack(this) ||
3365 ((mResumedActivity != r) && (mResumedActivity != null))) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003366 return;
3367 }
3368
3369 final ActivityRecord next = topRunningActivityLocked();
3370 final String myReason = reason + " adjustFocus";
Bryce Lee3345c4e2017-04-25 07:40:41 -07003371
Wale Ogunwaled045c822015-12-02 09:14:28 -08003372 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003373 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003374 // For freeform, docked, and pinned stacks we always keep the focus within the
Chong Zhang6cda19c2016-06-14 19:07:56 -07003375 // stack as long as there is a running activity.
Wale Ogunwaled045c822015-12-02 09:14:28 -08003376 return;
3377 } else {
Bryce Lee3345c4e2017-04-25 07:40:41 -07003378 // Task is not guaranteed to be non-null. For example, destroying the
3379 // {@link ActivityRecord} will disassociate the task from the activity.
Bryce Leeaf691c02017-03-20 14:20:22 -07003380 final TaskRecord task = r.getTask();
Bryce Lee3345c4e2017-04-25 07:40:41 -07003381
3382 if (task == null) {
3383 throw new IllegalStateException("activity no longer associated with task:" + r);
3384 }
3385
Winson Chung83471632016-12-13 11:02:12 -08003386 final boolean isAssistantOrOverAssistant = task.getStack().isAssistantStack() ||
3387 task.isOverAssistantStack();
Shunta Sato63b8ee32016-07-11 13:32:52 +09003388 if (r.frontOfTask && isATopFinishingTask(task)
3389 && (task.isOverHomeStack() || isAssistantOrOverAssistant)) {
Winson Chung83471632016-12-13 11:02:12 -08003390 // For non-fullscreen or assistant stack, we want to move the focus to the next
3391 // visible stack to prevent the home screen from moving to the top and obscuring
Wale Ogunwaled045c822015-12-02 09:14:28 -08003392 // other visible stacks.
Winson Chung83471632016-12-13 11:02:12 -08003393 if ((!mFullscreen || isAssistantOrOverAssistant)
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003394 && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003395 return;
3396 }
3397 // Move the home stack to the top if this stack is fullscreen or there is no
3398 // other visible stack.
Winson Chung521f0112017-03-13 16:14:45 -07003399 if (task.isOverHomeStack() &&
3400 mStackSupervisor.moveHomeStackTaskToTop(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003401 // Activity focus was already adjusted. Nothing else to do...
3402 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003403 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003404 }
3405 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003406 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003407
Chong Zhang6cda19c2016-06-14 19:07:56 -07003408 mStackSupervisor.moveFocusableActivityStackToFrontLocked(
3409 mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003410 }
3411
Andrii Kulian250d6532017-02-08 23:30:45 -08003412 /** Find next proper focusable stack and make it focused. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003413 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003414 return adjustFocusToNextFocusableStackLocked(reason, false /* allowFocusSelf */);
3415 }
3416
3417 /**
3418 * Find next proper focusable stack and make it focused.
3419 * @param allowFocusSelf Is the focus allowed to remain on the same stack.
3420 */
3421 private boolean adjustFocusToNextFocusableStackLocked(String reason, boolean allowFocusSelf) {
Winson Chung1cebea62017-06-26 17:22:27 -07003422 if (isAssistantStack() && bottomTask() != null &&
3423 bottomTask().getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
3424 // If the current stack is the assistant stack, then use the return-to type to determine
3425 // whether to return to the home screen. This is needed to workaround an issue where
3426 // launching a fullscreen task (and subequently returning from that task) will cause
3427 // the fullscreen stack to be found as the next focusable stack below, even if the
3428 // assistant was launched over home.
3429 return mStackSupervisor.moveHomeStackTaskToTop(reason);
3430 }
3431
Andrii Kulian250d6532017-02-08 23:30:45 -08003432 final ActivityStack stack = mStackSupervisor.getNextFocusableStackLocked(
3433 allowFocusSelf ? null : this);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003434 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003435 if (stack == null) {
3436 return false;
3437 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003438
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003439 final ActivityRecord top = stack.topRunningActivityLocked();
3440
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003441 if (stack.isHomeOrRecentsStack() && (top == null || !top.visible)) {
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003442 // If we will be focusing on the home stack next and its current top activity isn't
Andrii Kulian250d6532017-02-08 23:30:45 -08003443 // visible, then use the task return to value to determine the home task to display
3444 // next.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003445 return mStackSupervisor.moveHomeStackTaskToTop(reason);
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003446 }
Chong Zhang6cda19c2016-06-14 19:07:56 -07003447
Bryce Lee43f27212017-08-07 16:10:43 -07003448 if (stack.isAssistantStack() && top != null
3449 && top.getTask().getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
3450 // It is possible for the home stack to not be directly underneath the assistant stack.
3451 // For example, the assistant may start an activity in the fullscreen stack. Upon
3452 // returning to the assistant stack, we must ensure that the home stack is underneath
3453 // when appropriate.
3454 mStackSupervisor.moveHomeStackTaskToTop("adjustAssistantReturnToHome");
3455 }
3456
Chong Zhang6cda19c2016-06-14 19:07:56 -07003457 stack.moveToFront(myReason);
3458 return true;
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003459 }
3460
Craig Mautnerf3333272013-04-22 10:55:53 -07003461 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003462 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003463 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3464 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3465 if (!r.finishing) {
David Stevens9440dc82017-03-16 19:00:20 -07003466 if (!shouldSleepActivities()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003467 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003468 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3469 "stop-no-history", false)) {
Bryce Lee3345c4e2017-04-25 07:40:41 -07003470 // If {@link requestFinishActivityLocked} returns {@code true},
3471 // {@link adjustFocusedActivityStackLocked} would have been already called.
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003472 r.resumeKeyDispatchingLocked();
3473 return;
3474 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003475 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003476 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003477 + " on stop because we're just sleeping");
3478 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003479 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003480 }
3481
3482 if (r.app != null && r.app.thread != null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003483 adjustFocusedActivityStackLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003484 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003485 try {
3486 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003487 if (DEBUG_STATES) Slog.v(TAG_STATES,
3488 "Moving to STOPPING: " + r + " (stop requested)");
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003489 r.state = STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003490 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3491 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003492 if (!r.visible) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003493 r.setVisible(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003494 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003495 EventLogTags.writeAmStopActivity(
3496 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003497 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
David Stevens9440dc82017-03-16 19:00:20 -07003498 if (shouldSleepOrShutDownActivities()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003499 r.setSleeping(true);
3500 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003501 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003502 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003503 } catch (Exception e) {
3504 // Maybe just ignore exceptions here... if the process
3505 // has crashed, our death notification will clean things
3506 // up.
3507 Slog.w(TAG, "Exception thrown during pause", e);
3508 // Just in case, assume it to be stopped.
3509 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003510 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003511 r.state = STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003512 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003513 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003514 }
3515 }
3516 }
3517 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003518
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003519 /**
3520 * @return Returns true if the activity is being finished, false if for
3521 * some reason it is being left as-is.
3522 */
3523 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003524 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003525 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003526 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3527 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003528 + ", result=" + resultCode + ", data=" + resultData
3529 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003530 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531 return false;
3532 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533
Craig Mautnerd44711d2013-02-23 11:24:36 -08003534 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003535 return true;
3536 }
3537
Craig Mautnerd2328952013-03-05 12:46:26 -08003538 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003539 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3540 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3541 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3542 ActivityRecord r = activities.get(activityNdx);
3543 if (r.resultTo == self && r.requestCode == requestCode) {
3544 if ((r.resultWho == null && resultWho == null) ||
3545 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3546 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3547 false);
3548 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003549 }
3550 }
3551 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003552 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003553 }
3554
Adrian Roos20d7df32016-01-12 18:59:43 +01003555 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003556 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003557 TaskRecord finishedTask = null;
3558 if (r == null || r.app != app) {
3559 return null;
3560 }
3561 Slog.w(TAG, " Force finishing activity "
3562 + r.intent.getComponent().flattenToShortString());
Bryce Leeaf691c02017-03-20 14:20:22 -07003563 finishedTask = r.getTask();
3564 int taskNdx = mTaskHistory.indexOf(finishedTask);
3565 final TaskRecord task = finishedTask;
3566 int activityNdx = task.mActivities.indexOf(r);
Adrian Roos20d7df32016-01-12 18:59:43 +01003567 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Bryce Leeaf691c02017-03-20 14:20:22 -07003568 finishedTask = task;
Adrian Roos20d7df32016-01-12 18:59:43 +01003569 // Also terminate any activities below it that aren't yet
3570 // stopped, to avoid a situation where one will get
3571 // re-start our crashing activity once it gets resumed again.
3572 --activityNdx;
3573 if (activityNdx < 0) {
3574 do {
3575 --taskNdx;
3576 if (taskNdx < 0) {
3577 break;
3578 }
3579 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3580 } while (activityNdx < 0);
3581 }
3582 if (activityNdx >= 0) {
3583 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3584 if (r.state == ActivityState.RESUMED
3585 || r.state == ActivityState.PAUSING
3586 || r.state == ActivityState.PAUSED) {
3587 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3588 Slog.w(TAG, " Force finishing activity "
3589 + r.intent.getComponent().flattenToShortString());
3590 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003591 }
3592 }
3593 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003594 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003595 }
3596
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003597 final void finishVoiceTask(IVoiceInteractionSession session) {
3598 IBinder sessionBinder = session.asBinder();
3599 boolean didOne = false;
3600 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3601 TaskRecord tr = mTaskHistory.get(taskNdx);
3602 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3603 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3604 ActivityRecord r = tr.mActivities.get(activityNdx);
3605 if (!r.finishing) {
3606 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3607 false);
3608 didOne = true;
3609 }
3610 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003611 } else {
3612 // Check if any of the activities are using voice
3613 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3614 ActivityRecord r = tr.mActivities.get(activityNdx);
3615 if (r.voiceSession != null
3616 && r.voiceSession.asBinder() == sessionBinder) {
3617 // Inform of cancellation
3618 r.clearVoiceSessionLocked();
3619 try {
3620 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3621 null);
3622 } catch (RemoteException re) {
3623 // Ok
3624 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003625 mService.finishRunningVoiceLocked();
3626 break;
3627 }
3628 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003629 }
3630 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003631
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003632 if (didOne) {
3633 mService.updateOomAdjLocked();
3634 }
3635 }
3636
Craig Mautnerd2328952013-03-05 12:46:26 -08003637 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003638 ArrayList<ActivityRecord> activities = r.getTask().mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003639 for (int index = activities.indexOf(r); index >= 0; --index) {
3640 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003641 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003642 break;
3643 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003644 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003645 }
3646 return true;
3647 }
3648
Andrii Kulian21713ac2016-10-12 22:05:05 -07003649 private void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
Dianne Hackborn5c607432012-02-28 14:44:19 -08003650 // send the result
3651 ActivityRecord resultTo = r.resultTo;
3652 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003653 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003654 + " who=" + r.resultWho + " req=" + r.requestCode
3655 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003656 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003657 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003658 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003659 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003660 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003661 if (r.info.applicationInfo.uid > 0) {
3662 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3663 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003664 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003665 }
3666 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3667 resultData);
3668 r.resultTo = null;
3669 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003670 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003671
3672 // Make sure this HistoryRecord is not holding on to other resources,
3673 // because clients have remote IPC references to this object so we
3674 // can't assume that will go away and want to avoid circular IPC refs.
3675 r.results = null;
3676 r.pendingResults = null;
3677 r.newIntents = null;
3678 r.icicle = null;
3679 }
3680
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003681 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003682 * See {@link #finishActivityLocked(ActivityRecord, int, Intent, String, boolean, boolean)}
3683 */
3684 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3685 String reason, boolean oomAdj) {
3686 return finishActivityLocked(r, resultCode, resultData, reason, oomAdj, !PAUSE_IMMEDIATELY);
3687 }
3688
3689 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003690 * @return Returns true if this activity has been removed from the history
3691 * list, or false if it is still in the list and will be removed later.
3692 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003693 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
Winson Chung6954fc92017-03-24 16:22:12 -07003694 String reason, boolean oomAdj, boolean pauseImmediately) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003695 if (r.finishing) {
3696 Slog.w(TAG, "Duplicate finish request for " + r);
3697 return false;
3698 }
3699
Jorim Jaggife762342016-10-13 14:33:27 +02003700 mWindowManager.deferSurfaceLayout();
3701 try {
3702 r.makeFinishingLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07003703 final TaskRecord task = r.getTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003704 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3705 r.userId, System.identityHashCode(r),
3706 task.taskId, r.shortComponentName, reason);
3707 final ArrayList<ActivityRecord> activities = task.mActivities;
3708 final int index = activities.indexOf(r);
3709 if (index < (activities.size() - 1)) {
3710 task.setFrontOfTask();
3711 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3712 // If the caller asked that this activity (and all above it)
3713 // be cleared when the task is reset, don't lose that information,
3714 // but propagate it up to the next activity.
3715 ActivityRecord next = activities.get(index+1);
3716 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3717 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003718 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003719
Jorim Jaggife762342016-10-13 14:33:27 +02003720 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003721
Jorim Jaggife762342016-10-13 14:33:27 +02003722 adjustFocusedActivityStackLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003723
Jorim Jaggife762342016-10-13 14:33:27 +02003724 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003725
Yorke Leebdef5372017-04-10 16:38:51 -07003726 final boolean endTask = index <= 0 && !task.isClearingToReuseTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003727 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
3728 if (mResumedActivity == r) {
3729 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
3730 "Prepare close transition: finishing " + r);
Winson Chung6954fc92017-03-24 16:22:12 -07003731 if (endTask) {
3732 mService.mTaskChangeNotificationController.notifyTaskRemovalStarted(
3733 task.taskId);
3734 }
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003735 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003736
Jorim Jaggife762342016-10-13 14:33:27 +02003737 // Tell window manager to prepare for this one to be removed.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003738 r.setVisibility(false);
Jorim Jaggife762342016-10-13 14:33:27 +02003739
3740 if (mPausingActivity == null) {
3741 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3742 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3743 "finish() => pause with userLeaving=false");
Winson Chung6954fc92017-03-24 16:22:12 -07003744 startPausingLocked(false, false, null, pauseImmediately);
Jorim Jaggife762342016-10-13 14:33:27 +02003745 }
3746
3747 if (endTask) {
3748 mStackSupervisor.removeLockedTaskLocked(task);
3749 }
3750 } else if (r.state != ActivityState.PAUSING) {
3751 // If the activity is PAUSING, we will complete the finish once
3752 // it is done pausing; else we can just directly finish it here.
3753 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
3754 if (r.visible) {
Winson Chung6954fc92017-03-24 16:22:12 -07003755 prepareActivityHideTransitionAnimation(r, transit);
3756 }
3757
3758 final int finishMode = (r.visible || r.nowVisible) ? FINISH_AFTER_VISIBLE
3759 : FINISH_AFTER_PAUSE;
3760 final boolean removedActivity = finishCurrentActivityLocked(r, finishMode, oomAdj)
3761 == null;
3762
3763 // The following code is an optimization. When the last non-task overlay activity
3764 // is removed from the task, we remove the entire task from the stack. However,
3765 // since that is done after the scheduled destroy callback from the activity, that
3766 // call to change the visibility of the task overlay activities would be out of
3767 // sync with the activitiy visibility being set for this finishing activity above.
3768 // In this case, we can set the visibility of all the task overlay activities when
3769 // we detect the last one is finishing to keep them in sync.
3770 if (task.onlyHasTaskOverlayActivities(true /* excludeFinishing */)) {
3771 for (ActivityRecord taskOverlay : task.mActivities) {
3772 if (!taskOverlay.mTaskOverlay) {
3773 continue;
3774 }
3775 prepareActivityHideTransitionAnimation(taskOverlay, transit);
Jorim Jaggife762342016-10-13 14:33:27 +02003776 }
3777 }
Winson Chung6954fc92017-03-24 16:22:12 -07003778 return removedActivity;
Jorim Jaggife762342016-10-13 14:33:27 +02003779 } else {
3780 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
3781 }
3782
3783 return false;
3784 } finally {
3785 mWindowManager.continueSurfaceLayout();
3786 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003787 }
3788
Winson Chung6954fc92017-03-24 16:22:12 -07003789 private void prepareActivityHideTransitionAnimation(ActivityRecord r, int transit) {
3790 mWindowManager.prepareAppTransition(transit, false);
3791 r.setVisibility(false);
3792 mWindowManager.executeAppTransition();
Bryce Lee4a194382017-04-04 14:32:48 -07003793 if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(r)) {
3794 mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(r);
Winson Chung6954fc92017-03-24 16:22:12 -07003795 }
3796 }
3797
Craig Mautnerf3333272013-04-22 10:55:53 -07003798 static final int FINISH_IMMEDIATELY = 0;
3799 static final int FINISH_AFTER_PAUSE = 1;
3800 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003801
Craig Mautnerf3333272013-04-22 10:55:53 -07003802 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003803 // First things first: if this activity is currently visible,
3804 // and the resumed activity is not yet visible, then hold off on
3805 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003806
3807 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
3808
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003809 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003810 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003811 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Winson Chung4dabf232017-01-25 13:25:22 -08003812 addToStopping(r, false /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003813 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003814 if (DEBUG_STATES) Slog.v(TAG_STATES,
3815 "Moving to STOPPING: "+ r + " (finish requested)");
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003816 r.state = STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003817 if (oomAdj) {
3818 mService.updateOomAdjLocked();
3819 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003820 return r;
3821 }
3822
3823 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003824 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003825 mStackSupervisor.mGoingToSleepActivities.remove(r);
Bryce Lee4a194382017-04-04 14:32:48 -07003826 mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003827 if (mResumedActivity == r) {
3828 mResumedActivity = null;
3829 }
3830 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003831 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003832 r.state = ActivityState.FINISHING;
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003833 final boolean finishingActivityInNonFocusedStack
Andrii Kulian02b7a832016-10-06 23:11:56 -07003834 = r.getStack() != mStackSupervisor.getFocusedStack()
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003835 && prevState == ActivityState.PAUSED && mode == FINISH_AFTER_VISIBLE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003836
3837 if (mode == FINISH_IMMEDIATELY
Wale Ogunwaleae30f302016-05-02 10:35:45 -07003838 || (prevState == ActivityState.PAUSED
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003839 && (mode == FINISH_AFTER_PAUSE || mStackId == PINNED_STACK_ID))
3840 || finishingActivityInNonFocusedStack
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003841 || prevState == STOPPING
3842 || prevState == STOPPED
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003843 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003844 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003845 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Andrii Kulian7318d632016-07-20 18:59:28 -07003846
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003847 if (finishingActivityInNonFocusedStack) {
3848 // Finishing activity that was in paused state and it was in not currently focused
3849 // stack, need to make something visible in its place.
Andrii Kulian7318d632016-07-20 18:59:28 -07003850 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3851 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003852 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003853 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003854 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003855 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003856 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3857 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003858 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003859 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003860
3861 // Need to go through the full pause cycle to get this
3862 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003863 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003864 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003865 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003866 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003867 return r;
3868 }
3869
Craig Mautneree36c772014-07-16 14:56:05 -07003870 void finishAllActivitiesLocked(boolean immediately) {
3871 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003872 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3873 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3874 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3875 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003876 noActivitiesInStack = false;
3877 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003878 continue;
3879 }
Craig Mautneree36c772014-07-16 14:56:05 -07003880 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003881 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3882 }
3883 }
Craig Mautneree36c772014-07-16 14:56:05 -07003884 if (noActivitiesInStack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003885 remove();
Craig Mautneree36c772014-07-16 14:56:05 -07003886 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003887 }
3888
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003889 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3890 // Basic case: for simple app-centric recents, we need to recreate
3891 // the task if the affinity has changed.
Bryce Leeaf691c02017-03-20 14:20:22 -07003892 if (srec == null || srec.getTask().affinity == null ||
3893 !srec.getTask().affinity.equals(destAffinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003894 return true;
3895 }
3896 // Document-centric case: an app may be split in to multiple documents;
3897 // they need to re-create their task if this current activity is the root
3898 // of a document, unless simply finishing it will return them to the the
3899 // correct app behind.
Bryce Leeaf691c02017-03-20 14:20:22 -07003900 final TaskRecord task = srec.getTask();
3901 if (srec.frontOfTask && task != null && task.getBaseIntent() != null
3902 && task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003903 // Okay, this activity is at the root of its task. What to do, what to do...
Bryce Leeaf691c02017-03-20 14:20:22 -07003904 if (task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003905 // Finishing won't return to an application, so we need to recreate.
3906 return true;
3907 }
3908 // We now need to get the task below it to determine what to do.
Bryce Leeaf691c02017-03-20 14:20:22 -07003909 int taskIdx = mTaskHistory.indexOf(task);
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003910 if (taskIdx <= 0) {
3911 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3912 return false;
3913 }
3914 if (taskIdx == 0) {
3915 // At the bottom of the stack, nothing to go back to.
3916 return true;
3917 }
3918 TaskRecord prevTask = mTaskHistory.get(taskIdx);
Bryce Leeaf691c02017-03-20 14:20:22 -07003919 if (!task.affinity.equals(prevTask.affinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003920 // These are different apps, so need to recreate.
3921 return true;
3922 }
3923 }
3924 return false;
3925 }
3926
Wale Ogunwale7d701172015-03-11 15:36:30 -07003927 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003928 Intent resultData) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003929 final TaskRecord task = srec.getTask();
Craig Mautner0247fc82013-02-28 14:32:06 -08003930 final ArrayList<ActivityRecord> activities = task.mActivities;
3931 final int start = activities.indexOf(srec);
3932 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003933 return false;
3934 }
3935 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003936 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003937 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003938 final ComponentName dest = destIntent.getComponent();
3939 if (start > 0 && dest != null) {
3940 for (int i = finishTo; i >= 0; i--) {
3941 ActivityRecord r = activities.get(i);
3942 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003943 r.info.name.equals(dest.getClassName())) {
3944 finishTo = i;
3945 parent = r;
3946 foundParentInTask = true;
3947 break;
3948 }
3949 }
3950 }
3951
3952 IActivityController controller = mService.mController;
3953 if (controller != null) {
3954 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3955 if (next != null) {
3956 // ask watcher if this is allowed
3957 boolean resumeOK = true;
3958 try {
3959 resumeOK = controller.activityResuming(next.packageName);
3960 } catch (RemoteException e) {
3961 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003962 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003963 }
3964
3965 if (!resumeOK) {
3966 return false;
3967 }
3968 }
3969 }
3970 final long origId = Binder.clearCallingIdentity();
3971 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003972 ActivityRecord r = activities.get(i);
3973 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003974 // Only return the supplied result for the first activity finished
3975 resultCode = Activity.RESULT_CANCELED;
3976 resultData = null;
3977 }
3978
3979 if (parent != null && foundParentInTask) {
3980 final int parentLaunchMode = parent.info.launchMode;
3981 final int destIntentFlags = destIntent.getFlags();
3982 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3983 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3984 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3985 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003986 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3987 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003988 } else {
3989 try {
3990 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3991 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003992 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3993 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3994 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003995 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003996 false, true, null, null, "navigateUpTo");
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003997 foundParentInTask = res == ActivityManager.START_SUCCESS;
3998 } catch (RemoteException e) {
3999 foundParentInTask = false;
4000 }
4001 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08004002 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004003 }
4004 }
4005 Binder.restoreCallingIdentity(origId);
4006 return foundParentInTask;
4007 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004008
4009 /**
4010 * Remove any state associated with the {@link ActivityRecord}. This should be called whenever
4011 * an activity moves away from the stack.
4012 */
4013 void onActivityRemovedFromStack(ActivityRecord r) {
4014 if (mResumedActivity == r) {
4015 mResumedActivity = null;
4016 }
4017 if (mPausingActivity == r) {
4018 mPausingActivity = null;
4019 }
4020
4021 removeTimeoutsForActivityLocked(r);
4022 }
4023
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004024 /**
4025 * Perform the common clean-up of an activity record. This is called both
4026 * as part of destroyActivityLocked() (when destroying the client-side
4027 * representation) and cleaning things up as a result of its hosting
4028 * processing going away, in which case there is no remaining client-side
4029 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08004030 *
4031 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004032 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07004033 private void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices, boolean setState) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004034 onActivityRemovedFromStack(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004035
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004036 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004037 r.frozenBeforeDestroy = false;
4038
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004039 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004040 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004041 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004042 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004043 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004044 }
4045
Bryce Lee4a194382017-04-04 14:32:48 -07004046 // Inform supervisor the activity has been removed.
4047 mStackSupervisor.cleanupActivity(r);
4048
Craig Mautner2420ead2013-04-01 17:13:20 -07004049
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004050 // Remove any pending results.
4051 if (r.finishing && r.pendingResults != null) {
4052 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
4053 PendingIntentRecord rec = apr.get();
4054 if (rec != null) {
4055 mService.cancelIntentSenderLocked(rec, false);
4056 }
4057 }
4058 r.pendingResults = null;
4059 }
4060
4061 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07004062 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004063 }
4064
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004065 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004066 removeTimeoutsForActivityLocked(r);
Wale Ogunwale8fd75422016-06-24 14:20:37 -07004067 // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
4068 // manager so it can update its bookkeeping.
4069 mWindowManager.notifyAppRelaunchesCleared(r.appToken);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004070 }
4071
Winson Chung4dabf232017-01-25 13:25:22 -08004072 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004073 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004074 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004075 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004076 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07004077 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004078 }
4079
Chong Zhangbffd8892016-08-08 11:16:06 -07004080 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004081 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07004082 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004083 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
4084 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
4085
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004086 r.takeFromHistory();
4087 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004088 if (DEBUG_STATES) Slog.v(TAG_STATES,
4089 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004090 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004091 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004092 r.app = null;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08004093 r.removeWindowContainer();
Bryce Leeaf691c02017-03-20 14:20:22 -07004094 final TaskRecord task = r.getTask();
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004095 final boolean lastActivity = task != null ? task.removeActivity(r) : false;
Winson Chung6954fc92017-03-24 16:22:12 -07004096 // If we are removing the last activity in the task, not including task overlay activities,
4097 // then fall through into the block below to remove the entire task itself
4098 final boolean onlyHasTaskOverlays = task != null
4099 ? task.onlyHasTaskOverlayActivities(false /* excludingFinishing */) : false;
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004100
Winson Chung6954fc92017-03-24 16:22:12 -07004101 if (lastActivity || onlyHasTaskOverlays) {
4102 if (DEBUG_STACK) {
4103 Slog.i(TAG_STACK,
4104 "removeActivityFromHistoryLocked: last activity removed from " + this
4105 + " onlyHasTaskOverlays=" + onlyHasTaskOverlays);
4106 }
4107
Chong Zhangbffd8892016-08-08 11:16:06 -07004108 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07004109 task.isOverHomeStack()) {
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004110 mStackSupervisor.moveHomeStackTaskToTop(reason);
Craig Mautner312ba862014-02-10 17:55:01 -08004111 }
Winson Chunge3c21e02017-04-11 18:02:17 -07004112
Bryce Leed6d26752017-05-25 13:57:46 -07004113 // The following block can be executed multiple times if there is more than one overlay.
4114 // {@link ActivityStackSupervisor#removeTaskByIdLocked} handles this by reverse lookup
4115 // of the task by id and exiting early if not found.
Winson Chunge3c21e02017-04-11 18:02:17 -07004116 if (onlyHasTaskOverlays) {
4117 // When destroying a task, tell the supervisor to remove it so that any activity it
4118 // has can be cleaned up correctly. This is currently the only place where we remove
4119 // a task with the DESTROYING mode, so instead of passing the onlyHasTaskOverlays
4120 // state into removeTask(), we just clear the task here before the other residual
4121 // work.
4122 // TODO: If the callers to removeTask() changes such that we have multiple places
4123 // where we are destroying the task, move this back into removeTask()
4124 mStackSupervisor.removeTaskByIdLocked(task.taskId, false /* killProcess */,
4125 !REMOVE_FROM_RECENTS, PAUSE_IMMEDIATELY);
Winson Chunge3c21e02017-04-11 18:02:17 -07004126 }
Bryce Lee1d930582017-05-01 08:35:24 -07004127
Bryce Leed6d26752017-05-25 13:57:46 -07004128 // We must keep the task around until all activities are destroyed. The following
4129 // statement will only execute once since overlays are also considered activities.
Bryce Lee1d930582017-05-01 08:35:24 -07004130 if (lastActivity) {
4131 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
4132 }
Craig Mautner312ba862014-02-10 17:55:01 -08004133 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004134 cleanUpActivityServicesLocked(r);
4135 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004136 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004138 /**
4139 * Perform clean-up of service connections in an activity record.
4140 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07004141 private void cleanUpActivityServicesLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004142 // Throw away any services that have been bound by this activity.
4143 if (r.connections != null) {
4144 Iterator<ConnectionRecord> it = r.connections.iterator();
4145 while (it.hasNext()) {
4146 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004147 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004148 }
4149 r.connections = null;
4150 }
4151 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004152
Craig Mautneree2e45a2014-06-27 12:10:03 -07004153 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004154 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004155 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004156 mHandler.sendMessage(msg);
4157 }
4158
Andrii Kulian21713ac2016-10-12 22:05:05 -07004159 private void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004160 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004161 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004162 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4163 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4164 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4165 final ActivityRecord r = activities.get(activityNdx);
4166 if (r.finishing) {
4167 continue;
4168 }
4169 if (r.fullscreen) {
4170 lastIsOpaque = true;
4171 }
4172 if (owner != null && r.app != owner) {
4173 continue;
4174 }
4175 if (!lastIsOpaque) {
4176 continue;
4177 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004178 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004179 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08004180 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004181 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004182 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004183 activityRemoved = true;
4184 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004185 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004186 }
4187 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004188 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004189 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004190 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004191 }
4192
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004193 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
4194 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004195 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4196 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004197 + " pausing=" + mPausingActivity + " for reason " + reason);
4198 return destroyActivityLocked(r, true, reason);
4199 }
4200 return false;
4201 }
4202
4203 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
4204 String reason) {
4205 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004206 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004207 int maxTasks = tasks.size() / 4;
4208 if (maxTasks < 1) {
4209 maxTasks = 1;
4210 }
4211 int numReleased = 0;
4212 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
4213 final TaskRecord task = mTaskHistory.get(taskNdx);
4214 if (!tasks.contains(task)) {
4215 continue;
4216 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004217 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004218 int curNum = 0;
4219 final ArrayList<ActivityRecord> activities = task.mActivities;
4220 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
4221 final ActivityRecord activity = activities.get(actNdx);
4222 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004223 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004224 + " in state " + activity.state + " resumed=" + mResumedActivity
4225 + " pausing=" + mPausingActivity + " for reason " + reason);
4226 destroyActivityLocked(activity, true, reason);
4227 if (activities.get(actNdx) != activity) {
4228 // Was removed from list, back up so we don't miss the next one.
4229 actNdx--;
4230 }
4231 curNum++;
4232 }
4233 }
4234 if (curNum > 0) {
4235 numReleased += curNum;
4236 maxTasks--;
4237 if (mTaskHistory.get(taskNdx) != task) {
4238 // The entire task got removed, back up so we don't miss the next one.
4239 taskNdx--;
4240 }
4241 }
4242 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004243 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
4244 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004245 return numReleased;
4246 }
4247
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004248 /**
4249 * Destroy the current CLIENT SIDE instance of an activity. This may be
4250 * called both when actually finishing an activity, or when performing
4251 * a configuration switch where we destroy the current client-side object
4252 * but then create a new client-side object for this same HistoryRecord.
4253 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07004254 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004255 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
4256 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004257 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004258 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004259 r.userId, System.identityHashCode(r),
Bryce Leeaf691c02017-03-20 14:20:22 -07004260 r.getTask().taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004261
4262 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004263
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004264 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004265
4266 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004267
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004268 if (hadApp) {
4269 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004270 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004271 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
4272 mService.mHeavyWeightProcess = null;
4273 mService.mHandler.sendEmptyMessage(
4274 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
4275 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004276 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07004277 // Update any services we are bound to that might care about whether
4278 // their client may have activities.
4279 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07004280 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07004281 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07004282 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004283 }
4284 }
4285
4286 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004287
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004288 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004289 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004290 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004291 r.configChangeFlags);
4292 } catch (Exception e) {
4293 // We can just ignore exceptions here... if the process
4294 // has crashed, our death notification will clean things
4295 // up.
4296 //Slog.w(TAG, "Exception thrown during finish", e);
4297 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004298 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299 removedFromHistory = true;
4300 skipDestroy = true;
4301 }
4302 }
4303
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004304 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004305
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004306 // If the activity is finishing, we need to wait on removing it
4307 // from the list to give it a chance to do its cleanup. During
4308 // that time it may make calls back with its token so we need to
4309 // be able to find it on the list and so we don't want to remove
4310 // it from the list yet. Otherwise, we can just immediately put
4311 // it in the destroyed state since we are not removing it from the
4312 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004313 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004314 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004315 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004316 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07004317 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004318 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4319 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004320 if (DEBUG_STATES) Slog.v(TAG_STATES,
4321 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004322 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004323 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004324 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004325 }
4326 } else {
4327 // remove this record from the history.
4328 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004329 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004330 removedFromHistory = true;
4331 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004332 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004333 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004334 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004335 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004336 }
4337 }
4338
4339 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004340
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004341 if (!mLRUActivities.remove(r) && hadApp) {
4342 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4343 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004345 return removedFromHistory;
4346 }
4347
Craig Mautner299f9602015-01-26 09:47:33 -08004348 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004349 final long origId = Binder.clearCallingIdentity();
4350 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004351 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08004352 if (r != null) {
4353 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004354 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004355 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08004356
Wale Ogunwale60454db2015-01-23 16:05:07 -08004357 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004358 if (r.state == ActivityState.DESTROYING) {
4359 cleanUpActivityLocked(r, true, false);
Chong Zhangbffd8892016-08-08 11:16:06 -07004360 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004361 }
4362 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004363 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08004364 } finally {
4365 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004366 }
4367 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004368
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004369 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4370 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004371 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004372 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4373 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004374 while (i > 0) {
4375 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004376 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004377 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004378 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004379 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004380 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004381 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004382 }
4383 }
4384 }
4385
Andrii Kulian21713ac2016-10-12 22:05:05 -07004386 private boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004387 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004388 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4389 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004390 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4391 "mGoingToSleepActivities");
Bryce Lee4a194382017-04-04 14:32:48 -07004392 removeHistoryRecordsForAppLocked(mStackSupervisor.mActivitiesWaitingForVisibleActivity, app,
4393 "mActivitiesWaitingForVisibleActivity");
Craig Mautnerf3333272013-04-22 10:55:53 -07004394 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4395 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004396
4397 boolean hasVisibleActivities = false;
4398
4399 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004400 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004401 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4402 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004403 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4404 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4405 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4406 final ActivityRecord r = activities.get(activityNdx);
4407 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004408 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4409 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08004410 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004411 if (r.visible) {
4412 hasVisibleActivities = true;
4413 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004414 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004415 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4416 // Don't currently have state for the activity, or
4417 // it is finishing -- always remove it.
4418 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004419 } else if (!r.visible && r.launchCount > 2 &&
4420 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004421 // We have launched this activity too many times since it was
4422 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004423 // (Note if the activity is visible, we don't remove the record.
4424 // We leave the dead window on the screen but the process will
4425 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004426 remove = true;
4427 } else {
4428 // The process may be gone, but the activity lives on!
4429 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004430 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004431 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004432 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4433 "Removing activity " + r + " from stack at " + i
4434 + ": haveState=" + r.haveState
4435 + " stateNotNeeded=" + r.stateNotNeeded
4436 + " finishing=" + r.finishing
4437 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004438 if (!r.finishing) {
4439 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4440 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4441 r.userId, System.identityHashCode(r),
Bryce Leeaf691c02017-03-20 14:20:22 -07004442 r.getTask().taskId, r.shortComponentName,
Craig Mautner0247fc82013-02-28 14:32:06 -08004443 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07004444 if (r.state == ActivityState.RESUMED) {
4445 mService.updateUsageStats(r, false);
4446 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004447 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004448 } else {
4449 // We have the current state for this activity, so
4450 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004451 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4452 if (DEBUG_APP) Slog.v(TAG_APP,
4453 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004454 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004455 // Set nowVisible to previous visible state. If the app was visible while
4456 // it died, we leave the dead window on screen so it's basically visible.
4457 // This is needed when user later tap on the dead window, we need to stop
4458 // other apps when user transfers focus to the restarted activity.
4459 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004460 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004461 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004462 "App died, clearing saved state of " + r);
4463 r.icicle = null;
4464 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004465 }
Craig Mautnerd2328952013-03-05 12:46:26 -08004466 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004467 if (remove) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004468 removeActivityFromHistoryLocked(r, "appDied");
Craig Mautneracebdc82015-02-24 10:53:03 -08004469 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004470 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004471 }
4472 }
4473
4474 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004475 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004476
Andrii Kulian21713ac2016-10-12 22:05:05 -07004477 private void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004478 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004479 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004480 if (r != null && r.state != ActivityState.RESUMED) {
4481 r.updateOptionsLocked(options);
4482 } else {
4483 ActivityOptions.abort(options);
4484 }
4485 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004486 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004487 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004488
Andrii Kulian21713ac2016-10-12 22:05:05 -07004489 private void updateTaskMovement(TaskRecord task, boolean toFront) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004490 if (task.isPersistable) {
4491 task.mLastTimeMoved = System.currentTimeMillis();
4492 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4493 // recently will be most negative, tasks sent to the bottom before that will be less
4494 // negative. Similarly for recent tasks moved to the top which will be most positive.
4495 if (!toFront) {
4496 task.mLastTimeMoved *= -1;
4497 }
4498 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004499 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004500 }
4501
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004502 void moveHomeStackTaskToTop() {
Craig Mautnera82aa092013-09-13 15:34:08 -07004503 final int top = mTaskHistory.size() - 1;
4504 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4505 final TaskRecord task = mTaskHistory.get(taskNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004506 if (task.taskType == HOME_ACTIVITY_TYPE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004507 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4508 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004509 mTaskHistory.remove(taskNdx);
4510 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004511 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004512 return;
4513 }
4514 }
4515 }
4516
Chong Zhang280d3322015-11-03 17:27:26 -08004517 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004518 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004519 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004520
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004521 final ActivityStack topStack = getTopStackOnDisplay();
4522 final ActivityRecord topActivity = topStack != null ? topStack.topActivity() : null;
Craig Mautner11bf9a52013-02-19 14:08:51 -08004523 final int numTasks = mTaskHistory.size();
4524 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004525 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004526 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004527 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004528 ActivityOptions.abort(options);
4529 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004530 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004531 }
4532 return;
4533 }
4534
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004535 if (timeTracker != null) {
4536 // The caller wants a time tracker associated with this task.
4537 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4538 tr.mActivities.get(i).appTimeTracker = timeTracker;
4539 }
4540 }
4541
Craig Mautner11bf9a52013-02-19 14:08:51 -08004542 // Shift all activities with this task up to the top
4543 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004544 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004545
Chong Zhang45c25ce2015-08-10 22:18:26 -07004546 // Don't refocus if invisible to current user
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004547 final ActivityRecord top = tr.getTopActivity();
Chong Zhang87761972016-08-22 13:53:24 -07004548 if (top == null || !top.okToShowLocked()) {
Chong Zhang45c25ce2015-08-10 22:18:26 -07004549 addRecentActivityLocked(top);
4550 ActivityOptions.abort(options);
4551 return;
4552 }
4553
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004554 // Set focus to the top running activity of this stack.
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004555 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang6cda19c2016-06-14 19:07:56 -07004556 mStackSupervisor.moveFocusableActivityStackToFrontLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004557
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004558 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004559 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004560 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004561 if (r != null) {
4562 mNoAnimActivities.add(r);
4563 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004564 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004565 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004566 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004567 }
Winson Chungc2baac02017-01-11 13:34:47 -08004568 // If a new task is moved to the front, then mark the existing top activity as supporting
Winson Chung942a85c2017-07-11 15:07:45 -07004569 // picture-in-picture while paused only if the task would not be considered an oerlay on top
4570 // of the current activity (eg. not fullscreen, or the assistant)
Winson Chung4aede8a2017-07-05 12:23:25 -07004571 if (topActivity != null && topActivity.getStackId() != PINNED_STACK_ID
Winson Chung942a85c2017-07-11 15:07:45 -07004572 && tr.getStackId() != ASSISTANT_STACK_ID && tr.containsOnlyFullscreenActivities()) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004573 topActivity.supportsPictureInPictureWhilePausing = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08004574 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004575
Wale Ogunwaled046a012015-12-24 13:05:59 -08004576 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004577 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Yorke Leebd54c2a2016-10-25 13:49:23 -07004578
4579 mService.mTaskChangeNotificationController.notifyTaskMovedToFront(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004580 }
4581
4582 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004583 * Worker method for rearranging history stack. Implements the function of moving all
4584 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004585 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004586 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004587 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4588 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004589 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004590 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004591 * @return Returns true if the move completed, false if not.
4592 */
Craig Mautner299f9602015-01-26 09:47:33 -08004593 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004594 final TaskRecord tr = taskForIdLocked(taskId);
4595 if (tr == null) {
4596 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4597 return false;
4598 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004599 Slog.i(TAG, "moveTaskToBack: " + tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004600
4601 // If the task is locked, then show the lock task toast
4602 if (mStackSupervisor.isLockedTask(tr)) {
4603 mStackSupervisor.showLockTaskToast();
4604 return false;
4605 }
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004606
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004607 // If we have a watcher, preflight the move before committing to it. First check
4608 // for *other* available tasks, but if none are available, then try again allowing the
4609 // current task to be selected.
Andrii Kulian7d95df42017-02-15 10:11:48 -08004610 if (mStackSupervisor.isFrontStackOnDisplay(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004611 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004612 if (next == null) {
4613 next = topRunningActivityLocked(null, 0);
4614 }
4615 if (next != null) {
4616 // ask watcher if this is allowed
4617 boolean moveOK = true;
4618 try {
4619 moveOK = mService.mController.activityResuming(next.packageName);
4620 } catch (RemoteException e) {
4621 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004622 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004623 }
4624 if (!moveOK) {
4625 return false;
4626 }
4627 }
4628 }
4629
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004630 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004631
riddle_hsuc215a4f2014-12-27 12:10:45 +08004632 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004633
4634 // If true, we should resume the home activity next if the task we are moving to the
4635 // back is over the home stack. We force to false if the task we are moving to back
4636 // is the home task and we don't want it resumed after moving to the back.
4637 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4638 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004639 final TaskRecord nextTask = getNextTask(tr);
4640 if (nextTask != null) {
4641 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4642 } else {
4643 prevIsHome = true;
4644 }
4645 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004646
Winson Chung261c0f32017-03-07 16:54:31 -08004647 boolean requiresMove = mTaskHistory.indexOf(tr) != 0;
4648 if (requiresMove) {
4649 mTaskHistory.remove(tr);
4650 mTaskHistory.add(0, tr);
4651 updateTaskMovement(tr, false);
4652
4653 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
4654 mWindowContainerController.positionChildAtBottom(tr.getWindowContainerController());
4655 }
4656
4657 if (mStackId == PINNED_STACK_ID) {
4658 mStackSupervisor.removeStackLocked(PINNED_STACK_ID);
4659 return true;
4660 }
4661
4662 // Otherwise, there is an assumption that moving a task to the back moves it behind the
4663 // home activity. We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004664 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004665 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4666 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004667 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004668 break;
4669 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004670 if (taskNdx == 1) {
4671 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004672 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004673 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004674 }
4675
Bryce Leeaf691c02017-03-20 14:20:22 -07004676 final TaskRecord task = mResumedActivity != null ? mResumedActivity.getTask() : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004677 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004678 if (!mService.mBooting && !mService.mBooted) {
4679 // Not ready yet!
4680 return false;
4681 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004682 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004683 return mStackSupervisor.resumeHomeStackTask(null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004684 }
4685
Wale Ogunwaled046a012015-12-24 13:05:59 -08004686 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004687 return true;
4688 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004689
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004690 /**
4691 * Get the topmost stack on the current display. It may be different from focused stack, because
4692 * focus may be on another display.
4693 */
4694 private ActivityStack getTopStackOnDisplay() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004695 final ArrayList<ActivityStack> stacks = getDisplay().mStacks;
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004696 return stacks.isEmpty() ? null : stacks.get(stacks.size() - 1);
4697 }
4698
Andrii Kulian21713ac2016-10-12 22:05:05 -07004699 static void logStartActivity(int tag, ActivityRecord r, TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004700 final Uri data = r.intent.getData();
4701 final String strData = data != null ? data.toSafeString() : null;
4702
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004703 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004704 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004705 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004706 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004707 }
4708
4709 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004710 * Ensures all visible activities at or below the input activity have the right configuration.
4711 */
4712 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4713 if (start == null || !start.visible) {
4714 return;
4715 }
4716
Bryce Leeaf691c02017-03-20 14:20:22 -07004717 final TaskRecord startTask = start.getTask();
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004718 boolean behindFullscreen = false;
4719 boolean updatedConfig = false;
4720
4721 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4722 final TaskRecord task = mTaskHistory.get(taskIndex);
4723 final ArrayList<ActivityRecord> activities = task.mActivities;
4724 int activityIndex =
Bryce Leeaf691c02017-03-20 14:20:22 -07004725 (start.getTask() == task) ? activities.indexOf(start) : activities.size() - 1;
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004726 for (; activityIndex >= 0; --activityIndex) {
4727 final ActivityRecord r = activities.get(activityIndex);
Andrii Kulian21713ac2016-10-12 22:05:05 -07004728 updatedConfig |= r.ensureActivityConfigurationLocked(0 /* globalChanges */,
4729 preserveWindow);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004730 if (r.fullscreen) {
4731 behindFullscreen = true;
4732 break;
4733 }
4734 }
4735 if (behindFullscreen) {
4736 break;
4737 }
4738 }
4739 if (updatedConfig) {
Wale Ogunwale089586f2016-06-20 14:16:22 -07004740 // Ensure the resumed state of the focus activity if we updated the configuration of
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004741 // any activity.
4742 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4743 }
4744 }
4745
Wale Ogunwale1666e312016-12-16 11:27:18 -08004746 // TODO: Figure-out a way to consolidate with resize() method below.
4747 @Override
4748 public void requestResize(Rect bounds) {
4749 mService.resizeStack(mStackId, bounds, true /* allowResizeInDockedMode */,
4750 false /* preserveWindows */, false /* animate */, -1 /* animationDuration */);
4751 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07004752
Wale Ogunwale1666e312016-12-16 11:27:18 -08004753 // TODO: Can only be called from special methods in ActivityStackSupervisor.
4754 // Need to consolidate those calls points into this resize method so anyone can call directly.
4755 void resize(Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds) {
4756 bounds = TaskRecord.validateBounds(bounds);
4757
4758 if (!updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
4759 return;
4760 }
4761
4762 // Update override configurations of all tasks in the stack.
4763 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
Andrii Kulian1e32e022016-09-16 15:29:34 -07004764 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
4765
4766 mTmpBounds.clear();
4767 mTmpConfigs.clear();
4768 mTmpInsetBounds.clear();
4769
4770 for (int i = mTaskHistory.size() - 1; i >= 0; i--) {
4771 final TaskRecord task = mTaskHistory.get(i);
4772 if (task.isResizeable()) {
4773 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
4774 // For freeform stack we don't adjust the size of the tasks to match that
4775 // of the stack, but we do try to make sure the tasks are still contained
4776 // with the bounds of the stack.
Wale Ogunwale1666e312016-12-16 11:27:18 -08004777 mTmpRect2.set(task.mBounds);
4778 fitWithinBounds(mTmpRect2, bounds);
4779 task.updateOverrideConfiguration(mTmpRect2);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004780 } else {
4781 task.updateOverrideConfiguration(taskBounds, insetBounds);
4782 }
4783 }
4784
Andrii Kulian1779e612016-10-12 21:58:25 -07004785 mTmpConfigs.put(task.taskId, task.getOverrideConfiguration());
Andrii Kulian1e32e022016-09-16 15:29:34 -07004786 mTmpBounds.put(task.taskId, task.mBounds);
4787 if (tempTaskInsetBounds != null) {
4788 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
4789 }
4790 }
4791
Wale Ogunwale1666e312016-12-16 11:27:18 -08004792 mFullscreen = mWindowContainerController.resize(bounds, mTmpConfigs, mTmpBounds,
Andrii Kulian1e32e022016-09-16 15:29:34 -07004793 mTmpInsetBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004794 setBounds(bounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004795 }
4796
4797
4798 /**
4799 * Adjust bounds to stay within stack bounds.
4800 *
4801 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4802 * that keep them unchanged, but be contained within the stack bounds.
4803 *
4804 * @param bounds Bounds to be adjusted.
4805 * @param stackBounds Bounds within which the other bounds should remain.
4806 */
4807 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4808 if (stackBounds == null || stackBounds.contains(bounds)) {
4809 return;
4810 }
4811
4812 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4813 final int maxRight = stackBounds.right
4814 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4815 int horizontalDiff = stackBounds.left - bounds.left;
4816 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4817 || (bounds.left + horizontalDiff >= maxRight)) {
4818 horizontalDiff = maxRight - bounds.left;
4819 }
4820 bounds.left += horizontalDiff;
4821 bounds.right += horizontalDiff;
4822 }
4823
4824 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4825 final int maxBottom = stackBounds.bottom
4826 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4827 int verticalDiff = stackBounds.top - bounds.top;
4828 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4829 || (bounds.top + verticalDiff >= maxBottom)) {
4830 verticalDiff = maxBottom - bounds.top;
4831 }
4832 bounds.top += verticalDiff;
4833 bounds.bottom += verticalDiff;
4834 }
4835 }
4836
Craig Mautnercae015f2013-02-08 14:31:27 -08004837 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004838 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4839 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4840 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4841 final ActivityRecord r = activities.get(activityNdx);
4842 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004843 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004844 }
4845 if (r.fullscreen && !r.finishing) {
4846 return false;
4847 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004848 }
4849 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004850 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004851 if (r == null) {
4852 return false;
4853 }
4854 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4855 + " would have returned true for r=" + r);
4856 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004857 }
4858
4859 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004860 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4861 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4862 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4863 final ActivityRecord r = activities.get(activityNdx);
4864 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004865 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004866 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004867 }
4868 }
4869 }
4870
Wale Ogunwale540e1232015-05-01 15:35:39 -07004871 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4872 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004873 boolean didSomething = false;
4874 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004875 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004876 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4877 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4878 int numActivities = activities.size();
4879 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4880 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004881 final boolean sameComponent =
4882 (r.packageName.equals(packageName) && (filterByClasses == null
4883 || filterByClasses.contains(r.realActivity.getClassName())))
4884 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004885 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Bryce Leeaf691c02017-03-20 14:20:22 -07004886 && (sameComponent || r.getTask() == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004887 && (r.app == null || evenPersistent || !r.app.persistent)) {
4888 if (!doit) {
4889 if (r.finishing) {
4890 // If this activity is just finishing, then it is not
4891 // interesting as far as something to stop.
4892 continue;
4893 }
4894 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004895 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004896 if (r.isHomeActivity()) {
4897 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4898 Slog.i(TAG, "Skip force-stop again " + r);
4899 continue;
4900 } else {
4901 homeActivity = r.realActivity;
4902 }
4903 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004904 didSomething = true;
4905 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004906 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004907 if (r.app != null) {
4908 r.app.removed = true;
4909 }
4910 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004911 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004912 lastTask = r.getTask();
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004913 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4914 true)) {
4915 // r has been deleted from mActivities, accommodate.
4916 --numActivities;
4917 --activityNdx;
4918 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004919 }
4920 }
4921 }
4922 return didSomething;
4923 }
4924
Dianne Hackborn09233282014-04-30 11:33:59 -07004925 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004926 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4927 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004928 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004929 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004930 if (task.getTopActivity() == null) {
4931 continue;
4932 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004933 ActivityRecord r = null;
4934 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004935 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004936 int numActivities = 0;
4937 int numRunning = 0;
4938 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004939 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004940 continue;
4941 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004942 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004943 tmp = activities.get(activityNdx);
4944 if (tmp.finishing) {
4945 continue;
4946 }
4947 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004948
Craig Mautneraab647e2013-02-28 16:31:36 -08004949 // Initialize state for next task if needed.
4950 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4951 top = r;
4952 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004953 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004954
4955 // Add 'r' into the current task.
4956 numActivities++;
4957 if (r.app != null && r.app.thread != null) {
4958 numRunning++;
4959 }
4960
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004961 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004962 TAG, r.intent.getComponent().flattenToShortString()
Bryce Leeaf691c02017-03-20 14:20:22 -07004963 + ": task=" + r.getTask());
Craig Mautneraab647e2013-02-28 16:31:36 -08004964 }
4965
4966 RunningTaskInfo ci = new RunningTaskInfo();
4967 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004968 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004969 ci.baseActivity = r.intent.getComponent();
4970 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004971 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004972 if (focusedStack && topTask) {
4973 // Give the latest time to ensure foreground task can be sorted
4974 // at the first, because lastActiveTime of creating task is 0.
4975 ci.lastActiveTime = System.currentTimeMillis();
4976 topTask = false;
4977 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004978
Bryce Leeaf691c02017-03-20 14:20:22 -07004979 if (top.getTask() != null) {
4980 ci.description = top.getTask().lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004981 }
4982 ci.numActivities = numActivities;
4983 ci.numRunning = numRunning;
Winson Chungd3395382016-12-13 11:49:09 -08004984 ci.supportsSplitScreenMultiWindow = task.supportsSplitScreen();
Jorim Jaggi29379ec2016-04-11 23:43:42 -07004985 ci.resizeMode = task.mResizeMode;
Craig Mautneraab647e2013-02-28 16:31:36 -08004986 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004987 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004988 }
4989
Andrii Kulian21713ac2016-10-12 22:05:05 -07004990 void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004991 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004992 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004993 if (top >= 0) {
4994 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4995 int activityTop = activities.size() - 1;
Dianne Hackborn354736e2016-08-22 17:00:05 -07004996 if (activityTop >= 0) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004997 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4998 "unhandled-back", true);
4999 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005000 }
5001 }
5002
Craig Mautner6b74cb52013-09-27 17:02:21 -07005003 /**
5004 * Reset local parameters because an app's activity died.
5005 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07005006 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07005007 */
5008 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07005009 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005010 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07005011 "App died while pausing: " + mPausingActivity);
5012 mPausingActivity = null;
5013 }
5014 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
5015 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07005016 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07005017 }
5018
Craig Mautner19091252013-10-05 00:03:53 -07005019 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07005020 }
5021
Craig Mautnercae015f2013-02-08 14:31:27 -08005022 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08005023 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5024 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5025 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5026 final ActivityRecord r = activities.get(activityNdx);
5027 if (r.app == app) {
5028 Slog.w(TAG, " Force finishing activity "
5029 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07005030 // Force the destroy to skip right to removal.
5031 r.app = null;
5032 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08005033 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005034 }
5035 }
5036 }
5037
Dianne Hackborn390517b2013-05-30 15:03:32 -07005038 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005039 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07005040 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08005041 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5042 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07005043 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
5044 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005045 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07005046 " Task id #" + task.taskId + "\n" +
5047 " mFullscreen=" + task.mFullscreen + "\n" +
5048 " mBounds=" + task.mBounds + "\n" +
Andrii Kulianf66a83d2016-05-17 12:17:44 -07005049 " mMinWidth=" + task.mMinWidth + "\n" +
5050 " mMinHeight=" + task.mMinHeight + "\n" +
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07005051 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005052 if (printed) {
5053 header = null;
5054 }
Craig Mautneraab647e2013-02-28 16:31:36 -08005055 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07005056 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08005057 }
5058
5059 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Winson Chung6998bc42017-02-28 17:07:05 -08005060 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautnercae015f2013-02-08 14:31:27 -08005061
5062 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005063 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5064 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08005065 }
5066 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005067 final int top = mTaskHistory.size() - 1;
5068 if (top >= 0) {
5069 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
5070 int listTop = list.size() - 1;
5071 if (listTop >= 0) {
5072 activities.add(list.get(listTop));
5073 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005074 }
5075 } else {
5076 ItemMatcher matcher = new ItemMatcher();
5077 matcher.build(name);
5078
Craig Mautneraab647e2013-02-28 16:31:36 -08005079 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5080 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
5081 if (matcher.match(r1, r1.intent.getComponent())) {
5082 activities.add(r1);
5083 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005084 }
5085 }
5086 }
5087
5088 return activities;
5089 }
5090
5091 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07005092 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08005093
5094 // All activities that came from the package must be
5095 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08005096 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5097 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5098 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5099 final ActivityRecord a = activities.get(activityNdx);
5100 if (a.info.packageName.equals(packageName)) {
5101 a.forceNewConfig = true;
5102 if (starting != null && a == starting && a.visible) {
5103 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08005104 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08005105 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005106 }
5107 }
5108 }
5109
5110 return starting;
5111 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005112
Wale Ogunwale000957c2015-04-03 08:19:12 -07005113 /**
5114 * Removes the input task from this stack.
5115 * @param task to remove.
5116 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07005117 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
5118 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07005119 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07005120 void removeTask(TaskRecord task, String reason, int mode) {
Bryce Leeaf691c02017-03-20 14:20:22 -07005121 for (ActivityRecord record : task.mActivities) {
5122 onActivityRemovedFromStack(record);
Craig Mautner04a0ea62014-01-13 12:51:26 -08005123 }
5124
Craig Mautnerae7ecab2013-09-18 11:48:14 -07005125 final int taskNdx = mTaskHistory.indexOf(task);
5126 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07005127 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
5128 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
Winson Chung83471632016-12-13 11:02:12 -08005129 if (!nextTask.isOverHomeStack() && !nextTask.isOverAssistantStack()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07005130 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
5131 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07005132 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005133 mTaskHistory.remove(task);
Winson Chungabb433b2017-03-24 09:35:42 -07005134 removeActivitiesFromLRUListLocked(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005135 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07005136
Wale Ogunwale06579d62016-04-30 15:29:06 -07005137 if (mode == REMOVE_TASK_MODE_DESTROYING && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005138 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07005139 final boolean isVoiceSession = task.voiceSession != null;
5140 if (isVoiceSession) {
5141 try {
5142 task.voiceSession.taskFinished(task.intent, task.taskId);
5143 } catch (RemoteException e) {
5144 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07005145 }
Craig Mautner41db4a72014-05-07 17:20:56 -07005146 if (task.autoRemoveFromRecents() || isVoiceSession) {
5147 // Task creator asked to remove this when done, or this task was a voice
5148 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08005149 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08005150 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07005151 }
Bryce Lee840c5662017-04-13 10:02:51 -07005152
5153 task.removeWindowContainer();
Dianne Hackborn91097de2014-04-04 18:02:06 -07005154 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005155
5156 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005157 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07005158 // We only need to adjust focused stack if this stack is in focus and we are not in the
5159 // process of moving the task to the top of the stack that will be focused.
5160 if (isOnHomeDisplay() && mode != REMOVE_TASK_MODE_MOVING_TO_TOP
5161 && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005162 String myReason = reason + " leftTaskHistoryEmpty";
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005163 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005164 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005165 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005166 }
Craig Mautner593a4e62014-01-15 17:55:51 -08005167 if (mStacks != null) {
5168 mStacks.remove(this);
5169 mStacks.add(0, this);
5170 }
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005171 if (!isHomeOrRecentsStack()) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07005172 remove();
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005173 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005174 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005175
Andrii Kulian02b7a832016-10-06 23:11:56 -07005176 task.setStack(null);
Winson Chungc81c0ce2017-03-17 12:27:30 -07005177
5178 // Notify if a task from the pinned stack is being removed (or moved depending on the mode)
5179 if (mStackId == PINNED_STACK_ID) {
5180 mService.mTaskChangeNotificationController.notifyActivityUnpinned();
5181 }
Craig Mautner0247fc82013-02-28 14:32:06 -08005182 }
5183
Dianne Hackborn91097de2014-04-04 18:02:06 -07005184 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5185 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Wale Ogunwale72919d22016-12-08 18:58:50 -08005186 boolean toTop, int type) {
Craig Mautner21d24a22014-04-23 11:45:37 -07005187 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
Wale Ogunwale72919d22016-12-08 18:58:50 -08005188 voiceInteractor, type);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005189 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005190 addTask(task, toTop, "createTaskRecord");
Jorim Jaggife762342016-10-13 14:33:27 +02005191 final boolean isLockscreenShown =
5192 mService.mStackSupervisor.mKeyguardController.isKeyguardShowing();
Andrii Kulian2e751b82016-03-16 16:59:32 -07005193 if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08005194 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07005195 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005196 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005197 task.createWindowContainer(toTop, (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005198 return task;
5199 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005200
Andrii Kulian2e751b82016-03-16 16:59:32 -07005201 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08005202 if (mTaskPositioner == null) {
5203 return false;
5204 }
Andrii Kulian2e751b82016-03-16 16:59:32 -07005205 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
Wale Ogunwale935e5022015-11-10 12:36:10 -08005206 return true;
5207 }
5208
Craig Mautnerc00204b2013-03-05 15:02:14 -08005209 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005210 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005211 }
5212
Wale Ogunwale5f986092015-12-04 15:35:38 -08005213 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Winson Chung5af42fc2017-03-24 17:11:33 -07005214 addTask(task, toTop ? MAX_VALUE : 0, true /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005215 if (toTop) {
5216 // TODO: figure-out a way to remove this call.
Wale Ogunwale1666e312016-12-16 11:27:18 -08005217 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
5218 true /* includingParents */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005219 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005220 }
5221
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005222 // TODO: This shouldn't allow automatic reparenting. Remove the call to preAddTask and deal
5223 // with the fall-out...
Winson Chung5af42fc2017-03-24 17:11:33 -07005224 void addTask(final TaskRecord task, int position, boolean schedulePictureInPictureModeChange,
5225 String reason) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005226 // TODO: Is this remove really needed? Need to look into the call path for the other addTask
5227 mTaskHistory.remove(task);
5228 position = getAdjustedPositionForTask(task, position, null /* starting */);
5229 final boolean toTop = position >= mTaskHistory.size();
Wale Ogunwale06579d62016-04-30 15:29:06 -07005230 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005231
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005232 mTaskHistory.add(position, task);
Andrii Kulian02b7a832016-10-06 23:11:56 -07005233 task.setStack(this);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005234
Craig Mautnerc00204b2013-03-05 15:02:14 -08005235 if (toTop) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005236 updateTaskReturnToForTopInsertion(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005237 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005238
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005239 updateTaskMovement(task, toTop);
5240
Winson Chung5af42fc2017-03-24 17:11:33 -07005241 postAddTask(task, prevStack, schedulePictureInPictureModeChange);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005242 }
5243
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005244 void positionChildAt(TaskRecord task, int index) {
5245
5246 if (task.getStack() != this) {
5247 throw new IllegalArgumentException("AS.positionChildAt: task=" + task
5248 + " is not a child of stack=" + this + " current parent=" + task.getStack());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005249 }
5250
5251 task.updateOverrideConfigurationForStack(this);
5252
Jorim Jaggi023da532016-04-20 22:42:32 -07005253 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07005254 final boolean wasResumed = topRunningActivity == task.getStack().mResumedActivity;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005255 insertTaskAtPosition(task, index);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005256 task.setStack(this);
Winson Chung5af42fc2017-03-24 17:11:33 -07005257 postAddTask(task, null /* prevStack */, true /* schedulePictureInPictureModeChange */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005258
Jorim Jaggi023da532016-04-20 22:42:32 -07005259 if (wasResumed) {
5260 if (mResumedActivity != null) {
5261 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5262 + " other stack to this stack mResumedActivity=" + mResumedActivity
5263 + " other mResumedActivity=" + topRunningActivity);
5264 }
5265 mResumedActivity = topRunningActivity;
5266 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005267
5268 // The task might have already been running and its visibility needs to be synchronized with
5269 // the visibility of the stack / windows.
5270 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
5271 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005272 }
5273
Wale Ogunwale06579d62016-04-30 15:29:06 -07005274 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005275 final ActivityStack prevStack = task.getStack();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005276 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005277 prevStack.removeTask(task, reason,
5278 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005279 }
5280 return prevStack;
5281 }
5282
Winson Chung5af42fc2017-03-24 17:11:33 -07005283 /**
5284 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
5285 * change. Callers may set this to false if they are explicitly scheduling PiP mode
5286 * changes themselves, like during the PiP animation
5287 */
5288 private void postAddTask(TaskRecord task, ActivityStack prevStack,
5289 boolean schedulePictureInPictureModeChange) {
5290 if (schedulePictureInPictureModeChange && prevStack != null) {
5291 mStackSupervisor.scheduleUpdatePictureInPictureModeIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005292 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005293 try {
5294 task.voiceSession.taskStarted(task.intent, task.taskId);
5295 } catch (RemoteException e) {
5296 }
5297 }
5298 }
5299
Winson Chungc2baac02017-01-11 13:34:47 -08005300 void moveToFrontAndResumeStateIfNeeded(ActivityRecord r, boolean moveToFront, boolean setResume,
5301 boolean setPause, String reason) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08005302 if (!moveToFront) {
5303 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005304 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005305
5306 // If the activity owns the last resumed activity, transfer that together,
5307 // so that we don't resume the same activity again in the new stack.
5308 // Apps may depend on onResume()/onPause() being called in pairs.
5309 if (setResume) {
5310 mResumedActivity = r;
Winson Chungabb433b2017-03-24 09:35:42 -07005311 updateLRUListLocked(r);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005312 }
Winson Chungc2baac02017-01-11 13:34:47 -08005313 // If the activity was previously pausing, then ensure we transfer that as well
5314 if (setPause) {
5315 mPausingActivity = r;
Winson Chung4dabf232017-01-25 13:25:22 -08005316 schedulePauseTimeout(r);
Winson Chungc2baac02017-01-11 13:34:47 -08005317 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005318 // Move the stack in which we are placing the activity to the front. The call will also
5319 // make sure the activity focus is set.
5320 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005321 }
5322
Craig Mautnerc00204b2013-03-05 15:02:14 -08005323 public int getStackId() {
5324 return mStackId;
5325 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005326
5327 @Override
5328 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005329 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5330 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005331 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005332
Craig Mautner15df08a2015-04-01 12:17:18 -07005333 void onLockTaskPackagesUpdatedLocked() {
5334 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5335 mTaskHistory.get(taskNdx).setLockTaskAuth();
5336 }
5337 }
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005338
5339 void executeAppTransition(ActivityOptions options) {
5340 mWindowManager.executeAppTransition();
5341 mNoAnimActivities.clear();
5342 ActivityOptions.abort(options);
5343 }
David Stevens9440dc82017-03-16 19:00:20 -07005344
5345 boolean shouldSleepActivities() {
5346 final ActivityStackSupervisor.ActivityDisplay display = getDisplay();
5347 return display != null ? display.isSleeping() : mService.isSleepingLocked();
5348 }
5349
5350 boolean shouldSleepOrShutDownActivities() {
5351 return shouldSleepActivities() || mService.isShuttingDownLocked();
5352 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005353}