blob: e479f85ab614c528104a5069ea3f3c7f2db13af7 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale56d75cf2016-03-09 15:14:47 -080023import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070024import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080025import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080026import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070027import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwalef5d1e352016-09-22 08:34:42 -070028import static android.view.Display.DEFAULT_DISPLAY;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070029import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
30import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
31import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_APP;
32import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CLEANUP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070033import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070034import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
35import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
36import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RESULTS;
37import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070038import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
39import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
40import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
41import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TRANSITION;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_USER_LEAVING;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
45import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
46import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_APP;
47import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CLEANUP;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070048import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
49import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
50import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
51import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RESULTS;
52import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070053import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TRANSITION;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_USER_LEAVING;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
60import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
61import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Craig Mautner84984fa2014-06-19 11:19:20 -070062import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070063import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Wale Ogunwale39381972015-12-17 17:15:29 -080064import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070065import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070066import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_CLOSE;
67import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_OPEN;
68import static com.android.server.wm.AppTransition.TRANSIT_NONE;
69import static com.android.server.wm.AppTransition.TRANSIT_TASK_CLOSE;
70import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN;
71import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN_BEHIND;
72import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_BACK;
73import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_FRONT;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080074import static java.lang.Integer.MAX_VALUE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075
76import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070077import android.app.ActivityManager;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070078import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070079import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070080import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080082import android.app.IActivityController;
Wale Ogunwale1666e312016-12-16 11:27:18 -080083import android.app.RemoteAction;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import android.app.ResultInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -080088import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089import android.content.res.Configuration;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070090import android.graphics.Point;
91import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -080092import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070093import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070094import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080095import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096import android.os.Handler;
97import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090098import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070099import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700100import android.os.RemoteException;
101import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -0800102import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700103import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700104import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700105import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700106import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800107import android.util.IntArray;
Jorim Jaggi023da532016-04-20 22:42:32 -0700108import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109import android.util.Slog;
Andrii Kulian1e32e022016-09-16 15:29:34 -0700110import android.util.SparseArray;
Craig Mautner59c00972012-07-30 12:10:24 -0700111import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700113import com.android.internal.app.IVoiceInteractor;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700114import com.android.internal.os.BatteryStatsImpl;
115import com.android.server.Watchdog;
116import com.android.server.am.ActivityManagerService.ItemMatcher;
117import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800118import com.android.server.wm.StackWindowController;
119import com.android.server.wm.StackWindowListener;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700120import com.android.server.wm.WindowManagerService;
121
Craig Mautnercae015f2013-02-08 14:31:27 -0800122import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800123import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124import java.lang.ref.WeakReference;
125import java.util.ArrayList;
126import java.util.Iterator;
127import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800128import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700129import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700130
131/**
132 * State and management of a single stack of activities.
133 */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800134final class ActivityStack extends ConfigurationContainer implements StackWindowListener {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800135
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800136 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700137 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
138 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800139 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700142 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700143 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700144 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700145 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700146 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700147 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
148 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
149 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
150 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
151 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700152
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700153 // Ticks during which we check progress while waiting for an app to launch.
154 static final int LAUNCH_TICK = 500;
155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700156 // How long we wait until giving up on the last activity to pause. This
157 // is short because it directly impacts the responsiveness of starting the
158 // next activity.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700159 private static final int PAUSE_TIMEOUT = 500;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700161 // How long we wait for the activity to tell us it has stopped before
162 // giving up. This is a good amount of time because we really need this
163 // from the application in order to get its saved state.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700164 private static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700165
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166 // How long we wait until giving up on an activity telling us it has
167 // finished destroying itself.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700168 private static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800171 // disabled.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700172 private static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800173
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174 // Set to false to disable the preview that is shown while a new activity
175 // is being started.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700176 private static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800177
Craig Mautner5eda9b32013-07-02 11:58:16 -0700178 // How long to wait for all background Activities to redraw following a call to
179 // convertToTranslucent().
Andrii Kulian21713ac2016-10-12 22:05:05 -0700180 private static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700181
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800182 // How many activities have to be scheduled to stop to force a stop pass.
183 private static final int MAX_STOPPING_TO_FORCE = 3;
184
Andrii Kulian1779e612016-10-12 21:58:25 -0700185 @Override
186 protected int getChildCount() {
187 return mTaskHistory.size();
188 }
189
190 @Override
191 protected ConfigurationContainer getChildAt(int index) {
192 return mTaskHistory.get(index);
193 }
194
195 @Override
196 protected ConfigurationContainer getParent() {
197 return mActivityContainer.mActivityDisplay;
198 }
199
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800200 @Override
201 void onParentChanged() {
202 super.onParentChanged();
203 mStackSupervisor.updateUIDsPresentOnDisplay();
204 }
205
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700206 enum ActivityState {
207 INITIALIZING,
208 RESUMED,
209 PAUSING,
210 PAUSED,
211 STOPPING,
212 STOPPED,
213 FINISHING,
214 DESTROYING,
215 DESTROYED
216 }
217
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800218 // Stack is not considered visible.
219 static final int STACK_INVISIBLE = 0;
220 // Stack is considered visible
221 static final int STACK_VISIBLE = 1;
222 // Stack is considered visible, but only becuase it has activity that is visible behind other
223 // activities and there is a specific combination of stacks.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700224 private static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800225
Wale Ogunwale06579d62016-04-30 15:29:06 -0700226 /* The various modes for the method {@link #removeTask}. */
227 // Task is being completely removed from all stacks in the system.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700228 private static final int REMOVE_TASK_MODE_DESTROYING = 0;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700229 // Task is being removed from this stack so we can add it to another stack. In the case we are
230 // moving we don't want to perform some operations on the task like removing it from window
231 // manager or recents.
232 static final int REMOVE_TASK_MODE_MOVING = 1;
233 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
234 // stack and the new stack will be on top of all stacks.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700235 private static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700236
Andrii Kulian1e32e022016-09-16 15:29:34 -0700237 // The height/width divide used when fitting a task within a bounds with method
238 // {@link #fitWithinBounds}.
239 // We always want the task to to be visible in the bounds without affecting its size when
240 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
241 // the input bounds right or bottom side minus the width or height divided by this value.
242 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
243
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700244 final ActivityManagerService mService;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700245 private final WindowManagerService mWindowManager;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800246 private StackWindowController mWindowContainerController;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800247 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249 /**
250 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800251 * running) activities. It contains #TaskRecord objects.
252 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800253 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800254
255 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700256 * List of running activities, sorted by recent usage.
257 * The first entry in the list is the least recently used.
258 * It contains HistoryRecord objects.
259 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800260 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700261
262 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700263 * Animations that for the current transition have requested not to
264 * be considered for the transition animation.
265 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800266 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267
268 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700269 * When we are in the process of pausing an activity, before starting the
270 * next one, this variable holds the activity that is currently being paused.
271 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800272 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273
274 /**
275 * This is the last activity that we put into the paused state. This is
276 * used to determine if we need to do an activity transition while sleeping,
277 * when we normally hold the top activity paused.
278 */
279 ActivityRecord mLastPausedActivity = null;
280
281 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700282 * Activities that specify No History must be removed once the user navigates away from them.
283 * If the device goes to sleep with such an activity in the paused state then we save it here
284 * and finish it later if another activity replaces it on wakeup.
285 */
286 ActivityRecord mLastNoHistoryActivity = null;
287
288 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289 * Current activity that is resumed, or null if there is none.
290 */
291 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800292
Craig Mautner5eda9b32013-07-02 11:58:16 -0700293 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
294 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
295 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
296 // Activity in mTranslucentActivityWaiting is notified via
297 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
298 // background activity being drawn then the same call will be made with a true value.
299 ActivityRecord mTranslucentActivityWaiting = null;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700300 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700301
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700302 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700303 * Set when we know we are going to be calling updateConfiguration()
304 * soon, so want to skip intermediate config checks.
305 */
306 boolean mConfigWillChange;
307
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700308 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800309 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700310 // Current bounds of the stack or null if fullscreen.
311 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800312
Andrii Kulian21713ac2016-10-12 22:05:05 -0700313 private boolean mUpdateBoundsDeferred;
314 private boolean mUpdateBoundsDeferredCalled;
315 private final Rect mDeferredBounds = new Rect();
316 private final Rect mDeferredTaskBounds = new Rect();
317 private final Rect mDeferredTaskInsetBounds = new Rect();
Jorim Jaggi192086e2016-03-11 17:17:03 +0100318
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700319 long mLaunchStartTime = 0;
320 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800321
Craig Mautner858d8a62013-04-23 17:08:34 -0700322 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700323
Craig Mautnerc00204b2013-03-05 15:02:14 -0800324 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800325 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800326 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
327 ArrayList<ActivityStack> mStacks;
328 /** The attached Display's unique identifier, or -1 if detached */
329 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800330
Andrii Kulian1e32e022016-09-16 15:29:34 -0700331 /** Temp variables used during override configuration update. */
332 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
333 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
334 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800335 private final Rect mTmpRect2 = new Rect();
Andrii Kulian1e32e022016-09-16 15:29:34 -0700336
Craig Mautner27084302013-03-25 08:05:25 -0700337 /** Run all ActivityStacks through this */
Andrii Kulian21713ac2016-10-12 22:05:05 -0700338 private final ActivityStackSupervisor mStackSupervisor;
Craig Mautner27084302013-03-25 08:05:25 -0700339
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700340 private final LaunchingTaskPositioner mTaskPositioner;
341
Jorim Jaggife762342016-10-13 14:33:27 +0200342 private boolean mTopActivityOccludesKeyguard;
343 private ActivityRecord mTopDismissingKeyguardActivity;
344
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700345 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700346 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
347 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
348 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
349 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700350 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700351 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700352 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700353
Andrii Kulian21713ac2016-10-12 22:05:05 -0700354 private static class ScheduleDestroyArgs {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700355 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700356 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700357 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700358 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700359 mReason = reason;
360 }
361 }
362
Zoran Marcetaf958b322012-08-09 20:27:12 +0900363 final Handler mHandler;
364
Andrii Kulian21713ac2016-10-12 22:05:05 -0700365 private class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800366
Craig Mautnerc8143c62013-09-03 12:15:57 -0700367 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900368 super(looper);
369 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700370
Zoran Marcetaf958b322012-08-09 20:27:12 +0900371 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 public void handleMessage(Message msg) {
373 switch (msg.what) {
374 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800375 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 // We don't at this point know if the activity is fullscreen,
377 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800378 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700379 synchronized (mService) {
380 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700381 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700382 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700383 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800384 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700385 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700386 case LAUNCH_TICK_MSG: {
387 ActivityRecord r = (ActivityRecord)msg.obj;
388 synchronized (mService) {
389 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700390 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700391 }
392 }
393 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800395 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700396 // We don't at this point know if the activity is fullscreen,
397 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800398 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800399 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800400 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800401 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700402 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700403 case STOP_TIMEOUT_MSG: {
404 ActivityRecord r = (ActivityRecord)msg.obj;
405 // We don't at this point know if the activity is fullscreen,
406 // so we need to be conservative and assume it isn't.
407 Slog.w(TAG, "Activity stop timeout for " + r);
408 synchronized (mService) {
409 if (r.isInHistory()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -0700410 r.activityStoppedLocked(null /* icicle */,
411 null /* persistentState */, null /* description */);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700412 }
413 }
414 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700415 case DESTROY_ACTIVITIES_MSG: {
416 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
417 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700418 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700419 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700420 } break;
421 case TRANSLUCENT_TIMEOUT_MSG: {
422 synchronized (mService) {
423 notifyActivityDrawnLocked(null);
424 }
425 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700426 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700427 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700428 final ActivityRecord r = getVisibleBehindActivity();
429 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700430 if (r != null) {
431 mService.killAppAtUsersRequest(r.app, null);
432 }
433 }
434 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 }
436 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800437 }
438
Craig Mautner34b73df2014-01-12 21:11:08 -0800439 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800440 int count = 0;
441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
442 count += mTaskHistory.get(taskNdx).mActivities.size();
443 }
444 return count;
445 }
446
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800447 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
Wale Ogunwale1666e312016-12-16 11:27:18 -0800448 RecentTasks recentTasks, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800449 mActivityContainer = activityContainer;
450 mStackSupervisor = activityContainer.getOuter();
451 mService = mStackSupervisor.mService;
452 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
453 mWindowManager = mService.mWindowManager;
454 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700455 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800456 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700457 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
458 ? new LaunchingTaskPositioner() : null;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800459 final ActivityStackSupervisor.ActivityDisplay display = mActivityContainer.mActivityDisplay;
460 mTmpRect2.setEmpty();
461 mWindowContainerController = new StackWindowController(mStackId, this,
462 display.mDisplayId, onTop, mTmpRect2);
463 activityContainer.mStack = this;
464 mStackSupervisor.mActivityContainers.put(mStackId, activityContainer);
465 postAddToDisplay(display, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
466 }
467
468 StackWindowController getWindowContainerController() {
469 return mWindowContainerController;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700470 }
471
Andrii Kulian839def92016-11-02 10:58:58 -0700472 /** Adds the stack to specified display and calls WindowManager to do the same. */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800473 void reparent(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
474 removeFromDisplay();
475 mTmpRect2.setEmpty();
476 mWindowContainerController.reparent(activityDisplay.mDisplayId, mTmpRect2);
477 postAddToDisplay(activityDisplay, mTmpRect2.isEmpty() ? null : mTmpRect2, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -0700478 }
479
480 /**
481 * Updates internal state after adding to new display.
482 * @param activityDisplay New display to which this stack was attached.
483 * @param bounds Updated bounds.
484 */
485 private void postAddToDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay,
Wale Ogunwale1666e312016-12-16 11:27:18 -0800486 Rect bounds, boolean onTop) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700487 mDisplayId = activityDisplay.mDisplayId;
488 mStacks = activityDisplay.mStacks;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800489 mBounds = bounds != null ? new Rect(bounds) : null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700490 mFullscreen = mBounds == null;
491 if (mTaskPositioner != null) {
492 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
493 mTaskPositioner.configure(mBounds);
494 }
Andrii Kulian1779e612016-10-12 21:58:25 -0700495 onParentChanged();
Wale Ogunwale961f4852016-02-01 20:25:54 -0800496
Wale Ogunwale1666e312016-12-16 11:27:18 -0800497 activityDisplay.attachStack(this, onTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -0800498 if (mStackId == DOCKED_STACK_ID) {
499 // If we created a docked stack we want to resize it so it resizes all other stacks
500 // in the system.
501 mStackSupervisor.resizeDockedStackLocked(
502 mBounds, null, null, null, null, PRESERVE_WINDOWS);
503 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700504 }
505
Andrii Kulian839def92016-11-02 10:58:58 -0700506 /**
Andrii Kulian839def92016-11-02 10:58:58 -0700507 * Updates the inner state of the stack to remove it from its current parent, so it can be
508 * either destroyed completely or re-parented.
509 */
510 private void removeFromDisplay() {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700511 mDisplayId = Display.INVALID_DISPLAY;
512 mStacks = null;
513 if (mTaskPositioner != null) {
514 mTaskPositioner.reset();
515 }
Jorim Jaggi899327f2016-02-25 20:44:18 -0500516 if (mStackId == DOCKED_STACK_ID) {
517 // If we removed a docked stack we want to resize it so it resizes all other stacks
518 // in the system to fullscreen.
519 mStackSupervisor.resizeDockedStackLocked(
520 null, null, null, null, null, PRESERVE_WINDOWS);
521 }
Andrii Kulian839def92016-11-02 10:58:58 -0700522 }
523
524 /** Removes the stack completely. Also calls WindowManager to do the same on its side. */
525 void remove() {
526 removeFromDisplay();
Andrii Kulian6d6fb402016-10-26 16:15:25 -0700527 mStackSupervisor.deleteActivityContainerRecord(mStackId);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800528 mWindowContainerController.removeContainer();
529 mWindowContainerController = null;
Andrii Kulian6d6fb402016-10-26 16:15:25 -0700530 onParentChanged();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700531 }
532
Andrii Kulian21713ac2016-10-12 22:05:05 -0700533 void getDisplaySize(Point out) {
Winsonc809cbb2015-11-02 12:06:15 -0800534 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
535 }
536
Wale Ogunwale1666e312016-12-16 11:27:18 -0800537 void animateResizePinnedStack(Rect bounds, int animationDuration) {
538 mWindowContainerController.animateResizePinnedStack(bounds, animationDuration);
539 }
540
541 void setPictureInPictureAspectRatio(float aspectRatio) {
542 mWindowContainerController.setPictureInPictureAspectRatio(aspectRatio);
543 }
544
545 void getStackDockedModeBounds(Rect outBounds, boolean ignoreVisibility) {
546 mWindowContainerController.getStackDockedModeBounds(outBounds, ignoreVisibility);
547 }
548
549 void prepareFreezingTaskBounds() {
550 mWindowContainerController.prepareFreezingTaskBounds();
551 }
552
553 void setPictureInPictureActions(List<RemoteAction> actions) {
554 mWindowContainerController.setPictureInPictureActions(actions);
555 }
556
557 void getWindowContainerBounds(Rect outBounds) {
558 if (mWindowContainerController != null) {
559 mWindowContainerController.getBounds(outBounds);
Winson Chungb00dc5e2017-01-25 09:44:25 -0800560 return;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800561 }
562 outBounds.setEmpty();
563 }
564
565 Rect getBoundsForNewConfiguration() {
566 return mWindowContainerController.getBoundsForNewConfiguration();
567 }
568
569 void positionChildWindowContainerAtTop(TaskRecord child) {
570 mWindowContainerController.positionChildAtTop(child.getWindowContainerController(),
571 true /* includingParents */);
572 }
573
Jorim Jaggi192086e2016-03-11 17:17:03 +0100574 /**
575 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
576 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
577 */
578 void deferUpdateBounds() {
579 if (!mUpdateBoundsDeferred) {
580 mUpdateBoundsDeferred = true;
581 mUpdateBoundsDeferredCalled = false;
582 }
583 }
584
585 /**
586 * Continues updating bounds after updates have been deferred. If there was a resize attempt
587 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
588 * be resized to that bounds.
589 */
590 void continueUpdateBounds() {
591 final boolean wasDeferred = mUpdateBoundsDeferred;
592 mUpdateBoundsDeferred = false;
593 if (wasDeferred && mUpdateBoundsDeferredCalled) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800594 resize(mDeferredBounds.isEmpty() ? null : mDeferredBounds,
Jorim Jaggi192086e2016-03-11 17:17:03 +0100595 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
596 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
597 }
598 }
599
600 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
601 Rect tempTaskInsetBounds) {
602 if (!mUpdateBoundsDeferred) {
603 return true;
604 }
605 if (bounds != null) {
606 mDeferredBounds.set(bounds);
607 } else {
608 mDeferredBounds.setEmpty();
609 }
610 if (tempTaskBounds != null) {
611 mDeferredTaskBounds.set(tempTaskBounds);
612 } else {
613 mDeferredTaskBounds.setEmpty();
614 }
615 if (tempTaskInsetBounds != null) {
616 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
617 } else {
618 mDeferredTaskInsetBounds.setEmpty();
619 }
620 mUpdateBoundsDeferredCalled = true;
621 return false;
622 }
623
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700624 void setBounds(Rect bounds) {
625 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700626 if (mTaskPositioner != null) {
627 mTaskPositioner.configure(bounds);
628 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 }
Craig Mautner5962b122012-10-05 14:45:52 -0700630
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700631 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800632 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700633 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700634 if (r != null) {
635 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800636 }
637 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700638 return null;
639 }
640
641 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800642 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
643 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800644 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800645 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
646 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -0700647 if (!r.finishing && !r.delayedResume && r != notTop && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800648 return r;
649 }
650 }
651 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700652 return null;
653 }
654
655 /**
656 * This is a simplified version of topRunningActivityLocked that provides a number of
657 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800658 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700659 * @param token If non-null, any history records matching this token will be skipped.
660 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800661 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700662 * @return Returns the HistoryRecord of the next activity on the stack.
663 */
664 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800665 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
666 TaskRecord task = mTaskHistory.get(taskNdx);
667 if (task.taskId == taskId) {
668 continue;
669 }
670 ArrayList<ActivityRecord> activities = task.mActivities;
671 for (int i = activities.size() - 1; i >= 0; --i) {
672 final ActivityRecord r = activities.get(i);
673 // Note: the taskId check depends on real taskId fields being non-zero
Chong Zhang87761972016-08-22 13:53:24 -0700674 if (!r.finishing && (token != r.appToken) && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800675 return r;
676 }
677 }
678 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700679 return null;
680 }
681
Craig Mautner8849a5e2013-04-02 16:41:03 -0700682 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700683 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
684 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700685 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
686 final ActivityRecord r = activities.get(activityNdx);
687 if (!r.finishing) {
688 return r;
689 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700690 }
691 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700692 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700693 }
694
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700695 final TaskRecord topTask() {
696 final int size = mTaskHistory.size();
697 if (size > 0) {
698 return mTaskHistory.get(size - 1);
699 }
700 return null;
701 }
702
Craig Mautnerd2328952013-03-05 12:46:26 -0800703 TaskRecord taskForIdLocked(int id) {
704 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
705 final TaskRecord task = mTaskHistory.get(taskNdx);
706 if (task.taskId == id) {
707 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800708 }
709 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700710 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700711 }
712
Craig Mautnerd2328952013-03-05 12:46:26 -0800713 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700714 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800715 return isInStackLocked(r);
716 }
717
718 ActivityRecord isInStackLocked(ActivityRecord r) {
719 if (r == null) {
720 return null;
721 }
722 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -0700723 final ActivityStack stack = r.getStack();
724 if (stack != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
725 if (stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800726 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800727 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800728 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800729 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800730 }
731
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800732 boolean isInStackLocked(TaskRecord task) {
733 return mTaskHistory.contains(task);
734 }
735
736 /** Checks if there are tasks with specific UID in the stack. */
737 boolean isUidPresent(int uid) {
738 for (TaskRecord task : mTaskHistory) {
739 if (task.effectiveUid == uid) {
740 return true;
741 }
742 }
743 return false;
744 }
745
746 /** Get all UIDs that are present in the stack. */
747 void getPresentUIDs(IntArray presentUIDs) {
748 for (TaskRecord task : mTaskHistory) {
749 presentUIDs.add(task.effectiveUid);
750 }
751 }
752
Craig Mautner2420ead2013-04-01 17:13:20 -0700753 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700754 final boolean hadit = mLRUActivities.remove(r);
755 mLRUActivities.add(r);
756 return hadit;
757 }
758
Craig Mautnerde4ef022013-04-07 19:01:33 -0700759 final boolean isHomeStack() {
760 return mStackId == HOME_STACK_ID;
761 }
762
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800763 final boolean isHomeOrRecentsStack() {
764 return StackId.isHomeOrRecentsStack(mStackId);
765 }
766
Winson Chung0583d3d2015-12-18 10:03:12 -0500767 final boolean isDockedStack() {
768 return mStackId == DOCKED_STACK_ID;
769 }
770
771 final boolean isPinnedStack() {
772 return mStackId == PINNED_STACK_ID;
773 }
774
Craig Mautnere0a38842013-12-16 16:14:02 -0800775 final boolean isOnHomeDisplay() {
776 return isAttached() &&
Wale Ogunwalef5d1e352016-09-22 08:34:42 -0700777 mActivityContainer.mActivityDisplay.mDisplayId == DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -0800778 }
779
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700780 void moveToFront(String reason) {
781 moveToFront(reason, null);
782 }
783
784 /**
785 * @param reason The reason for moving the stack to the front.
786 * @param task If non-null, the task will be moved to the top of the stack.
787 * */
788 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700789 if (!isAttached()) {
790 return;
791 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700792
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700793 mStacks.remove(this);
794 int addIndex = mStacks.size();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700795 if (addIndex > 0) {
796 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800797 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
798 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700799 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800800 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700801 }
802
803 mStacks.add(addIndex, this);
Andrii Kulian839def92016-11-02 10:58:58 -0700804 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700805 if (task != null) {
806 insertTaskAtTop(task, null);
Wale Ogunwaleaa47c122016-09-23 16:39:53 -0700807 return;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700808 }
Wale Ogunwaleaa47c122016-09-23 16:39:53 -0700809
810 task = topTask();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700811 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800812 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
813 true /* includingParents */);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800814 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800815 }
816
Jiaquan Hee13e9642016-06-15 15:16:13 -0700817 /**
818 * @param task If non-null, the task will be moved to the back of the stack.
819 * */
Andrii Kulian21713ac2016-10-12 22:05:05 -0700820 private void moveToBack(TaskRecord task) {
Jiaquan Hee13e9642016-06-15 15:16:13 -0700821 if (!isAttached()) {
822 return;
823 }
824
825 mStacks.remove(this);
826 mStacks.add(0, this);
827
828 if (task != null) {
829 mTaskHistory.remove(task);
830 mTaskHistory.add(0, task);
831 updateTaskMovement(task, false);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800832 mWindowContainerController.positionChildAtBottom(task.getWindowContainerController());
Jiaquan Hee13e9642016-06-15 15:16:13 -0700833 }
834 }
835
Wale Ogunwaled046a012015-12-24 13:05:59 -0800836 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800837 if (StackId.canReceiveKeys(mStackId)) {
838 return true;
839 }
840 // The stack isn't focusable. See if its top activity is focusable to force focus on the
841 // stack.
842 final ActivityRecord r = topRunningActivityLocked();
843 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800844 }
845
Craig Mautnere0a38842013-12-16 16:14:02 -0800846 final boolean isAttached() {
847 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800848 }
849
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800851 * Returns the top activity in any existing task matching the given Intent in the input result.
852 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800854 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700855 Intent intent = target.intent;
856 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 ComponentName cls = intent.getComponent();
858 if (info.targetActivity != null) {
859 cls = new ComponentName(info.packageName, info.targetActivity);
860 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700861 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700862 boolean isDocument = intent != null & intent.isDocument();
863 // If documentData is non-null then it must match the existing task data.
864 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800865
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700866 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800867 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
868 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700869 if (task.voiceSession != null) {
870 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700871 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700872 continue;
873 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700874 if (task.userId != userId) {
875 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700876 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700877 continue;
878 }
Craig Mautner000f0022013-02-26 15:04:29 -0800879 final ActivityRecord r = task.getTopActivity();
880 if (r == null || r.finishing || r.userId != userId ||
881 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700882 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800883 continue;
884 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700885 if (r.mActivityType != target.mActivityType) {
886 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
887 continue;
888 }
Craig Mautner000f0022013-02-26 15:04:29 -0800889
Craig Mautnerd00f4742014-03-12 14:17:26 -0700890 final Intent taskIntent = task.intent;
891 final Intent affinityIntent = task.affinityIntent;
892 final boolean taskIsDocument;
893 final Uri taskDocumentData;
894 if (taskIntent != null && taskIntent.isDocument()) {
895 taskIsDocument = true;
896 taskDocumentData = taskIntent.getData();
897 } else if (affinityIntent != null && affinityIntent.isDocument()) {
898 taskIsDocument = true;
899 taskDocumentData = affinityIntent.getData();
900 } else {
901 taskIsDocument = false;
902 taskDocumentData = null;
903 }
904
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700905 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700906 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700907 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700908 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700909 // TODO Refactor to remove duplications. Check if logic can be simplified.
910 if (taskIntent != null && taskIntent.getComponent() != null &&
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700911 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700912 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800914 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700915 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
916 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800917 result.r = r;
918 result.matchedByRootAffinity = false;
919 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700920 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
921 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700922 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700923 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800924 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700925 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
926 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800927 result.r = r;
928 result.matchedByRootAffinity = false;
929 break;
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700930 } else if (!isDocument && !taskIsDocument
931 && result.r == null && task.canMatchRootAffinity()) {
932 if (task.rootAffinity.equals(target.taskAffinity)) {
933 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
934 // It is possible for multiple tasks to have the same root affinity especially
935 // if they are in separate stacks. We save off this candidate, but keep looking
936 // to see if there is a better candidate.
937 result.r = r;
938 result.matchedByRootAffinity = true;
939 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700940 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700942 }
943
944 /**
945 * Returns the first activity (starting from the top of the stack) that
946 * is the same as the given activity. Returns null if no such activity
947 * is found.
948 */
Andrii Kuliand3bbb132016-06-16 16:00:20 -0700949 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
950 boolean compareIntentFilters) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700951 ComponentName cls = intent.getComponent();
952 if (info.targetActivity != null) {
953 cls = new ComponentName(info.packageName, info.targetActivity);
954 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700955 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700956
Craig Mautner000f0022013-02-26 15:04:29 -0800957 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700958 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700959 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700960
Craig Mautner000f0022013-02-26 15:04:29 -0800961 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
962 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -0700963 if (!r.okToShowLocked()) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700964 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700965 }
Andrii Kuliand3bbb132016-06-16 16:00:20 -0700966 if (!r.finishing && r.userId == userId) {
967 if (compareIntentFilters) {
968 if (r.intent.filterEquals(intent)) {
969 return r;
970 }
971 } else {
972 if (r.intent.getComponent().equals(cls)) {
973 return r;
974 }
975 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700976 }
977 }
978 }
979
980 return null;
981 }
982
Amith Yamasani742a6712011-05-04 14:49:28 -0700983 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700984 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700985 */
Craig Mautner93529a42013-10-04 15:03:13 -0700986 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800987 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700988 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800989 }
990 mCurrentUser = userId;
991
992 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800993 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800994 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700995 final TaskRecord task = mTaskHistory.get(i);
996
Chong Zhang87761972016-08-22 13:53:24 -0700997 if (task.okToShowLocked()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700998 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700999 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001000 mTaskHistory.remove(i);
1001 mTaskHistory.add(task);
1002 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001003 // Use same value for i.
1004 } else {
1005 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001006 }
1007 }
Amith Yamasani742a6712011-05-04 14:49:28 -07001008 }
1009
Craig Mautner2420ead2013-04-01 17:13:20 -07001010 void minimalResumeActivityLocked(ActivityRecord r) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001011 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
1012 + " callers=" + Debug.getCallers(5));
Chong Zhang6cda19c2016-06-14 19:07:56 -07001013 setResumedActivityLocked(r, "minimalResumeActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001014 r.completeResumeLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -07001015 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001016 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
1017 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018 }
1019
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08001020 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -07001021 if (r != null) {
1022 mRecentTasks.addLocked(r.task);
1023 r.task.touchActiveTime();
1024 }
1025 }
1026
Narayan Kamath7829c812015-06-08 17:39:43 +01001027 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001028 if (mFullyDrawnStartTime != 0) {
1029 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1030 }
Narayan Kamath7829c812015-06-08 17:39:43 +01001031 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -07001032 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1033 }
1034
1035 private void stopFullyDrawnTraceIfNeeded() {
1036 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
1037 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1038 mFullyDrawnStartTime = 0;
1039 }
1040 }
1041
Craig Mautnere79d42682013-04-01 19:01:53 -07001042 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001043 if (r.displayStartTime == 0) {
1044 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
1045 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001046 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001047 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001048 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001049 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001050 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001051 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001052 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001053 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001054
Andrii Kulian21713ac2016-10-12 22:05:05 -07001055 private void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -07001056 // Make sure that there is no activity waiting for this to launch.
1057 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
1058 r.displayStartTime = r.fullyDrawnStartTime = 0;
1059 } else {
1060 mStackSupervisor.removeTimeoutsForActivityLocked(r);
1061 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1062 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001063 }
1064
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001065 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001066 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001067 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1068 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1069 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1070 activities.get(activityNdx).setSleeping(false);
1071 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001072 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -08001073 if (mPausingActivity != null) {
1074 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
1075 activityPausedLocked(mPausingActivity.appToken, true);
1076 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001077 }
1078
Todd Kennedy39bfee52016-02-24 10:28:21 -08001079 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1080 final String packageName = aInfo.packageName;
1081 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1082 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1083 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1084 if (packageName.equals(activities.get(activityNdx).packageName)) {
1085 activities.get(activityNdx).info.applicationInfo = aInfo;
1086 }
1087 }
1088 }
1089 }
1090
Craig Mautner0eea92c2013-05-16 13:35:39 -07001091 /**
1092 * @return true if something must be done before going to sleep.
1093 */
1094 boolean checkReadyForSleepLocked() {
1095 if (mResumedActivity != null) {
1096 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001097 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
1098 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1099 "Sleep => pause with userLeaving=false");
Wale Ogunwale950faff2016-08-08 09:51:04 -07001100 startPausingLocked(false, true, null, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001101 return true;
1102 }
1103 if (mPausingActivity != null) {
1104 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001105 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001106 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001107 }
1108
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001109 if (hasVisibleBehindActivity()) {
1110 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001111 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001112 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001113 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001114 return true;
1115 }
1116
Craig Mautner0eea92c2013-05-16 13:35:39 -07001117 return false;
1118 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001119
Craig Mautner0eea92c2013-05-16 13:35:39 -07001120 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001121 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001122
Andrii Kulianf9949d52016-05-06 12:53:25 -07001123 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001124 // This ensures that the activity's onStop() is called.
1125 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1126 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1127 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1128 final ActivityRecord r = activities.get(activityNdx);
Andrii Kulianf9949d52016-05-06 12:53:25 -07001129 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED
1130 || r.state == ActivityState.PAUSED || r.state == ActivityState.PAUSING) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001131 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001132 }
1133 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001134 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135 }
Craig Mautner59c00972012-07-30 12:10:24 -07001136
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001137 /**
1138 * Start pausing the currently resumed activity. It is an error to call this if there
1139 * is already an activity being paused or there is no resumed activity.
1140 *
1141 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1142 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1143 * screen turning off).
Wale Ogunwale950faff2016-08-08 09:51:04 -07001144 * @param resuming The activity we are currently trying to resume or null if this is not being
1145 * called as part of resuming the top activity, so we shouldn't try to instigate
1146 * a resume here if not null.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001147 * @param dontWait True if the caller does not want to wait for the pause to complete. If
1148 * set to true, we will immediately complete the pause here before returning.
1149 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1150 * it to tell us when it is done.
1151 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001152 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
1153 ActivityRecord resuming, boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001154 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001155 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1156 + " state=" + mPausingActivity.state);
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001157 if (!mService.isSleepingLocked()) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001158 // Avoid recursion among check for sleep and complete pause during sleeping.
1159 // Because activity will be paused immediately after resume, just let pause
1160 // be completed by the order of activity paused from clients.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001161 completePauseLocked(false, resuming);
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001162 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001163 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001164 ActivityRecord prev = mResumedActivity;
1165 if (prev == null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07001166 if (resuming == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001167 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001168 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001169 }
1170 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001171 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001172
1173 if (mActivityContainer.mParentActivity == null) {
1174 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001175 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001176 }
1177
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001178 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001179 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001180 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001181 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001182 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001183 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1184 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001185 prev.state = ActivityState.PAUSING;
1186 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001187 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001188 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -07001189 if (mService.mHasRecents
1190 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001191 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001192 }
Dianne Hackborncee04b52013-07-03 17:01:28 -07001193 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194
1195 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001196
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001198 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199 try {
1200 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001201 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001202 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001203 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001204 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001205 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001206 } catch (Exception e) {
1207 // Ignore exception, if process died other code will cleanup.
1208 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001209 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001210 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001211 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 }
1213 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001214 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001216 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001217 }
1218
1219 // If we are not going to sleep, we want to ensure the device is
1220 // awake until the next activity is started.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001221 if (!uiSleeping && !mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001222 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001223 }
1224
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001225 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001226 // Have the window manager pause its key dispatching until the new
1227 // activity has started. If we're pausing the activity just because
1228 // the screen is being turned off and the UI is sleeping, don't interrupt
1229 // key dispatch; the same activity will pick it up again on wakeup.
1230 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001231 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001232 } else if (DEBUG_PAUSE) {
1233 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234 }
1235
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001236 if (dontWait) {
1237 // If the caller said they don't want to wait for the pause, then complete
1238 // the pause now.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001239 completePauseLocked(false, resuming);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001240 return false;
1241
1242 } else {
1243 // Schedule a pause timeout in case the app doesn't respond.
1244 // We don't give it much time because this directly impacts the
1245 // responsiveness seen by the user.
1246 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1247 msg.obj = prev;
1248 prev.pauseTime = SystemClock.uptimeMillis();
1249 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001250 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001251 return true;
1252 }
1253
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001254 } else {
1255 // This activity failed to schedule the
1256 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001257 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Wale Ogunwale950faff2016-08-08 09:51:04 -07001258 if (resuming == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001259 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001260 }
1261 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001262 }
1263 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001264
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001265 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001266 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1267 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001268
Craig Mautnerd2328952013-03-05 12:46:26 -08001269 final ActivityRecord r = isInStackLocked(token);
1270 if (r != null) {
1271 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1272 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001273 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001274 + (timeout ? " (due to timeout)" : " (pause complete)"));
Jorim Jaggife762342016-10-13 14:33:27 +02001275 mService.mWindowManager.deferSurfaceLayout();
1276 try {
1277 completePauseLocked(true, null);
1278 } finally {
1279 mService.mWindowManager.continueSurfaceLayout();
1280 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001281 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001282 } else {
1283 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1284 r.userId, System.identityHashCode(r), r.shortComponentName,
1285 mPausingActivity != null
1286 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001287 if (r.state == ActivityState.PAUSING) {
1288 r.state = ActivityState.PAUSED;
1289 if (r.finishing) {
1290 if (DEBUG_PAUSE) Slog.v(TAG,
1291 "Executing finish of failed to pause activity: " + r);
1292 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1293 }
louis_chang047dfd42015-04-08 16:35:55 +08001294 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001295 }
1296 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001297 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001298 }
1299
Wale Ogunwale950faff2016-08-08 09:51:04 -07001300 private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001301 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001302 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001303
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001304 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001305 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001306 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001307 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001308 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001309 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001310 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001311 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1312 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001313 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001314 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1315 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001316 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001317 if (prev.deferRelaunchUntilPaused) {
1318 // Complete the deferred relaunch that was waiting for pause to complete.
1319 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001320 prev.relaunchActivityLocked(false /* andResume */,
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001321 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001322 } else if (wasStopping) {
1323 // We are also stopping, the stop request must have gone soon after the pause.
1324 // We can't clobber it, because the stop confirmation will not be handled.
1325 // We don't need to schedule another stop, we only need to let it happen.
1326 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001327 } else if ((!prev.visible && !hasVisibleBehindActivity())
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001328 || mService.isSleepingOrShuttingDownLocked()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001329 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001330 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001331 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001332 }
1333 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001334 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001335 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001336 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001337 // It is possible the activity was freezing the screen before it was paused.
1338 // In that case go ahead and remove the freeze this activity has on the screen
1339 // since it is no longer visible.
Wale Ogunwalee8524002016-09-13 16:34:57 -07001340 if (prev != null) {
1341 prev.stopFreezingScreenLocked(true /*force*/);
1342 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001343 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001344 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001345
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001346 if (resumeNext) {
1347 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001348 if (!mService.isSleepingOrShuttingDownLocked()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001349 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001350 } else {
1351 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001352 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001353 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001354 // If there are no more activities available to run, do resume anyway to start
1355 // something. Also if the top activity on the stack is not the just paused
1356 // activity, we need to go ahead and resume it to ensure we complete an
1357 // in-flight app switch.
1358 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001359 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001360 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001361 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001362
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001363 if (prev != null) {
1364 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001365
Craig Mautner525f3d92013-05-07 14:01:50 -07001366 if (prev.app != null && prev.cpuTimeAtResume > 0
1367 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001368 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1369 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001370 if (diff > 0) {
1371 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1372 synchronized (bsi) {
1373 BatteryStatsImpl.Uid.Proc ps =
1374 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001375 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001376 if (ps != null) {
1377 ps.addForegroundTimeLocked(diff);
1378 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001379 }
1380 }
1381 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001382 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001383 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001384
Andrii Kulian8290f8f2016-06-30 17:51:32 -07001385 // Notify when the task stack has changed, but only if visibilities changed (not just
1386 // focus). Also if there is an active pinned stack - we always want to notify it about
1387 // task stack changes, because its positioning may depend on it.
1388 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause
1389 || mService.mStackSupervisor.getStack(PINNED_STACK_ID) != null) {
Yorke Leebd54c2a2016-10-25 13:49:23 -07001390 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001391 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1392 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001393
Wale Ogunwale950faff2016-08-08 09:51:04 -07001394 mStackSupervisor.ensureActivitiesVisibleLocked(resuming, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001395 }
1396
Andrii Kulian21713ac2016-10-12 22:05:05 -07001397 void addToStopping(ActivityRecord r, boolean immediate) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001398 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1399 mStackSupervisor.mStoppingActivities.add(r);
1400 }
1401
1402 // If we already have a few activities waiting to stop, then give up
1403 // on things going idle and start clearing them out. Or if r is the
1404 // last of activity of the last task the stack will be empty and must
1405 // be cleared immediately.
1406 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1407 || (r.frontOfTask && mTaskHistory.size() <= 1);
1408
1409 if (immediate || forceIdle) {
1410 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1411 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001412 mStackSupervisor.scheduleIdleLocked();
1413 } else {
1414 mStackSupervisor.checkReadyForSleepLocked();
1415 }
1416 }
1417
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001418 // Find the first visible activity above the passed activity and if it is translucent return it
1419 // otherwise return null;
1420 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1421 TaskRecord task = r.task;
1422 if (task == null) {
1423 return null;
1424 }
1425
Andrii Kulian02b7a832016-10-06 23:11:56 -07001426 final ActivityStack stack = task.getStack();
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001427 if (stack == null) {
1428 return null;
1429 }
1430
1431 int stackNdx = mStacks.indexOf(stack);
1432
1433 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1434 int taskNdx = tasks.indexOf(task);
1435
1436 ArrayList<ActivityRecord> activities = task.mActivities;
1437 int activityNdx = activities.indexOf(r) + 1;
1438
1439 final int numStacks = mStacks.size();
1440 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001441 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001442 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001443 final int numTasks = tasks.size();
1444 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001445 final TaskRecord currentTask = tasks.get(taskNdx);
1446 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001447 final int numActivities = activities.size();
1448 while (activityNdx < numActivities) {
1449 final ActivityRecord activity = activities.get(activityNdx);
1450 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001451 return historyStack.mFullscreen
1452 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001453 }
1454 ++activityNdx;
1455 }
1456 activityNdx = 0;
1457 ++taskNdx;
1458 }
1459 taskNdx = 0;
1460 ++stackNdx;
1461 }
1462
1463 return null;
1464 }
1465
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001466 /** Returns true if the stack contains a fullscreen task. */
1467 private boolean hasFullscreenTask() {
1468 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1469 final TaskRecord task = mTaskHistory.get(i);
1470 if (task.mFullscreen) {
1471 return true;
1472 }
1473 }
1474 return false;
1475 }
1476
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001477 /**
1478 * Returns true if the stack is translucent and can have other contents visible behind it if
1479 * needed. A stack is considered translucent if it don't contain a visible or
1480 * starting (about to be visible) activity that is fullscreen (opaque).
1481 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001482 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001483 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001484 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001485 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1486 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001487 final ArrayList<ActivityRecord> activities = task.mActivities;
1488 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1489 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001490
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001491 if (r.finishing) {
1492 // We don't factor in finishing activities when determining translucency since
1493 // they will be gone soon.
1494 continue;
1495 }
1496
1497 if (!r.visible && r != starting) {
1498 // Also ignore invisible activities that are not the currently starting
1499 // activity (about to be visible).
1500 continue;
1501 }
1502
1503 if (r.fullscreen) {
1504 // Stack isn't translucent if it has at least one fullscreen activity
1505 // that is visible.
1506 return false;
1507 }
1508
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001509 if (!isHomeOrRecentsStack() && r.frontOfTask
1510 && task.isOverHomeStack() && !StackId.isHomeOrRecentsStack(stackBehindId)) {
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001511 // Stack isn't translucent if it's top activity should have the home stack
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001512 // behind it and the stack currently behind it isn't the home or recents stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001513 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001514 }
1515 }
1516 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001517 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001518 }
1519
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001520 /**
1521 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1522 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001523 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001524 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001525 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001526 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001527 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001528 }
1529
Andrii Kuliane8d928a2016-11-14 18:38:14 -08001530 if (mStackSupervisor.isFrontStackOnDisplay(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001531 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001532 }
1533
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001534 final int stackIndex = mStacks.indexOf(this);
1535
1536 if (stackIndex == mStacks.size() - 1) {
1537 Slog.wtf(TAG,
1538 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001539 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001540 }
1541
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001542 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1543 final int focusedStackId = focusedStack.mStackId;
1544
Jiaquan He9bc01a82016-07-08 10:29:38 -07001545 final TaskRecord topFocusedTask = focusedStack.topTask();
1546 final boolean isOnTopLauncherFocused = topFocusedTask != null &&
1547 topFocusedTask.isOnTopLauncher();
1548 if (isOnTopLauncherFocused) {
1549 // When an on-top launcher is focused, we should find out whether the freeform stack or
1550 // the fullscreen stack appears first underneath and has activities to show, and then
1551 // make it visible.
1552 boolean behindFullscreenOrFreeForm = false;
1553 for (int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1554 stackBehindFocusedIndex >= 0; stackBehindFocusedIndex--) {
1555 ActivityStack stack = mStacks.get(stackBehindFocusedIndex);
1556 if ((stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
1557 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID)
1558 && stack.topRunningActivityLocked() != null) {
1559 if (stackIndex == stackBehindFocusedIndex) {
1560 return !behindFullscreenOrFreeForm ? STACK_VISIBLE : STACK_INVISIBLE;
1561 }
1562 behindFullscreenOrFreeForm = true;
1563 }
1564 }
1565 }
Jiaquan Hec4dcd4b2016-07-29 15:31:10 -07001566 // If an on-top launcher is on the top of the home stack but the home stack is not focused,
1567 // then the whole stack should be invisible.
1568 TaskRecord topTask = topTask();
1569 if (mStackId != focusedStackId && topTask != null && topTask.isOnTopLauncher()) {
1570 // We're here mostly because the on-top launcher didn't have a chance to move itself to
1571 // back. We should move it to back as soon as possible to avoid other activities
1572 // returning to it or other visibility issues.
1573 moveTaskToBackLocked(topTask.taskId);
1574 return STACK_INVISIBLE;
1575 }
Jiaquan He9bc01a82016-07-08 10:29:38 -07001576
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001577 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001578 && hasVisibleBehindActivity() && StackId.isHomeOrRecentsStack(focusedStackId)
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001579 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001580 // The fullscreen stack should be visible if it has a visible behind activity behind
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001581 // the home or recents stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001582 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001583 }
1584
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001585 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001586 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001587 // task in the focus stack doesn't support any form of resizing but we show it for the
1588 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001589 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001590 final TaskRecord task = r != null ? r.task : null;
Winson Chungd3395382016-12-13 11:49:09 -08001591 return task == null || task.supportsSplitScreen() || task.isHomeTask() ? STACK_VISIBLE
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001592 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001593 }
1594
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001595 // Find the first stack behind focused stack that actually got something visible.
1596 int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1597 while (stackBehindFocusedIndex >= 0 &&
1598 mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1599 stackBehindFocusedIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001600 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001601 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001602 && stackIndex == stackBehindFocusedIndex) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001603 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001604 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001605 }
1606
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001607 final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1608 ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1609
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001610 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001611 && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001612 // Stacks behind the fullscreen stack with a translucent activity are always
1613 // visible so they can act as a backdrop to the translucent activity.
1614 // For example, dialog activities
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001615 if (stackIndex == stackBehindFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001616 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001617 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001618 if (stackBehindFocusedIndex >= 0) {
1619 if ((stackBehindFocusedId == DOCKED_STACK_ID
1620 || stackBehindFocusedId == PINNED_STACK_ID)
1621 && stackIndex == (stackBehindFocusedIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001622 // The stack behind the docked or pinned stack is also visible so we can have a
1623 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001624 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001625 }
1626 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001627 }
1628
Wale Ogunwale3797c222015-10-27 14:21:58 -07001629 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001630 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001631 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001632 }
1633
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001634 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001635 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001636
1637 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1638 continue;
1639 }
1640
Wale Ogunwale3797c222015-10-27 14:21:58 -07001641 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001642 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001643 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001644 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001645
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001646 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001647 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001648 }
1649 }
1650
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001651 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001652 }
1653
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001654 final int rankTaskLayers(int baseLayer) {
1655 int layer = 0;
1656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1657 final TaskRecord task = mTaskHistory.get(taskNdx);
1658 ActivityRecord r = task.topRunningActivityLocked();
1659 if (r == null || r.finishing || !r.visible) {
1660 task.mLayerRank = -1;
1661 } else {
1662 task.mLayerRank = baseLayer + layer++;
1663 }
1664 }
1665 return layer;
1666 }
1667
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001668 /**
1669 * Make sure that all activities that need to be visible (that is, they
1670 * currently can be seen by the user) actually are.
1671 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001672 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1673 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02001674 mTopActivityOccludesKeyguard = false;
1675 mTopDismissingKeyguardActivity = null;
1676 mStackSupervisor.mKeyguardController.beginActivityVisibilityUpdate();
1677 try {
1678 ActivityRecord top = topRunningActivityLocked();
1679 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
1680 + " configChanges=0x" + Integer.toHexString(configChanges));
1681 if (top != null) {
1682 checkTranslucentActivityWaiting(top);
1683 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001684
Jorim Jaggife762342016-10-13 14:33:27 +02001685 // If the top activity is not fullscreen, then we need to
1686 // make sure any activities under it are now visible.
1687 boolean aboveTop = top != null;
1688 final int stackVisibility = getStackVisibilityLocked(starting);
1689 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1690 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
1691 boolean behindFullscreenActivity = stackInvisible;
1692 boolean resumeNextActivity = mStackSupervisor.isFocusedStack(this)
1693 && (isInStackLocked(starting) == null);
1694 boolean behindTranslucentActivity = false;
1695 final ActivityRecord visibleBehind = getVisibleBehindActivity();
1696 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1697 final TaskRecord task = mTaskHistory.get(taskNdx);
1698 final ArrayList<ActivityRecord> activities = task.mActivities;
1699 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1700 final ActivityRecord r = activities.get(activityNdx);
1701 if (r.finishing) {
1702 // Normally the screenshot will be taken in makeInvisible(). When an activity
1703 // is finishing, we no longer change its visibility, but we still need to take
1704 // the screenshots if startPausingLocked decided it should be taken.
1705 if (r.mUpdateTaskThumbnailWhenHidden) {
1706 r.updateThumbnailLocked(r.screenshotActivityLocked(),
1707 null /* description */);
1708 r.mUpdateTaskThumbnailWhenHidden = false;
1709 }
1710 continue;
Chong Zhang22bc8512016-04-07 13:47:18 -07001711 }
Jorim Jaggife762342016-10-13 14:33:27 +02001712 final boolean isTop = r == top;
1713 if (aboveTop && !isTop) {
1714 continue;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 }
Jorim Jaggife762342016-10-13 14:33:27 +02001716 aboveTop = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001717
Jorim Jaggife762342016-10-13 14:33:27 +02001718 // Check whether activity should be visible without Keyguard influence
Jorim Jaggi241ae102016-11-02 21:57:33 -07001719 final boolean visibleIgnoringKeyguard = r.shouldBeVisibleIgnoringKeyguard(
1720 behindTranslucentActivity, stackVisibleBehind, visibleBehind,
1721 behindFullscreenActivity);
1722 r.visibleIgnoringKeyguard = visibleIgnoringKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +02001723
1724 // Now check whether it's really visible depending on Keyguard state.
Jorim Jaggi241ae102016-11-02 21:57:33 -07001725 final boolean reallyVisible = checkKeyguardVisibility(r,
1726 visibleIgnoringKeyguard, isTop);
1727 if (visibleIgnoringKeyguard) {
Jorim Jaggife762342016-10-13 14:33:27 +02001728 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1729 behindFullscreenActivity, task, r);
1730 if (behindFullscreenActivity && !r.fullscreen) {
1731 behindTranslucentActivity = true;
1732 }
1733 }
1734 if (reallyVisible) {
1735 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1736 + " finishing=" + r.finishing + " state=" + r.state);
1737 // First: if this is not the current activity being started, make
1738 // sure it matches the current configuration.
1739 if (r != starting) {
1740 r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindows);
1741 }
1742
1743 if (r.app == null || r.app.thread == null) {
1744 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
1745 resumeNextActivity, r)) {
1746 if (activityNdx >= activities.size()) {
1747 // Record may be removed if its process needs to restart.
1748 activityNdx = activities.size() - 1;
1749 } else {
1750 resumeNextActivity = false;
1751 }
1752 }
1753 } else if (r.visible) {
1754 // If this activity is already visible, then there is nothing to do here.
1755 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1756 "Skipping: already visible at " + r);
1757
1758 if (r.handleAlreadyVisible()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001759 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001760 }
Jorim Jaggife762342016-10-13 14:33:27 +02001761 } else {
1762 r.makeVisibleIfNeeded(starting);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001763 }
Jorim Jaggife762342016-10-13 14:33:27 +02001764 // Aggregate current change flags.
1765 configChanges |= r.configChangeFlags;
Craig Mautnerbb742462014-07-07 15:28:55 -07001766 } else {
Jorim Jaggife762342016-10-13 14:33:27 +02001767 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
1768 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1769 + stackInvisible + " behindFullscreenActivity="
1770 + behindFullscreenActivity + " mLaunchTaskBehind="
1771 + r.mLaunchTaskBehind);
Winson Chungc2baac02017-01-11 13:34:47 -08001772 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001773 }
Jorim Jaggife762342016-10-13 14:33:27 +02001774 }
1775 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1776 // The visibility of tasks and the activities they contain in freeform stack are
1777 // determined individually unlike other stacks where the visibility or fullscreen
1778 // status of an activity in a previous task affects other.
1779 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
1780 } else if (mStackId == HOME_STACK_ID) {
1781 if (task.isOnTopLauncher()) {
1782 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "On-top launcher: at " + task
1783 + " stackInvisible=" + stackInvisible
1784 + " behindFullscreenActivity=" + behindFullscreenActivity);
1785 // When an on-top launcher is visible, (e.g. it's on the top of the home stack),
1786 // other tasks in the home stack could be visible if and only if:
1787 // - some app is running in the docked stack;
1788 // - no app is running in either the fullscreen stack or the freefrom stack.
1789 final ActivityStack dockedStack = mStackSupervisor.getStack(DOCKED_STACK_ID);
1790 final ActivityStack fullscreenStack = mStackSupervisor.getStack(
1791 FULLSCREEN_WORKSPACE_STACK_ID);
1792 final ActivityStack freeformStack = mStackSupervisor.getStack(
1793 FREEFORM_WORKSPACE_STACK_ID);
1794 final boolean dockedStackEmpty = dockedStack == null ||
1795 dockedStack.topRunningActivityLocked() == null;
1796 final boolean fullscreenStackEmpty = fullscreenStack == null ||
1797 fullscreenStack.topRunningActivityLocked() == null;
1798 final boolean freeformStackEmpty = freeformStack == null ||
1799 freeformStack.topRunningActivityLocked() == null;
1800 behindFullscreenActivity = dockedStackEmpty || !fullscreenStackEmpty ||
1801 !freeformStackEmpty;
1802 } else if (task.isHomeTask()) {
1803 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1804 + " stackInvisible=" + stackInvisible
1805 + " behindFullscreenActivity=" + behindFullscreenActivity);
1806 // No other task in the home stack should be visible behind the home activity.
1807 // Home activities is usually a translucent activity with the wallpaper behind
1808 // them. However, when they don't have the wallpaper behind them, we want to
1809 // show activities in the next application stack behind them vs. another
1810 // task in the home stack like recents.
1811 behindFullscreenActivity = true;
1812 } else if (task.isRecentsTask()
1813 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1814 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1815 "Recents task returning to app: at " + task
1816 + " stackInvisible=" + stackInvisible
1817 + " behindFullscreenActivity=" + behindFullscreenActivity);
1818 // We don't want any other tasks in the home stack visible if the recents
1819 // activity is going to be returning to an application activity type.
1820 // We do this to preserve the visible order the user used to get into the
1821 // recents activity. The recents activity is normally translucent and if it
1822 // doesn't have the wallpaper behind it the next activity in the home stack
1823 // shouldn't be visible when the home stack is brought to the front to display
1824 // the recents activity from an app.
1825 behindFullscreenActivity = true;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001826 }
Jorim Jaggife762342016-10-13 14:33:27 +02001827
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 }
1829 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001830
Jorim Jaggife762342016-10-13 14:33:27 +02001831 if (mTranslucentActivityWaiting != null &&
1832 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1833 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1834 notifyActivityDrawnLocked(null);
1835 }
1836 } finally {
1837 mStackSupervisor.mKeyguardController.endActivityVisibilityUpdate();
1838 }
1839 }
1840
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01001841 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
1842 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1843 mTaskHistory.get(taskNdx).addStartingWindowsForVisibleActivities(taskSwitch);
1844 }
1845 }
1846
Jorim Jaggife762342016-10-13 14:33:27 +02001847 /**
1848 * @return true if the top visible activity wants to occlude the Keyguard, false otherwise
1849 */
1850 boolean topActivityOccludesKeyguard() {
1851 return mTopActivityOccludesKeyguard;
1852 }
1853
1854 /**
1855 * @return the top most visible activity that wants to dismiss Keyguard
1856 */
1857 ActivityRecord getTopDismissingKeyguardActivity() {
1858 return mTopDismissingKeyguardActivity;
1859 }
1860
1861 /**
1862 * Checks whether {@param r} should be visible depending on Keyguard state and updates
1863 * {@link #mTopActivityOccludesKeyguard} and {@link #mTopDismissingKeyguardActivity} if
1864 * necessary.
1865 *
1866 * @return true if {@param r} is visible taken Keyguard state into account, false otherwise
1867 */
1868 private boolean checkKeyguardVisibility(ActivityRecord r, boolean shouldBeVisible,
1869 boolean isTop) {
Winson Chung0655da02016-12-16 17:05:01 -08001870 final boolean isInPinnedStack = r.getStack().getStackId() == PINNED_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +02001871 final boolean keyguardShowing = mStackSupervisor.mKeyguardController.isKeyguardShowing();
1872 final boolean keyguardLocked = mStackSupervisor.mKeyguardController.isKeyguardLocked();
Winson Chung0655da02016-12-16 17:05:01 -08001873 final boolean showWhenLocked = r.hasShowWhenLockedWindows() && !isInPinnedStack;
Jorim Jaggie69c9312016-10-31 18:24:38 -07001874 final boolean dismissKeyguard = r.hasDismissKeyguardWindows();
Jorim Jaggife762342016-10-13 14:33:27 +02001875 if (shouldBeVisible) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07001876 if (dismissKeyguard && mTopDismissingKeyguardActivity == null) {
Jorim Jaggife762342016-10-13 14:33:27 +02001877 mTopDismissingKeyguardActivity = r;
1878 }
1879
1880 // Only the top activity may control occluded, as we can't occlude the Keyguard if the
1881 // top app doesn't want to occlude it.
1882 if (isTop) {
1883 mTopActivityOccludesKeyguard |= showWhenLocked;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001884 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 }
Jorim Jaggife762342016-10-13 14:33:27 +02001886 if (keyguardShowing) {
Craig Mautnereb8abf72014-07-02 15:04:09 -07001887
Jorim Jaggie69c9312016-10-31 18:24:38 -07001888 // If keyguard is showing, nothing is visible, except if we are able to dismiss Keyguard
1889 // right away.
1890 return shouldBeVisible && mStackSupervisor.mKeyguardController
Jorim Jaggi07961872016-11-23 11:28:57 +01001891 .canShowActivityWhileKeyguardShowing(r, dismissKeyguard);
Jorim Jaggife762342016-10-13 14:33:27 +02001892 } else if (keyguardLocked) {
Jorim Jaggi07961872016-11-23 11:28:57 +01001893 return shouldBeVisible && mStackSupervisor.mKeyguardController.canShowWhileOccluded(
1894 dismissKeyguard, showWhenLocked);
Jorim Jaggife762342016-10-13 14:33:27 +02001895 } else {
1896 return shouldBeVisible;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001897 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 }
Craig Mautner58547802013-03-05 08:23:53 -08001899
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001900 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1901 if (mTranslucentActivityWaiting != top) {
1902 mUndrawnActivitiesBelowTopTranslucent.clear();
1903 if (mTranslucentActivityWaiting != null) {
1904 // Call the callback with a timeout indication.
1905 notifyActivityDrawnLocked(null);
1906 mTranslucentActivityWaiting = null;
1907 }
1908 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1909 }
1910 }
1911
1912 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001913 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001914 // We need to make sure the app is running if it's the top, or it is just made visible from
1915 // invisible. If the app is already visible, it must have died while it was visible. In this
1916 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1917 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001918 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001919 // This activity needs to be visible, but isn't even running...
1920 // get it started and resume if no other stack in this stack is resumed.
1921 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1922 if (r != starting) {
1923 r.startFreezingScreenLocked(r.app, configChanges);
1924 }
1925 if (!r.visible || r.mLaunchTaskBehind) {
1926 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001927 r.setVisible(true);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001928 }
1929 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001930 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001931 return true;
1932 }
1933 }
1934 return false;
1935 }
1936
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001937 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001938 if (!r.visible) {
1939 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1940 return;
1941 }
1942 // Now for any activities that aren't visible to the user, make sure they no longer are
1943 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001944 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001945 try {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001946 r.setVisible(false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001947 switch (r.state) {
1948 case STOPPING:
1949 case STOPPED:
1950 if (r.app != null && r.app.thread != null) {
1951 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1952 "Scheduling invisibility: " + r);
1953 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1954 }
Winson Chungc2baac02017-01-11 13:34:47 -08001955
1956 // Reset the flag indicating that an app can enter picture-in-picture once the
1957 // activity is hidden
1958 r.supportsPictureInPictureWhilePausing = false;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001959 break;
1960
1961 case INITIALIZING:
1962 case RESUMED:
1963 case PAUSING:
1964 case PAUSED:
1965 // This case created for transitioning activities from
1966 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001967 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001968 releaseBackgroundResources(r);
1969 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001970 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001971 }
1972 break;
1973
1974 default:
1975 break;
1976 }
1977 } catch (Exception e) {
1978 // Just skip on any failure; we'll make it visible when it next restarts.
1979 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1980 }
1981 }
1982
1983 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1984 TaskRecord task, ActivityRecord r) {
1985 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001986 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001987 + " stackInvisible=" + stackInvisible
1988 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001989 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001990 behindFullscreenActivity = true;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001991 } else if (!isHomeOrRecentsStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001992 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1993 + " stackInvisible=" + stackInvisible
1994 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001995 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001996 }
1997 return behindFullscreenActivity;
1998 }
1999
Todd Kennedyaab56db2015-01-30 09:39:53 -08002000 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002001 mTranslucentActivityWaiting = r;
2002 mUndrawnActivitiesBelowTopTranslucent.clear();
2003 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
2004 }
2005
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002006 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2007 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2008 final TaskRecord task = mTaskHistory.get(taskNdx);
2009 final ArrayList<ActivityRecord> activities = task.mActivities;
2010 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2011 final ActivityRecord r = activities.get(activityNdx);
2012 if ( r.appTimeTracker != except) {
2013 r.appTimeTracker = null;
2014 }
2015 }
2016 }
2017 }
2018
Craig Mautner5eda9b32013-07-02 11:58:16 -07002019 /**
2020 * Called as activities below the top translucent activity are redrawn. When the last one is
2021 * redrawn notify the top activity by calling
2022 * {@link Activity#onTranslucentConversionComplete}.
2023 *
2024 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2025 * occurred and the activity will be notified immediately.
2026 */
2027 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07002028 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002029 if ((r == null)
2030 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2031 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2032 // The last undrawn activity below the top has just been drawn. If there is an
2033 // opaque activity at the top, notify it that it can become translucent safely now.
2034 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2035 mTranslucentActivityWaiting = null;
2036 mUndrawnActivitiesBelowTopTranslucent.clear();
2037 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2038
Craig Mautner71dd1b62014-02-18 15:48:52 -08002039 if (waitingActivity != null) {
2040 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
2041 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
2042 try {
2043 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
2044 waitingActivity.appToken, r != null);
2045 } catch (RemoteException e) {
2046 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002047 }
2048 }
2049 }
2050 }
2051
Craig Mautnera61bc652013-10-28 15:43:18 -07002052 /** If any activities below the top running one are in the INITIALIZING state and they have a
2053 * starting window displayed then remove that starting window. It is possible that the activity
2054 * in this state will never resumed in which case that starting window will be orphaned. */
2055 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002056 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002057 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002058 // We don't want to clear starting window for activities that aren't behind fullscreen
2059 // activities as we need to display their starting window until they are done initializing.
2060 boolean behindFullscreenActivity = false;
Wale Ogunwale98742a52016-07-12 10:29:12 -07002061
2062 if (getStackVisibilityLocked(null) == STACK_INVISIBLE) {
2063 // The stack is not visible, so no activity in it should be displaying a starting
2064 // window. Mark all activities below top and behind fullscreen.
2065 aboveTop = false;
2066 behindFullscreenActivity = true;
2067 }
2068
Craig Mautnera61bc652013-10-28 15:43:18 -07002069 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2070 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2071 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2072 final ActivityRecord r = activities.get(activityNdx);
2073 if (aboveTop) {
2074 if (r == topActivity) {
2075 aboveTop = false;
2076 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002077 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002078 continue;
2079 }
2080
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002081 r.removeOrphanedStartingWindow(behindFullscreenActivity);
Wale Ogunwale68741142016-05-17 09:40:02 -07002082 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002083 }
2084 }
2085 }
2086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 /**
2088 * Ensure that the top activity in the stack is resumed.
2089 *
2090 * @param prev The previously resumed activity, for when in the process
2091 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002092 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 *
2094 * @return Returns true if something is being resumed, or false if
2095 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002096 *
2097 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2098 * non-focused stack to be resumed.
2099 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2100 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002101 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08002102 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002103 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002104 // Don't even start recursing.
2105 return false;
2106 }
2107
2108 boolean result = false;
2109 try {
2110 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002111 mStackSupervisor.inResumeTopActivity = true;
Craig Mautner544efa72014-09-04 16:41:20 -07002112 result = resumeTopActivityInnerLocked(prev, options);
2113 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002114 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002115 }
2116 return result;
2117 }
2118
Chong Zhang6cda19c2016-06-14 19:07:56 -07002119 void setResumedActivityLocked(ActivityRecord r, String reason) {
2120 // TODO: move mResumedActivity to stack supervisor,
2121 // there should only be 1 global copy of resumed activity.
2122 mResumedActivity = r;
Chong Zhangd2cb3112016-07-20 11:13:22 -07002123 r.state = ActivityState.RESUMED;
Chong Zhang6cda19c2016-06-14 19:07:56 -07002124 mService.setResumedActivityUncheckLocked(r, reason);
2125 r.task.touchActiveTime();
2126 mRecentTasks.addLocked(r.task);
2127 }
2128
Chong Zhang280d3322015-11-03 17:27:26 -08002129 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002130 if (!mService.mBooting && !mService.mBooted) {
2131 // Not ready yet!
2132 return false;
2133 }
2134
Andrii Kulian7fc22812016-12-28 13:04:11 -08002135 // Find the topmost activity in this stack that is not finishing.
2136 final ActivityRecord next = topRunningActivityLocked();
2137
2138 final boolean hasRunningActivity = next != null;
2139
2140 final ActivityRecord parent = mActivityContainer.mParentActivity;
2141 final boolean isParentNotResumed = parent != null && parent.state != ActivityState.RESUMED;
2142 if (hasRunningActivity
2143 && (isParentNotResumed || !mActivityContainer.isAttachedLocked())) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002144 // Do not resume this stack if its parent is not resumed.
2145 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
2146 return false;
2147 }
2148
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002149 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 // Remember how we'll process this pause/resume situation, and ensure
2152 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002153 final boolean userLeaving = mStackSupervisor.mUserLeaving;
2154 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155
Andrii Kulian7fc22812016-12-28 13:04:11 -08002156 if (!hasRunningActivity) {
2157 // There are no activities left in the stack, let's look somewhere else.
2158 return resumeTopActivityInNextFocusableStack(prev, options, "noMoreActivities");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 }
2160
2161 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002162
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002164 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2165 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002166 // Make sure we have executed any pending transitions, since there
2167 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002168 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002169 if (DEBUG_STATES) Slog.d(TAG_STATES,
2170 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002171 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 return false;
2173 }
2174
Craig Mautner525f3d92013-05-07 14:01:50 -07002175 final TaskRecord nextTask = next.task;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002176 final TaskRecord prevTask = prev != null ? prev.task : null;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002177 if (prevTask != null && prevTask.getStack() == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002178 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002179 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002180 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002181 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002182 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002183 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002184 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002185 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002186 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002187 } else if (!isOnHomeDisplay()) {
2188 return false;
2189 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002190 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002191 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002192 return isOnHomeDisplay() &&
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002193 mStackSupervisor.resumeHomeStackTask(prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002194 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002195 }
2196
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 // If we are sleeping, and there is no resumed activity, and the top
2198 // activity is paused, well that is the state we want.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002199 if (mService.isSleepingOrShuttingDownLocked()
p13451dbad2872012-04-18 11:39:23 +09002200 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002201 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002202 // Make sure we have executed any pending transitions, since there
2203 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002204 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002205 if (DEBUG_STATES) Slog.d(TAG_STATES,
2206 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002207 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 return false;
2209 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002210
2211 // Make sure that the user who owns this activity is started. If not,
2212 // we will just leave it as is because someone should be bringing
2213 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002214 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002215 Slog.w(TAG, "Skipping resume of top activity " + next
2216 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002217 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002218 return false;
2219 }
2220
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221 // The activity may be waiting for stop, but that is no longer
2222 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002223 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002224 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002225 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002226 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002227
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002228 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002230 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002231 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002232 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002233 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002234 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002235 return false;
2236 }
2237
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002238 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2239
Winson Chungc2baac02017-01-11 13:34:47 -08002240 // If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity
2241 // to be paused, while at the same time resuming the new resume activity
2242 final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
2243 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002244 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002245 if (DEBUG_STATES) Slog.d(TAG_STATES,
2246 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Winson Chungc2baac02017-01-11 13:34:47 -08002247 pausing |= startPausingLocked(userLeaving, false, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002248 }
Winson Chungc2baac02017-01-11 13:34:47 -08002249 if (pausing && !resumeWhilePausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002250 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002251 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002252 // At this point we want to put the upcoming activity's process
2253 // at the top of the LRU list, since we know we will be needing it
2254 // very soon and it would be a waste to let it get killed if it
2255 // happens to be sitting towards the end.
2256 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002257 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002258 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002259 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 return true;
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002261 } else if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2262 mStackSupervisor.allResumedActivitiesComplete()) {
2263 // It is possible for the activity to be resumed when we paused back stacks above if the
2264 // next activity doesn't have to wait for pause to complete.
2265 // So, nothing else to-do except:
2266 // Make sure we have executed any pending transitions, since there
2267 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002268 executeAppTransition(options);
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002269 if (DEBUG_STATES) Slog.d(TAG_STATES,
2270 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
2271 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2272 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002273 }
2274
Christopher Tated3f175c2012-06-14 14:16:54 -07002275 // If the most recent activity was noHistory but was only stopped rather
2276 // than stopped+finished because the device went to sleep, we need to make
2277 // sure to finish it as we're making a new activity topmost.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002278 if (mService.isSleepingLocked() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002279 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002280 if (DEBUG_STATES) Slog.d(TAG_STATES,
2281 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002282 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002283 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002284 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002285 }
2286
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002287 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002288 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2289 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002290 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002291 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2292 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 } else {
2294 // The next activity is already visible, so hide the previous
2295 // activity's windows right now so we can show the new one ASAP.
2296 // We only do this if the previous is finishing, which should mean
2297 // it is on top of the one being resumed so hiding it quickly
2298 // is good. Otherwise, we want to do the normal route of allowing
2299 // the resumed activity to be shown so we can decide if the
2300 // previous should actually be hidden depending on whether the
2301 // new one is found to be full-screen or not.
2302 if (prev.finishing) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002303 prev.setVisibility(false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002304 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2305 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002306 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002307 + ", nowVisible=" + next.nowVisible);
2308 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002309 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002310 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002311 + ", waitingVisible="
2312 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2313 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314 }
2315 }
2316 }
2317
Dianne Hackborne7f97212011-02-24 14:40:20 -08002318 // Launching this app's activity, make sure the app is no longer
2319 // considered stopped.
2320 try {
2321 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002322 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002323 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002324 } catch (IllegalArgumentException e) {
2325 Slog.w(TAG, "Failed trying to unstop package "
2326 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002327 }
2328
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 // We are starting up the next activity, so tell the window manager
2330 // that the previous one will be hidden soon. This way it can know
2331 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002332 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 if (prev != null) {
2334 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002335 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 "Prepare close transition: prev=" + prev);
2337 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002338 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002339 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002341 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002342 ? TRANSIT_ACTIVITY_CLOSE
2343 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002344 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002345 prev.setVisibility(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002346 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002347 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2348 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002349 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002350 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002351 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002353 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002354 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002355 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002356 ? TRANSIT_TASK_OPEN_BEHIND
2357 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 }
2359 }
Craig Mautner967212c2013-04-13 21:10:58 -07002360 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002361 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002362 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002363 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002364 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002366 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002367 }
2368 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002369
2370 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002371 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002372 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2373 if (opts != null) {
2374 resumeAnimOptions = opts.toBundle();
2375 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002376 next.applyOptionsLocked();
2377 } else {
2378 next.clearOptionsLocked();
2379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002380
Craig Mautnercf910b02013-04-23 11:23:27 -07002381 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002383 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2384 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385
Chong Zhangd127c6d2016-05-02 16:36:41 -07002386 // If the previous activity is translucent, force a visibility update of
2387 // the next activity, so that it's added to WM's opening app list, and
2388 // transition animation can be set up properly.
2389 // For example, pressing Home button with a translucent activity in focus.
2390 // Launcher is already visible in this case. If we don't add it to opening
2391 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2392 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2393 final boolean lastActivityTranslucent = lastStack != null
2394 && (!lastStack.mFullscreen
2395 || (lastStack.mLastPausedActivity != null
2396 && !lastStack.mLastPausedActivity.fullscreen));
2397
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 // This activity is now becoming visible.
Chong Zhangd127c6d2016-05-02 16:36:41 -07002399 if (!next.visible || next.stopped || lastActivityTranslucent) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002400 next.setVisibility(true);
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002401 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002402
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002403 // schedule launch ticks to collect information about slow apps.
2404 next.startLaunchTickingLocked();
2405
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002406 ActivityRecord lastResumedActivity =
2407 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002408 ActivityState lastState = next.state;
2409
2410 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002411
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002412 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Chong Zhang6cda19c2016-06-14 19:07:56 -07002413
2414 setResumedActivityLocked(next, "resumeTopActivityInnerLocked");
2415
Dianne Hackborndb926082013-10-31 16:32:44 -07002416 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002418 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002419
2420 // Have the window manager re-evaluate the orientation of
2421 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002422 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002423 if (mStackSupervisor.isFocusedStack(this)) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002424 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
2425 mStackSupervisor.getDisplayOverrideConfiguration(mDisplayId),
2426 next.mayFreezeScreenLocked(next.app) ? next.appToken : null, mDisplayId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002427 if (config != null) {
2428 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 }
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002430 notUpdated = !mService.updateDisplayOverrideConfigurationLocked(config, next,
2431 false /* deferResume */, mDisplayId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002433
Craig Mautner525f3d92013-05-07 14:01:50 -07002434 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 // The configuration update wasn't able to keep the existing
2436 // instance of the activity, and instead started a new one.
2437 // We should be all done, but let's just make sure our activity
2438 // is still at the top and schedule another run if something
2439 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002440 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002441 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002442 "Activity config changed during resume: " + next
2443 + ", new next: " + nextNext);
2444 if (nextNext != next) {
2445 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002446 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002447 }
Chong Zhangd2cb3112016-07-20 11:13:22 -07002448 if (!next.visible || next.stopped) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002449 next.setVisibility(true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002450 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002451 next.completeResumeLocked();
Craig Mautnercf910b02013-04-23 11:23:27 -07002452 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Chong Zhangd2cb3112016-07-20 11:13:22 -07002453 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002454 }
Craig Mautner58547802013-03-05 08:23:53 -08002455
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002456 try {
2457 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002458 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 if (a != null) {
2460 final int N = a.size();
2461 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002462 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2463 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002464 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002465 }
2466 }
2467
Chong Zhangad24f962016-08-25 12:12:33 -07002468 boolean allowSavedSurface = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 if (next.newIntents != null) {
Chong Zhangad24f962016-08-25 12:12:33 -07002470 // Restrict saved surface to launcher start, or there is no intent at all
2471 // (eg. task being brought to front). If the intent is something else,
2472 // likely the app is going to show some specific page or view, instead of
2473 // what's left last time.
2474 for (int i = next.newIntents.size() - 1; i >= 0; i--) {
2475 final Intent intent = next.newIntents.get(i);
Chong Zhangb0a87432016-08-25 12:12:33 -07002476 if (intent != null && !ActivityRecord.isMainIntent(intent)) {
Chong Zhangad24f962016-08-25 12:12:33 -07002477 allowSavedSurface = false;
2478 break;
2479 }
2480 }
Wale Ogunwale826c7062016-09-13 08:25:54 -07002481 next.app.thread.scheduleNewIntent(
2482 next.newIntents, next.appToken, false /* andPause */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 }
2484
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002485 // Well the app will no longer be stopped.
2486 // Clear app token stopped state in window manager if needed.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002487 next.notifyAppResumed(next.stopped, allowSavedSurface);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002488
Craig Mautner299f9602015-01-26 09:47:33 -08002489 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2490 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002491
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002492 next.sleeping = false;
Alan Viverette5f31b982016-06-21 14:46:14 -04002493 mService.showUnsupportedZoomDialogIfNeededLocked(next);
Craig Mautner2420ead2013-04-01 17:13:20 -07002494 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002495 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002496 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002497 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002498 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002499 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002500
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002501 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002502 } catch (Exception e) {
2503 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002504 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002505 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002507 if (lastStack != null) {
2508 lastStack.mResumedActivity = lastResumedActivity;
2509 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002510 Slog.i(TAG, "Restarting because process died: " + next);
2511 if (!next.hasBeenLaunched) {
2512 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002513 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2514 mStackSupervisor.isFrontStack(lastStack)) {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002515 next.showStartingWindow(null /* prev */, false /* newTask */,
2516 false /* taskSwitch */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002517 }
George Mount2c92c972014-03-20 09:38:23 -07002518 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002519 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002520 return true;
2521 }
2522
2523 // From this point on, if something goes wrong there is no way
2524 // to recover the activity.
2525 try {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002526 next.completeResumeLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002527 } catch (Exception e) {
2528 // If any exception gets thrown, toss away this
2529 // activity and try the next one.
2530 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002531 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002532 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002533 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 return true;
2535 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002536 } else {
2537 // Whoops, need to restart this activity!
2538 if (!next.hasBeenLaunched) {
2539 next.hasBeenLaunched = true;
2540 } else {
2541 if (SHOW_APP_STARTING_PREVIEW) {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002542 next.showStartingWindow(null /* prev */, false /* newTask */,
2543 false /* taskSwich */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002545 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002546 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002547 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002548 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002549 }
2550
Craig Mautnercf910b02013-04-23 11:23:27 -07002551 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002552 return true;
2553 }
2554
Andrii Kulian7fc22812016-12-28 13:04:11 -08002555 private boolean resumeTopActivityInNextFocusableStack(ActivityRecord prev,
2556 ActivityOptions options, String reason) {
2557 if ((!mFullscreen || !isOnHomeDisplay())
2558 && adjustFocusToNextFocusableStackLocked(reason)) {
2559 // Try to move focus to the next visible stack with a running activity if this
2560 // stack is not covering the entire screen or is on a secondary display (with no home
2561 // stack).
2562 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2563 mStackSupervisor.getFocusedStack(), prev, null);
2564 }
2565
2566 // Let's just start up the Launcher...
2567 ActivityOptions.abort(options);
2568 if (DEBUG_STATES) Slog.d(TAG_STATES,
2569 "resumeTopActivityInNextFocusableStack: " + reason + ", go home");
2570 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2571 // Only resume home if on home display
2572 return isOnHomeDisplay() &&
2573 mStackSupervisor.resumeHomeStackTask(prev, reason);
2574 }
2575
riddle_hsuc215a4f2014-12-27 12:10:45 +08002576 private TaskRecord getNextTask(TaskRecord targetTask) {
2577 final int index = mTaskHistory.indexOf(targetTask);
2578 if (index >= 0) {
2579 final int numTasks = mTaskHistory.size();
2580 for (int i = index + 1; i < numTasks; ++i) {
2581 TaskRecord task = mTaskHistory.get(i);
2582 if (task.userId == targetTask.userId) {
2583 return task;
2584 }
2585 }
2586 }
2587 return null;
2588 }
2589
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002590 /** Returns the position the input task should be placed in this stack. */
2591 int getAdjustedPositionForTask(TaskRecord task, int suggestedPosition,
2592 ActivityRecord starting) {
2593
2594 int maxPosition = mTaskHistory.size();
2595 if ((starting != null && starting.okToShowLocked())
2596 || (starting == null && task.okToShowLocked())) {
2597 // If the task or starting activity can be shown, then whatever position is okay.
2598 return Math.min(suggestedPosition, maxPosition);
2599 }
2600
2601 // The task can't be shown, put non-current user tasks below current user tasks.
2602 while (maxPosition > 0) {
2603 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2604 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2605 || tmpTask.topRunningActivityLocked() == null) {
2606 break;
2607 }
2608 maxPosition--;
2609 }
2610
2611 return Math.min(suggestedPosition, maxPosition);
2612 }
2613
Andrii Kuliand2765632016-12-12 22:26:34 -08002614 /**
2615 * Used from {@link ActivityStack#positionTask(TaskRecord, int)}.
2616 * @see ActivityManagerService#positionTaskInStack(int, int, int).
2617 */
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002618 private void insertTaskAtPosition(TaskRecord task, int position) {
2619 if (position >= mTaskHistory.size()) {
2620 insertTaskAtTop(task, null);
2621 return;
2622 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002623 position = getAdjustedPositionForTask(task, position, null /* starting */);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002624 mTaskHistory.remove(task);
2625 mTaskHistory.add(position, task);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002626 mWindowContainerController.positionChildAt(task.getWindowContainerController(), position,
2627 task.mBounds, task.getOverrideConfiguration());
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002628 updateTaskMovement(task, true);
2629 }
2630
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002631 private void insertTaskAtTop(TaskRecord task, ActivityRecord starting) {
2632 updateTaskReturnToForTopInsertion(task);
2633 // TODO: Better place to put all the code below...may be addTask...
2634 mTaskHistory.remove(task);
2635 // Now put task at top.
2636 final int position = getAdjustedPositionForTask(task, mTaskHistory.size(), starting);
2637 mTaskHistory.add(position, task);
2638 updateTaskMovement(task, true);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002639 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
2640 true /* includingParents */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002641 }
2642
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002643 private void updateTaskReturnToForTopInsertion(TaskRecord task) {
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002644 boolean isLastTaskOverHome = false;
riddle_hsuc215a4f2014-12-27 12:10:45 +08002645 // If the moving task is over home stack, transfer its return type to next task
2646 if (task.isOverHomeStack()) {
2647 final TaskRecord nextTask = getNextTask(task);
2648 if (nextTask != null) {
2649 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002650 } else {
2651 isLastTaskOverHome = true;
riddle_hsuc215a4f2014-12-27 12:10:45 +08002652 }
2653 }
2654
Craig Mautner9c85c202013-10-04 20:11:26 -07002655 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002656 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002657 if (isOnHomeDisplay()) {
2658 ActivityStack lastStack = mStackSupervisor.getLastStack();
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002659 final boolean fromHomeOrRecents = lastStack.isHomeOrRecentsStack();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002660 final TaskRecord topTask = lastStack.topTask();
2661 final boolean fromOnTopLauncher = topTask != null && topTask.isOnTopLauncher();
Jiaquan Hee13e9642016-06-15 15:16:13 -07002662 if (fromOnTopLauncher) {
2663 // Since an on-top launcher will is moved to back when tasks are launched from it,
2664 // those tasks should first try to return to a non-home activity.
2665 // This also makes sure that non-home activities are visible under a transparent
2666 // non-home activity.
2667 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002668 } else if (!isHomeOrRecentsStack() && (fromHomeOrRecents || topTask() != task)) {
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002669 // If it's a last task over home - we default to keep its return to type not to
2670 // make underlying task focused when this one will be finished.
2671 int returnToType = isLastTaskOverHome
2672 ? task.getTaskToReturnTo() : APPLICATION_ACTIVITY_TYPE;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002673 if (fromHomeOrRecents && StackId.allowTopTaskToReturnHome(mStackId)) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002674 returnToType = topTask == null ? HOME_ACTIVITY_TYPE : topTask.taskType;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002675 }
2676 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002677 }
2678 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002679 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002680 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002681 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002682
Winson Chungb5c41b72016-12-07 15:00:47 -08002683 final void startActivityLocked(ActivityRecord r, ActivityRecord focusedTopActivity,
2684 boolean newTask, boolean keepCurTransition, ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002685 TaskRecord rTask = r.task;
2686 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002687 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2688 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002689 // Last activity in task had been removed or ActivityManagerService is reusing task.
2690 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002691 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002692 insertTaskAtTop(rTask, r);
Craig Mautner77878772013-03-04 19:46:24 -08002693 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002694 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 if (!newTask) {
2696 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002697 boolean startIt = true;
2698 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2699 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002700 if (task.getTopActivity() == null) {
2701 // All activities in task are finishing.
2702 continue;
2703 }
Craig Mautner70a86932013-02-28 22:37:44 -08002704 if (task == r.task) {
2705 // Here it is! Now, if this is not yet visible to the
2706 // user, then just add it without starting; it will
2707 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002708 if (!startIt) {
2709 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2710 + task, new RuntimeException("here").fillInStackTrace());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002711 r.createWindowContainer();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002712 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713 return;
2714 }
2715 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002716 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002717 startIt = false;
2718 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 }
2720 }
2721
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002722 // Place a new activity at top of stack, so it is next to interact with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002723
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002724 // If we are not placing the new activity frontmost, we do not want to deliver the
2725 // onUserLeaving callback to the actual frontmost activity
Craig Mautner70a86932013-02-28 22:37:44 -08002726 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002727 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002728 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002729 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 }
Craig Mautner70a86932013-02-28 22:37:44 -08002731
2732 task = r.task;
2733
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002734 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002735 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002736 new RuntimeException("here").fillInStackTrace());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002737 r.createWindowContainer();
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002738 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002739
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002740 if (!isHomeOrRecentsStack() || numActivities() > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002741 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002743 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002744 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002746 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002747 int transit = TRANSIT_ACTIVITY_OPEN;
2748 if (newTask) {
2749 if (r.mLaunchTaskBehind) {
2750 transit = TRANSIT_TASK_OPEN_BEHIND;
2751 } else {
Winson Chungc2baac02017-01-11 13:34:47 -08002752 // If a new task is being launched, then mark the existing top activity as
2753 // supporting picture-in-picture while pausing
Winson Chungb5c41b72016-12-07 15:00:47 -08002754 if (focusedTopActivity != null) {
Winson Chungc2baac02017-01-11 13:34:47 -08002755 focusedTopActivity.supportsPictureInPictureWhilePausing = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08002756 }
2757 transit = TRANSIT_TASK_OPEN;
2758 }
2759 }
2760 mWindowManager.prepareAppTransition(transit, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 mNoAnimActivities.remove(r);
2762 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 boolean doShow = true;
2764 if (newTask) {
2765 // Even though this activity is starting fresh, we still need
2766 // to reset it to make sure we apply affinities to move any
2767 // existing activities from other tasks in to it.
2768 // If the caller has requested that the target task be
2769 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002770 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002771 resetTaskIfNeededLocked(r, r);
2772 doShow = topRunningNonDelayedActivityLocked(null) == r;
2773 }
Chong Zhang280d3322015-11-03 17:27:26 -08002774 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002775 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2776 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002777 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002778 if (r.mLaunchTaskBehind) {
2779 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2780 // tell WindowManager that r is visible even though it is at the back of the stack.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002781 r.setVisibility(true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002782 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002783 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 // Figure out if we are transitioning from another activity that is
2785 // "has the same starting icon" as the next one. This allows the
2786 // window manager to keep the previous window it had previously
2787 // created, if it still had one.
Wale Ogunwale3b232392016-05-13 15:37:13 -07002788 ActivityRecord prev = r.task.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 if (prev != null) {
2790 // We don't want to reuse the previous starting preview if:
2791 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002792 if (prev.task != r.task) {
2793 prev = null;
2794 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002796 else if (prev.nowVisible) {
2797 prev = null;
2798 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002799 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002800 r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 }
2802 } else {
2803 // If this is the first activity, don't do any fancy animations,
2804 // because there is nothing for it to animate on top of.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002805 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002807 }
2808
Jorim Jaggi02886a82016-12-06 09:10:06 -08002809 private boolean isTaskSwitch(ActivityRecord r,
2810 ActivityRecord topFocusedActivity) {
2811 return topFocusedActivity != null && r.task != topFocusedActivity.task;
2812 }
2813
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 /**
2815 * Perform a reset of the given task, if needed as part of launching it.
2816 * Returns the new HistoryRecord at the top of the task.
2817 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002818 /**
2819 * Helper method for #resetTaskIfNeededLocked.
2820 * We are inside of the task being reset... we'll either finish this activity, push it out
2821 * for another task, or leave it as-is.
2822 * @param task The task containing the Activity (taskTop) that might be reset.
2823 * @param forceReset
2824 * @return An ActivityOptions that needs to be processed.
2825 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07002826 private ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002827 ActivityOptions topOptions = null;
2828
2829 int replyChainEnd = -1;
2830 boolean canMoveOptions = true;
2831
2832 // We only do this for activities that are not the root of the task (since if we finish
2833 // the root, we may no longer have the task!).
2834 final ArrayList<ActivityRecord> activities = task.mActivities;
2835 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002836 final int rootActivityNdx = task.findEffectiveRootIndex();
2837 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002838 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002839 if (target.frontOfTask)
2840 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002841
2842 final int flags = target.info.flags;
2843 final boolean finishOnTaskLaunch =
2844 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2845 final boolean allowTaskReparenting =
2846 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2847 final boolean clearWhenTaskReset =
2848 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2849
2850 if (!finishOnTaskLaunch
2851 && !clearWhenTaskReset
2852 && target.resultTo != null) {
2853 // If this activity is sending a reply to a previous
2854 // activity, we can't do anything with it now until
2855 // we reach the start of the reply chain.
2856 // XXX note that we are assuming the result is always
2857 // to the previous activity, which is almost always
2858 // the case but we really shouldn't count on.
2859 if (replyChainEnd < 0) {
2860 replyChainEnd = i;
2861 }
2862 } else if (!finishOnTaskLaunch
2863 && !clearWhenTaskReset
2864 && allowTaskReparenting
2865 && target.taskAffinity != null
2866 && !target.taskAffinity.equals(task.affinity)) {
2867 // If this activity has an affinity for another
2868 // task, then we need to move it out of here. We will
2869 // move it as far out of the way as possible, to the
2870 // bottom of the activity stack. This also keeps it
2871 // correctly ordered with any activities we previously
2872 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002873 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002874 final ActivityRecord bottom =
2875 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002876 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002877 if (bottom != null && target.taskAffinity != null
2878 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002879 // If the activity currently at the bottom has the
2880 // same task affinity as the one we are moving,
2881 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002882 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002883 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002884 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002885 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002886 targetTask = createTaskRecord(
2887 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
Wale Ogunwale72919d22016-12-08 18:58:50 -08002888 target.info, null, null, null, false, target.mActivityType);
Craig Mautner329f4122013-11-07 09:10:42 -08002889 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002890 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002891 + " out to new task " + target.task);
2892 }
2893
Craig Mautner525f3d92013-05-07 14:01:50 -07002894 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002895 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2896 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002897 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002898 if (p.finishing) {
2899 continue;
2900 }
2901
Craig Mautnere3a74d52013-02-22 14:14:58 -08002902 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002903 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002904 topOptions = p.takeOptionsLocked();
2905 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002906 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002907 }
2908 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002909 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2910 "Removing activity " + p + " from task=" + task + " adding to task="
2911 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002912 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2913 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002914 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002915 targetTask.addActivityAtBottom(p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002916 }
2917
Wale Ogunwale1666e312016-12-16 11:27:18 -08002918 mWindowContainerController.positionChildAtBottom(
2919 targetTask.getWindowContainerController());
Craig Mautnere3a74d52013-02-22 14:14:58 -08002920 replyChainEnd = -1;
2921 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2922 // If the activity should just be removed -- either
2923 // because it asks for it, or the task should be
2924 // cleared -- then finish it and anything that is
2925 // part of its reply chain.
2926 int end;
2927 if (clearWhenTaskReset) {
2928 // In this case, we want to finish this activity
2929 // and everything above it, so be sneaky and pretend
2930 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002931 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002932 } else if (replyChainEnd < 0) {
2933 end = i;
2934 } else {
2935 end = replyChainEnd;
2936 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002937 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002938 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002939 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002940 if (p.finishing) {
2941 continue;
2942 }
2943 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002944 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002945 topOptions = p.takeOptionsLocked();
2946 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002947 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002948 }
2949 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002950 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002951 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002952 if (finishActivityLocked(
2953 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002954 end--;
2955 srcPos--;
2956 }
2957 }
2958 replyChainEnd = -1;
2959 } else {
2960 // If we were in the middle of a chain, well the
2961 // activity that started it all doesn't want anything
2962 // special, so leave it all as-is.
2963 replyChainEnd = -1;
2964 }
2965 }
2966
2967 return topOptions;
2968 }
2969
2970 /**
2971 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2972 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2973 * @param affinityTask The task we are looking for an affinity to.
2974 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2975 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2976 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2977 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002978 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002979 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002980 int replyChainEnd = -1;
2981 final int taskId = task.taskId;
2982 final String taskAffinity = task.affinity;
2983
2984 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2985 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002986 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2987
2988 // Do not operate on or below the effective root Activity.
2989 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002990 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002991 if (target.frontOfTask)
2992 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002993
2994 final int flags = target.info.flags;
2995 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2996 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2997
2998 if (target.resultTo != null) {
2999 // If this activity is sending a reply to a previous
3000 // activity, we can't do anything with it now until
3001 // we reach the start of the reply chain.
3002 // XXX note that we are assuming the result is always
3003 // to the previous activity, which is almost always
3004 // the case but we really shouldn't count on.
3005 if (replyChainEnd < 0) {
3006 replyChainEnd = i;
3007 }
3008 } else if (topTaskIsHigher
3009 && allowTaskReparenting
3010 && taskAffinity != null
3011 && taskAffinity.equals(target.taskAffinity)) {
3012 // This activity has an affinity for our task. Either remove it if we are
3013 // clearing or move it over to our task. Note that
3014 // we currently punt on the case where we are resetting a
3015 // task that is not at the top but who has activities above
3016 // with an affinity to it... this is really not a normal
3017 // case, and we will need to later pull that task to the front
3018 // and usually at that point we will do the reset and pick
3019 // up those remaining activities. (This only happens if
3020 // someone starts an activity in a new task from an activity
3021 // in a task that is not currently on top.)
3022 if (forceReset || finishOnTaskLaunch) {
3023 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003024 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3025 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003026 for (int srcPos = start; srcPos >= i; --srcPos) {
3027 final ActivityRecord p = activities.get(srcPos);
3028 if (p.finishing) {
3029 continue;
3030 }
Todd Kennedy539db512014-12-15 09:57:55 -08003031 finishActivityLocked(
3032 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003033 }
3034 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003035 if (taskInsertionPoint < 0) {
3036 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003037
Craig Mautner77878772013-03-04 19:46:24 -08003038 }
Craig Mautner77878772013-03-04 19:46:24 -08003039
3040 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003041 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3042 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3043 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003044 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003045 final ActivityRecord p = activities.get(srcPos);
Winson Chung30480042017-01-26 10:55:34 -08003046 p.reparent(task, taskInsertionPoint, "resetAffinityTaskIfNeededLocked");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003047
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003048 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3049 "Removing and adding activity " + p + " to stack at " + task
3050 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003051 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3052 + " from " + srcPos + " in to resetting task " + task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003053 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08003054 mWindowContainerController.positionChildAtTop(
3055 task.getWindowContainerController(), true /* includingParents */);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003056
3057 // Now we've moved it in to place... but what if this is
3058 // a singleTop activity and we have put it on top of another
3059 // instance of the same activity? Then we drop the instance
3060 // below so it remains singleTop.
3061 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3062 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003063 int targetNdx = taskActivities.indexOf(target);
3064 if (targetNdx > 0) {
3065 ActivityRecord p = taskActivities.get(targetNdx - 1);
3066 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003067 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3068 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003069 }
3070 }
3071 }
3072 }
3073
3074 replyChainEnd = -1;
3075 }
3076 }
Craig Mautner77878772013-03-04 19:46:24 -08003077 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003078 }
3079
Craig Mautner8849a5e2013-04-02 16:41:03 -07003080 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003081 ActivityRecord newActivity) {
3082 boolean forceReset =
3083 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
3084 if (ACTIVITY_INACTIVE_RESET_TIME > 0
3085 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
3086 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
3087 forceReset = true;
3088 }
3089 }
3090
3091 final TaskRecord task = taskTop.task;
3092
3093 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3094 * for remaining tasks. Used for later tasks to reparent to task. */
3095 boolean taskFound = false;
3096
3097 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3098 ActivityOptions topOptions = null;
3099
Craig Mautner77878772013-03-04 19:46:24 -08003100 // Preserve the location for reparenting in the new task.
3101 int reparentInsertionPoint = -1;
3102
Craig Mautnere3a74d52013-02-22 14:14:58 -08003103 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3104 final TaskRecord targetTask = mTaskHistory.get(i);
3105
3106 if (targetTask == task) {
3107 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3108 taskFound = true;
3109 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003110 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3111 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003112 }
3113 }
3114
Craig Mautner70a86932013-02-28 22:37:44 -08003115 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003116 if (taskNdx >= 0) {
3117 do {
3118 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3119 } while (taskTop == null && taskNdx >= 0);
3120 }
Craig Mautner70a86932013-02-28 22:37:44 -08003121
Craig Mautnere3a74d52013-02-22 14:14:58 -08003122 if (topOptions != null) {
3123 // If we got some ActivityOptions from an activity on top that
3124 // was removed from the task, propagate them to the new real top.
3125 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003126 taskTop.updateOptionsLocked(topOptions);
3127 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003128 topOptions.abort();
3129 }
3130 }
3131
3132 return taskTop;
3133 }
3134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3136 String resultWho, int requestCode, int resultCode, Intent data) {
3137
3138 if (callingUid > 0) {
3139 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003140 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 }
3142
3143 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3144 + " : who=" + resultWho + " req=" + requestCode
3145 + " res=" + resultCode + " data=" + data);
3146 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3147 try {
3148 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3149 list.add(new ResultInfo(resultWho, requestCode,
3150 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003151 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 return;
3153 } catch (Exception e) {
3154 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3155 }
3156 }
3157
3158 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3159 }
3160
Chong Zhang6cda19c2016-06-14 19:07:56 -07003161 private void adjustFocusedActivityStackLocked(ActivityRecord r, String reason) {
3162 if (!mStackSupervisor.isFocusedStack(this) || mResumedActivity != r) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003163 return;
3164 }
3165
3166 final ActivityRecord next = topRunningActivityLocked();
3167 final String myReason = reason + " adjustFocus";
3168 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003169 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003170 // For freeform, docked, and pinned stacks we always keep the focus within the
Chong Zhang6cda19c2016-06-14 19:07:56 -07003171 // stack as long as there is a running activity.
Wale Ogunwaled045c822015-12-02 09:14:28 -08003172 return;
3173 } else {
3174 final TaskRecord task = r.task;
3175 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
3176 // For non-fullscreen stack, we want to move the focus to the next visible
3177 // stack to prevent the home screen from moving to the top and obscuring
3178 // other visible stacks.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003179 if (!mFullscreen
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003180 && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003181 return;
3182 }
3183 // Move the home stack to the top if this stack is fullscreen or there is no
3184 // other visible stack.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003185 if (mStackSupervisor.moveHomeStackTaskToTop(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003186 // Activity focus was already adjusted. Nothing else to do...
3187 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003188 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003189 }
3190 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003191 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003192
Chong Zhang6cda19c2016-06-14 19:07:56 -07003193 mStackSupervisor.moveFocusableActivityStackToFrontLocked(
3194 mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003195 }
3196
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003197 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08003198 final ActivityStack stack = mStackSupervisor.getNextFocusableStackLocked(this);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003199 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003200 if (stack == null) {
3201 return false;
3202 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003203
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003204 final ActivityRecord top = stack.topRunningActivityLocked();
3205
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003206 if (stack.isHomeOrRecentsStack() && (top == null || !top.visible)) {
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003207 // If we will be focusing on the home stack next and its current top activity isn't
3208 // visible, then use the task return to value to determine the home task to display next.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003209 return mStackSupervisor.moveHomeStackTaskToTop(reason);
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003210 }
Chong Zhang6cda19c2016-06-14 19:07:56 -07003211
3212 stack.moveToFront(myReason);
3213 return true;
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003214 }
3215
Craig Mautnerf3333272013-04-22 10:55:53 -07003216 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003217 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3219 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3220 if (!r.finishing) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003221 if (!mService.isSleepingLocked()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003222 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003223 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3224 "stop-no-history", false)) {
3225 // Activity was finished, no need to continue trying to schedule stop.
Chong Zhang6cda19c2016-06-14 19:07:56 -07003226 adjustFocusedActivityStackLocked(r, "stopActivityFinished");
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003227 r.resumeKeyDispatchingLocked();
3228 return;
3229 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003230 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003231 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003232 + " on stop because we're just sleeping");
3233 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003234 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003235 }
3236
3237 if (r.app != null && r.app.thread != null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003238 adjustFocusedActivityStackLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003239 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003240 try {
3241 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003242 if (DEBUG_STATES) Slog.v(TAG_STATES,
3243 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003244 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003245 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3246 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003247 if (!r.visible) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003248 r.setVisibility(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003250 EventLogTags.writeAmStopActivity(
3251 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003252 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003253 if (mService.isSleepingOrShuttingDownLocked()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003254 r.setSleeping(true);
3255 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003256 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003257 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003258 } catch (Exception e) {
3259 // Maybe just ignore exceptions here... if the process
3260 // has crashed, our death notification will clean things
3261 // up.
3262 Slog.w(TAG, "Exception thrown during pause", e);
3263 // Just in case, assume it to be stopped.
3264 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003265 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003266 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003267 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003268 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003269 }
3270 }
3271 }
3272 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003273
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003274 /**
3275 * @return Returns true if the activity is being finished, false if for
3276 * some reason it is being left as-is.
3277 */
3278 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003279 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003280 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003281 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3282 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003283 + ", result=" + resultCode + ", data=" + resultData
3284 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003285 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286 return false;
3287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288
Craig Mautnerd44711d2013-02-23 11:24:36 -08003289 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003290 return true;
3291 }
3292
Craig Mautnerd2328952013-03-05 12:46:26 -08003293 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003294 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3295 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3296 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3297 ActivityRecord r = activities.get(activityNdx);
3298 if (r.resultTo == self && r.requestCode == requestCode) {
3299 if ((r.resultWho == null && resultWho == null) ||
3300 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3301 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3302 false);
3303 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003304 }
3305 }
3306 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003307 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003308 }
3309
Adrian Roos20d7df32016-01-12 18:59:43 +01003310 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003311 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003312 TaskRecord finishedTask = null;
3313 if (r == null || r.app != app) {
3314 return null;
3315 }
3316 Slog.w(TAG, " Force finishing activity "
3317 + r.intent.getComponent().flattenToShortString());
3318 int taskNdx = mTaskHistory.indexOf(r.task);
3319 int activityNdx = r.task.mActivities.indexOf(r);
3320 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3321 finishedTask = r.task;
3322 // Also terminate any activities below it that aren't yet
3323 // stopped, to avoid a situation where one will get
3324 // re-start our crashing activity once it gets resumed again.
3325 --activityNdx;
3326 if (activityNdx < 0) {
3327 do {
3328 --taskNdx;
3329 if (taskNdx < 0) {
3330 break;
3331 }
3332 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3333 } while (activityNdx < 0);
3334 }
3335 if (activityNdx >= 0) {
3336 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3337 if (r.state == ActivityState.RESUMED
3338 || r.state == ActivityState.PAUSING
3339 || r.state == ActivityState.PAUSED) {
3340 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3341 Slog.w(TAG, " Force finishing activity "
3342 + r.intent.getComponent().flattenToShortString());
3343 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003344 }
3345 }
3346 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003347 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003348 }
3349
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003350 final void finishVoiceTask(IVoiceInteractionSession session) {
3351 IBinder sessionBinder = session.asBinder();
3352 boolean didOne = false;
3353 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3354 TaskRecord tr = mTaskHistory.get(taskNdx);
3355 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3356 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3357 ActivityRecord r = tr.mActivities.get(activityNdx);
3358 if (!r.finishing) {
3359 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3360 false);
3361 didOne = true;
3362 }
3363 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003364 } else {
3365 // Check if any of the activities are using voice
3366 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3367 ActivityRecord r = tr.mActivities.get(activityNdx);
3368 if (r.voiceSession != null
3369 && r.voiceSession.asBinder() == sessionBinder) {
3370 // Inform of cancellation
3371 r.clearVoiceSessionLocked();
3372 try {
3373 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3374 null);
3375 } catch (RemoteException re) {
3376 // Ok
3377 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003378 mService.finishRunningVoiceLocked();
3379 break;
3380 }
3381 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003382 }
3383 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003384
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003385 if (didOne) {
3386 mService.updateOomAdjLocked();
3387 }
3388 }
3389
Craig Mautnerd2328952013-03-05 12:46:26 -08003390 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003391 ArrayList<ActivityRecord> activities = r.task.mActivities;
3392 for (int index = activities.indexOf(r); index >= 0; --index) {
3393 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003394 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003395 break;
3396 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003397 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003398 }
3399 return true;
3400 }
3401
Andrii Kulian21713ac2016-10-12 22:05:05 -07003402 private void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
Dianne Hackborn5c607432012-02-28 14:44:19 -08003403 // send the result
3404 ActivityRecord resultTo = r.resultTo;
3405 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003406 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003407 + " who=" + r.resultWho + " req=" + r.requestCode
3408 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003409 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003410 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003411 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003412 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003413 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003414 if (r.info.applicationInfo.uid > 0) {
3415 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3416 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003417 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003418 }
3419 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3420 resultData);
3421 r.resultTo = null;
3422 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003423 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003424
3425 // Make sure this HistoryRecord is not holding on to other resources,
3426 // because clients have remote IPC references to this object so we
3427 // can't assume that will go away and want to avoid circular IPC refs.
3428 r.results = null;
3429 r.pendingResults = null;
3430 r.newIntents = null;
3431 r.icicle = null;
3432 }
3433
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003434 /**
3435 * @return Returns true if this activity has been removed from the history
3436 * list, or false if it is still in the list and will be removed later.
3437 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003438 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3439 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003440 if (r.finishing) {
3441 Slog.w(TAG, "Duplicate finish request for " + r);
3442 return false;
3443 }
3444
Jorim Jaggife762342016-10-13 14:33:27 +02003445 mWindowManager.deferSurfaceLayout();
3446 try {
3447 r.makeFinishingLocked();
3448 final TaskRecord task = r.task;
3449 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3450 r.userId, System.identityHashCode(r),
3451 task.taskId, r.shortComponentName, reason);
3452 final ArrayList<ActivityRecord> activities = task.mActivities;
3453 final int index = activities.indexOf(r);
3454 if (index < (activities.size() - 1)) {
3455 task.setFrontOfTask();
3456 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3457 // If the caller asked that this activity (and all above it)
3458 // be cleared when the task is reset, don't lose that information,
3459 // but propagate it up to the next activity.
3460 ActivityRecord next = activities.get(index+1);
3461 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3462 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003463 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003464
Jorim Jaggife762342016-10-13 14:33:27 +02003465 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003466
Jorim Jaggife762342016-10-13 14:33:27 +02003467 adjustFocusedActivityStackLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003468
Jorim Jaggife762342016-10-13 14:33:27 +02003469 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003470
Jorim Jaggife762342016-10-13 14:33:27 +02003471 final boolean endTask = index <= 0;
3472 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
3473 if (mResumedActivity == r) {
3474 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
3475 "Prepare close transition: finishing " + r);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003476 if (endTask) {
3477 mService.mTaskChangeNotificationController.notifyTaskRemovalStarted(task.taskId);
3478 }
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003479 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480
Jorim Jaggife762342016-10-13 14:33:27 +02003481 // Tell window manager to prepare for this one to be removed.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003482 r.setVisibility(false);
Jorim Jaggife762342016-10-13 14:33:27 +02003483
3484 if (mPausingActivity == null) {
3485 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3486 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3487 "finish() => pause with userLeaving=false");
3488 startPausingLocked(false, false, null, false);
3489 }
3490
3491 if (endTask) {
3492 mStackSupervisor.removeLockedTaskLocked(task);
3493 }
3494 } else if (r.state != ActivityState.PAUSING) {
3495 // If the activity is PAUSING, we will complete the finish once
3496 // it is done pausing; else we can just directly finish it here.
3497 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
3498 if (r.visible) {
3499 mWindowManager.prepareAppTransition(transit, false);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003500 r.setVisibility(false);
Jorim Jaggife762342016-10-13 14:33:27 +02003501 mWindowManager.executeAppTransition();
3502 if (!mStackSupervisor.mWaitingVisibleActivities.contains(r)) {
3503 mStackSupervisor.mWaitingVisibleActivities.add(r);
3504 }
3505 }
3506 return finishCurrentActivityLocked(r, (r.visible || r.nowVisible) ?
3507 FINISH_AFTER_VISIBLE : FINISH_AFTER_PAUSE, oomAdj) == null;
3508 } else {
3509 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
3510 }
3511
3512 return false;
3513 } finally {
3514 mWindowManager.continueSurfaceLayout();
3515 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003516 }
3517
Craig Mautnerf3333272013-04-22 10:55:53 -07003518 static final int FINISH_IMMEDIATELY = 0;
3519 static final int FINISH_AFTER_PAUSE = 1;
3520 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003521
Craig Mautnerf3333272013-04-22 10:55:53 -07003522 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 // First things first: if this activity is currently visible,
3524 // and the resumed activity is not yet visible, then hold off on
3525 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003526
3527 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
3528
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003529 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003530 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003531 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003532 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003534 if (DEBUG_STATES) Slog.v(TAG_STATES,
3535 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003536 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003537 if (oomAdj) {
3538 mService.updateOomAdjLocked();
3539 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003540 return r;
3541 }
3542
3543 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003544 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003545 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003546 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003547 if (mResumedActivity == r) {
3548 mResumedActivity = null;
3549 }
3550 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003551 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003552 r.state = ActivityState.FINISHING;
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003553 final boolean finishingActivityInNonFocusedStack
Andrii Kulian02b7a832016-10-06 23:11:56 -07003554 = r.getStack() != mStackSupervisor.getFocusedStack()
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003555 && prevState == ActivityState.PAUSED && mode == FINISH_AFTER_VISIBLE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003556
3557 if (mode == FINISH_IMMEDIATELY
Wale Ogunwaleae30f302016-05-02 10:35:45 -07003558 || (prevState == ActivityState.PAUSED
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003559 && (mode == FINISH_AFTER_PAUSE || mStackId == PINNED_STACK_ID))
3560 || finishingActivityInNonFocusedStack
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003561 || prevState == ActivityState.STOPPED
3562 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003563 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003564 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Andrii Kulian7318d632016-07-20 18:59:28 -07003565
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003566 if (finishingActivityInNonFocusedStack) {
3567 // Finishing activity that was in paused state and it was in not currently focused
3568 // stack, need to make something visible in its place.
Andrii Kulian7318d632016-07-20 18:59:28 -07003569 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3570 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003571 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003572 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003573 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003574 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003575 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3576 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003577 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003578 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003579
3580 // Need to go through the full pause cycle to get this
3581 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003582 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003583 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003584 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003585 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003586 return r;
3587 }
3588
Craig Mautneree36c772014-07-16 14:56:05 -07003589 void finishAllActivitiesLocked(boolean immediately) {
3590 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003591 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3592 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3593 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3594 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003595 noActivitiesInStack = false;
3596 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003597 continue;
3598 }
Craig Mautneree36c772014-07-16 14:56:05 -07003599 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003600 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3601 }
3602 }
Craig Mautneree36c772014-07-16 14:56:05 -07003603 if (noActivitiesInStack) {
3604 mActivityContainer.onTaskListEmptyLocked();
3605 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003606 }
3607
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003608 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3609 // Basic case: for simple app-centric recents, we need to recreate
3610 // the task if the affinity has changed.
3611 if (srec == null || srec.task.affinity == null ||
3612 !srec.task.affinity.equals(destAffinity)) {
3613 return true;
3614 }
3615 // Document-centric case: an app may be split in to multiple documents;
3616 // they need to re-create their task if this current activity is the root
3617 // of a document, unless simply finishing it will return them to the the
3618 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003619 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3620 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003621 // Okay, this activity is at the root of its task. What to do, what to do...
3622 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3623 // Finishing won't return to an application, so we need to recreate.
3624 return true;
3625 }
3626 // We now need to get the task below it to determine what to do.
3627 int taskIdx = mTaskHistory.indexOf(srec.task);
3628 if (taskIdx <= 0) {
3629 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3630 return false;
3631 }
3632 if (taskIdx == 0) {
3633 // At the bottom of the stack, nothing to go back to.
3634 return true;
3635 }
3636 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3637 if (!srec.task.affinity.equals(prevTask.affinity)) {
3638 // These are different apps, so need to recreate.
3639 return true;
3640 }
3641 }
3642 return false;
3643 }
3644
Wale Ogunwale7d701172015-03-11 15:36:30 -07003645 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003646 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003647 final TaskRecord task = srec.task;
3648 final ArrayList<ActivityRecord> activities = task.mActivities;
3649 final int start = activities.indexOf(srec);
3650 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003651 return false;
3652 }
3653 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003654 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003655 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003656 final ComponentName dest = destIntent.getComponent();
3657 if (start > 0 && dest != null) {
3658 for (int i = finishTo; i >= 0; i--) {
3659 ActivityRecord r = activities.get(i);
3660 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003661 r.info.name.equals(dest.getClassName())) {
3662 finishTo = i;
3663 parent = r;
3664 foundParentInTask = true;
3665 break;
3666 }
3667 }
3668 }
3669
3670 IActivityController controller = mService.mController;
3671 if (controller != null) {
3672 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3673 if (next != null) {
3674 // ask watcher if this is allowed
3675 boolean resumeOK = true;
3676 try {
3677 resumeOK = controller.activityResuming(next.packageName);
3678 } catch (RemoteException e) {
3679 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003680 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003681 }
3682
3683 if (!resumeOK) {
3684 return false;
3685 }
3686 }
3687 }
3688 final long origId = Binder.clearCallingIdentity();
3689 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003690 ActivityRecord r = activities.get(i);
3691 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003692 // Only return the supplied result for the first activity finished
3693 resultCode = Activity.RESULT_CANCELED;
3694 resultData = null;
3695 }
3696
3697 if (parent != null && foundParentInTask) {
3698 final int parentLaunchMode = parent.info.launchMode;
3699 final int destIntentFlags = destIntent.getFlags();
3700 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3701 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3702 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3703 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003704 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3705 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003706 } else {
3707 try {
3708 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3709 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003710 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3711 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3712 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003713 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3714 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003715 foundParentInTask = res == ActivityManager.START_SUCCESS;
3716 } catch (RemoteException e) {
3717 foundParentInTask = false;
3718 }
3719 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003720 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003721 }
3722 }
3723 Binder.restoreCallingIdentity(origId);
3724 return foundParentInTask;
3725 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003726 /**
3727 * Perform the common clean-up of an activity record. This is called both
3728 * as part of destroyActivityLocked() (when destroying the client-side
3729 * representation) and cleaning things up as a result of its hosting
3730 * processing going away, in which case there is no remaining client-side
3731 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003732 *
3733 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003734 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07003735 private void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices, boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003736 if (mResumedActivity == r) {
3737 mResumedActivity = null;
3738 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003739 if (mPausingActivity == r) {
3740 mPausingActivity = null;
3741 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003742
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003743 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003744 r.frozenBeforeDestroy = false;
3745
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003746 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003747 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003748 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003749 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003750 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003751 }
3752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003753 // Make sure this record is no longer in the pending finishes list.
3754 // This could happen, for example, if we are trimming activities
3755 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003756 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003757 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003758
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003759 // Remove any pending results.
3760 if (r.finishing && r.pendingResults != null) {
3761 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3762 PendingIntentRecord rec = apr.get();
3763 if (rec != null) {
3764 mService.cancelIntentSenderLocked(rec, false);
3765 }
3766 }
3767 r.pendingResults = null;
3768 }
3769
3770 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003771 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003772 }
3773
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003774 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003775 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003776 if (getVisibleBehindActivity() == r) {
3777 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003778 }
Wale Ogunwale8fd75422016-06-24 14:20:37 -07003779
3780 // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
3781 // manager so it can update its bookkeeping.
3782 mWindowManager.notifyAppRelaunchesCleared(r.appToken);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003783 }
3784
3785 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003786 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003787 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003788 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003789 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003790 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003791 }
3792
Chong Zhangbffd8892016-08-08 11:16:06 -07003793 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003794 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003795 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003796 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003797 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3798 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3799
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003800 r.takeFromHistory();
3801 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003802 if (DEBUG_STATES) Slog.v(TAG_STATES,
3803 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003804 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003805 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003806 r.app = null;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003807 r.removeWindowContainer();
Craig Mautner312ba862014-02-10 17:55:01 -08003808 final TaskRecord task = r.task;
3809 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003810 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003811 "removeActivityFromHistoryLocked: last activity removed from " + this);
Chong Zhangbffd8892016-08-08 11:16:06 -07003812 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003813 task.isOverHomeStack()) {
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003814 mStackSupervisor.moveHomeStackTaskToTop(reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003815 }
Craig Mautner299f9602015-01-26 09:47:33 -08003816 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003817 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003818 cleanUpActivityServicesLocked(r);
3819 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003820 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003821
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822 /**
3823 * Perform clean-up of service connections in an activity record.
3824 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07003825 private void cleanUpActivityServicesLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003826 // Throw away any services that have been bound by this activity.
3827 if (r.connections != null) {
3828 Iterator<ConnectionRecord> it = r.connections.iterator();
3829 while (it.hasNext()) {
3830 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003831 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003832 }
3833 r.connections = null;
3834 }
3835 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003836
Craig Mautneree2e45a2014-06-27 12:10:03 -07003837 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003838 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003839 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003840 mHandler.sendMessage(msg);
3841 }
3842
Andrii Kulian21713ac2016-10-12 22:05:05 -07003843 private void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003844 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003845 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003846 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3847 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3848 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3849 final ActivityRecord r = activities.get(activityNdx);
3850 if (r.finishing) {
3851 continue;
3852 }
3853 if (r.fullscreen) {
3854 lastIsOpaque = true;
3855 }
3856 if (owner != null && r.app != owner) {
3857 continue;
3858 }
3859 if (!lastIsOpaque) {
3860 continue;
3861 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003862 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003863 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003864 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003865 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003866 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003867 activityRemoved = true;
3868 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003869 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003870 }
3871 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003872 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003873 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003874 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003875 }
3876
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003877 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3878 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003879 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3880 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003881 + " pausing=" + mPausingActivity + " for reason " + reason);
3882 return destroyActivityLocked(r, true, reason);
3883 }
3884 return false;
3885 }
3886
3887 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3888 String reason) {
3889 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003890 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003891 int maxTasks = tasks.size() / 4;
3892 if (maxTasks < 1) {
3893 maxTasks = 1;
3894 }
3895 int numReleased = 0;
3896 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3897 final TaskRecord task = mTaskHistory.get(taskNdx);
3898 if (!tasks.contains(task)) {
3899 continue;
3900 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003901 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003902 int curNum = 0;
3903 final ArrayList<ActivityRecord> activities = task.mActivities;
3904 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3905 final ActivityRecord activity = activities.get(actNdx);
3906 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003907 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003908 + " in state " + activity.state + " resumed=" + mResumedActivity
3909 + " pausing=" + mPausingActivity + " for reason " + reason);
3910 destroyActivityLocked(activity, true, reason);
3911 if (activities.get(actNdx) != activity) {
3912 // Was removed from list, back up so we don't miss the next one.
3913 actNdx--;
3914 }
3915 curNum++;
3916 }
3917 }
3918 if (curNum > 0) {
3919 numReleased += curNum;
3920 maxTasks--;
3921 if (mTaskHistory.get(taskNdx) != task) {
3922 // The entire task got removed, back up so we don't miss the next one.
3923 taskNdx--;
3924 }
3925 }
3926 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003927 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3928 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003929 return numReleased;
3930 }
3931
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003932 /**
3933 * Destroy the current CLIENT SIDE instance of an activity. This may be
3934 * called both when actually finishing an activity, or when performing
3935 * a configuration switch where we destroy the current client-side object
3936 * but then create a new client-side object for this same HistoryRecord.
3937 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003938 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003939 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3940 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003941 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003942 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003943 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003944 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003945
3946 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003947
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003948 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003949
3950 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003951
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003952 if (hadApp) {
3953 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003954 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003955 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3956 mService.mHeavyWeightProcess = null;
3957 mService.mHandler.sendEmptyMessage(
3958 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3959 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003960 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003961 // Update any services we are bound to that might care about whether
3962 // their client may have activities.
3963 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003964 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003965 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003966 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003967 }
3968 }
3969
3970 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003971
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003972 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003973 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003974 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003975 r.configChangeFlags);
3976 } catch (Exception e) {
3977 // We can just ignore exceptions here... if the process
3978 // has crashed, our death notification will clean things
3979 // up.
3980 //Slog.w(TAG, "Exception thrown during finish", e);
3981 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07003982 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003983 removedFromHistory = true;
3984 skipDestroy = true;
3985 }
3986 }
3987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003988 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003989
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003990 // If the activity is finishing, we need to wait on removing it
3991 // from the list to give it a chance to do its cleanup. During
3992 // that time it may make calls back with its token so we need to
3993 // be able to find it on the list and so we don't want to remove
3994 // it from the list yet. Otherwise, we can just immediately put
3995 // it in the destroyed state since we are not removing it from the
3996 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003997 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003998 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003999 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004000 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07004001 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004002 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4003 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004004 if (DEBUG_STATES) Slog.v(TAG_STATES,
4005 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004006 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004007 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004008 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004009 }
4010 } else {
4011 // remove this record from the history.
4012 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004013 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004014 removedFromHistory = true;
4015 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004016 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004017 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004018 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004019 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004020 }
4021 }
4022
4023 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004024
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004025 if (!mLRUActivities.remove(r) && hadApp) {
4026 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4027 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004028
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004029 return removedFromHistory;
4030 }
4031
Craig Mautner299f9602015-01-26 09:47:33 -08004032 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004033 final long origId = Binder.clearCallingIdentity();
4034 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004035 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08004036 if (r != null) {
4037 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004038 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004039 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08004040
Wale Ogunwale60454db2015-01-23 16:05:07 -08004041 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004042 if (r.state == ActivityState.DESTROYING) {
4043 cleanUpActivityLocked(r, true, false);
Chong Zhangbffd8892016-08-08 11:16:06 -07004044 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004045 }
4046 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004047 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08004048 } finally {
4049 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004050 }
4051 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004052
Todd Kennedyaab56db2015-01-30 09:39:53 -08004053 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004054 if (hasVisibleBehindActivity() &&
4055 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08004056 if (r == topRunningActivityLocked()
4057 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07004058 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08004059 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07004060 return;
4061 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004062 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07004063 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07004064 " thread=" + r.app.thread);
4065 if (r != null && r.app != null && r.app.thread != null) {
4066 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07004067 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004068 } catch (RemoteException e) {
4069 }
Jose Lima4b6c6692014-08-12 17:41:12 -07004070 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004071 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07004072 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004073 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004074 }
4075 }
4076 }
4077
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004078 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07004079 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
4080 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004081 if (r != null) {
4082 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07004083 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004084 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004085 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004086 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004087 }
4088
Jose Lima4b6c6692014-08-12 17:41:12 -07004089 boolean hasVisibleBehindActivity() {
4090 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004091 }
4092
Jose Lima4b6c6692014-08-12 17:41:12 -07004093 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07004094 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004095 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004096 }
4097 }
4098
Jose Lima4b6c6692014-08-12 17:41:12 -07004099 ActivityRecord getVisibleBehindActivity() {
4100 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004101 }
4102
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004103 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4104 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004105 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004106 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4107 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004108 while (i > 0) {
4109 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004110 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004111 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004112 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004113 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004114 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004115 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004116 }
4117 }
4118 }
4119
Andrii Kulian21713ac2016-10-12 22:05:05 -07004120 private boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004121 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004122 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4123 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004124 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4125 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004126 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004127 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07004128 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4129 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004130
4131 boolean hasVisibleActivities = false;
4132
4133 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004134 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004135 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4136 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004137 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4138 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4139 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4140 final ActivityRecord r = activities.get(activityNdx);
4141 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004142 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4143 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08004144 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004145 if (r.visible) {
4146 hasVisibleActivities = true;
4147 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004148 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004149 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4150 // Don't currently have state for the activity, or
4151 // it is finishing -- always remove it.
4152 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004153 } else if (!r.visible && r.launchCount > 2 &&
4154 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004155 // We have launched this activity too many times since it was
4156 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004157 // (Note if the activity is visible, we don't remove the record.
4158 // We leave the dead window on the screen but the process will
4159 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004160 remove = true;
4161 } else {
4162 // The process may be gone, but the activity lives on!
4163 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004164 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004165 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004166 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4167 "Removing activity " + r + " from stack at " + i
4168 + ": haveState=" + r.haveState
4169 + " stateNotNeeded=" + r.stateNotNeeded
4170 + " finishing=" + r.finishing
4171 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004172 if (!r.finishing) {
4173 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4174 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4175 r.userId, System.identityHashCode(r),
4176 r.task.taskId, r.shortComponentName,
4177 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07004178 if (r.state == ActivityState.RESUMED) {
4179 mService.updateUsageStats(r, false);
4180 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004181 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004182 } else {
4183 // We have the current state for this activity, so
4184 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004185 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4186 if (DEBUG_APP) Slog.v(TAG_APP,
4187 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004188 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004189 // Set nowVisible to previous visible state. If the app was visible while
4190 // it died, we leave the dead window on screen so it's basically visible.
4191 // This is needed when user later tap on the dead window, we need to stop
4192 // other apps when user transfers focus to the restarted activity.
4193 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004194 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004195 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004196 "App died, clearing saved state of " + r);
4197 r.icicle = null;
4198 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004199 }
Craig Mautnerd2328952013-03-05 12:46:26 -08004200 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004201 if (remove) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004202 removeActivityFromHistoryLocked(r, "appDied");
Craig Mautneracebdc82015-02-24 10:53:03 -08004203 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004204 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004205 }
4206 }
4207
4208 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004209 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004210
Andrii Kulian21713ac2016-10-12 22:05:05 -07004211 private void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004212 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004213 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004214 if (r != null && r.state != ActivityState.RESUMED) {
4215 r.updateOptionsLocked(options);
4216 } else {
4217 ActivityOptions.abort(options);
4218 }
4219 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004220 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004221 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004222
Andrii Kulian21713ac2016-10-12 22:05:05 -07004223 private void updateTaskMovement(TaskRecord task, boolean toFront) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004224 if (task.isPersistable) {
4225 task.mLastTimeMoved = System.currentTimeMillis();
4226 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4227 // recently will be most negative, tasks sent to the bottom before that will be less
4228 // negative. Similarly for recent tasks moved to the top which will be most positive.
4229 if (!toFront) {
4230 task.mLastTimeMoved *= -1;
4231 }
4232 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004233 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004234 }
4235
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004236 void moveHomeStackTaskToTop() {
Craig Mautnera82aa092013-09-13 15:34:08 -07004237 final int top = mTaskHistory.size() - 1;
4238 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4239 final TaskRecord task = mTaskHistory.get(taskNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004240 if (task.taskType == HOME_ACTIVITY_TYPE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004241 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4242 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004243 mTaskHistory.remove(taskNdx);
4244 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004245 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004246 return;
4247 }
4248 }
4249 }
4250
Chong Zhang280d3322015-11-03 17:27:26 -08004251 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004252 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004253 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004254
Winson Chungb5c41b72016-12-07 15:00:47 -08004255 final ActivityRecord focusedTopActivity = mStackSupervisor.getFocusedStack() != null
4256 ? mStackSupervisor.getFocusedStack().topActivity() : null;
Craig Mautner11bf9a52013-02-19 14:08:51 -08004257 final int numTasks = mTaskHistory.size();
4258 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004259 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004260 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004261 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004262 ActivityOptions.abort(options);
4263 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004264 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004265 }
4266 return;
4267 }
4268
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004269 if (timeTracker != null) {
4270 // The caller wants a time tracker associated with this task.
4271 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4272 tr.mActivities.get(i).appTimeTracker = timeTracker;
4273 }
4274 }
4275
Craig Mautner11bf9a52013-02-19 14:08:51 -08004276 // Shift all activities with this task up to the top
4277 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004278 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004279
Chong Zhang45c25ce2015-08-10 22:18:26 -07004280 // Don't refocus if invisible to current user
4281 ActivityRecord top = tr.getTopActivity();
Chong Zhang87761972016-08-22 13:53:24 -07004282 if (top == null || !top.okToShowLocked()) {
Chong Zhang45c25ce2015-08-10 22:18:26 -07004283 addRecentActivityLocked(top);
4284 ActivityOptions.abort(options);
4285 return;
4286 }
4287
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004288 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004289 ActivityRecord r = topRunningActivityLocked();
Chong Zhang6cda19c2016-06-14 19:07:56 -07004290 mStackSupervisor.moveFocusableActivityStackToFrontLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004291
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004292 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004293 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004294 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004295 if (r != null) {
4296 mNoAnimActivities.add(r);
4297 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004298 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004300 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004301 }
Winson Chungc2baac02017-01-11 13:34:47 -08004302 // If a new task is moved to the front, then mark the existing top activity as supporting
4303 // picture-in-picture while paused
Winson Chungb5c41b72016-12-07 15:00:47 -08004304 if (focusedTopActivity != null) {
Winson Chungc2baac02017-01-11 13:34:47 -08004305 focusedTopActivity.supportsPictureInPictureWhilePausing = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08004306 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004307
Wale Ogunwaled046a012015-12-24 13:05:59 -08004308 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004309 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Yorke Leebd54c2a2016-10-25 13:49:23 -07004310
4311 mService.mTaskChangeNotificationController.notifyTaskMovedToFront(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004312 }
4313
4314 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004315 * Worker method for rearranging history stack. Implements the function of moving all
4316 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004317 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004318 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004319 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4320 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004321 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004322 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004323 * @return Returns true if the move completed, false if not.
4324 */
Craig Mautner299f9602015-01-26 09:47:33 -08004325 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004326 final TaskRecord tr = taskForIdLocked(taskId);
4327 if (tr == null) {
4328 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4329 return false;
4330 }
4331
4332 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004333 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004334
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004335 // If we have a watcher, preflight the move before committing to it. First check
4336 // for *other* available tasks, but if none are available, then try again allowing the
4337 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004338 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004339 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004340 if (next == null) {
4341 next = topRunningActivityLocked(null, 0);
4342 }
4343 if (next != null) {
4344 // ask watcher if this is allowed
4345 boolean moveOK = true;
4346 try {
4347 moveOK = mService.mController.activityResuming(next.packageName);
4348 } catch (RemoteException e) {
4349 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004350 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004351 }
4352 if (!moveOK) {
4353 return false;
4354 }
4355 }
4356 }
4357
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004358 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004359
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004360 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
Jiaquan Hee13e9642016-06-15 15:16:13 -07004361 if (topTask().isOnTopLauncher()) {
4362 // An on-top launcher doesn't affect the visibility of activities on other stacks
4363 // behind it. So if we're moving an on-top launcher to the back, we want to move the
4364 // focus to the next focusable stack and resume an activity there.
4365 // Besides, when the docked stack is visible, we should also move the home stack to
4366 // the back to avoid the recents pops up on top of a fullscreen or freeform
4367 // activity.
4368
4369 // Move the home stack to back.
4370 moveToBack(topTask());
4371
4372 // Resume an activity in the next focusable stack.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004373 adjustFocusToNextFocusableStackLocked("moveTaskToBack");
Jiaquan Hee13e9642016-06-15 15:16:13 -07004374 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4375 return true;
4376 }
4377
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004378 // For the case where we are moving the home task back and there is an activity visible
4379 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4380 // activity to maintain order with what the user is seeing.
4381 final ActivityStack fullscreenStack =
4382 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4383 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4384 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
Chong Zhang6cda19c2016-06-14 19:07:56 -07004385 mStackSupervisor.moveFocusableActivityStackToFrontLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004386 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004387 return true;
4388 }
4389 }
4390
riddle_hsuc215a4f2014-12-27 12:10:45 +08004391 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004392
4393 // If true, we should resume the home activity next if the task we are moving to the
4394 // back is over the home stack. We force to false if the task we are moving to back
4395 // is the home task and we don't want it resumed after moving to the back.
4396 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4397 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004398 final TaskRecord nextTask = getNextTask(tr);
4399 if (nextTask != null) {
4400 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4401 } else {
4402 prevIsHome = true;
4403 }
4404 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004405 mTaskHistory.remove(tr);
4406 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004407 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004408
Craig Mautnerc8143c62013-09-03 12:15:57 -07004409 // There is an assumption that moving a task to the back moves it behind the home activity.
4410 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004411 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004412 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4413 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004414 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004415 break;
4416 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004417 if (taskNdx == 1) {
4418 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004419 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004420 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004421 }
4422
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004423 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004424 mWindowContainerController.positionChildAtBottom(tr.getWindowContainerController());
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004425
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004426 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004427 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004428 if (!mService.mBooting && !mService.mBooted) {
4429 // Not ready yet!
4430 return false;
4431 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004432 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004433 return mStackSupervisor.resumeHomeStackTask(null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004434 }
4435
Wale Ogunwaled046a012015-12-24 13:05:59 -08004436 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004437 return true;
4438 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004439
Andrii Kulian21713ac2016-10-12 22:05:05 -07004440 static void logStartActivity(int tag, ActivityRecord r, TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004441 final Uri data = r.intent.getData();
4442 final String strData = data != null ? data.toSafeString() : null;
4443
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004444 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004445 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004446 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004447 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004448 }
4449
4450 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004451 * Ensures all visible activities at or below the input activity have the right configuration.
4452 */
4453 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4454 if (start == null || !start.visible) {
4455 return;
4456 }
4457
4458 final TaskRecord startTask = start.task;
4459 boolean behindFullscreen = false;
4460 boolean updatedConfig = false;
4461
4462 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4463 final TaskRecord task = mTaskHistory.get(taskIndex);
4464 final ArrayList<ActivityRecord> activities = task.mActivities;
4465 int activityIndex =
4466 (start.task == task) ? activities.indexOf(start) : activities.size() - 1;
4467 for (; activityIndex >= 0; --activityIndex) {
4468 final ActivityRecord r = activities.get(activityIndex);
Andrii Kulian21713ac2016-10-12 22:05:05 -07004469 updatedConfig |= r.ensureActivityConfigurationLocked(0 /* globalChanges */,
4470 preserveWindow);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004471 if (r.fullscreen) {
4472 behindFullscreen = true;
4473 break;
4474 }
4475 }
4476 if (behindFullscreen) {
4477 break;
4478 }
4479 }
4480 if (updatedConfig) {
Wale Ogunwale089586f2016-06-20 14:16:22 -07004481 // Ensure the resumed state of the focus activity if we updated the configuration of
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004482 // any activity.
4483 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4484 }
4485 }
4486
Wale Ogunwale1666e312016-12-16 11:27:18 -08004487 // TODO: Figure-out a way to consolidate with resize() method below.
4488 @Override
4489 public void requestResize(Rect bounds) {
4490 mService.resizeStack(mStackId, bounds, true /* allowResizeInDockedMode */,
4491 false /* preserveWindows */, false /* animate */, -1 /* animationDuration */);
4492 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07004493
Wale Ogunwale1666e312016-12-16 11:27:18 -08004494 // TODO: Can only be called from special methods in ActivityStackSupervisor.
4495 // Need to consolidate those calls points into this resize method so anyone can call directly.
4496 void resize(Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds) {
4497 bounds = TaskRecord.validateBounds(bounds);
4498
4499 if (!updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
4500 return;
4501 }
4502
4503 // Update override configurations of all tasks in the stack.
4504 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
Andrii Kulian1e32e022016-09-16 15:29:34 -07004505 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
4506
4507 mTmpBounds.clear();
4508 mTmpConfigs.clear();
4509 mTmpInsetBounds.clear();
4510
4511 for (int i = mTaskHistory.size() - 1; i >= 0; i--) {
4512 final TaskRecord task = mTaskHistory.get(i);
4513 if (task.isResizeable()) {
4514 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
4515 // For freeform stack we don't adjust the size of the tasks to match that
4516 // of the stack, but we do try to make sure the tasks are still contained
4517 // with the bounds of the stack.
Wale Ogunwale1666e312016-12-16 11:27:18 -08004518 mTmpRect2.set(task.mBounds);
4519 fitWithinBounds(mTmpRect2, bounds);
4520 task.updateOverrideConfiguration(mTmpRect2);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004521 } else {
4522 task.updateOverrideConfiguration(taskBounds, insetBounds);
4523 }
4524 }
4525
Andrii Kulian1779e612016-10-12 21:58:25 -07004526 mTmpConfigs.put(task.taskId, task.getOverrideConfiguration());
Andrii Kulian1e32e022016-09-16 15:29:34 -07004527 mTmpBounds.put(task.taskId, task.mBounds);
4528 if (tempTaskInsetBounds != null) {
4529 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
4530 }
4531 }
4532
Wale Ogunwale1666e312016-12-16 11:27:18 -08004533 mFullscreen = mWindowContainerController.resize(bounds, mTmpConfigs, mTmpBounds,
Andrii Kulian1e32e022016-09-16 15:29:34 -07004534 mTmpInsetBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004535 setBounds(bounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07004536 }
4537
4538
4539 /**
4540 * Adjust bounds to stay within stack bounds.
4541 *
4542 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4543 * that keep them unchanged, but be contained within the stack bounds.
4544 *
4545 * @param bounds Bounds to be adjusted.
4546 * @param stackBounds Bounds within which the other bounds should remain.
4547 */
4548 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4549 if (stackBounds == null || stackBounds.contains(bounds)) {
4550 return;
4551 }
4552
4553 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4554 final int maxRight = stackBounds.right
4555 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4556 int horizontalDiff = stackBounds.left - bounds.left;
4557 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4558 || (bounds.left + horizontalDiff >= maxRight)) {
4559 horizontalDiff = maxRight - bounds.left;
4560 }
4561 bounds.left += horizontalDiff;
4562 bounds.right += horizontalDiff;
4563 }
4564
4565 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4566 final int maxBottom = stackBounds.bottom
4567 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4568 int verticalDiff = stackBounds.top - bounds.top;
4569 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4570 || (bounds.top + verticalDiff >= maxBottom)) {
4571 verticalDiff = maxBottom - bounds.top;
4572 }
4573 bounds.top += verticalDiff;
4574 bounds.bottom += verticalDiff;
4575 }
4576 }
4577
Craig Mautnercae015f2013-02-08 14:31:27 -08004578 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004579 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4580 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4581 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4582 final ActivityRecord r = activities.get(activityNdx);
4583 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004584 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004585 }
4586 if (r.fullscreen && !r.finishing) {
4587 return false;
4588 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004589 }
4590 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004591 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004592 if (r == null) {
4593 return false;
4594 }
4595 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4596 + " would have returned true for r=" + r);
4597 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004598 }
4599
4600 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004601 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4602 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4603 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4604 final ActivityRecord r = activities.get(activityNdx);
4605 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004606 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004607 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004608 }
4609 }
4610 }
4611
Wale Ogunwale540e1232015-05-01 15:35:39 -07004612 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4613 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004614 boolean didSomething = false;
4615 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004616 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004617 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4618 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4619 int numActivities = activities.size();
4620 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4621 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004622 final boolean sameComponent =
4623 (r.packageName.equals(packageName) && (filterByClasses == null
4624 || filterByClasses.contains(r.realActivity.getClassName())))
4625 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004626 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004627 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004628 && (r.app == null || evenPersistent || !r.app.persistent)) {
4629 if (!doit) {
4630 if (r.finishing) {
4631 // If this activity is just finishing, then it is not
4632 // interesting as far as something to stop.
4633 continue;
4634 }
4635 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004636 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004637 if (r.isHomeActivity()) {
4638 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4639 Slog.i(TAG, "Skip force-stop again " + r);
4640 continue;
4641 } else {
4642 homeActivity = r.realActivity;
4643 }
4644 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004645 didSomething = true;
4646 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004647 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004648 if (r.app != null) {
4649 r.app.removed = true;
4650 }
4651 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004652 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004653 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004654 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4655 true)) {
4656 // r has been deleted from mActivities, accommodate.
4657 --numActivities;
4658 --activityNdx;
4659 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004660 }
4661 }
4662 }
4663 return didSomething;
4664 }
4665
Dianne Hackborn09233282014-04-30 11:33:59 -07004666 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004667 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4668 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004669 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004670 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004671 if (task.getTopActivity() == null) {
4672 continue;
4673 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004674 ActivityRecord r = null;
4675 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004676 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004677 int numActivities = 0;
4678 int numRunning = 0;
4679 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004680 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004681 continue;
4682 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004683 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004684 tmp = activities.get(activityNdx);
4685 if (tmp.finishing) {
4686 continue;
4687 }
4688 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004689
Craig Mautneraab647e2013-02-28 16:31:36 -08004690 // Initialize state for next task if needed.
4691 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4692 top = r;
4693 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004694 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004695
4696 // Add 'r' into the current task.
4697 numActivities++;
4698 if (r.app != null && r.app.thread != null) {
4699 numRunning++;
4700 }
4701
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004702 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004703 TAG, r.intent.getComponent().flattenToShortString()
4704 + ": task=" + r.task);
4705 }
4706
4707 RunningTaskInfo ci = new RunningTaskInfo();
4708 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004709 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004710 ci.baseActivity = r.intent.getComponent();
4711 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004712 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004713 if (focusedStack && topTask) {
4714 // Give the latest time to ensure foreground task can be sorted
4715 // at the first, because lastActiveTime of creating task is 0.
4716 ci.lastActiveTime = System.currentTimeMillis();
4717 topTask = false;
4718 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004719
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004720 if (top.task != null) {
4721 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004722 }
4723 ci.numActivities = numActivities;
4724 ci.numRunning = numRunning;
Winson Chungd3395382016-12-13 11:49:09 -08004725 ci.supportsSplitScreenMultiWindow = task.supportsSplitScreen();
Jorim Jaggi29379ec2016-04-11 23:43:42 -07004726 ci.resizeMode = task.mResizeMode;
Craig Mautneraab647e2013-02-28 16:31:36 -08004727 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004728 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004729 }
4730
Andrii Kulian21713ac2016-10-12 22:05:05 -07004731 void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004732 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004733 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004734 if (top >= 0) {
4735 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4736 int activityTop = activities.size() - 1;
Dianne Hackborn354736e2016-08-22 17:00:05 -07004737 if (activityTop >= 0) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004738 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4739 "unhandled-back", true);
4740 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004741 }
4742 }
4743
Craig Mautner6b74cb52013-09-27 17:02:21 -07004744 /**
4745 * Reset local parameters because an app's activity died.
4746 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004747 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004748 */
4749 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004750 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004751 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004752 "App died while pausing: " + mPausingActivity);
4753 mPausingActivity = null;
4754 }
4755 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4756 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004757 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004758 }
4759
Craig Mautner19091252013-10-05 00:03:53 -07004760 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004761 }
4762
Craig Mautnercae015f2013-02-08 14:31:27 -08004763 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004764 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4765 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4766 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4767 final ActivityRecord r = activities.get(activityNdx);
4768 if (r.app == app) {
4769 Slog.w(TAG, " Force finishing activity "
4770 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004771 // Force the destroy to skip right to removal.
4772 r.app = null;
4773 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004774 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004775 }
4776 }
4777 }
4778
Dianne Hackborn390517b2013-05-30 15:03:32 -07004779 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004780 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004781 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004782 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4783 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004784 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4785 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004786 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004787 " Task id #" + task.taskId + "\n" +
4788 " mFullscreen=" + task.mFullscreen + "\n" +
4789 " mBounds=" + task.mBounds + "\n" +
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004790 " mMinWidth=" + task.mMinWidth + "\n" +
4791 " mMinHeight=" + task.mMinHeight + "\n" +
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004792 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004793 if (printed) {
4794 header = null;
4795 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004796 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004797 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004798 }
4799
4800 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4801 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4802
4803 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004804 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4805 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004806 }
4807 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004808 final int top = mTaskHistory.size() - 1;
4809 if (top >= 0) {
4810 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4811 int listTop = list.size() - 1;
4812 if (listTop >= 0) {
4813 activities.add(list.get(listTop));
4814 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004815 }
4816 } else {
4817 ItemMatcher matcher = new ItemMatcher();
4818 matcher.build(name);
4819
Craig Mautneraab647e2013-02-28 16:31:36 -08004820 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4821 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4822 if (matcher.match(r1, r1.intent.getComponent())) {
4823 activities.add(r1);
4824 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004825 }
4826 }
4827 }
4828
4829 return activities;
4830 }
4831
4832 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004833 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004834
4835 // All activities that came from the package must be
4836 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004837 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4838 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4839 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4840 final ActivityRecord a = activities.get(activityNdx);
4841 if (a.info.packageName.equals(packageName)) {
4842 a.forceNewConfig = true;
4843 if (starting != null && a == starting && a.visible) {
4844 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004845 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004846 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004847 }
4848 }
4849 }
4850
4851 return starting;
4852 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004853
Craig Mautner299f9602015-01-26 09:47:33 -08004854 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07004855 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004856 }
4857
Wale Ogunwale000957c2015-04-03 08:19:12 -07004858 /**
4859 * Removes the input task from this stack.
4860 * @param task to remove.
4861 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07004862 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
4863 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004864 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07004865 void removeTask(TaskRecord task, String reason, int mode) {
4866 if (mode == REMOVE_TASK_MODE_DESTROYING) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004867 task.removeWindowContainer();
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004868 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004869
Craig Mautner04a0ea62014-01-13 12:51:26 -08004870 final ActivityRecord r = mResumedActivity;
4871 if (r != null && r.task == task) {
4872 mResumedActivity = null;
4873 }
4874
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004875 final int taskNdx = mTaskHistory.indexOf(task);
4876 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004877 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4878 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4879 if (!nextTask.isOverHomeStack()) {
4880 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4881 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004882 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004883 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004884 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004885
Wale Ogunwale06579d62016-04-30 15:29:06 -07004886 if (mode == REMOVE_TASK_MODE_DESTROYING && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004887 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004888 final boolean isVoiceSession = task.voiceSession != null;
4889 if (isVoiceSession) {
4890 try {
4891 task.voiceSession.taskFinished(task.intent, task.taskId);
4892 } catch (RemoteException e) {
4893 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004894 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004895 if (task.autoRemoveFromRecents() || isVoiceSession) {
4896 // Task creator asked to remove this when done, or this task was a voice
4897 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004898 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004899 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004900 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004901 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004902
4903 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004904 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07004905 // We only need to adjust focused stack if this stack is in focus and we are not in the
4906 // process of moving the task to the top of the stack that will be focused.
4907 if (isOnHomeDisplay() && mode != REMOVE_TASK_MODE_MOVING_TO_TOP
4908 && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004909 String myReason = reason + " leftTaskHistoryEmpty";
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004910 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004911 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004912 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004913 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004914 if (mStacks != null) {
4915 mStacks.remove(this);
4916 mStacks.add(0, this);
4917 }
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004918 if (!isHomeOrRecentsStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004919 mActivityContainer.onTaskListEmptyLocked();
4920 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004921 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004922
Andrii Kulian02b7a832016-10-06 23:11:56 -07004923 task.setStack(null);
Craig Mautner0247fc82013-02-28 14:32:06 -08004924 }
4925
Dianne Hackborn91097de2014-04-04 18:02:06 -07004926 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4927 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Wale Ogunwale72919d22016-12-08 18:58:50 -08004928 boolean toTop, int type) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004929 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
Wale Ogunwale72919d22016-12-08 18:58:50 -08004930 voiceInteractor, type);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004931 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004932 addTask(task, toTop, "createTaskRecord");
Jorim Jaggife762342016-10-13 14:33:27 +02004933 final boolean isLockscreenShown =
4934 mService.mStackSupervisor.mKeyguardController.isKeyguardShowing();
Andrii Kulian2e751b82016-03-16 16:59:32 -07004935 if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004936 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004937 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004938 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004939 task.createWindowContainer(toTop, (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004940 return task;
4941 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004942
Andrii Kulian2e751b82016-03-16 16:59:32 -07004943 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08004944 if (mTaskPositioner == null) {
4945 return false;
4946 }
Andrii Kulian2e751b82016-03-16 16:59:32 -07004947 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
Wale Ogunwale935e5022015-11-10 12:36:10 -08004948 return true;
4949 }
4950
Craig Mautnerc00204b2013-03-05 15:02:14 -08004951 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004952 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004953 }
4954
Wale Ogunwale5f986092015-12-04 15:35:38 -08004955 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004956 addTask(task, toTop ? MAX_VALUE : 0, reason);
4957 if (toTop) {
4958 // TODO: figure-out a way to remove this call.
Wale Ogunwale1666e312016-12-16 11:27:18 -08004959 mWindowContainerController.positionChildAtTop(task.getWindowContainerController(),
4960 true /* includingParents */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004961 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004962 }
4963
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004964 // TODO: This shouldn't allow automatic reparenting. Remove the call to preAddTask and deal
4965 // with the fall-out...
4966 void addTask(final TaskRecord task, int position, String reason) {
4967 // TODO: Is this remove really needed? Need to look into the call path for the other addTask
4968 mTaskHistory.remove(task);
4969 position = getAdjustedPositionForTask(task, position, null /* starting */);
4970 final boolean toTop = position >= mTaskHistory.size();
Wale Ogunwale06579d62016-04-30 15:29:06 -07004971 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004972
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004973 mTaskHistory.add(position, task);
Andrii Kulian02b7a832016-10-06 23:11:56 -07004974 task.setStack(this);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004975
Craig Mautnerc00204b2013-03-05 15:02:14 -08004976 if (toTop) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004977 updateTaskReturnToForTopInsertion(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004978 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004979
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004980 updateTaskMovement(task, toTop);
4981
Wale Ogunwale5f986092015-12-04 15:35:38 -08004982 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004983 }
4984
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004985 void positionChildAt(TaskRecord task, int index) {
4986
4987 if (task.getStack() != this) {
4988 throw new IllegalArgumentException("AS.positionChildAt: task=" + task
4989 + " is not a child of stack=" + this + " current parent=" + task.getStack());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004990 }
4991
4992 task.updateOverrideConfigurationForStack(this);
4993
Jorim Jaggi023da532016-04-20 22:42:32 -07004994 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07004995 final boolean wasResumed = topRunningActivity == task.getStack().mResumedActivity;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004996 insertTaskAtPosition(task, index);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004997 task.setStack(this);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08004998 postAddTask(task, null /* prevStack */);
4999
Jorim Jaggi023da532016-04-20 22:42:32 -07005000 if (wasResumed) {
5001 if (mResumedActivity != null) {
5002 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5003 + " other stack to this stack mResumedActivity=" + mResumedActivity
5004 + " other mResumedActivity=" + topRunningActivity);
5005 }
5006 mResumedActivity = topRunningActivity;
5007 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005008
5009 // The task might have already been running and its visibility needs to be synchronized with
5010 // the visibility of the stack / windows.
5011 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
5012 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005013 }
5014
Wale Ogunwale06579d62016-04-30 15:29:06 -07005015 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005016 final ActivityStack prevStack = task.getStack();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005017 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005018 prevStack.removeTask(task, reason,
5019 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005020 }
5021 return prevStack;
5022 }
5023
5024 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
5025 if (prevStack != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07005026 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005027 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005028 try {
5029 task.voiceSession.taskStarted(task.intent, task.taskId);
5030 } catch (RemoteException e) {
5031 }
5032 }
5033 }
5034
Winson Chungc2baac02017-01-11 13:34:47 -08005035 void moveToFrontAndResumeStateIfNeeded(ActivityRecord r, boolean moveToFront, boolean setResume,
5036 boolean setPause, String reason) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08005037 if (!moveToFront) {
5038 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005039 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005040
5041 // If the activity owns the last resumed activity, transfer that together,
5042 // so that we don't resume the same activity again in the new stack.
5043 // Apps may depend on onResume()/onPause() being called in pairs.
5044 if (setResume) {
5045 mResumedActivity = r;
5046 }
Winson Chungc2baac02017-01-11 13:34:47 -08005047 // If the activity was previously pausing, then ensure we transfer that as well
5048 if (setPause) {
5049 mPausingActivity = r;
5050 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005051 // Move the stack in which we are placing the activity to the front. The call will also
5052 // make sure the activity focus is set.
5053 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005054 }
5055
5056 /**
5057 * Moves the input activity from its current stack to this one.
5058 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
5059 * created on this stack which the activity is added to.
5060 * */
5061 void moveActivityToStack(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005062 final ActivityStack prevStack = r.getStack();
Wale Ogunwale079a0042015-10-24 11:44:07 -07005063 if (prevStack.mStackId == mStackId) {
5064 // You are already in the right stack silly...
5065 return;
5066 }
5067
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07005068 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07005069 && (mStackSupervisor.topRunningActivityLocked() == r);
5070 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
Winson Chungc2baac02017-01-11 13:34:47 -08005071 final boolean wasPaused = prevStack.mPausingActivity == r;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005072
Winson Chung30480042017-01-26 10:55:34 -08005073 // Create a new task for the activity to be parented in
Wale Ogunwale079a0042015-10-24 11:44:07 -07005074 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08005075 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
Wale Ogunwale72919d22016-12-08 18:58:50 -08005076 r.info, r.intent, null, null, true, r.mActivityType);
Winson Chung30480042017-01-26 10:55:34 -08005077 // This is a new task, so reparenting it to position 0 will move it to the top
5078 r.reparent(task, 0 /* position */, "moveActivityToStack");
5079
5080 // Notify the task actiivties if it was moved to/from a pinned stack
Andrii Kulian933076d2016-03-29 17:04:42 -07005081 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Winson Chung30480042017-01-26 10:55:34 -08005082
5083 // Resume the activity if necessary after it has moved
Winson Chungc2baac02017-01-11 13:34:47 -08005084 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, wasPaused,
5085 "moveActivityToStack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08005086 if (wasResumed) {
5087 prevStack.mResumedActivity = null;
5088 }
Winson Chungc2baac02017-01-11 13:34:47 -08005089 if (wasPaused) {
5090 prevStack.mPausingActivity = null;
5091 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07005092 }
5093
Craig Mautnerc00204b2013-03-05 15:02:14 -08005094 public int getStackId() {
5095 return mStackId;
5096 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005097
5098 @Override
5099 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005100 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5101 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005102 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005103
Craig Mautner15df08a2015-04-01 12:17:18 -07005104 void onLockTaskPackagesUpdatedLocked() {
5105 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5106 mTaskHistory.get(taskNdx).setLockTaskAuth();
5107 }
5108 }
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005109
5110 void executeAppTransition(ActivityOptions options) {
5111 mWindowManager.executeAppTransition();
5112 mNoAnimActivities.clear();
5113 ActivityOptions.abort(options);
5114 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005115}