blob: 9cae38237b980fb254867b5ebafc512d99573224 [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_ORIENTATION;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
27import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
28import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080029import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070030import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080031import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070032import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
33import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
34import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_APP;
35import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CLEANUP;
36import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
37import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
38import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
39import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
40import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
41import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RESULTS;
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TRANSITION;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_USER_LEAVING;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
51import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
52import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_APP;
53import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CLEANUP;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RESULTS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TRANSITION;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_USER_LEAVING;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
68import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
69import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Chong Zhang75b37202015-12-04 14:16:36 -080070import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070071import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070072import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -080073import static com.android.server.am.ActivityRecord.STARTING_WINDOW_REMOVED;
74import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwale39381972015-12-17 17:15:29 -080075import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Wale Ogunwale06579d62016-04-30 15:29:06 -070076import static com.android.server.am.ActivityStackSupervisor.ON_TOP;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070077import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070078import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_CLOSE;
79import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_OPEN;
80import static com.android.server.wm.AppTransition.TRANSIT_NONE;
81import static com.android.server.wm.AppTransition.TRANSIT_TASK_CLOSE;
82import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN;
83import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN_BEHIND;
84import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_BACK;
85import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_FRONT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
87import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070088import android.app.ActivityManager;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070089import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070090import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070091import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080093import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094import android.app.ResultInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070095import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070097import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -080098import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070099import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800100import android.graphics.Bitmap;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700101import android.graphics.Point;
102import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -0800103import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700104import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700105import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -0800106import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700107import android.os.Handler;
108import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +0900109import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700110import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -0700111import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112import android.os.RemoteException;
113import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -0800114import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700115import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700116import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700117import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118import android.util.EventLog;
Jorim Jaggi023da532016-04-20 22:42:32 -0700119import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -0700121import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700122
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700123import com.android.internal.app.IVoiceInteractor;
124import com.android.internal.content.ReferrerIntent;
125import com.android.internal.os.BatteryStatsImpl;
126import com.android.server.Watchdog;
127import com.android.server.am.ActivityManagerService.ItemMatcher;
128import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
129import com.android.server.wm.TaskGroup;
130import com.android.server.wm.WindowManagerService;
131
Craig Mautnercae015f2013-02-08 14:31:27 -0800132import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800133import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134import java.lang.ref.WeakReference;
135import java.util.ArrayList;
136import java.util.Iterator;
137import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800138import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700139import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140
141/**
142 * State and management of a single stack of activities.
143 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700144final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800145
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800146 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700147 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
148 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800149 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700150 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700152 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700154 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700155 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
156 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700157 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
160 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
161 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
162 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
163 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700164
165 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800166
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700167 // Ticks during which we check progress while waiting for an app to launch.
168 static final int LAUNCH_TICK = 500;
169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 // How long we wait until giving up on the last activity to pause. This
171 // is short because it directly impacts the responsiveness of starting the
172 // next activity.
173 static final int PAUSE_TIMEOUT = 500;
174
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700175 // How long we wait for the activity to tell us it has stopped before
176 // giving up. This is a good amount of time because we really need this
177 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700178 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180 // How long we wait until giving up on an activity telling us it has
181 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700182 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800185 // disabled.
186 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800187
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700188 // How long between activity launches that we consider safe to not warn
189 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700190 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 // Set to false to disable the preview that is shown while a new activity
193 // is being started.
194 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800195
Craig Mautner5eda9b32013-07-02 11:58:16 -0700196 // How long to wait for all background Activities to redraw following a call to
197 // convertToTranslucent().
198 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
199
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800200 // How many activities have to be scheduled to stop to force a stop pass.
201 private static final int MAX_STOPPING_TO_FORCE = 3;
202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 enum ActivityState {
204 INITIALIZING,
205 RESUMED,
206 PAUSING,
207 PAUSED,
208 STOPPING,
209 STOPPED,
210 FINISHING,
211 DESTROYING,
212 DESTROYED
213 }
214
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800215 // Stack is not considered visible.
216 static final int STACK_INVISIBLE = 0;
217 // Stack is considered visible
218 static final int STACK_VISIBLE = 1;
219 // Stack is considered visible, but only becuase it has activity that is visible behind other
220 // activities and there is a specific combination of stacks.
221 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
222
Wale Ogunwale06579d62016-04-30 15:29:06 -0700223 /* The various modes for the method {@link #removeTask}. */
224 // Task is being completely removed from all stacks in the system.
225 static final int REMOVE_TASK_MODE_DESTROYING = 0;
226 // Task is being removed from this stack so we can add it to another stack. In the case we are
227 // moving we don't want to perform some operations on the task like removing it from window
228 // manager or recents.
229 static final int REMOVE_TASK_MODE_MOVING = 1;
230 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
231 // stack and the new stack will be on top of all stacks.
232 static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700234 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700235 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800236 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800237
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700238 /**
239 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800240 * running) activities. It contains #TaskRecord objects.
241 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800242 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800243
244 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800245 * Used for validating app tokens with window manager.
246 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800247 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249 /**
250 * List of running activities, sorted by recent usage.
251 * The first entry in the list is the least recently used.
252 * It contains HistoryRecord objects.
253 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800254 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700255
256 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700257 * Animations that for the current transition have requested not to
258 * be considered for the transition animation.
259 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800260 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700261
262 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700263 * When we are in the process of pausing an activity, before starting the
264 * next one, this variable holds the activity that is currently being paused.
265 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800266 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267
268 /**
269 * This is the last activity that we put into the paused state. This is
270 * used to determine if we need to do an activity transition while sleeping,
271 * when we normally hold the top activity paused.
272 */
273 ActivityRecord mLastPausedActivity = null;
274
275 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700276 * Activities that specify No History must be removed once the user navigates away from them.
277 * If the device goes to sleep with such an activity in the paused state then we save it here
278 * and finish it later if another activity replaces it on wakeup.
279 */
280 ActivityRecord mLastNoHistoryActivity = null;
281
282 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700283 * Current activity that is resumed, or null if there is none.
284 */
285 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800286
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700288 * This is the last activity that has been started. It is only used to
289 * identify when multiple activities are started at once so that the user
290 * can be warned they may not be in the activity they think they are.
291 */
292 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800293
Craig Mautner5eda9b32013-07-02 11:58:16 -0700294 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
295 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
296 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
297 // Activity in mTranslucentActivityWaiting is notified via
298 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
299 // background activity being drawn then the same call will be made with a true value.
300 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700301 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700302
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700303 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 * Set when we know we are going to be calling updateConfiguration()
305 * soon, so want to skip intermediate config checks.
306 */
307 boolean mConfigWillChange;
308
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700309 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800310 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700311 // Current bounds of the stack or null if fullscreen.
312 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800313
Jorim Jaggi192086e2016-03-11 17:17:03 +0100314 boolean mUpdateBoundsDeferred;
315 boolean mUpdateBoundsDeferredCalled;
316 final Rect mDeferredBounds = new Rect();
317 final Rect mDeferredTaskBounds = new Rect();
318 final Rect mDeferredTaskInsetBounds = new Rect();
319
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700320 long mLaunchStartTime = 0;
321 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800322
Craig Mautner858d8a62013-04-23 17:08:34 -0700323 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700324
Craig Mautnerc00204b2013-03-05 15:02:14 -0800325 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800327 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
328 ArrayList<ActivityStack> mStacks;
329 /** The attached Display's unique identifier, or -1 if detached */
330 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800331
Craig Mautner27084302013-03-25 08:05:25 -0700332 /** Run all ActivityStacks through this */
333 final ActivityStackSupervisor mStackSupervisor;
334
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700335 private final LaunchingTaskPositioner mTaskPositioner;
336
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700337 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700338 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
339 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
340 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
341 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700342 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700343 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700344 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700345
346 static class ScheduleDestroyArgs {
347 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700348 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700349 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700350 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700351 mReason = reason;
352 }
353 }
354
Zoran Marcetaf958b322012-08-09 20:27:12 +0900355 final Handler mHandler;
356
357 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800358
Craig Mautnerc8143c62013-09-03 12:15:57 -0700359 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900360 super(looper);
361 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362
Zoran Marcetaf958b322012-08-09 20:27:12 +0900363 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700364 public void handleMessage(Message msg) {
365 switch (msg.what) {
366 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800367 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700368 // We don't at this point know if the activity is fullscreen,
369 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800370 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700371 synchronized (mService) {
372 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700373 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700374 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700375 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800376 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700377 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700378 case LAUNCH_TICK_MSG: {
379 ActivityRecord r = (ActivityRecord)msg.obj;
380 synchronized (mService) {
381 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700382 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700383 }
384 }
385 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700386 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800387 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700388 // We don't at this point know if the activity is fullscreen,
389 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800390 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800391 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800392 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800393 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700395 case STOP_TIMEOUT_MSG: {
396 ActivityRecord r = (ActivityRecord)msg.obj;
397 // We don't at this point know if the activity is fullscreen,
398 // so we need to be conservative and assume it isn't.
399 Slog.w(TAG, "Activity stop timeout for " + r);
400 synchronized (mService) {
401 if (r.isInHistory()) {
402 activityStoppedLocked(r, null, null, null);
403 }
404 }
405 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700406 case DESTROY_ACTIVITIES_MSG: {
407 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
408 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700409 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700410 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700411 } break;
412 case TRANSLUCENT_TIMEOUT_MSG: {
413 synchronized (mService) {
414 notifyActivityDrawnLocked(null);
415 }
416 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700417 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700418 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700419 final ActivityRecord r = getVisibleBehindActivity();
420 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700421 if (r != null) {
422 mService.killAppAtUsersRequest(r.app, null);
423 }
424 }
425 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700426 }
427 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800428 }
429
Craig Mautner34b73df2014-01-12 21:11:08 -0800430 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800431 int count = 0;
432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
433 count += mTaskHistory.get(taskNdx).mActivities.size();
434 }
435 return count;
436 }
437
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800438 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
439 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800440 mActivityContainer = activityContainer;
441 mStackSupervisor = activityContainer.getOuter();
442 mService = mStackSupervisor.mService;
443 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
444 mWindowManager = mService.mWindowManager;
445 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700446 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800447 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700448 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
449 ? new LaunchingTaskPositioner() : null;
450 }
451
452 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
453 mDisplayId = activityDisplay.mDisplayId;
454 mStacks = activityDisplay.mStacks;
455 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
456 mFullscreen = mBounds == null;
457 if (mTaskPositioner != null) {
458 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
459 mTaskPositioner.configure(mBounds);
460 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800461
462 if (mStackId == DOCKED_STACK_ID) {
463 // If we created a docked stack we want to resize it so it resizes all other stacks
464 // in the system.
465 mStackSupervisor.resizeDockedStackLocked(
466 mBounds, null, null, null, null, PRESERVE_WINDOWS);
467 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700468 }
469
470 void detachDisplay() {
471 mDisplayId = Display.INVALID_DISPLAY;
472 mStacks = null;
473 if (mTaskPositioner != null) {
474 mTaskPositioner.reset();
475 }
476 mWindowManager.detachStack(mStackId);
Jorim Jaggi899327f2016-02-25 20:44:18 -0500477 if (mStackId == DOCKED_STACK_ID) {
478 // If we removed a docked stack we want to resize it so it resizes all other stacks
479 // in the system to fullscreen.
480 mStackSupervisor.resizeDockedStackLocked(
481 null, null, null, null, null, PRESERVE_WINDOWS);
482 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700483 }
484
Winsonc809cbb2015-11-02 12:06:15 -0800485 public void getDisplaySize(Point out) {
486 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
487 }
488
Jorim Jaggi192086e2016-03-11 17:17:03 +0100489 /**
490 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
491 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
492 */
493 void deferUpdateBounds() {
494 if (!mUpdateBoundsDeferred) {
495 mUpdateBoundsDeferred = true;
496 mUpdateBoundsDeferredCalled = false;
497 }
498 }
499
500 /**
501 * Continues updating bounds after updates have been deferred. If there was a resize attempt
502 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
503 * be resized to that bounds.
504 */
505 void continueUpdateBounds() {
506 final boolean wasDeferred = mUpdateBoundsDeferred;
507 mUpdateBoundsDeferred = false;
508 if (wasDeferred && mUpdateBoundsDeferredCalled) {
509 mStackSupervisor.resizeStackUncheckedLocked(this,
510 mDeferredBounds.isEmpty() ? null : mDeferredBounds,
511 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
512 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
513 }
514 }
515
516 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
517 Rect tempTaskInsetBounds) {
518 if (!mUpdateBoundsDeferred) {
519 return true;
520 }
521 if (bounds != null) {
522 mDeferredBounds.set(bounds);
523 } else {
524 mDeferredBounds.setEmpty();
525 }
526 if (tempTaskBounds != null) {
527 mDeferredTaskBounds.set(tempTaskBounds);
528 } else {
529 mDeferredTaskBounds.setEmpty();
530 }
531 if (tempTaskInsetBounds != null) {
532 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
533 } else {
534 mDeferredTaskInsetBounds.setEmpty();
535 }
536 mUpdateBoundsDeferredCalled = true;
537 return false;
538 }
539
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700540 void setBounds(Rect bounds) {
541 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700542 if (mTaskPositioner != null) {
543 mTaskPositioner.configure(bounds);
544 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700545 }
Craig Mautner5962b122012-10-05 14:45:52 -0700546
Amith Yamasani734983f2014-03-04 16:48:05 -0800547 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700548 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700549 }
550
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700551 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800552 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700553 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700554 if (r != null) {
555 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800556 }
557 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700558 return null;
559 }
560
561 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800562 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
563 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800564 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800565 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
566 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800567 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800568 return r;
569 }
570 }
571 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700572 return null;
573 }
574
575 /**
576 * This is a simplified version of topRunningActivityLocked that provides a number of
577 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800578 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700579 * @param token If non-null, any history records matching this token will be skipped.
580 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800581 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700582 * @return Returns the HistoryRecord of the next activity on the stack.
583 */
584 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800585 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
586 TaskRecord task = mTaskHistory.get(taskNdx);
587 if (task.taskId == taskId) {
588 continue;
589 }
590 ArrayList<ActivityRecord> activities = task.mActivities;
591 for (int i = activities.size() - 1; i >= 0; --i) {
592 final ActivityRecord r = activities.get(i);
593 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800594 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800595 return r;
596 }
597 }
598 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700599 return null;
600 }
601
Craig Mautner8849a5e2013-04-02 16:41:03 -0700602 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700603 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
604 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700605 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
606 final ActivityRecord r = activities.get(activityNdx);
607 if (!r.finishing) {
608 return r;
609 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700610 }
611 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700612 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700613 }
614
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700615 final TaskRecord topTask() {
616 final int size = mTaskHistory.size();
617 if (size > 0) {
618 return mTaskHistory.get(size - 1);
619 }
620 return null;
621 }
622
Craig Mautnerd2328952013-03-05 12:46:26 -0800623 TaskRecord taskForIdLocked(int id) {
624 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
625 final TaskRecord task = mTaskHistory.get(taskNdx);
626 if (task.taskId == id) {
627 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800628 }
629 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700630 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700631 }
632
Craig Mautnerd2328952013-03-05 12:46:26 -0800633 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700634 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800635 return isInStackLocked(r);
636 }
637
638 ActivityRecord isInStackLocked(ActivityRecord r) {
639 if (r == null) {
640 return null;
641 }
642 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700643 if (task != null && task.stack != null
644 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800645 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800646 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800647 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800648 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800649 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800650 }
651
Craig Mautner2420ead2013-04-01 17:13:20 -0700652 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700653 final boolean hadit = mLRUActivities.remove(r);
654 mLRUActivities.add(r);
655 return hadit;
656 }
657
Craig Mautnerde4ef022013-04-07 19:01:33 -0700658 final boolean isHomeStack() {
659 return mStackId == HOME_STACK_ID;
660 }
661
Winson Chung0583d3d2015-12-18 10:03:12 -0500662 final boolean isDockedStack() {
663 return mStackId == DOCKED_STACK_ID;
664 }
665
666 final boolean isPinnedStack() {
667 return mStackId == PINNED_STACK_ID;
668 }
669
Craig Mautnere0a38842013-12-16 16:14:02 -0800670 final boolean isOnHomeDisplay() {
671 return isAttached() &&
672 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
673 }
674
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700675 void moveToFront(String reason) {
676 moveToFront(reason, null);
677 }
678
679 /**
680 * @param reason The reason for moving the stack to the front.
681 * @param task If non-null, the task will be moved to the top of the stack.
682 * */
683 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700684 if (!isAttached()) {
685 return;
686 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700687
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700688 mStacks.remove(this);
689 int addIndex = mStacks.size();
690
691 if (addIndex > 0) {
692 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800693 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
694 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700695 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800696 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700697 }
698
699 mStacks.add(addIndex, this);
700
701 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
702 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800703 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700704 }
705 if (task != null) {
706 insertTaskAtTop(task, null);
707 } else {
708 task = topTask();
709 }
710 if (task != null) {
711 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 }
714
Jiaquan Hee13e9642016-06-15 15:16:13 -0700715 /**
716 * @param task If non-null, the task will be moved to the back of the stack.
717 * */
718 void moveToBack(TaskRecord task) {
719 if (!isAttached()) {
720 return;
721 }
722
723 mStacks.remove(this);
724 mStacks.add(0, this);
725
726 if (task != null) {
727 mTaskHistory.remove(task);
728 mTaskHistory.add(0, task);
729 updateTaskMovement(task, false);
730 mWindowManager.moveTaskToBottom(task.taskId);
731 }
732 }
733
Wale Ogunwaled046a012015-12-24 13:05:59 -0800734 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800735 if (StackId.canReceiveKeys(mStackId)) {
736 return true;
737 }
738 // The stack isn't focusable. See if its top activity is focusable to force focus on the
739 // stack.
740 final ActivityRecord r = topRunningActivityLocked();
741 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800742 }
743
Craig Mautnere0a38842013-12-16 16:14:02 -0800744 final boolean isAttached() {
745 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 }
747
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700748 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800749 * Returns the top activity in any existing task matching the given Intent in the input result.
750 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700751 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800752 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700753 Intent intent = target.intent;
754 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 ComponentName cls = intent.getComponent();
756 if (info.targetActivity != null) {
757 cls = new ComponentName(info.packageName, info.targetActivity);
758 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700759 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700760 boolean isDocument = intent != null & intent.isDocument();
761 // If documentData is non-null then it must match the existing task data.
762 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800763
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700764 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800765 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
766 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700767 if (task.voiceSession != null) {
768 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700769 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700770 continue;
771 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700772 if (task.userId != userId) {
773 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700774 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700775 continue;
776 }
Craig Mautner000f0022013-02-26 15:04:29 -0800777 final ActivityRecord r = task.getTopActivity();
778 if (r == null || r.finishing || r.userId != userId ||
779 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700780 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800781 continue;
782 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700783 if (r.mActivityType != target.mActivityType) {
784 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
785 continue;
786 }
Craig Mautner000f0022013-02-26 15:04:29 -0800787
Craig Mautnerd00f4742014-03-12 14:17:26 -0700788 final Intent taskIntent = task.intent;
789 final Intent affinityIntent = task.affinityIntent;
790 final boolean taskIsDocument;
791 final Uri taskDocumentData;
792 if (taskIntent != null && taskIntent.isDocument()) {
793 taskIsDocument = true;
794 taskDocumentData = taskIntent.getData();
795 } else if (affinityIntent != null && affinityIntent.isDocument()) {
796 taskIsDocument = true;
797 taskDocumentData = affinityIntent.getData();
798 } else {
799 taskIsDocument = false;
800 taskDocumentData = null;
801 }
802
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700803 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700804 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700805 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700806 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700807 // TODO Refactor to remove duplications. Check if logic can be simplified.
808 if (taskIntent != null && taskIntent.getComponent() != null &&
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700809 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700810 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700811 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800812 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700813 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
814 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800815 result.r = r;
816 result.matchedByRootAffinity = false;
817 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700818 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
819 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700820 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700821 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800822 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700823 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
824 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800825 result.r = r;
826 result.matchedByRootAffinity = false;
827 break;
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700828 } else if (!isDocument && !taskIsDocument
829 && result.r == null && task.canMatchRootAffinity()) {
830 if (task.rootAffinity.equals(target.taskAffinity)) {
831 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
832 // It is possible for multiple tasks to have the same root affinity especially
833 // if they are in separate stacks. We save off this candidate, but keep looking
834 // to see if there is a better candidate.
835 result.r = r;
836 result.matchedByRootAffinity = true;
837 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700838 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 }
841
842 /**
843 * Returns the first activity (starting from the top of the stack) that
844 * is the same as the given activity. Returns null if no such activity
845 * is found.
846 */
Andrii Kuliand3bbb132016-06-16 16:00:20 -0700847 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
848 boolean compareIntentFilters) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 ComponentName cls = intent.getComponent();
850 if (info.targetActivity != null) {
851 cls = new ComponentName(info.packageName, info.targetActivity);
852 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700853 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854
Craig Mautner000f0022013-02-26 15:04:29 -0800855 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700856 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700857 final boolean notCurrentUserTask =
858 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700859 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700860
Craig Mautner000f0022013-02-26 15:04:29 -0800861 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
862 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700863 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700864 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700865 }
Andrii Kuliand3bbb132016-06-16 16:00:20 -0700866 if (!r.finishing && r.userId == userId) {
867 if (compareIntentFilters) {
868 if (r.intent.filterEquals(intent)) {
869 return r;
870 }
871 } else {
872 if (r.intent.getComponent().equals(cls)) {
873 return r;
874 }
875 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700876 }
877 }
878 }
879
880 return null;
881 }
882
Amith Yamasani742a6712011-05-04 14:49:28 -0700883 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700884 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700885 */
Craig Mautner93529a42013-10-04 15:03:13 -0700886 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800887 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700888 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800889 }
890 mCurrentUser = userId;
891
892 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800893 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800894 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700895 final TaskRecord task = mTaskHistory.get(i);
896
897 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
898 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700899 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700900 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700901 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700902 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800903 mTaskHistory.remove(i);
904 mTaskHistory.add(task);
905 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800906 // Use same value for i.
907 } else {
908 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800909 }
910 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700911 if (VALIDATE_TOKENS) {
912 validateAppTokensLocked();
913 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700914 }
915
Craig Mautner2420ead2013-04-01 17:13:20 -0700916 void minimalResumeActivityLocked(ActivityRecord r) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800917 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
918 + " callers=" + Debug.getCallers(5));
Chong Zhang6cda19c2016-06-14 19:07:56 -0700919 setResumedActivityLocked(r, "minimalResumeActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -0700920 completeResumeLocked(r);
Craig Mautner1e8b8722013-10-14 18:24:52 -0700921 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700922 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
923 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700924 }
925
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800926 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700927 if (r != null) {
928 mRecentTasks.addLocked(r.task);
929 r.task.touchActiveTime();
930 }
931 }
932
Narayan Kamath7829c812015-06-08 17:39:43 +0100933 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700934 if (mFullyDrawnStartTime != 0) {
935 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
936 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100937 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700938 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
939 }
940
941 private void stopFullyDrawnTraceIfNeeded() {
942 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
943 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
944 mFullyDrawnStartTime = 0;
945 }
946 }
947
Craig Mautnere79d42682013-04-01 19:01:53 -0700948 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700949 if (r.displayStartTime == 0) {
950 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
951 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100952 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700953 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700954 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700955 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100956 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700957 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700958 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700959 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800960
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700961 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700962 // Make sure that there is no activity waiting for this to launch.
963 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
964 r.displayStartTime = r.fullyDrawnStartTime = 0;
965 } else {
966 mStackSupervisor.removeTimeoutsForActivityLocked(r);
967 mStackSupervisor.scheduleIdleTimeoutLocked(r);
968 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700969 }
970
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800971 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800972 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800973 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
974 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
975 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
976 activities.get(activityNdx).setSleeping(false);
977 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800978 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800979 if (mPausingActivity != null) {
980 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
981 activityPausedLocked(mPausingActivity.appToken, true);
982 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800983 }
984
Todd Kennedy39bfee52016-02-24 10:28:21 -0800985 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
986 final String packageName = aInfo.packageName;
987 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
988 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
989 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
990 if (packageName.equals(activities.get(activityNdx).packageName)) {
991 activities.get(activityNdx).info.applicationInfo = aInfo;
992 }
993 }
994 }
995 }
996
Craig Mautner0eea92c2013-05-16 13:35:39 -0700997 /**
998 * @return true if something must be done before going to sleep.
999 */
1000 boolean checkReadyForSleepLocked() {
1001 if (mResumedActivity != null) {
1002 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001003 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
1004 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1005 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001006 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001007 return true;
1008 }
1009 if (mPausingActivity != null) {
1010 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001011 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001012 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001013 }
1014
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001015 if (hasVisibleBehindActivity()) {
1016 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001017 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001018 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001019 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001020 return true;
1021 }
1022
Craig Mautner0eea92c2013-05-16 13:35:39 -07001023 return false;
1024 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001025
Craig Mautner0eea92c2013-05-16 13:35:39 -07001026 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001027 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001028
Andrii Kulianf9949d52016-05-06 12:53:25 -07001029 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001030 // This ensures that the activity's onStop() is called.
1031 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1032 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1033 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1034 final ActivityRecord r = activities.get(activityNdx);
Andrii Kulianf9949d52016-05-06 12:53:25 -07001035 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED
1036 || r.state == ActivityState.PAUSED || r.state == ActivityState.PAUSING) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001037 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001038 }
1039 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001040 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 }
Craig Mautner59c00972012-07-30 12:10:24 -07001042
Winson8b1871d2015-11-20 09:56:20 -08001043 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
1044 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001045 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001046 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001047 return null;
1048 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001049
Winson Chung083baf92014-07-11 10:32:42 -07001050 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -07001051 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001052 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001053 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -07001054 return null;
1055 }
1056
Winson Chung48a10a52014-08-27 14:36:51 -07001057 int w = mService.mThumbnailWidth;
1058 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -08001059 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001060 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001061 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -08001062
1063 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -08001064 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
1065 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -05001066 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -08001067 w = h = -1;
Winson21700932016-03-24 17:26:23 -07001068 scale = mService.mFullscreenThumbnailScale;
Winson8b1871d2015-11-20 09:56:20 -08001069 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001070 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -08001071 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001072 }
Winson Chung376543b2014-05-21 17:43:28 -07001073 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001074 return null;
1075 }
1076
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001077 /**
1078 * Start pausing the currently resumed activity. It is an error to call this if there
1079 * is already an activity being paused or there is no resumed activity.
1080 *
1081 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1082 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1083 * screen turning off).
1084 * @param resuming True if this is being called as part of resuming the top activity, so
1085 * we shouldn't try to instigate a resume here.
1086 * @param dontWait True if the caller does not want to wait for the pause to complete. If
1087 * set to true, we will immediately complete the pause here before returning.
1088 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1089 * it to tell us when it is done.
1090 */
1091 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
1092 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001093 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001094 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1095 + " state=" + mPausingActivity.state);
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001096 if (!mService.isSleepingLocked()) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001097 // Avoid recursion among check for sleep and complete pause during sleeping.
1098 // Because activity will be paused immediately after resume, just let pause
1099 // be completed by the order of activity paused from clients.
1100 completePauseLocked(false);
1101 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001102 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001103 ActivityRecord prev = mResumedActivity;
1104 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001105 if (!resuming) {
1106 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001107 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001108 }
1109 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001110 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001111
1112 if (mActivityContainer.mParentActivity == null) {
1113 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001114 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001115 }
1116
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001117 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001118 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001119 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001120 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001121 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001122 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1123 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001124 prev.state = ActivityState.PAUSING;
1125 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001126 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001127 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -07001128 if (mService.mHasRecents
1129 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001130 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001131 }
Dianne Hackborncee04b52013-07-03 17:01:28 -07001132 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001133
1134 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001136 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001137 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 try {
1139 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001140 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001141 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001142 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001143 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001144 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001145 } catch (Exception e) {
1146 // Ignore exception, if process died other code will cleanup.
1147 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001148 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001149 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001150 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001151 }
1152 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001153 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001154 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001155 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 }
1157
1158 // If we are not going to sleep, we want to ensure the device is
1159 // awake until the next activity is started.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001160 if (!uiSleeping && !mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001161 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001162 }
1163
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001164 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001165 // Have the window manager pause its key dispatching until the new
1166 // activity has started. If we're pausing the activity just because
1167 // the screen is being turned off and the UI is sleeping, don't interrupt
1168 // key dispatch; the same activity will pick it up again on wakeup.
1169 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001170 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001171 } else if (DEBUG_PAUSE) {
1172 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 }
1174
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001175 if (dontWait) {
1176 // If the caller said they don't want to wait for the pause, then complete
1177 // the pause now.
1178 completePauseLocked(false);
1179 return false;
1180
1181 } else {
1182 // Schedule a pause timeout in case the app doesn't respond.
1183 // We don't give it much time because this directly impacts the
1184 // responsiveness seen by the user.
1185 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1186 msg.obj = prev;
1187 prev.pauseTime = SystemClock.uptimeMillis();
1188 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001189 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001190 return true;
1191 }
1192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193 } else {
1194 // This activity failed to schedule the
1195 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001196 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001197 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001198 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001199 }
1200 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001201 }
1202 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001203
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001204 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001205 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1206 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207
Craig Mautnerd2328952013-03-05 12:46:26 -08001208 final ActivityRecord r = isInStackLocked(token);
1209 if (r != null) {
1210 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1211 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001212 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001213 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001214 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001215 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001216 } else {
1217 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1218 r.userId, System.identityHashCode(r), r.shortComponentName,
1219 mPausingActivity != null
1220 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001221 if (r.state == ActivityState.PAUSING) {
1222 r.state = ActivityState.PAUSED;
1223 if (r.finishing) {
1224 if (DEBUG_PAUSE) Slog.v(TAG,
1225 "Executing finish of failed to pause activity: " + r);
1226 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1227 }
louis_chang047dfd42015-04-08 16:35:55 +08001228 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001229 }
1230 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001231 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001232 }
1233
Chong Zhangd78ddb42016-03-02 17:01:14 -08001234 final void activityResumedLocked(IBinder token) {
1235 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1236 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1237 r.icicle = null;
1238 r.haveState = false;
1239 }
1240
Craig Mautnera0026042014-04-23 11:45:37 -07001241 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1242 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001243 if (r.state != ActivityState.STOPPING) {
1244 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1245 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1246 return;
1247 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001248 if (persistentState != null) {
1249 r.persistentState = persistentState;
1250 mService.notifyTaskPersisterLocked(r.task, false);
1251 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001252 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001253 if (icicle != null) {
1254 // If icicle is null, this is happening due to a timeout, so we
1255 // haven't really saved the state.
1256 r.icicle = icicle;
1257 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001258 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001259 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001260 }
1261 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001262 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001263 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1264 r.stopped = true;
1265 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001266
Chong Zhang45e6d2d2016-07-20 18:33:56 -07001267 mWindowManager.notifyAppStopped(r.appToken);
Robert Carre12aece2016-02-02 22:43:27 -08001268
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001269 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001270 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001271 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001272 if (r.finishing) {
1273 r.clearOptionsLocked();
1274 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001275 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001276 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001277 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001278 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001279 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001280 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001281 }
1282 }
1283 }
1284
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001285 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001286 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001287 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001288
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001289 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001290 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001291 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001292 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001293 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001294 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001295 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001296 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1297 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001298 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001299 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1300 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001301 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001302 if (prev.deferRelaunchUntilPaused) {
1303 // Complete the deferred relaunch that was waiting for pause to complete.
1304 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1305 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1306 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001307 } else if (wasStopping) {
1308 // We are also stopping, the stop request must have gone soon after the pause.
1309 // We can't clobber it, because the stop confirmation will not be handled.
1310 // We don't need to schedule another stop, we only need to let it happen.
1311 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001312 } else if ((!prev.visible && !hasVisibleBehindActivity())
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001313 || mService.isSleepingOrShuttingDownLocked()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001314 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001315 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001316 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001317 }
1318 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001319 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001320 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001321 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001322 // It is possible the activity was freezing the screen before it was paused.
1323 // In that case go ahead and remove the freeze this activity has on the screen
1324 // since it is no longer visible.
1325 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001326 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001327 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001328
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001329 if (resumeNext) {
1330 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001331 if (!mService.isSleepingOrShuttingDownLocked()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001332 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001333 } else {
1334 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001335 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001336 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001337 // If there are no more activities available to run, do resume anyway to start
1338 // something. Also if the top activity on the stack is not the just paused
1339 // activity, we need to go ahead and resume it to ensure we complete an
1340 // in-flight app switch.
1341 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001342 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001343 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001344 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001345
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001346 if (prev != null) {
1347 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001348
Craig Mautner525f3d92013-05-07 14:01:50 -07001349 if (prev.app != null && prev.cpuTimeAtResume > 0
1350 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001351 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1352 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001353 if (diff > 0) {
1354 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1355 synchronized (bsi) {
1356 BatteryStatsImpl.Uid.Proc ps =
1357 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001358 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001359 if (ps != null) {
1360 ps.addForegroundTimeLocked(diff);
1361 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001362 }
1363 }
1364 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001365 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001367
Andrii Kulian8290f8f2016-06-30 17:51:32 -07001368 // Notify when the task stack has changed, but only if visibilities changed (not just
1369 // focus). Also if there is an active pinned stack - we always want to notify it about
1370 // task stack changes, because its positioning may depend on it.
1371 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause
1372 || mService.mStackSupervisor.getStack(PINNED_STACK_ID) != null) {
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001373 mService.notifyTaskStackChangedLocked();
1374 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1375 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001376
1377 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001378 }
1379
Chong Zhang46b1ac62016-02-18 17:53:57 -08001380 private void addToStopping(ActivityRecord r, boolean immediate) {
1381 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1382 mStackSupervisor.mStoppingActivities.add(r);
1383 }
1384
1385 // If we already have a few activities waiting to stop, then give up
1386 // on things going idle and start clearing them out. Or if r is the
1387 // last of activity of the last task the stack will be empty and must
1388 // be cleared immediately.
1389 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1390 || (r.frontOfTask && mTaskHistory.size() <= 1);
1391
1392 if (immediate || forceIdle) {
1393 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1394 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001395 mStackSupervisor.scheduleIdleLocked();
1396 } else {
1397 mStackSupervisor.checkReadyForSleepLocked();
1398 }
1399 }
1400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 /**
1402 * Once we know that we have asked an application to put an activity in
1403 * the resumed state (either by launching it or explicitly telling it),
1404 * this function updates the rest of our state to match that fact.
1405 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001406 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001407 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001408 next.idle = false;
1409 next.results = null;
1410 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001411 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001412
Chong Zhang85ee6542015-10-02 13:36:38 -07001413 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001414 ProcessRecord app = next.task.mActivities.get(0).app;
1415 if (app != null && app != mService.mHomeProcess) {
1416 mService.mHomeProcess = app;
1417 }
1418 }
1419
Craig Mautner07566322013-09-26 16:42:55 -07001420 if (next.nowVisible) {
1421 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Chong Zhang5022da32016-06-21 16:31:37 -07001422 mStackSupervisor.reportActivityVisibleLocked(next);
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001423 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001424 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001425
1426 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001427 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001428
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001429 mStackSupervisor.reportResumedActivityLocked(next);
1430
1431 next.resumeKeyDispatchingLocked();
1432 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433
1434 // Mark the point when the activity is resuming
1435 // TODO: To be more accurate, the mark should be before the onCreate,
1436 // not after the onResume. But for subsequent starts, onResume is fine.
1437 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001438 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 } else {
1440 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1441 }
Winson Chung376543b2014-05-21 17:43:28 -07001442
George Mount6ba042b2014-07-28 11:12:28 -07001443 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001444
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001445 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001446 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001447 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001448 }
Chong Zhang6cda19c2016-06-14 19:07:56 -07001449 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001450 }
1451
Craig Mautner2568c3a2015-03-26 14:22:34 -07001452 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001453 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001454 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1455 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1456 r.mUpdateTaskThumbnailWhenHidden = false;
1457 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001458 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001459 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001460 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001461 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001462 container.setVisible(visible);
1463 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001464 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001465 }
1466
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001467 // Find the first visible activity above the passed activity and if it is translucent return it
1468 // otherwise return null;
1469 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1470 TaskRecord task = r.task;
1471 if (task == null) {
1472 return null;
1473 }
1474
1475 ActivityStack stack = task.stack;
1476 if (stack == null) {
1477 return null;
1478 }
1479
1480 int stackNdx = mStacks.indexOf(stack);
1481
1482 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1483 int taskNdx = tasks.indexOf(task);
1484
1485 ArrayList<ActivityRecord> activities = task.mActivities;
1486 int activityNdx = activities.indexOf(r) + 1;
1487
1488 final int numStacks = mStacks.size();
1489 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001490 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001491 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001492 final int numTasks = tasks.size();
1493 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001494 final TaskRecord currentTask = tasks.get(taskNdx);
1495 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001496 final int numActivities = activities.size();
1497 while (activityNdx < numActivities) {
1498 final ActivityRecord activity = activities.get(activityNdx);
1499 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001500 return historyStack.mFullscreen
1501 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001502 }
1503 ++activityNdx;
1504 }
1505 activityNdx = 0;
1506 ++taskNdx;
1507 }
1508 taskNdx = 0;
1509 ++stackNdx;
1510 }
1511
1512 return null;
1513 }
1514
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001515 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001516 ArrayList<ActivityStack> stacks = mStacks;
1517 final ActivityRecord parent = mActivityContainer.mParentActivity;
1518 if (parent != null) {
1519 stacks = parent.task.stack.mStacks;
1520 }
1521 if (stacks != null) {
1522 for (int i = stacks.size() - 1; i >= 0; --i) {
1523 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001524 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001525 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001526 return stack;
1527 }
1528 }
1529 }
1530 return null;
1531 }
1532
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001533 /** Returns true if the stack contains a fullscreen task. */
1534 private boolean hasFullscreenTask() {
1535 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1536 final TaskRecord task = mTaskHistory.get(i);
1537 if (task.mFullscreen) {
1538 return true;
1539 }
1540 }
1541 return false;
1542 }
1543
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001544 /**
1545 * Returns true if the stack is translucent and can have other contents visible behind it if
1546 * needed. A stack is considered translucent if it don't contain a visible or
1547 * starting (about to be visible) activity that is fullscreen (opaque).
1548 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001549 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001550 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001551 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001552 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1553 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001554 final ArrayList<ActivityRecord> activities = task.mActivities;
1555 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1556 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001557
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001558 if (r.finishing) {
1559 // We don't factor in finishing activities when determining translucency since
1560 // they will be gone soon.
1561 continue;
1562 }
1563
1564 if (!r.visible && r != starting) {
1565 // Also ignore invisible activities that are not the currently starting
1566 // activity (about to be visible).
1567 continue;
1568 }
1569
1570 if (r.fullscreen) {
1571 // Stack isn't translucent if it has at least one fullscreen activity
1572 // that is visible.
1573 return false;
1574 }
1575
1576 if (!isHomeStack() && r.frontOfTask
1577 && task.isOverHomeStack() && stackBehindId != HOME_STACK_ID) {
1578 // Stack isn't translucent if it's top activity should have the home stack
1579 // behind it and the stack currently behind it isn't the home stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001580 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001581 }
1582 }
1583 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001584 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001585 }
1586
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001587 /**
1588 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1589 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001590 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001591 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001592 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001593 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001594 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001595 }
1596
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001597 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001598 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001599 }
1600
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001601 final int stackIndex = mStacks.indexOf(this);
1602
1603 if (stackIndex == mStacks.size() - 1) {
1604 Slog.wtf(TAG,
1605 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001606 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001607 }
1608
Chong Zhang75b37202015-12-04 14:16:36 -08001609 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1610 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001611 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001612 }
1613
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001614 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1615 final int focusedStackId = focusedStack.mStackId;
1616
Jiaquan He9bc01a82016-07-08 10:29:38 -07001617 final TaskRecord topFocusedTask = focusedStack.topTask();
1618 final boolean isOnTopLauncherFocused = topFocusedTask != null &&
1619 topFocusedTask.isOnTopLauncher();
1620 if (isOnTopLauncherFocused) {
1621 // When an on-top launcher is focused, we should find out whether the freeform stack or
1622 // the fullscreen stack appears first underneath and has activities to show, and then
1623 // make it visible.
1624 boolean behindFullscreenOrFreeForm = false;
1625 for (int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1626 stackBehindFocusedIndex >= 0; stackBehindFocusedIndex--) {
1627 ActivityStack stack = mStacks.get(stackBehindFocusedIndex);
1628 if ((stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
1629 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID)
1630 && stack.topRunningActivityLocked() != null) {
1631 if (stackIndex == stackBehindFocusedIndex) {
1632 return !behindFullscreenOrFreeForm ? STACK_VISIBLE : STACK_INVISIBLE;
1633 }
1634 behindFullscreenOrFreeForm = true;
1635 }
1636 }
1637 }
Jiaquan Hec4dcd4b2016-07-29 15:31:10 -07001638 // If an on-top launcher is on the top of the home stack but the home stack is not focused,
1639 // then the whole stack should be invisible.
1640 TaskRecord topTask = topTask();
1641 if (mStackId != focusedStackId && topTask != null && topTask.isOnTopLauncher()) {
1642 // We're here mostly because the on-top launcher didn't have a chance to move itself to
1643 // back. We should move it to back as soon as possible to avoid other activities
1644 // returning to it or other visibility issues.
1645 moveTaskToBackLocked(topTask.taskId);
1646 return STACK_INVISIBLE;
1647 }
Jiaquan He9bc01a82016-07-08 10:29:38 -07001648
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001649 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001650 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1651 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001652 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001653 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001654 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001655 }
1656
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001657 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001658 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001659 // task in the focus stack doesn't support any form of resizing but we show it for the
1660 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001661 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001662 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001663 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1664 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001665 }
1666
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001667 // Find the first stack behind focused stack that actually got something visible.
1668 int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1669 while (stackBehindFocusedIndex >= 0 &&
1670 mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1671 stackBehindFocusedIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001672 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001673 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001674 && stackIndex == stackBehindFocusedIndex) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001675 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001676 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001677 }
1678
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001679 final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1680 ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1681
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001682 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001683 && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001684 // Stacks behind the fullscreen stack with a translucent activity are always
1685 // visible so they can act as a backdrop to the translucent activity.
1686 // For example, dialog activities
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001687 if (stackIndex == stackBehindFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001688 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001689 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001690 if (stackBehindFocusedIndex >= 0) {
1691 if ((stackBehindFocusedId == DOCKED_STACK_ID
1692 || stackBehindFocusedId == PINNED_STACK_ID)
1693 && stackIndex == (stackBehindFocusedIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001694 // The stack behind the docked or pinned stack is also visible so we can have a
1695 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001696 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001697 }
1698 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001699 }
1700
Wale Ogunwale3797c222015-10-27 14:21:58 -07001701 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001702 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001703 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001704 }
1705
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001706 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001707 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001708
1709 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1710 continue;
1711 }
1712
Wale Ogunwale3797c222015-10-27 14:21:58 -07001713 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001714 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001715 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001716 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001717
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001718 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001719 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001720 }
1721 }
1722
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001723 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001724 }
1725
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001726 final int rankTaskLayers(int baseLayer) {
1727 int layer = 0;
1728 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1729 final TaskRecord task = mTaskHistory.get(taskNdx);
1730 ActivityRecord r = task.topRunningActivityLocked();
1731 if (r == null || r.finishing || !r.visible) {
1732 task.mLayerRank = -1;
1733 } else {
1734 task.mLayerRank = baseLayer + layer++;
1735 }
1736 }
1737 return layer;
1738 }
1739
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 /**
1741 * Make sure that all activities that need to be visible (that is, they
1742 * currently can be seen by the user) actually are.
1743 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001744 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1745 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001746 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001747 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001749 if (top != null) {
1750 checkTranslucentActivityWaiting(top);
1751 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 // If the top activity is not fullscreen, then we need to
1754 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001755 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001756 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001757 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1758 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001759 boolean behindFullscreenActivity = stackInvisible;
Andrii Kulianc11ce7b2016-06-27 10:53:53 -07001760 boolean resumeNextActivity = mStackSupervisor.isFocusedStack(this)
1761 && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001762 boolean behindTranslucentActivity = false;
1763 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001764 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001765 final TaskRecord task = mTaskHistory.get(taskNdx);
1766 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001767 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1768 final ActivityRecord r = activities.get(activityNdx);
1769 if (r.finishing) {
Chong Zhang22bc8512016-04-07 13:47:18 -07001770 // Normally the screenshot will be taken in makeInvisible(). When an activity
1771 // is finishing, we no longer change its visibility, but we still need to take
1772 // the screenshots if startPausingLocked decided it should be taken.
1773 if (r.mUpdateTaskThumbnailWhenHidden) {
1774 r.updateThumbnailLocked(screenshotActivitiesLocked(r), null);
1775 r.mUpdateTaskThumbnailWhenHidden = false;
1776 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001777 continue;
1778 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001779 final boolean isTop = r == top;
1780 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001781 continue;
1782 }
1783 aboveTop = false;
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001784
1785 if (shouldBeVisible(r, behindTranslucentActivity, stackVisibleBehind,
1786 visibleBehind, behindFullscreenActivity)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001787 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1788 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001789 // First: if this is not the current activity being started, make
1790 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001791 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001792 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001794
1795 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001796 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001797 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001798 if (activityNdx >= activities.size()) {
1799 // Record may be removed if its process needs to restart.
1800 activityNdx = activities.size() - 1;
1801 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001802 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001803 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001804 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001805 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001806 // If this activity is already visible, then there is nothing to do here.
Chong Zhang371c4422016-05-11 10:48:32 -07001807 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1808 "Skipping: already visible at " + r);
1809
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001810 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001811 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001812 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001813 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001814 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001816 // Aggregate current change flags.
1817 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001818 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1819 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001820 if (behindFullscreenActivity && !r.fullscreen) {
1821 behindTranslucentActivity = true;
1822 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001823 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001824 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001825 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1826 + stackInvisible + " behindFullscreenActivity="
1827 + behindFullscreenActivity + " mLaunchTaskBehind="
1828 + r.mLaunchTaskBehind);
1829 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001830 }
1831 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001832 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1833 // The visibility of tasks and the activities they contain in freeform stack are
1834 // determined individually unlike other stacks where the visibility or fullscreen
1835 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001836 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001837 } else if (mStackId == HOME_STACK_ID) {
Jiaquan He9bc01a82016-07-08 10:29:38 -07001838 if (task.isOnTopLauncher()) {
1839 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "On-top launcher: at " + task
1840 + " stackInvisible=" + stackInvisible
1841 + " behindFullscreenActivity=" + behindFullscreenActivity);
1842 // When an on-top launcher is visible, (e.g. it's on the top of the home stack),
1843 // other tasks in the home stack could be visible if and only if:
1844 // - some app is running in the docked stack;
1845 // - no app is running in either the fullscreen stack or the freefrom stack.
1846 final ActivityStack dockedStack = mStackSupervisor.getStack(DOCKED_STACK_ID);
1847 final ActivityStack fullscreenStack = mStackSupervisor.getStack(
1848 FULLSCREEN_WORKSPACE_STACK_ID);
1849 final ActivityStack freeformStack = mStackSupervisor.getStack(
1850 FREEFORM_WORKSPACE_STACK_ID);
1851 final boolean dockedStackEmpty = dockedStack == null ||
1852 dockedStack.topRunningActivityLocked() == null;
1853 final boolean fullscreenStackEmpty = fullscreenStack == null ||
1854 fullscreenStack.topRunningActivityLocked() == null;
1855 final boolean freeformStackEmpty = freeformStack == null ||
1856 freeformStack.topRunningActivityLocked() == null;
1857 behindFullscreenActivity = dockedStackEmpty || !fullscreenStackEmpty ||
1858 !freeformStackEmpty;
1859 } else if (task.isHomeTask()) {
Wale Ogunwale74e26592016-02-05 11:48:37 -08001860 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1861 + " stackInvisible=" + stackInvisible
1862 + " behindFullscreenActivity=" + behindFullscreenActivity);
1863 // No other task in the home stack should be visible behind the home activity.
1864 // Home activities is usually a translucent activity with the wallpaper behind
1865 // them. However, when they don't have the wallpaper behind them, we want to
1866 // show activities in the next application stack behind them vs. another
1867 // task in the home stack like recents.
1868 behindFullscreenActivity = true;
1869 } else if (task.isRecentsTask()
1870 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1871 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1872 "Recents task returning to app: at " + task
1873 + " stackInvisible=" + stackInvisible
1874 + " behindFullscreenActivity=" + behindFullscreenActivity);
1875 // We don't want any other tasks in the home stack visible if the recents
1876 // activity is going to be returning to an application activity type.
1877 // We do this to preserve the visible order the user used to get into the
1878 // recents activity. The recents activity is normally translucent and if it
1879 // doesn't have the wallpaper behind it the next activity in the home stack
1880 // shouldn't be visible when the home stack is brought to the front to display
1881 // the recents activity from an app.
1882 behindFullscreenActivity = true;
1883 }
1884
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001885 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001886 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001887
1888 if (mTranslucentActivityWaiting != null &&
1889 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1890 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1891 notifyActivityDrawnLocked(null);
1892 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001893 }
Craig Mautner58547802013-03-05 08:23:53 -08001894
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001895 /** Return true if the input activity should be made visible */
1896 private boolean shouldBeVisible(ActivityRecord r, boolean behindTranslucentActivity,
1897 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1898 boolean behindFullscreenActivity) {
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001899
Wale Ogunwale60920d52016-06-20 10:45:13 -07001900 if (!okToShowLocked(r)) {
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001901 return false;
1902 }
1903
Wale Ogunwale8c09c7d2016-05-23 09:10:24 -07001904 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1905 // but must be drawn initially for the animation as though they were visible.
1906 final boolean activityVisibleBehind =
1907 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1908
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001909 boolean isVisible =
1910 !behindFullscreenActivity || r.mLaunchTaskBehind || activityVisibleBehind;
1911
Wale Ogunwaleca2af9e2016-05-26 14:43:19 -07001912 if (mService.mSupportsLeanbackOnly && isVisible && r.isRecentsActivity()) {
1913 // On devices that support leanback only (Android TV), Recents activity can only be
1914 // visible if the home stack is the focused stack or we are in split-screen mode.
Wale Ogunwale8c09c7d2016-05-23 09:10:24 -07001915 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1916 || mStackSupervisor.isFocusedStack(this);
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001917 }
1918
1919 return isVisible;
1920 }
1921
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001922 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1923 if (mTranslucentActivityWaiting != top) {
1924 mUndrawnActivitiesBelowTopTranslucent.clear();
1925 if (mTranslucentActivityWaiting != null) {
1926 // Call the callback with a timeout indication.
1927 notifyActivityDrawnLocked(null);
1928 mTranslucentActivityWaiting = null;
1929 }
1930 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1931 }
1932 }
1933
1934 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001935 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001936 // We need to make sure the app is running if it's the top, or it is just made visible from
1937 // invisible. If the app is already visible, it must have died while it was visible. In this
1938 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1939 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001940 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001941 // This activity needs to be visible, but isn't even running...
1942 // get it started and resume if no other stack in this stack is resumed.
1943 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1944 if (r != starting) {
1945 r.startFreezingScreenLocked(r.app, configChanges);
1946 }
1947 if (!r.visible || r.mLaunchTaskBehind) {
1948 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1949 setVisible(r, true);
1950 }
1951 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001952 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001953 return true;
1954 }
1955 }
1956 return false;
1957 }
1958
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001959 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001960 if (!r.visible) {
1961 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1962 return;
1963 }
1964 // Now for any activities that aren't visible to the user, make sure they no longer are
1965 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001966 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001967 try {
1968 setVisible(r, false);
1969 switch (r.state) {
1970 case STOPPING:
1971 case STOPPED:
1972 if (r.app != null && r.app.thread != null) {
1973 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1974 "Scheduling invisibility: " + r);
1975 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1976 }
1977 break;
1978
1979 case INITIALIZING:
1980 case RESUMED:
1981 case PAUSING:
1982 case PAUSED:
1983 // This case created for transitioning activities from
1984 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001985 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001986 releaseBackgroundResources(r);
1987 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001988 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001989 }
1990 break;
1991
1992 default:
1993 break;
1994 }
1995 } catch (Exception e) {
1996 // Just skip on any failure; we'll make it visible when it next restarts.
1997 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1998 }
1999 }
2000
2001 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
2002 TaskRecord task, ActivityRecord r) {
2003 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002004 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08002005 + " stackInvisible=" + stackInvisible
2006 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08002007 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002008 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08002009 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002010 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
2011 + " stackInvisible=" + stackInvisible
2012 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08002013 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002014 }
2015 return behindFullscreenActivity;
2016 }
2017
Jorim Jaggie66edb12016-02-05 12:41:17 -08002018 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
2019
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002020 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08002021 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08002022 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
2023 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08002024 return;
2025 }
2026
2027 // If this activity is paused, tell it to now show its window.
2028 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2029 "Making visible and scheduling visibility: " + r);
2030 try {
2031 if (mTranslucentActivityWaiting != null) {
2032 r.updateOptionsLocked(r.returningOptions);
2033 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002034 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08002035 setVisible(r, true);
2036 r.sleeping = false;
2037 r.app.pendingUiClean = true;
2038 r.app.thread.scheduleWindowVisibility(r.appToken, true);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002039 // The activity may be waiting for stop, but that is no longer
2040 // appropriate for it.
2041 mStackSupervisor.mStoppingActivities.remove(r);
2042 mStackSupervisor.mGoingToSleepActivities.remove(r);
Jorim Jaggie66edb12016-02-05 12:41:17 -08002043 } catch (Exception e) {
2044 // Just skip on any failure; we'll make it
2045 // visible when it next restarts.
2046 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002047 }
Chong Zhang371c4422016-05-11 10:48:32 -07002048 handleAlreadyVisible(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002049 }
2050
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002051 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002052 r.stopFreezingScreenLocked(false);
2053 try {
2054 if (r.returningOptions != null) {
2055 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
2056 }
2057 } catch(RemoteException e) {
2058 }
2059 return r.state == ActivityState.RESUMED;
2060 }
2061
Todd Kennedyaab56db2015-01-30 09:39:53 -08002062 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002063 mTranslucentActivityWaiting = r;
2064 mUndrawnActivitiesBelowTopTranslucent.clear();
2065 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
2066 }
2067
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002068 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2069 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2070 final TaskRecord task = mTaskHistory.get(taskNdx);
2071 final ArrayList<ActivityRecord> activities = task.mActivities;
2072 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2073 final ActivityRecord r = activities.get(activityNdx);
2074 if ( r.appTimeTracker != except) {
2075 r.appTimeTracker = null;
2076 }
2077 }
2078 }
2079 }
2080
Craig Mautner5eda9b32013-07-02 11:58:16 -07002081 /**
2082 * Called as activities below the top translucent activity are redrawn. When the last one is
2083 * redrawn notify the top activity by calling
2084 * {@link Activity#onTranslucentConversionComplete}.
2085 *
2086 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2087 * occurred and the activity will be notified immediately.
2088 */
2089 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07002090 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002091 if ((r == null)
2092 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2093 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2094 // The last undrawn activity below the top has just been drawn. If there is an
2095 // opaque activity at the top, notify it that it can become translucent safely now.
2096 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2097 mTranslucentActivityWaiting = null;
2098 mUndrawnActivitiesBelowTopTranslucent.clear();
2099 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2100
Craig Mautner71dd1b62014-02-18 15:48:52 -08002101 if (waitingActivity != null) {
2102 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
2103 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
2104 try {
2105 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
2106 waitingActivity.appToken, r != null);
2107 } catch (RemoteException e) {
2108 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002109 }
2110 }
2111 }
2112 }
2113
Craig Mautnera61bc652013-10-28 15:43:18 -07002114 /** If any activities below the top running one are in the INITIALIZING state and they have a
2115 * starting window displayed then remove that starting window. It is possible that the activity
2116 * in this state will never resumed in which case that starting window will be orphaned. */
2117 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002118 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002119 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002120 // We don't want to clear starting window for activities that aren't behind fullscreen
2121 // activities as we need to display their starting window until they are done initializing.
2122 boolean behindFullscreenActivity = false;
Wale Ogunwale98742a52016-07-12 10:29:12 -07002123
2124 if (getStackVisibilityLocked(null) == STACK_INVISIBLE) {
2125 // The stack is not visible, so no activity in it should be displaying a starting
2126 // window. Mark all activities below top and behind fullscreen.
2127 aboveTop = false;
2128 behindFullscreenActivity = true;
2129 }
2130
Craig Mautnera61bc652013-10-28 15:43:18 -07002131 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2132 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2133 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2134 final ActivityRecord r = activities.get(activityNdx);
2135 if (aboveTop) {
2136 if (r == topActivity) {
2137 aboveTop = false;
2138 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002139 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002140 continue;
2141 }
2142
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002143 if (r.state == ActivityState.INITIALIZING
Wale Ogunwale68741142016-05-17 09:40:02 -07002144 && r.mStartingWindowState == STARTING_WINDOW_SHOWN
2145 && behindFullscreenActivity) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002146 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
2147 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002148 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07002149 mWindowManager.removeAppStartingWindow(r.appToken);
2150 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002151
2152 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002153 }
2154 }
2155 }
2156
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 /**
2158 * Ensure that the top activity in the stack is resumed.
2159 *
2160 * @param prev The previously resumed activity, for when in the process
2161 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002162 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 *
2164 * @return Returns true if something is being resumed, or false if
2165 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002166 *
2167 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2168 * non-focused stack to be resumed.
2169 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2170 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08002172 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002173 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002174 // Don't even start recursing.
2175 return false;
2176 }
2177
2178 boolean result = false;
2179 try {
2180 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002181 mStackSupervisor.inResumeTopActivity = true;
2182 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
2183 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08002184 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08002185 }
Craig Mautner544efa72014-09-04 16:41:20 -07002186 result = resumeTopActivityInnerLocked(prev, options);
2187 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002188 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002189 }
2190 return result;
2191 }
2192
Chong Zhang6cda19c2016-06-14 19:07:56 -07002193 void setResumedActivityLocked(ActivityRecord r, String reason) {
2194 // TODO: move mResumedActivity to stack supervisor,
2195 // there should only be 1 global copy of resumed activity.
2196 mResumedActivity = r;
Chong Zhangd2cb3112016-07-20 11:13:22 -07002197 r.state = ActivityState.RESUMED;
Chong Zhang6cda19c2016-06-14 19:07:56 -07002198 mService.setResumedActivityUncheckLocked(r, reason);
2199 r.task.touchActiveTime();
2200 mRecentTasks.addLocked(r.task);
2201 }
2202
Chong Zhang280d3322015-11-03 17:27:26 -08002203 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002204 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07002205
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002206 if (!mService.mBooting && !mService.mBooted) {
2207 // Not ready yet!
2208 return false;
2209 }
2210
Craig Mautnerdf88d732014-01-27 09:21:32 -08002211 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002212 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07002213 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002214 // Do not resume this stack if its parent is not resumed.
2215 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
2216 return false;
2217 }
2218
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002219 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002220
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002222 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002223
2224 // Remember how we'll process this pause/resume situation, and ensure
2225 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002226 final boolean userLeaving = mStackSupervisor.mUserLeaving;
2227 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002228
Craig Mautner84984fa2014-06-19 11:19:20 -07002229 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002230 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002231 // There are no more activities!
2232 final String reason = "noMoreActivities";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07002233 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack()
2234 ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2235 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(returnTaskType, reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002236 // Try to move focus to the next visible stack with a running activity if this
2237 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002238 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2239 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002240 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002241
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002242 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07002243 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002244 if (DEBUG_STATES) Slog.d(TAG_STATES,
2245 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07002246 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08002247 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07002248 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002249 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002250 }
2251
2252 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002253
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002254 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002255 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2256 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002257 // Make sure we have executed any pending transitions, since there
2258 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002259 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002261 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002262 if (DEBUG_STATES) Slog.d(TAG_STATES,
2263 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002264 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002265 return false;
2266 }
2267
Craig Mautner525f3d92013-05-07 14:01:50 -07002268 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07002269 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002270 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002271 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002272 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002273 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002274 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002275 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002276 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002277 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002278 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002279 } else if (!isOnHomeDisplay()) {
2280 return false;
2281 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002282 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002283 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002284 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2285 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2286 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002287 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002288 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002289 }
2290
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002291 // If we are sleeping, and there is no resumed activity, and the top
2292 // activity is paused, well that is the state we want.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002293 if (mService.isSleepingOrShuttingDownLocked()
p13451dbad2872012-04-18 11:39:23 +09002294 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002295 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002296 // Make sure we have executed any pending transitions, since there
2297 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002298 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002299 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002300 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002301 if (DEBUG_STATES) Slog.d(TAG_STATES,
2302 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002303 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002304 return false;
2305 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002306
2307 // Make sure that the user who owns this activity is started. If not,
2308 // we will just leave it as is because someone should be bringing
2309 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002310 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002311 Slog.w(TAG, "Skipping resume of top activity " + next
2312 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002313 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002314 return false;
2315 }
2316
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 // The activity may be waiting for stop, but that is no longer
2318 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002319 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002320 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002321 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002322 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002324 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002325
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002326 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002327 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002328 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002329 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002330 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002331 return false;
2332 }
2333
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002334 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2335
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002336 // We need to start pausing the current activity so the top one can be resumed...
2337 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002338 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002339 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002340 if (DEBUG_STATES) Slog.d(TAG_STATES,
2341 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002342 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002343 }
2344 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002345 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002346 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002347 // At this point we want to put the upcoming activity's process
2348 // at the top of the LRU list, since we know we will be needing it
2349 // very soon and it would be a waste to let it get killed if it
2350 // happens to be sitting towards the end.
2351 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002352 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002353 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002354 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 return true;
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002356 } else if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2357 mStackSupervisor.allResumedActivitiesComplete()) {
2358 // It is possible for the activity to be resumed when we paused back stacks above if the
2359 // next activity doesn't have to wait for pause to complete.
2360 // So, nothing else to-do except:
2361 // Make sure we have executed any pending transitions, since there
2362 // should be nothing left to do at this point.
2363 mWindowManager.executeAppTransition();
2364 mNoAnimActivities.clear();
2365 ActivityOptions.abort(options);
2366 if (DEBUG_STATES) Slog.d(TAG_STATES,
2367 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
2368 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2369 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002370 }
2371
Christopher Tated3f175c2012-06-14 14:16:54 -07002372 // If the most recent activity was noHistory but was only stopped rather
2373 // than stopped+finished because the device went to sleep, we need to make
2374 // sure to finish it as we're making a new activity topmost.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002375 if (mService.isSleepingLocked() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002376 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002377 if (DEBUG_STATES) Slog.d(TAG_STATES,
2378 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002379 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002380 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002381 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002382 }
2383
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002385 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2386 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002387 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002388 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2389 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 } else {
2391 // The next activity is already visible, so hide the previous
2392 // activity's windows right now so we can show the new one ASAP.
2393 // We only do this if the previous is finishing, which should mean
2394 // it is on top of the one being resumed so hiding it quickly
2395 // is good. Otherwise, we want to do the normal route of allowing
2396 // the resumed activity to be shown so we can decide if the
2397 // previous should actually be hidden depending on whether the
2398 // new one is found to be full-screen or not.
2399 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002400 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002401 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2402 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002403 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 + ", nowVisible=" + next.nowVisible);
2405 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002406 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002407 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002408 + ", waitingVisible="
2409 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2410 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 }
2412 }
2413 }
2414
Dianne Hackborne7f97212011-02-24 14:40:20 -08002415 // Launching this app's activity, make sure the app is no longer
2416 // considered stopped.
2417 try {
2418 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002419 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002420 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002421 } catch (IllegalArgumentException e) {
2422 Slog.w(TAG, "Failed trying to unstop package "
2423 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002424 }
2425
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002426 // We are starting up the next activity, so tell the window manager
2427 // that the previous one will be hidden soon. This way it can know
2428 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002429 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002430 if (prev != null) {
2431 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002432 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 "Prepare close transition: prev=" + prev);
2434 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002435 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002436 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002437 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002438 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002439 ? TRANSIT_ACTIVITY_CLOSE
2440 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002442 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002443 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002444 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2445 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002446 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002447 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002448 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002450 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002451 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002452 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002453 ? TRANSIT_TASK_OPEN_BEHIND
2454 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002455 }
2456 }
Craig Mautner967212c2013-04-13 21:10:58 -07002457 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002458 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002460 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002461 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002462 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002463 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002464 }
2465 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002466
2467 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002468 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002469 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2470 if (opts != null) {
2471 resumeAnimOptions = opts.toBundle();
2472 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002473 next.applyOptionsLocked();
2474 } else {
2475 next.clearOptionsLocked();
2476 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002477
Craig Mautnercf910b02013-04-23 11:23:27 -07002478 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002479 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002480 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2481 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002482
Chong Zhangd127c6d2016-05-02 16:36:41 -07002483 // If the previous activity is translucent, force a visibility update of
2484 // the next activity, so that it's added to WM's opening app list, and
2485 // transition animation can be set up properly.
2486 // For example, pressing Home button with a translucent activity in focus.
2487 // Launcher is already visible in this case. If we don't add it to opening
2488 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2489 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2490 final boolean lastActivityTranslucent = lastStack != null
2491 && (!lastStack.mFullscreen
2492 || (lastStack.mLastPausedActivity != null
2493 && !lastStack.mLastPausedActivity.fullscreen));
2494
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002495 // This activity is now becoming visible.
Chong Zhangd127c6d2016-05-02 16:36:41 -07002496 if (!next.visible || next.stopped || lastActivityTranslucent) {
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002497 mWindowManager.setAppVisibility(next.appToken, true);
2498 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002499
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002500 // schedule launch ticks to collect information about slow apps.
2501 next.startLaunchTickingLocked();
2502
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002503 ActivityRecord lastResumedActivity =
2504 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 ActivityState lastState = next.state;
2506
2507 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002508
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002509 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Chong Zhang6cda19c2016-06-14 19:07:56 -07002510
2511 setResumedActivityLocked(next, "resumeTopActivityInnerLocked");
2512
Dianne Hackborndb926082013-10-31 16:32:44 -07002513 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002514 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002515 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516
2517 // Have the window manager re-evaluate the orientation of
2518 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002519 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002520 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002521 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002522 mService.mConfiguration,
2523 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2524 if (config != null) {
2525 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002526 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002527 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002528 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002529
Craig Mautner525f3d92013-05-07 14:01:50 -07002530 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 // The configuration update wasn't able to keep the existing
2532 // instance of the activity, and instead started a new one.
2533 // We should be all done, but let's just make sure our activity
2534 // is still at the top and schedule another run if something
2535 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002536 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002537 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 "Activity config changed during resume: " + next
2539 + ", new next: " + nextNext);
2540 if (nextNext != next) {
2541 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002542 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002543 }
Chong Zhangd2cb3112016-07-20 11:13:22 -07002544 if (!next.visible || next.stopped) {
2545 mWindowManager.setAppVisibility(next.appToken, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002546 }
Chong Zhangd2cb3112016-07-20 11:13:22 -07002547 completeResumeLocked(next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002548 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Chong Zhangd2cb3112016-07-20 11:13:22 -07002549 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 }
Craig Mautner58547802013-03-05 08:23:53 -08002551
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002552 try {
2553 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002554 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 if (a != null) {
2556 final int N = a.size();
2557 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002558 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2559 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002560 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002561 }
2562 }
2563
2564 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002565 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002566 }
2567
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002568 // Well the app will no longer be stopped.
2569 // Clear app token stopped state in window manager if needed.
Chong Zhang45e6d2d2016-07-20 18:33:56 -07002570 mWindowManager.notifyAppResumed(next.appToken, next.stopped);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002571
Craig Mautner299f9602015-01-26 09:47:33 -08002572 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2573 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002574
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002575 next.sleeping = false;
Alan Viverette5f31b982016-06-21 14:46:14 -04002576 mService.showUnsupportedZoomDialogIfNeededLocked(next);
Craig Mautner2420ead2013-04-01 17:13:20 -07002577 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002578 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002579 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002580 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002581 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002582 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002583
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002584 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002585 } catch (Exception e) {
2586 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002587 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002588 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002589 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002590 if (lastStack != null) {
2591 lastStack.mResumedActivity = lastResumedActivity;
2592 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 Slog.i(TAG, "Restarting because process died: " + next);
2594 if (!next.hasBeenLaunched) {
2595 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002596 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2597 mStackSupervisor.isFrontStack(lastStack)) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07002598 next.showStartingWindow(null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002599 }
George Mount2c92c972014-03-20 09:38:23 -07002600 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002601 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002602 return true;
2603 }
2604
2605 // From this point on, if something goes wrong there is no way
2606 // to recover the activity.
2607 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002608 completeResumeLocked(next);
2609 } catch (Exception e) {
2610 // If any exception gets thrown, toss away this
2611 // activity and try the next one.
2612 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002613 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002614 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002615 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002616 return true;
2617 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002618 } else {
2619 // Whoops, need to restart this activity!
2620 if (!next.hasBeenLaunched) {
2621 next.hasBeenLaunched = true;
2622 } else {
2623 if (SHOW_APP_STARTING_PREVIEW) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07002624 next.showStartingWindow(null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002626 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002627 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002628 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002629 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002630 }
2631
Craig Mautnercf910b02013-04-23 11:23:27 -07002632 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 return true;
2634 }
2635
riddle_hsuc215a4f2014-12-27 12:10:45 +08002636 private TaskRecord getNextTask(TaskRecord targetTask) {
2637 final int index = mTaskHistory.indexOf(targetTask);
2638 if (index >= 0) {
2639 final int numTasks = mTaskHistory.size();
2640 for (int i = index + 1; i < numTasks; ++i) {
2641 TaskRecord task = mTaskHistory.get(i);
2642 if (task.userId == targetTask.userId) {
2643 return task;
2644 }
2645 }
2646 }
2647 return null;
2648 }
2649
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002650 private void insertTaskAtPosition(TaskRecord task, int position) {
2651 if (position >= mTaskHistory.size()) {
2652 insertTaskAtTop(task, null);
2653 return;
2654 }
2655 // Calculate maximum possible position for this task.
2656 int maxPosition = mTaskHistory.size();
2657 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002658 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002659 // Put non-current user tasks below current user tasks.
2660 while (maxPosition > 0) {
2661 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2662 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002663 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002664 break;
2665 }
2666 maxPosition--;
2667 }
2668 }
2669 position = Math.min(position, maxPosition);
2670 mTaskHistory.remove(task);
2671 mTaskHistory.add(position, task);
2672 updateTaskMovement(task, true);
2673 }
2674
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002675 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002676 boolean isLastTaskOverHome = false;
riddle_hsuc215a4f2014-12-27 12:10:45 +08002677 // If the moving task is over home stack, transfer its return type to next task
2678 if (task.isOverHomeStack()) {
2679 final TaskRecord nextTask = getNextTask(task);
2680 if (nextTask != null) {
2681 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002682 } else {
2683 isLastTaskOverHome = true;
riddle_hsuc215a4f2014-12-27 12:10:45 +08002684 }
2685 }
2686
Craig Mautner9c85c202013-10-04 20:11:26 -07002687 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002688 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002689 if (isOnHomeDisplay()) {
2690 ActivityStack lastStack = mStackSupervisor.getLastStack();
2691 final boolean fromHome = lastStack.isHomeStack();
Jiaquan Hee13e9642016-06-15 15:16:13 -07002692 final boolean fromOnTopLauncher = lastStack.topTask() != null &&
2693 lastStack.topTask().isOnTopLauncher();
2694 if (fromOnTopLauncher) {
2695 // Since an on-top launcher will is moved to back when tasks are launched from it,
2696 // those tasks should first try to return to a non-home activity.
2697 // This also makes sure that non-home activities are visible under a transparent
2698 // non-home activity.
2699 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
2700 } else if (!isHomeStack() && (fromHome || topTask() != task)) {
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002701 // If it's a last task over home - we default to keep its return to type not to
2702 // make underlying task focused when this one will be finished.
2703 int returnToType = isLastTaskOverHome
2704 ? task.getTaskToReturnTo() : APPLICATION_ACTIVITY_TYPE;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002705 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2706 returnToType = lastStack.topTask() == null
2707 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2708 }
2709 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002710 }
2711 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002712 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002713 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002714
Craig Mautnerac6f8432013-07-17 13:24:59 -07002715 mTaskHistory.remove(task);
2716 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002717 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002718 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002719 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002720 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002721 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002722 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002723 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002724 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002725 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002726 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002727 break;
2728 }
2729 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002730 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002731 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002732 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002733 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002734 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002735
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002736 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2737 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002738 TaskRecord rTask = r.task;
2739 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002740 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2741 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002742 // Last activity in task had been removed or ActivityManagerService is reusing task.
2743 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002744 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002745 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002746 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002747 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002748 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 if (!newTask) {
2750 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002751 boolean startIt = true;
2752 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2753 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002754 if (task.getTopActivity() == null) {
2755 // All activities in task are finishing.
2756 continue;
2757 }
Craig Mautner70a86932013-02-28 22:37:44 -08002758 if (task == r.task) {
2759 // Here it is! Now, if this is not yet visible to the
2760 // user, then just add it without starting; it will
2761 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002762 if (!startIt) {
2763 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2764 + task, new RuntimeException("here").fillInStackTrace());
2765 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002766 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002767 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002768 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002769 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002771 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 return;
2773 }
2774 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002775 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002776 startIt = false;
2777 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002778 }
2779 }
2780
2781 // Place a new activity at top of stack, so it is next to interact
2782 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002783
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 // If we are not placing the new activity frontmost, we do not want
2785 // to deliver the onUserLeaving callback to the actual frontmost
2786 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002787 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002788 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002789 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002790 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002791 }
Craig Mautner70a86932013-02-28 22:37:44 -08002792
2793 task = r.task;
2794
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002796 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002797 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002798 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002799 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002800
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002801 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002802 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002803 // We want to show the starting preview window if we are
2804 // switching to a new task, or the next activity's process is
2805 // not currently running.
2806 boolean showStartingIcon = newTask;
2807 ProcessRecord proc = r.app;
2808 if (proc == null) {
2809 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2810 }
2811 if (proc == null || proc.thread == null) {
2812 showStartingIcon = true;
2813 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002814 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002815 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002816 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002817 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002818 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002820 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002821 ? r.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002822 ? TRANSIT_TASK_OPEN_BEHIND
2823 : TRANSIT_TASK_OPEN
2824 : TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002825 mNoAnimActivities.remove(r);
2826 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002827 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828 boolean doShow = true;
2829 if (newTask) {
2830 // Even though this activity is starting fresh, we still need
2831 // to reset it to make sure we apply affinities to move any
2832 // existing activities from other tasks in to it.
2833 // If the caller has requested that the target task be
2834 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002835 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002836 resetTaskIfNeededLocked(r, r);
2837 doShow = topRunningNonDelayedActivityLocked(null) == r;
2838 }
Chong Zhang280d3322015-11-03 17:27:26 -08002839 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002840 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2841 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002843 if (r.mLaunchTaskBehind) {
2844 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2845 // tell WindowManager that r is visible even though it is at the back of the stack.
2846 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002847 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002848 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849 // Figure out if we are transitioning from another activity that is
2850 // "has the same starting icon" as the next one. This allows the
2851 // window manager to keep the previous window it had previously
2852 // created, if it still had one.
Wale Ogunwale3b232392016-05-13 15:37:13 -07002853 ActivityRecord prev = r.task.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002854 if (prev != null) {
2855 // We don't want to reuse the previous starting preview if:
2856 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002857 if (prev.task != r.task) {
2858 prev = null;
2859 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002861 else if (prev.nowVisible) {
2862 prev = null;
2863 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002864 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07002865 r.showStartingWindow(prev, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002866 }
2867 } else {
2868 // If this is the first activity, don't do any fancy animations,
2869 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002870 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002871 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002872 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 }
2874 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002875 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002876 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002877 }
2878
Dianne Hackbornbe707852011-11-11 14:32:10 -08002879 final void validateAppTokensLocked() {
2880 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002881 mValidateAppTokens.ensureCapacity(numActivities());
2882 final int numTasks = mTaskHistory.size();
2883 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2884 TaskRecord task = mTaskHistory.get(taskNdx);
2885 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002886 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002887 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002888 }
Craig Mautner000f0022013-02-26 15:04:29 -08002889 TaskGroup group = new TaskGroup();
2890 group.taskId = task.taskId;
2891 mValidateAppTokens.add(group);
2892 final int numActivities = activities.size();
2893 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2894 final ActivityRecord r = activities.get(activityNdx);
2895 group.tokens.add(r.appToken);
2896 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002897 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002898 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002899 }
2900
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002901 /**
2902 * Perform a reset of the given task, if needed as part of launching it.
2903 * Returns the new HistoryRecord at the top of the task.
2904 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002905 /**
2906 * Helper method for #resetTaskIfNeededLocked.
2907 * We are inside of the task being reset... we'll either finish this activity, push it out
2908 * for another task, or leave it as-is.
2909 * @param task The task containing the Activity (taskTop) that might be reset.
2910 * @param forceReset
2911 * @return An ActivityOptions that needs to be processed.
2912 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002913 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002914 ActivityOptions topOptions = null;
2915
2916 int replyChainEnd = -1;
2917 boolean canMoveOptions = true;
2918
2919 // We only do this for activities that are not the root of the task (since if we finish
2920 // the root, we may no longer have the task!).
2921 final ArrayList<ActivityRecord> activities = task.mActivities;
2922 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002923 final int rootActivityNdx = task.findEffectiveRootIndex();
2924 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002925 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002926 if (target.frontOfTask)
2927 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002928
2929 final int flags = target.info.flags;
2930 final boolean finishOnTaskLaunch =
2931 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2932 final boolean allowTaskReparenting =
2933 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2934 final boolean clearWhenTaskReset =
2935 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2936
2937 if (!finishOnTaskLaunch
2938 && !clearWhenTaskReset
2939 && target.resultTo != null) {
2940 // If this activity is sending a reply to a previous
2941 // activity, we can't do anything with it now until
2942 // we reach the start of the reply chain.
2943 // XXX note that we are assuming the result is always
2944 // to the previous activity, which is almost always
2945 // the case but we really shouldn't count on.
2946 if (replyChainEnd < 0) {
2947 replyChainEnd = i;
2948 }
2949 } else if (!finishOnTaskLaunch
2950 && !clearWhenTaskReset
2951 && allowTaskReparenting
2952 && target.taskAffinity != null
2953 && !target.taskAffinity.equals(task.affinity)) {
2954 // If this activity has an affinity for another
2955 // task, then we need to move it out of here. We will
2956 // move it as far out of the way as possible, to the
2957 // bottom of the activity stack. This also keeps it
2958 // correctly ordered with any activities we previously
2959 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002960 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002961 final ActivityRecord bottom =
2962 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002963 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002964 if (bottom != null && target.taskAffinity != null
2965 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002966 // If the activity currently at the bottom has the
2967 // same task affinity as the one we are moving,
2968 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002969 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002970 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002971 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002972 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002973 targetTask = createTaskRecord(
2974 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2975 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002976 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002977 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002978 + " out to new task " + target.task);
2979 }
2980
Wale Ogunwale706ed792015-08-02 10:29:44 -07002981 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002982
Craig Mautner525f3d92013-05-07 14:01:50 -07002983 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002984 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2985 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002986 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002987 if (p.finishing) {
2988 continue;
2989 }
2990
Craig Mautnere3a74d52013-02-22 14:14:58 -08002991 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002992 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002993 topOptions = p.takeOptionsLocked();
2994 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002995 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002996 }
2997 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002998 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2999 "Removing activity " + p + " from task=" + task + " adding to task="
3000 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003001 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3002 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07003003 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003004 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08003005
Wale Ogunwale706ed792015-08-02 10:29:44 -07003006 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003007 }
3008
Wale Ogunwale706ed792015-08-02 10:29:44 -07003009 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003010 if (VALIDATE_TOKENS) {
3011 validateAppTokensLocked();
3012 }
3013
3014 replyChainEnd = -1;
3015 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
3016 // If the activity should just be removed -- either
3017 // because it asks for it, or the task should be
3018 // cleared -- then finish it and anything that is
3019 // part of its reply chain.
3020 int end;
3021 if (clearWhenTaskReset) {
3022 // In this case, we want to finish this activity
3023 // and everything above it, so be sneaky and pretend
3024 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08003025 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003026 } else if (replyChainEnd < 0) {
3027 end = i;
3028 } else {
3029 end = replyChainEnd;
3030 }
Craig Mautner525f3d92013-05-07 14:01:50 -07003031 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003032 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003033 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003034 if (p.finishing) {
3035 continue;
3036 }
3037 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003038 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003039 topOptions = p.takeOptionsLocked();
3040 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003041 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003042 }
3043 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003044 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08003045 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08003046 if (finishActivityLocked(
3047 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003048 end--;
3049 srcPos--;
3050 }
3051 }
3052 replyChainEnd = -1;
3053 } else {
3054 // If we were in the middle of a chain, well the
3055 // activity that started it all doesn't want anything
3056 // special, so leave it all as-is.
3057 replyChainEnd = -1;
3058 }
3059 }
3060
3061 return topOptions;
3062 }
3063
3064 /**
3065 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
3066 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
3067 * @param affinityTask The task we are looking for an affinity to.
3068 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
3069 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
3070 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
3071 */
Craig Mautner525f3d92013-05-07 14:01:50 -07003072 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08003073 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003074 int replyChainEnd = -1;
3075 final int taskId = task.taskId;
3076 final String taskAffinity = task.affinity;
3077
3078 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
3079 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003080 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
3081
3082 // Do not operate on or below the effective root Activity.
3083 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003084 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003085 if (target.frontOfTask)
3086 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003087
3088 final int flags = target.info.flags;
3089 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3090 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3091
3092 if (target.resultTo != null) {
3093 // If this activity is sending a reply to a previous
3094 // activity, we can't do anything with it now until
3095 // we reach the start of the reply chain.
3096 // XXX note that we are assuming the result is always
3097 // to the previous activity, which is almost always
3098 // the case but we really shouldn't count on.
3099 if (replyChainEnd < 0) {
3100 replyChainEnd = i;
3101 }
3102 } else if (topTaskIsHigher
3103 && allowTaskReparenting
3104 && taskAffinity != null
3105 && taskAffinity.equals(target.taskAffinity)) {
3106 // This activity has an affinity for our task. Either remove it if we are
3107 // clearing or move it over to our task. Note that
3108 // we currently punt on the case where we are resetting a
3109 // task that is not at the top but who has activities above
3110 // with an affinity to it... this is really not a normal
3111 // case, and we will need to later pull that task to the front
3112 // and usually at that point we will do the reset and pick
3113 // up those remaining activities. (This only happens if
3114 // someone starts an activity in a new task from an activity
3115 // in a task that is not currently on top.)
3116 if (forceReset || finishOnTaskLaunch) {
3117 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003118 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3119 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003120 for (int srcPos = start; srcPos >= i; --srcPos) {
3121 final ActivityRecord p = activities.get(srcPos);
3122 if (p.finishing) {
3123 continue;
3124 }
Todd Kennedy539db512014-12-15 09:57:55 -08003125 finishActivityLocked(
3126 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003127 }
3128 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003129 if (taskInsertionPoint < 0) {
3130 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003131
Craig Mautner77878772013-03-04 19:46:24 -08003132 }
Craig Mautner77878772013-03-04 19:46:24 -08003133
3134 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003135 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3136 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3137 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003138 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003139 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07003140 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08003141 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003142
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003143 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3144 "Removing and adding activity " + p + " to stack at " + task
3145 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003146 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3147 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003148 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003149 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003150 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08003151 if (VALIDATE_TOKENS) {
3152 validateAppTokensLocked();
3153 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08003154
3155 // Now we've moved it in to place... but what if this is
3156 // a singleTop activity and we have put it on top of another
3157 // instance of the same activity? Then we drop the instance
3158 // below so it remains singleTop.
3159 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3160 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003161 int targetNdx = taskActivities.indexOf(target);
3162 if (targetNdx > 0) {
3163 ActivityRecord p = taskActivities.get(targetNdx - 1);
3164 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003165 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3166 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003167 }
3168 }
3169 }
3170 }
3171
3172 replyChainEnd = -1;
3173 }
3174 }
Craig Mautner77878772013-03-04 19:46:24 -08003175 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003176 }
3177
Craig Mautner8849a5e2013-04-02 16:41:03 -07003178 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003179 ActivityRecord newActivity) {
3180 boolean forceReset =
3181 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
3182 if (ACTIVITY_INACTIVE_RESET_TIME > 0
3183 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
3184 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
3185 forceReset = true;
3186 }
3187 }
3188
3189 final TaskRecord task = taskTop.task;
3190
3191 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3192 * for remaining tasks. Used for later tasks to reparent to task. */
3193 boolean taskFound = false;
3194
3195 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3196 ActivityOptions topOptions = null;
3197
Craig Mautner77878772013-03-04 19:46:24 -08003198 // Preserve the location for reparenting in the new task.
3199 int reparentInsertionPoint = -1;
3200
Craig Mautnere3a74d52013-02-22 14:14:58 -08003201 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3202 final TaskRecord targetTask = mTaskHistory.get(i);
3203
3204 if (targetTask == task) {
3205 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3206 taskFound = true;
3207 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003208 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3209 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003210 }
3211 }
3212
Craig Mautner70a86932013-02-28 22:37:44 -08003213 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003214 if (taskNdx >= 0) {
3215 do {
3216 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3217 } while (taskTop == null && taskNdx >= 0);
3218 }
Craig Mautner70a86932013-02-28 22:37:44 -08003219
Craig Mautnere3a74d52013-02-22 14:14:58 -08003220 if (topOptions != null) {
3221 // If we got some ActivityOptions from an activity on top that
3222 // was removed from the task, propagate them to the new real top.
3223 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003224 taskTop.updateOptionsLocked(topOptions);
3225 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003226 topOptions.abort();
3227 }
3228 }
3229
3230 return taskTop;
3231 }
3232
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3234 String resultWho, int requestCode, int resultCode, Intent data) {
3235
3236 if (callingUid > 0) {
3237 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003238 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003239 }
3240
3241 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3242 + " : who=" + resultWho + " req=" + requestCode
3243 + " res=" + resultCode + " data=" + data);
3244 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3245 try {
3246 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3247 list.add(new ResultInfo(resultWho, requestCode,
3248 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003249 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 return;
3251 } catch (Exception e) {
3252 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3253 }
3254 }
3255
3256 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3257 }
3258
Chong Zhang6cda19c2016-06-14 19:07:56 -07003259 private void adjustFocusedActivityStackLocked(ActivityRecord r, String reason) {
3260 if (!mStackSupervisor.isFocusedStack(this) || mResumedActivity != r) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003261 return;
3262 }
3263
3264 final ActivityRecord next = topRunningActivityLocked();
3265 final String myReason = reason + " adjustFocus";
3266 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003267 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003268 // For freeform, docked, and pinned stacks we always keep the focus within the
Chong Zhang6cda19c2016-06-14 19:07:56 -07003269 // stack as long as there is a running activity.
Wale Ogunwaled045c822015-12-02 09:14:28 -08003270 return;
3271 } else {
3272 final TaskRecord task = r.task;
3273 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003274 final int taskToReturnTo = task.getTaskToReturnTo();
3275
Wale Ogunwaled045c822015-12-02 09:14:28 -08003276 // For non-fullscreen stack, we want to move the focus to the next visible
3277 // stack to prevent the home screen from moving to the top and obscuring
3278 // other visible stacks.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003279 if (!mFullscreen
3280 && adjustFocusToNextFocusableStackLocked(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003281 return;
3282 }
3283 // Move the home stack to the top if this stack is fullscreen or there is no
3284 // other visible stack.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003285 if (mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003286 // Activity focus was already adjusted. Nothing else to do...
3287 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003288 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003289 }
3290 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003291 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003292
Chong Zhang6cda19c2016-06-14 19:07:56 -07003293 mStackSupervisor.moveFocusableActivityStackToFrontLocked(
3294 mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003295 }
3296
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003297 private boolean adjustFocusToNextFocusableStackLocked(int taskToReturnTo, String reason) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003298 final ActivityStack stack = getNextFocusableStackLocked();
3299 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003300 if (stack == null) {
3301 return false;
3302 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003303
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003304 final ActivityRecord top = stack.topRunningActivityLocked();
3305
3306 if (stack.isHomeStack() && (top == null || !top.visible)) {
3307 // If we will be focusing on the home stack next and its current top activity isn't
3308 // visible, then use the task return to value to determine the home task to display next.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003309 return mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, reason);
3310 }
Chong Zhang6cda19c2016-06-14 19:07:56 -07003311
3312 stack.moveToFront(myReason);
3313 return true;
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003314 }
3315
Craig Mautnerf3333272013-04-22 10:55:53 -07003316 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003317 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3319 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3320 if (!r.finishing) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003321 if (!mService.isSleepingLocked()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003322 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003323 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3324 "stop-no-history", false)) {
3325 // Activity was finished, no need to continue trying to schedule stop.
Chong Zhang6cda19c2016-06-14 19:07:56 -07003326 adjustFocusedActivityStackLocked(r, "stopActivityFinished");
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003327 r.resumeKeyDispatchingLocked();
3328 return;
3329 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003330 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003331 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003332 + " on stop because we're just sleeping");
3333 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003335 }
3336
3337 if (r.app != null && r.app.thread != null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003338 adjustFocusedActivityStackLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003339 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003340 try {
3341 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003342 if (DEBUG_STATES) Slog.v(TAG_STATES,
3343 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003344 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003345 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3346 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003348 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003350 EventLogTags.writeAmStopActivity(
3351 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003352 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003353 if (mService.isSleepingOrShuttingDownLocked()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003354 r.setSleeping(true);
3355 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003356 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003357 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 } catch (Exception e) {
3359 // Maybe just ignore exceptions here... if the process
3360 // has crashed, our death notification will clean things
3361 // up.
3362 Slog.w(TAG, "Exception thrown during pause", e);
3363 // Just in case, assume it to be stopped.
3364 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003365 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003366 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003367 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003368 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 }
3370 }
3371 }
3372 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003373
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003374 /**
3375 * @return Returns true if the activity is being finished, false if for
3376 * some reason it is being left as-is.
3377 */
3378 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003379 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003380 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003381 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3382 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003383 + ", result=" + resultCode + ", data=" + resultData
3384 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003385 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 return false;
3387 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003388
Craig Mautnerd44711d2013-02-23 11:24:36 -08003389 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003390 return true;
3391 }
3392
Craig Mautnerd2328952013-03-05 12:46:26 -08003393 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003394 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3395 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3396 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3397 ActivityRecord r = activities.get(activityNdx);
3398 if (r.resultTo == self && r.requestCode == requestCode) {
3399 if ((r.resultWho == null && resultWho == null) ||
3400 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3401 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3402 false);
3403 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003404 }
3405 }
3406 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003407 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003408 }
3409
Adrian Roos20d7df32016-01-12 18:59:43 +01003410 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003411 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003412 TaskRecord finishedTask = null;
3413 if (r == null || r.app != app) {
3414 return null;
3415 }
3416 Slog.w(TAG, " Force finishing activity "
3417 + r.intent.getComponent().flattenToShortString());
3418 int taskNdx = mTaskHistory.indexOf(r.task);
3419 int activityNdx = r.task.mActivities.indexOf(r);
3420 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3421 finishedTask = r.task;
3422 // Also terminate any activities below it that aren't yet
3423 // stopped, to avoid a situation where one will get
3424 // re-start our crashing activity once it gets resumed again.
3425 --activityNdx;
3426 if (activityNdx < 0) {
3427 do {
3428 --taskNdx;
3429 if (taskNdx < 0) {
3430 break;
3431 }
3432 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3433 } while (activityNdx < 0);
3434 }
3435 if (activityNdx >= 0) {
3436 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3437 if (r.state == ActivityState.RESUMED
3438 || r.state == ActivityState.PAUSING
3439 || r.state == ActivityState.PAUSED) {
3440 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3441 Slog.w(TAG, " Force finishing activity "
3442 + r.intent.getComponent().flattenToShortString());
3443 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003444 }
3445 }
3446 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003447 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003448 }
3449
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003450 final void finishVoiceTask(IVoiceInteractionSession session) {
3451 IBinder sessionBinder = session.asBinder();
3452 boolean didOne = false;
3453 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3454 TaskRecord tr = mTaskHistory.get(taskNdx);
3455 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3456 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3457 ActivityRecord r = tr.mActivities.get(activityNdx);
3458 if (!r.finishing) {
3459 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3460 false);
3461 didOne = true;
3462 }
3463 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003464 } else {
3465 // Check if any of the activities are using voice
3466 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3467 ActivityRecord r = tr.mActivities.get(activityNdx);
3468 if (r.voiceSession != null
3469 && r.voiceSession.asBinder() == sessionBinder) {
3470 // Inform of cancellation
3471 r.clearVoiceSessionLocked();
3472 try {
3473 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3474 null);
3475 } catch (RemoteException re) {
3476 // Ok
3477 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003478 mService.finishRunningVoiceLocked();
3479 break;
3480 }
3481 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003482 }
3483 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003484
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003485 if (didOne) {
3486 mService.updateOomAdjLocked();
3487 }
3488 }
3489
Craig Mautnerd2328952013-03-05 12:46:26 -08003490 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003491 ArrayList<ActivityRecord> activities = r.task.mActivities;
3492 for (int index = activities.indexOf(r); index >= 0; --index) {
3493 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003494 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003495 break;
3496 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003497 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003498 }
3499 return true;
3500 }
3501
Dianne Hackborn5c607432012-02-28 14:44:19 -08003502 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3503 // send the result
3504 ActivityRecord resultTo = r.resultTo;
3505 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003506 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003507 + " who=" + r.resultWho + " req=" + r.requestCode
3508 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003509 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003510 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003511 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003512 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003513 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003514 if (r.info.applicationInfo.uid > 0) {
3515 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3516 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003517 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003518 }
3519 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3520 resultData);
3521 r.resultTo = null;
3522 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003523 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003524
3525 // Make sure this HistoryRecord is not holding on to other resources,
3526 // because clients have remote IPC references to this object so we
3527 // can't assume that will go away and want to avoid circular IPC refs.
3528 r.results = null;
3529 r.pendingResults = null;
3530 r.newIntents = null;
3531 r.icicle = null;
3532 }
3533
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003534 /**
3535 * @return Returns true if this activity has been removed from the history
3536 * list, or false if it is still in the list and will be removed later.
3537 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003538 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3539 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003540 if (r.finishing) {
3541 Slog.w(TAG, "Duplicate finish request for " + r);
3542 return false;
3543 }
3544
Wale Ogunwale7d701172015-03-11 15:36:30 -07003545 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003546 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003547 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003548 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003549 task.taskId, r.shortComponentName, reason);
3550 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003551 final int index = activities.indexOf(r);
3552 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003553 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003554 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003555 // If the caller asked that this activity (and all above it)
3556 // be cleared when the task is reset, don't lose that information,
3557 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003558 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003559 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003560 }
3561 }
3562
3563 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003564
Chong Zhang6cda19c2016-06-14 19:07:56 -07003565 adjustFocusedActivityStackLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003566
Dianne Hackborn5c607432012-02-28 14:44:19 -08003567 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003568
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003569 final boolean endTask = index <= 0;
3570 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003571 if (mResumedActivity == r) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003572
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003573 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003574 "Prepare close transition: finishing " + r);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003575 mWindowManager.prepareAppTransition(transit, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003576
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003577 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003578 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003579
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003580 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003581 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3582 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3583 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003584 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 }
3586
Craig Mautneraea74a52014-03-08 14:23:10 -08003587 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003588 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003589 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003590 } else if (r.state != ActivityState.PAUSING) {
3591 // If the activity is PAUSING, we will complete the finish once
3592 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003593 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003594 if (r.visible) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003595 mWindowManager.prepareAppTransition(transit, false);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003596 mWindowManager.setAppVisibility(r.appToken, false);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003597 mWindowManager.executeAppTransition();
Chong Zhangb8bd0692016-04-27 17:29:28 -07003598 if (!mStackSupervisor.mWaitingVisibleActivities.contains(r)) {
3599 mStackSupervisor.mWaitingVisibleActivities.add(r);
3600 }
Chong Zhang66ed4c62015-11-12 14:56:28 -08003601 }
Chong Zhang4c5f7ff2016-04-22 11:31:51 -07003602 return finishCurrentActivityLocked(r, (r.visible || r.nowVisible) ?
3603 FINISH_AFTER_VISIBLE : FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003604 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003605 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003606 }
3607
3608 return false;
3609 }
3610
Craig Mautnerf3333272013-04-22 10:55:53 -07003611 static final int FINISH_IMMEDIATELY = 0;
3612 static final int FINISH_AFTER_PAUSE = 1;
3613 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003614
Craig Mautnerf3333272013-04-22 10:55:53 -07003615 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003616 // First things first: if this activity is currently visible,
3617 // and the resumed activity is not yet visible, then hold off on
3618 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003619
3620 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
3621
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003622 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003623 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003624 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003625 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003626 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003627 if (DEBUG_STATES) Slog.v(TAG_STATES,
3628 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003629 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003630 if (oomAdj) {
3631 mService.updateOomAdjLocked();
3632 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003633 return r;
3634 }
3635
3636 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003637 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003638 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003639 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640 if (mResumedActivity == r) {
3641 mResumedActivity = null;
3642 }
3643 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003644 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003645 r.state = ActivityState.FINISHING;
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003646 final boolean finishingActivityInNonFocusedStack
3647 = r.task.stack != mStackSupervisor.getFocusedStack()
3648 && prevState == ActivityState.PAUSED && mode == FINISH_AFTER_VISIBLE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003649
3650 if (mode == FINISH_IMMEDIATELY
Wale Ogunwaleae30f302016-05-02 10:35:45 -07003651 || (prevState == ActivityState.PAUSED
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003652 && (mode == FINISH_AFTER_PAUSE || mStackId == PINNED_STACK_ID))
3653 || finishingActivityInNonFocusedStack
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654 || prevState == ActivityState.STOPPED
3655 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003656 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003657 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Andrii Kulian7318d632016-07-20 18:59:28 -07003658
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003659 if (finishingActivityInNonFocusedStack) {
3660 // Finishing activity that was in paused state and it was in not currently focused
3661 // stack, need to make something visible in its place.
Andrii Kulian7318d632016-07-20 18:59:28 -07003662 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3663 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003664 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003665 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003666 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003667 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003668 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3669 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003670 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003671 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003672
3673 // Need to go through the full pause cycle to get this
3674 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003675 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003676 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003677 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003678 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003679 return r;
3680 }
3681
Craig Mautneree36c772014-07-16 14:56:05 -07003682 void finishAllActivitiesLocked(boolean immediately) {
3683 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003684 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3685 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3686 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3687 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003688 noActivitiesInStack = false;
3689 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003690 continue;
3691 }
Craig Mautneree36c772014-07-16 14:56:05 -07003692 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003693 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3694 }
3695 }
Craig Mautneree36c772014-07-16 14:56:05 -07003696 if (noActivitiesInStack) {
3697 mActivityContainer.onTaskListEmptyLocked();
3698 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003699 }
3700
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003701 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3702 // Basic case: for simple app-centric recents, we need to recreate
3703 // the task if the affinity has changed.
3704 if (srec == null || srec.task.affinity == null ||
3705 !srec.task.affinity.equals(destAffinity)) {
3706 return true;
3707 }
3708 // Document-centric case: an app may be split in to multiple documents;
3709 // they need to re-create their task if this current activity is the root
3710 // of a document, unless simply finishing it will return them to the the
3711 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003712 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3713 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003714 // Okay, this activity is at the root of its task. What to do, what to do...
3715 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3716 // Finishing won't return to an application, so we need to recreate.
3717 return true;
3718 }
3719 // We now need to get the task below it to determine what to do.
3720 int taskIdx = mTaskHistory.indexOf(srec.task);
3721 if (taskIdx <= 0) {
3722 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3723 return false;
3724 }
3725 if (taskIdx == 0) {
3726 // At the bottom of the stack, nothing to go back to.
3727 return true;
3728 }
3729 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3730 if (!srec.task.affinity.equals(prevTask.affinity)) {
3731 // These are different apps, so need to recreate.
3732 return true;
3733 }
3734 }
3735 return false;
3736 }
3737
Wale Ogunwale7d701172015-03-11 15:36:30 -07003738 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003739 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003740 final TaskRecord task = srec.task;
3741 final ArrayList<ActivityRecord> activities = task.mActivities;
3742 final int start = activities.indexOf(srec);
3743 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003744 return false;
3745 }
3746 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003747 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003748 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003749 final ComponentName dest = destIntent.getComponent();
3750 if (start > 0 && dest != null) {
3751 for (int i = finishTo; i >= 0; i--) {
3752 ActivityRecord r = activities.get(i);
3753 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003754 r.info.name.equals(dest.getClassName())) {
3755 finishTo = i;
3756 parent = r;
3757 foundParentInTask = true;
3758 break;
3759 }
3760 }
3761 }
3762
3763 IActivityController controller = mService.mController;
3764 if (controller != null) {
3765 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3766 if (next != null) {
3767 // ask watcher if this is allowed
3768 boolean resumeOK = true;
3769 try {
3770 resumeOK = controller.activityResuming(next.packageName);
3771 } catch (RemoteException e) {
3772 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003773 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003774 }
3775
3776 if (!resumeOK) {
3777 return false;
3778 }
3779 }
3780 }
3781 final long origId = Binder.clearCallingIdentity();
3782 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003783 ActivityRecord r = activities.get(i);
3784 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003785 // Only return the supplied result for the first activity finished
3786 resultCode = Activity.RESULT_CANCELED;
3787 resultData = null;
3788 }
3789
3790 if (parent != null && foundParentInTask) {
3791 final int parentLaunchMode = parent.info.launchMode;
3792 final int destIntentFlags = destIntent.getFlags();
3793 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3794 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3795 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3796 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003797 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3798 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003799 } else {
3800 try {
3801 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3802 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003803 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3804 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3805 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003806 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3807 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003808 foundParentInTask = res == ActivityManager.START_SUCCESS;
3809 } catch (RemoteException e) {
3810 foundParentInTask = false;
3811 }
3812 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003813 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003814 }
3815 }
3816 Binder.restoreCallingIdentity(origId);
3817 return foundParentInTask;
3818 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003819 /**
3820 * Perform the common clean-up of an activity record. This is called both
3821 * as part of destroyActivityLocked() (when destroying the client-side
3822 * representation) and cleaning things up as a result of its hosting
3823 * processing going away, in which case there is no remaining client-side
3824 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003825 *
3826 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003827 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003828 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3829 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003830 if (mResumedActivity == r) {
3831 mResumedActivity = null;
3832 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003833 if (mPausingActivity == r) {
3834 mPausingActivity = null;
3835 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003836
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003837 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003838 r.frozenBeforeDestroy = false;
3839
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003840 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003841 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003842 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003843 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003844 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003845 }
3846
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003847 // Make sure this record is no longer in the pending finishes list.
3848 // This could happen, for example, if we are trimming activities
3849 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003850 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003851 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003852
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003853 // Remove any pending results.
3854 if (r.finishing && r.pendingResults != null) {
3855 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3856 PendingIntentRecord rec = apr.get();
3857 if (rec != null) {
3858 mService.cancelIntentSenderLocked(rec, false);
3859 }
3860 }
3861 r.pendingResults = null;
3862 }
3863
3864 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003865 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003866 }
3867
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003868 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003869 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003870 if (getVisibleBehindActivity() == r) {
3871 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003872 }
Wale Ogunwale8fd75422016-06-24 14:20:37 -07003873
3874 // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
3875 // manager so it can update its bookkeeping.
3876 mWindowManager.notifyAppRelaunchesCleared(r.appToken);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003877 }
3878
3879 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003880 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003881 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003882 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003883 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003884 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003885 }
3886
Chong Zhange4cf36f2016-06-03 14:38:33 -07003887 private void removeActivityFromHistoryLocked(
3888 ActivityRecord r, TaskRecord oldTop, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003889 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003890 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003891 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003892 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3893 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3894
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003895 r.takeFromHistory();
3896 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003897 if (DEBUG_STATES) Slog.v(TAG_STATES,
3898 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003899 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003900 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003901 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003902 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003903 if (VALIDATE_TOKENS) {
3904 validateAppTokensLocked();
3905 }
Craig Mautner312ba862014-02-10 17:55:01 -08003906 final TaskRecord task = r.task;
Chong Zhange4cf36f2016-06-03 14:38:33 -07003907 final TaskRecord topTask = oldTop != null ? oldTop : topTask();
Craig Mautner312ba862014-02-10 17:55:01 -08003908 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003909 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003910 "removeActivityFromHistoryLocked: last activity removed from " + this);
Chong Zhange4cf36f2016-06-03 14:38:33 -07003911 if (mStackSupervisor.isFocusedStack(this) && task == topTask &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003912 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003913 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003914 }
Craig Mautner299f9602015-01-26 09:47:33 -08003915 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003916 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003917 cleanUpActivityServicesLocked(r);
3918 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003919 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003920
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003921 /**
3922 * Perform clean-up of service connections in an activity record.
3923 */
3924 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3925 // Throw away any services that have been bound by this activity.
3926 if (r.connections != null) {
3927 Iterator<ConnectionRecord> it = r.connections.iterator();
3928 while (it.hasNext()) {
3929 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003930 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003931 }
3932 r.connections = null;
3933 }
3934 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003935
Craig Mautneree2e45a2014-06-27 12:10:03 -07003936 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003937 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003938 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003939 mHandler.sendMessage(msg);
3940 }
3941
Craig Mautneree2e45a2014-06-27 12:10:03 -07003942 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003943 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003944 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003945 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3946 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3947 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3948 final ActivityRecord r = activities.get(activityNdx);
3949 if (r.finishing) {
3950 continue;
3951 }
3952 if (r.fullscreen) {
3953 lastIsOpaque = true;
3954 }
3955 if (owner != null && r.app != owner) {
3956 continue;
3957 }
3958 if (!lastIsOpaque) {
3959 continue;
3960 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003961 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003962 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003963 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003964 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003965 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003966 activityRemoved = true;
3967 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003968 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003969 }
3970 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003971 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003972 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003973 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003974 }
3975
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003976 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3977 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003978 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3979 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003980 + " pausing=" + mPausingActivity + " for reason " + reason);
3981 return destroyActivityLocked(r, true, reason);
3982 }
3983 return false;
3984 }
3985
3986 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3987 String reason) {
3988 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003989 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003990 int maxTasks = tasks.size() / 4;
3991 if (maxTasks < 1) {
3992 maxTasks = 1;
3993 }
3994 int numReleased = 0;
3995 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3996 final TaskRecord task = mTaskHistory.get(taskNdx);
3997 if (!tasks.contains(task)) {
3998 continue;
3999 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004000 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004001 int curNum = 0;
4002 final ArrayList<ActivityRecord> activities = task.mActivities;
4003 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
4004 final ActivityRecord activity = activities.get(actNdx);
4005 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004006 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004007 + " in state " + activity.state + " resumed=" + mResumedActivity
4008 + " pausing=" + mPausingActivity + " for reason " + reason);
4009 destroyActivityLocked(activity, true, reason);
4010 if (activities.get(actNdx) != activity) {
4011 // Was removed from list, back up so we don't miss the next one.
4012 actNdx--;
4013 }
4014 curNum++;
4015 }
4016 }
4017 if (curNum > 0) {
4018 numReleased += curNum;
4019 maxTasks--;
4020 if (mTaskHistory.get(taskNdx) != task) {
4021 // The entire task got removed, back up so we don't miss the next one.
4022 taskNdx--;
4023 }
4024 }
4025 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004026 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
4027 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004028 return numReleased;
4029 }
4030
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004031 /**
4032 * Destroy the current CLIENT SIDE instance of an activity. This may be
4033 * called both when actually finishing an activity, or when performing
4034 * a configuration switch where we destroy the current client-side object
4035 * but then create a new client-side object for this same HistoryRecord.
4036 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07004037 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004038 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
4039 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004040 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004041 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004042 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07004043 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004044
4045 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004046
Chong Zhange4cf36f2016-06-03 14:38:33 -07004047 // If the activity is finishing, it's no longer considered in topRunningActivityLocked,
4048 // and cleanUpActivityLocked() may change focus to another activity (or task).
4049 // Get the current top task now, as removeActivityFromHistoryLocked() below need this
4050 // to decide whether to return to home stack after removal.
4051 final TaskRecord topTask = topTask();
4052
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004053 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004054
4055 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004056
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004057 if (hadApp) {
4058 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004059 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004060 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
4061 mService.mHeavyWeightProcess = null;
4062 mService.mHandler.sendEmptyMessage(
4063 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
4064 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004065 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07004066 // Update any services we are bound to that might care about whether
4067 // their client may have activities.
4068 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07004069 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07004070 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07004071 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004072 }
4073 }
4074
4075 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004076
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004077 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004078 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004079 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004080 r.configChangeFlags);
4081 } catch (Exception e) {
4082 // We can just ignore exceptions here... if the process
4083 // has crashed, our death notification will clean things
4084 // up.
4085 //Slog.w(TAG, "Exception thrown during finish", e);
4086 if (r.finishing) {
Chong Zhange4cf36f2016-06-03 14:38:33 -07004087 removeActivityFromHistoryLocked(
4088 r, topTask, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004089 removedFromHistory = true;
4090 skipDestroy = true;
4091 }
4092 }
4093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004094 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004095
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004096 // If the activity is finishing, we need to wait on removing it
4097 // from the list to give it a chance to do its cleanup. During
4098 // that time it may make calls back with its token so we need to
4099 // be able to find it on the list and so we don't want to remove
4100 // it from the list yet. Otherwise, we can just immediately put
4101 // it in the destroyed state since we are not removing it from the
4102 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004103 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004104 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004105 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004106 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07004107 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004108 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4109 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004110 if (DEBUG_STATES) Slog.v(TAG_STATES,
4111 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004112 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004113 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004114 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004115 }
4116 } else {
4117 // remove this record from the history.
4118 if (r.finishing) {
Chong Zhange4cf36f2016-06-03 14:38:33 -07004119 removeActivityFromHistoryLocked(r, topTask, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004120 removedFromHistory = true;
4121 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004122 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004123 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004124 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004125 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004126 }
4127 }
4128
4129 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004130
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004131 if (!mLRUActivities.remove(r) && hadApp) {
4132 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4133 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004135 return removedFromHistory;
4136 }
4137
Craig Mautner299f9602015-01-26 09:47:33 -08004138 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004139 final long origId = Binder.clearCallingIdentity();
4140 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004141 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08004142 if (r != null) {
4143 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004144 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004145 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08004146
Wale Ogunwale60454db2015-01-23 16:05:07 -08004147 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004148 if (r.state == ActivityState.DESTROYING) {
4149 cleanUpActivityLocked(r, true, false);
Chong Zhange4cf36f2016-06-03 14:38:33 -07004150 removeActivityFromHistoryLocked(r, null, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004151 }
4152 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004153 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08004154 } finally {
4155 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156 }
4157 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004158
Todd Kennedyaab56db2015-01-30 09:39:53 -08004159 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004160 if (hasVisibleBehindActivity() &&
4161 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08004162 if (r == topRunningActivityLocked()
4163 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07004164 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08004165 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07004166 return;
4167 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004168 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07004169 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07004170 " thread=" + r.app.thread);
4171 if (r != null && r.app != null && r.app.thread != null) {
4172 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07004173 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004174 } catch (RemoteException e) {
4175 }
Jose Lima4b6c6692014-08-12 17:41:12 -07004176 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004177 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07004178 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004179 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004180 }
4181 }
4182 }
4183
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004184 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07004185 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
4186 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004187 if (r != null) {
4188 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07004189 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004190 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004191 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004192 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004193 }
4194
Jose Lima4b6c6692014-08-12 17:41:12 -07004195 boolean hasVisibleBehindActivity() {
4196 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004197 }
4198
Jose Lima4b6c6692014-08-12 17:41:12 -07004199 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07004200 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004201 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004202 }
4203 }
4204
Jose Lima4b6c6692014-08-12 17:41:12 -07004205 ActivityRecord getVisibleBehindActivity() {
4206 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004207 }
4208
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004209 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4210 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004211 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004212 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4213 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004214 while (i > 0) {
4215 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004216 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004217 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004218 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004219 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004220 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004221 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004222 }
4223 }
4224 }
4225
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004226 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
4227 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004228 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4229 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004230 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4231 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004232 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004233 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07004234 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4235 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004236
4237 boolean hasVisibleActivities = false;
4238
4239 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004240 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004241 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4242 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004243 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4244 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4245 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4246 final ActivityRecord r = activities.get(activityNdx);
4247 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004248 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4249 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08004250 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004251 if (r.visible) {
4252 hasVisibleActivities = true;
4253 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004254 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004255 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4256 // Don't currently have state for the activity, or
4257 // it is finishing -- always remove it.
4258 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004259 } else if (!r.visible && r.launchCount > 2 &&
4260 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004261 // We have launched this activity too many times since it was
4262 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004263 // (Note if the activity is visible, we don't remove the record.
4264 // We leave the dead window on the screen but the process will
4265 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004266 remove = true;
4267 } else {
4268 // The process may be gone, but the activity lives on!
4269 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004270 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004271 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004272 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4273 "Removing activity " + r + " from stack at " + i
4274 + ": haveState=" + r.haveState
4275 + " stateNotNeeded=" + r.stateNotNeeded
4276 + " finishing=" + r.finishing
4277 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004278 if (!r.finishing) {
4279 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4280 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4281 r.userId, System.identityHashCode(r),
4282 r.task.taskId, r.shortComponentName,
4283 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07004284 if (r.state == ActivityState.RESUMED) {
4285 mService.updateUsageStats(r, false);
4286 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004287 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004288 } else {
4289 // We have the current state for this activity, so
4290 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004291 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4292 if (DEBUG_APP) Slog.v(TAG_APP,
4293 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004294 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004295 // Set nowVisible to previous visible state. If the app was visible while
4296 // it died, we leave the dead window on screen so it's basically visible.
4297 // This is needed when user later tap on the dead window, we need to stop
4298 // other apps when user transfers focus to the restarted activity.
4299 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004300 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004301 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004302 "App died, clearing saved state of " + r);
4303 r.icicle = null;
4304 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004305 }
Craig Mautnerd2328952013-03-05 12:46:26 -08004306 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004307 if (remove) {
Chong Zhange4cf36f2016-06-03 14:38:33 -07004308 removeActivityFromHistoryLocked(r, null, "appDied");
Craig Mautneracebdc82015-02-24 10:53:03 -08004309 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004310 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004311 }
4312 }
4313
4314 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004315 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004316
Chong Zhang280d3322015-11-03 17:27:26 -08004317 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004318 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004319 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004320 if (r != null && r.state != ActivityState.RESUMED) {
4321 r.updateOptionsLocked(options);
4322 } else {
4323 ActivityOptions.abort(options);
4324 }
4325 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004326 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004327 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004328
Craig Mautner21d24a22014-04-23 11:45:37 -07004329 void updateTaskMovement(TaskRecord task, boolean toFront) {
4330 if (task.isPersistable) {
4331 task.mLastTimeMoved = System.currentTimeMillis();
4332 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4333 // recently will be most negative, tasks sent to the bottom before that will be less
4334 // negative. Similarly for recent tasks moved to the top which will be most positive.
4335 if (!toFront) {
4336 task.mLastTimeMoved *= -1;
4337 }
4338 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004339 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004340 }
4341
Craig Mautner84984fa2014-06-19 11:19:20 -07004342 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004343 final int top = mTaskHistory.size() - 1;
4344 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4345 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004346 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004347 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4348 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004349 mTaskHistory.remove(taskNdx);
4350 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004351 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004352 return;
4353 }
4354 }
4355 }
4356
Chong Zhang280d3322015-11-03 17:27:26 -08004357 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004358 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004359 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004360
Craig Mautner11bf9a52013-02-19 14:08:51 -08004361 final int numTasks = mTaskHistory.size();
4362 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004363 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004364 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004365 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004366 ActivityOptions.abort(options);
4367 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004368 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004369 }
4370 return;
4371 }
4372
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004373 if (timeTracker != null) {
4374 // The caller wants a time tracker associated with this task.
4375 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4376 tr.mActivities.get(i).appTimeTracker = timeTracker;
4377 }
4378 }
4379
Craig Mautner11bf9a52013-02-19 14:08:51 -08004380 // Shift all activities with this task up to the top
4381 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004382 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004383
Chong Zhang45c25ce2015-08-10 22:18:26 -07004384 // Don't refocus if invisible to current user
4385 ActivityRecord top = tr.getTopActivity();
4386 if (!okToShowLocked(top)) {
4387 addRecentActivityLocked(top);
4388 ActivityOptions.abort(options);
4389 return;
4390 }
4391
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004392 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004393 ActivityRecord r = topRunningActivityLocked();
Chong Zhang6cda19c2016-06-14 19:07:56 -07004394 mStackSupervisor.moveFocusableActivityStackToFrontLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004395
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004396 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004397 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004398 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004399 if (r != null) {
4400 mNoAnimActivities.add(r);
4401 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004402 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004403 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004404 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004405 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004406
Wale Ogunwaled046a012015-12-24 13:05:59 -08004407 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004408 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004409
4410 if (VALIDATE_TOKENS) {
4411 validateAppTokensLocked();
4412 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004413 }
4414
4415 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004416 * Worker method for rearranging history stack. Implements the function of moving all
4417 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004418 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004419 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004420 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4421 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004422 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004423 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004424 * @return Returns true if the move completed, false if not.
4425 */
Craig Mautner299f9602015-01-26 09:47:33 -08004426 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004427 final TaskRecord tr = taskForIdLocked(taskId);
4428 if (tr == null) {
4429 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4430 return false;
4431 }
4432
4433 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004434 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004435
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004436 // If we have a watcher, preflight the move before committing to it. First check
4437 // for *other* available tasks, but if none are available, then try again allowing the
4438 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004439 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004440 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004441 if (next == null) {
4442 next = topRunningActivityLocked(null, 0);
4443 }
4444 if (next != null) {
4445 // ask watcher if this is allowed
4446 boolean moveOK = true;
4447 try {
4448 moveOK = mService.mController.activityResuming(next.packageName);
4449 } catch (RemoteException e) {
4450 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004451 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004452 }
4453 if (!moveOK) {
4454 return false;
4455 }
4456 }
4457 }
4458
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004459 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004460
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004461 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
Jiaquan Hee13e9642016-06-15 15:16:13 -07004462 if (topTask().isOnTopLauncher()) {
4463 // An on-top launcher doesn't affect the visibility of activities on other stacks
4464 // behind it. So if we're moving an on-top launcher to the back, we want to move the
4465 // focus to the next focusable stack and resume an activity there.
4466 // Besides, when the docked stack is visible, we should also move the home stack to
4467 // the back to avoid the recents pops up on top of a fullscreen or freeform
4468 // activity.
4469
4470 // Move the home stack to back.
4471 moveToBack(topTask());
4472
4473 // Resume an activity in the next focusable stack.
4474 adjustFocusToNextFocusableStackLocked(APPLICATION_ACTIVITY_TYPE, "moveTaskToBack");
4475 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4476 return true;
4477 }
4478
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004479 // For the case where we are moving the home task back and there is an activity visible
4480 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4481 // activity to maintain order with what the user is seeing.
4482 final ActivityStack fullscreenStack =
4483 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4484 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4485 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
Chong Zhang6cda19c2016-06-14 19:07:56 -07004486 mStackSupervisor.moveFocusableActivityStackToFrontLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004487 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004488 return true;
4489 }
4490 }
4491
riddle_hsuc215a4f2014-12-27 12:10:45 +08004492 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004493
4494 // If true, we should resume the home activity next if the task we are moving to the
4495 // back is over the home stack. We force to false if the task we are moving to back
4496 // is the home task and we don't want it resumed after moving to the back.
4497 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4498 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004499 final TaskRecord nextTask = getNextTask(tr);
4500 if (nextTask != null) {
4501 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4502 } else {
4503 prevIsHome = true;
4504 }
4505 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004506 mTaskHistory.remove(tr);
4507 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004508 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004509
Craig Mautnerc8143c62013-09-03 12:15:57 -07004510 // There is an assumption that moving a task to the back moves it behind the home activity.
4511 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004512 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004513 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4514 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004515 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004516 break;
4517 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004518 if (taskNdx == 1) {
4519 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004520 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004521 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004522 }
4523
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004524 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004525 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004526
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004527 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004528 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004529 }
4530
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004531 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004532 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004533 if (!mService.mBooting && !mService.mBooted) {
4534 // Not ready yet!
4535 return false;
4536 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004537 final int taskToReturnTo = tr.getTaskToReturnTo();
4538 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004539 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004540 }
4541
Wale Ogunwaled046a012015-12-24 13:05:59 -08004542 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004543 return true;
4544 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004545
Craig Mautner8849a5e2013-04-02 16:41:03 -07004546 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004547 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004548 final Uri data = r.intent.getData();
4549 final String strData = data != null ? data.toSafeString() : null;
4550
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004551 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004552 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004553 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004554 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004555 }
4556
4557 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004558 * Ensures all visible activities at or below the input activity have the right configuration.
4559 */
4560 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4561 if (start == null || !start.visible) {
4562 return;
4563 }
4564
4565 final TaskRecord startTask = start.task;
4566 boolean behindFullscreen = false;
4567 boolean updatedConfig = false;
4568
4569 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4570 final TaskRecord task = mTaskHistory.get(taskIndex);
4571 final ArrayList<ActivityRecord> activities = task.mActivities;
4572 int activityIndex =
4573 (start.task == task) ? activities.indexOf(start) : activities.size() - 1;
4574 for (; activityIndex >= 0; --activityIndex) {
4575 final ActivityRecord r = activities.get(activityIndex);
4576 updatedConfig |= ensureActivityConfigurationLocked(r, 0, preserveWindow);
4577 if (r.fullscreen) {
4578 behindFullscreen = true;
4579 break;
4580 }
4581 }
4582 if (behindFullscreen) {
4583 break;
4584 }
4585 }
4586 if (updatedConfig) {
Wale Ogunwale089586f2016-06-20 14:16:22 -07004587 // Ensure the resumed state of the focus activity if we updated the configuration of
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004588 // any activity.
4589 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4590 }
4591 }
4592
4593 /**
4594 * Make sure the given activity matches the current configuration. Returns false if the activity
4595 * had to be destroyed. Returns true if the configuration is the same, or the activity will
4596 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004597 * correct configuration and all other bookkeeping is handled.
4598 */
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004599 boolean ensureActivityConfigurationLocked(
4600 ActivityRecord r, int globalChanges, boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004601 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004602 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004603 "Skipping config check (will change): " + r);
4604 return true;
4605 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004606
Wale Ogunwale089586f2016-06-20 14:16:22 -07004607 // TODO: We could probably make the condition below just check that the activity state is
4608 // stopped, but also checking the sleep state for now to reduce change impact late in
4609 // development cycle.
4610 if (mService.isSleepingOrShuttingDownLocked() && r.state == ActivityState.STOPPED) {
4611 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4612 "Skipping config check (stopped while sleeping): " + r);
4613 return true;
4614 }
4615
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004616 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004617 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004618
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004619 // Short circuit: if the two configurations are equal (the common case), then there is
4620 // nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004621 final Configuration newConfig = mService.mConfiguration;
Andrii Kulianf12fce12016-05-27 17:30:16 -07004622 r.task.sanitizeOverrideConfiguration(newConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004623 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004624 if (r.configuration.equals(newConfig)
4625 && r.taskConfigOverride.equals(taskConfig)
Wale Ogunwale60454db2015-01-23 16:05:07 -08004626 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004627 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004628 "Configuration unchanged in " + r);
4629 return true;
4630 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004632 // We don't worry about activities that are finishing.
4633 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004634 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004635 "Configuration doesn't matter in finishing " + r);
4636 r.stopFreezingScreenLocked(false);
4637 return true;
4638 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004639
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004640 // Okay we now are going to make this activity have the new config.
4641 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004642 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004643 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004644 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004645 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004646
Filip Gruszczynski23493322015-07-29 17:02:59 -07004647 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004648 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004649 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004650 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004651 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004652 // There are no significant differences, so we won't relaunch but should still deliver
4653 // the new configuration to the client process.
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004654 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004655 return true;
4656 }
4657
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004658 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4659 "Configuration changes for " + r + " ; taskChanges="
4660 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4661 + Configuration.configurationDiffToString(changes));
4662
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004663 // If the activity isn't currently running, just leave the new
4664 // configuration and it will pick that up next time it starts.
4665 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004666 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004667 "Configuration doesn't matter not running " + r);
4668 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004669 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004670 return true;
4671 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004672
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004673 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004674 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4675 "Checking to restart " + r.info.name + ": changed=0x"
4676 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004677 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4678 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004679
Dianne Hackborne6676352011-06-01 16:51:20 -07004680 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004681 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4682 r.configChangeFlags |= changes;
4683 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004684 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004685 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004686 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004687 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004688 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004689 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004690 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004691 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4692 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004693 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004694 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004695 r.deferRelaunchUntilPaused = true;
4696 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004697 return true;
4698 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004699 // Try to optimize this case: the configuration is changing and we need to restart
4700 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004701 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004702 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004703 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004704
4705 if (DEBUG_STATES && !r.visible) {
4706 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4707 + " called by " + Debug.getCallers(4));
4708 }
4709
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004710 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004711 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004712 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004713 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004714 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004715 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004716
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004717 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004718 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004719 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004720
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004721 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004722 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004723 // changes is always sent to all processes when they happen so it can just use whatever
4724 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004725 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004726 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004727
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004728 return true;
4729 }
4730
Filip Gruszczynski23493322015-07-29 17:02:59 -07004731 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4732 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004733
4734 // If we went from full-screen to non-full-screen, make sure to use the correct
4735 // configuration task diff, so the diff stays as small as possible.
4736 if (Configuration.EMPTY.equals(oldTaskOverride)
4737 && !Configuration.EMPTY.equals(taskConfig)) {
4738 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4739 }
4740
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004741 // Conversely, do the same when going the other direction.
4742 if (Configuration.EMPTY.equals(taskConfig)
4743 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4744 taskConfig = record.task.extractOverrideConfig(record.configuration);
4745 }
4746
Filip Gruszczynski23493322015-07-29 17:02:59 -07004747 // Determine what has changed. May be nothing, if this is a config
4748 // that has come back from the app after going idle. In that case
4749 // we just want to leave the official config object now in the
4750 // activity and do nothing else.
4751 int taskChanges = oldTaskOverride.diff(taskConfig);
4752 // We don't want to use size changes if they don't cross boundaries that are important to
4753 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004754 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004755 final boolean crosses = record.crossesHorizontalSizeThreshold(
4756 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4757 || record.crossesVerticalSizeThreshold(
4758 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4759 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004760 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004761 }
4762 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004763 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004764 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4765 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004766 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004767 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004768 }
4769 }
4770 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4771 }
4772
4773 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4774 Configuration oldTaskOverride, int taskChanges) {
4775 if (taskChanges == 0) {
4776 // {@link Configuration#diff} doesn't catch changes from unset values.
4777 // Check for changes we care about.
4778 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004779 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004780 }
4781 // We want to explicitly track situations where the size configuration goes from
4782 // undefined to defined. We don't care about crossing the threshold in that case,
4783 // because there is no threshold.
4784 final int oldHeight = oldTaskOverride.screenHeightDp;
4785 final int newHeight = taskConfig.screenHeightDp;
4786 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4787 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4788 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004789 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004790 }
4791 final int oldWidth = oldTaskOverride.screenWidthDp;
4792 final int newWidth = taskConfig.screenWidthDp;
4793 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4794 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4795 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004796 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004797 }
4798 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4799 final int newSmallest = taskConfig.smallestScreenWidthDp;
4800 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4801 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4802 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004803 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4804 }
4805 final int oldLayout = oldTaskOverride.screenLayout;
4806 final int newLayout = taskConfig.screenLayout;
4807 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4808 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4809 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004810 }
4811 }
4812 return taskChanges;
4813 }
4814
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004815 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004816 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4817 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004818 }
4819
Wale Ogunwale83301a92015-09-24 15:54:08 -07004820 private void relaunchActivityLocked(
4821 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4822 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004823 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004824 return;
4825 }
4826
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004827 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004828 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004829 if (andResume) {
4830 results = r.results;
4831 newIntents = r.newIntents;
4832 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004833 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4834 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004835 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004836 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004837 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004838 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004839
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004840 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004841
Craig Mautner34b73df2014-01-12 21:11:08 -08004842 mStackSupervisor.removeChildActivityContainers(r);
4843
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004844 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004845 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004846 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4847 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004848 r.forceNewConfig = false;
Chong Zhangd78ddb42016-03-02 17:01:14 -08004849 mStackSupervisor.activityRelaunchingLocked(r);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004850 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4851 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004852 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004853 // Note: don't need to call pauseIfSleepingLocked() here, because
4854 // the caller will only pass in 'andResume' if this activity is
4855 // currently resumed, which implies we aren't sleeping.
4856 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004857 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004858 }
4859
4860 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004861 if (DEBUG_STATES) {
4862 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4863 }
Chong Zhangd2cb3112016-07-20 11:13:22 -07004864 r.results = null;
4865 r.newIntents = null;
Andrii Kulianf42cbe82016-06-22 14:49:58 -07004866 mService.showUnsupportedZoomDialogIfNeededLocked(r);
4867 mService.showAskCompatModeDialogLocked(r);
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004868 } else {
4869 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4870 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004871 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004872
4873 r.configChangeFlags = 0;
Chong Zhang225d1b62016-03-31 14:09:33 -07004874 r.deferRelaunchUntilPaused = false;
4875 r.preserveWindowOnDeferredRelaunch = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004876 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004877
4878 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004879 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4880 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4881 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4882 final ActivityRecord r = activities.get(activityNdx);
4883 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004884 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004885 }
4886 if (r.fullscreen && !r.finishing) {
4887 return false;
4888 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004889 }
4890 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004891 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004892 if (r == null) {
4893 return false;
4894 }
4895 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4896 + " would have returned true for r=" + r);
4897 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004898 }
4899
4900 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004901 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4902 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4903 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4904 final ActivityRecord r = activities.get(activityNdx);
4905 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004906 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004907 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004908 }
4909 }
4910 }
4911
Wale Ogunwale540e1232015-05-01 15:35:39 -07004912 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4913 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004914 boolean didSomething = false;
4915 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004916 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004917 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4918 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4919 int numActivities = activities.size();
4920 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4921 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004922 final boolean sameComponent =
4923 (r.packageName.equals(packageName) && (filterByClasses == null
4924 || filterByClasses.contains(r.realActivity.getClassName())))
4925 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004926 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004927 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004928 && (r.app == null || evenPersistent || !r.app.persistent)) {
4929 if (!doit) {
4930 if (r.finishing) {
4931 // If this activity is just finishing, then it is not
4932 // interesting as far as something to stop.
4933 continue;
4934 }
4935 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004936 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004937 if (r.isHomeActivity()) {
4938 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4939 Slog.i(TAG, "Skip force-stop again " + r);
4940 continue;
4941 } else {
4942 homeActivity = r.realActivity;
4943 }
4944 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004945 didSomething = true;
4946 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004947 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004948 if (r.app != null) {
4949 r.app.removed = true;
4950 }
4951 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004952 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004953 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004954 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4955 true)) {
4956 // r has been deleted from mActivities, accommodate.
4957 --numActivities;
4958 --activityNdx;
4959 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004960 }
4961 }
4962 }
4963 return didSomething;
4964 }
4965
Dianne Hackborn09233282014-04-30 11:33:59 -07004966 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004967 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4968 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004969 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004970 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004971 if (task.getTopActivity() == null) {
4972 continue;
4973 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004974 ActivityRecord r = null;
4975 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004976 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004977 int numActivities = 0;
4978 int numRunning = 0;
4979 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004980 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004981 continue;
4982 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004983 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004984 tmp = activities.get(activityNdx);
4985 if (tmp.finishing) {
4986 continue;
4987 }
4988 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004989
Craig Mautneraab647e2013-02-28 16:31:36 -08004990 // Initialize state for next task if needed.
4991 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4992 top = r;
4993 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004994 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004995
4996 // Add 'r' into the current task.
4997 numActivities++;
4998 if (r.app != null && r.app.thread != null) {
4999 numRunning++;
5000 }
5001
Wale Ogunwalee23149f2015-03-06 15:39:44 -08005002 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08005003 TAG, r.intent.getComponent().flattenToShortString()
5004 + ": task=" + r.task);
5005 }
5006
5007 RunningTaskInfo ci = new RunningTaskInfo();
5008 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07005009 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08005010 ci.baseActivity = r.intent.getComponent();
5011 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07005012 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08005013 if (focusedStack && topTask) {
5014 // Give the latest time to ensure foreground task can be sorted
5015 // at the first, because lastActiveTime of creating task is 0.
5016 ci.lastActiveTime = System.currentTimeMillis();
5017 topTask = false;
5018 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07005019
Craig Mautnerc0ffce52014-07-01 12:38:52 -07005020 if (top.task != null) {
5021 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08005022 }
5023 ci.numActivities = numActivities;
5024 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08005025 ci.isDockable = task.canGoInDockedStack();
Jorim Jaggi29379ec2016-04-11 23:43:42 -07005026 ci.resizeMode = task.mResizeMode;
Craig Mautneraab647e2013-02-28 16:31:36 -08005027 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08005028 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005029 }
5030
5031 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08005032 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005033 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08005034 if (top >= 0) {
5035 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
5036 int activityTop = activities.size() - 1;
5037 if (activityTop > 0) {
5038 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
5039 "unhandled-back", true);
5040 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005041 }
5042 }
5043
Craig Mautner6b74cb52013-09-27 17:02:21 -07005044 /**
5045 * Reset local parameters because an app's activity died.
5046 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07005047 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07005048 */
5049 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07005050 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005051 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07005052 "App died while pausing: " + mPausingActivity);
5053 mPausingActivity = null;
5054 }
5055 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
5056 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07005057 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07005058 }
5059
Craig Mautner19091252013-10-05 00:03:53 -07005060 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07005061 }
5062
Craig Mautnercae015f2013-02-08 14:31:27 -08005063 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08005064 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5065 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5066 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5067 final ActivityRecord r = activities.get(activityNdx);
5068 if (r.app == app) {
5069 Slog.w(TAG, " Force finishing activity "
5070 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07005071 // Force the destroy to skip right to removal.
5072 r.app = null;
5073 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08005074 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005075 }
5076 }
5077 }
5078
Dianne Hackborn390517b2013-05-30 15:03:32 -07005079 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005080 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07005081 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08005082 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5083 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07005084 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
5085 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005086 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07005087 " Task id #" + task.taskId + "\n" +
5088 " mFullscreen=" + task.mFullscreen + "\n" +
5089 " mBounds=" + task.mBounds + "\n" +
Andrii Kulianf66a83d2016-05-17 12:17:44 -07005090 " mMinWidth=" + task.mMinWidth + "\n" +
5091 " mMinHeight=" + task.mMinHeight + "\n" +
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07005092 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005093 if (printed) {
5094 header = null;
5095 }
Craig Mautneraab647e2013-02-28 16:31:36 -08005096 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07005097 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08005098 }
5099
5100 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
5101 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
5102
5103 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005104 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5105 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08005106 }
5107 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005108 final int top = mTaskHistory.size() - 1;
5109 if (top >= 0) {
5110 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
5111 int listTop = list.size() - 1;
5112 if (listTop >= 0) {
5113 activities.add(list.get(listTop));
5114 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005115 }
5116 } else {
5117 ItemMatcher matcher = new ItemMatcher();
5118 matcher.build(name);
5119
Craig Mautneraab647e2013-02-28 16:31:36 -08005120 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5121 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
5122 if (matcher.match(r1, r1.intent.getComponent())) {
5123 activities.add(r1);
5124 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005125 }
5126 }
5127 }
5128
5129 return activities;
5130 }
5131
5132 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07005133 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08005134
5135 // All activities that came from the package must be
5136 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08005137 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5138 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5139 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5140 final ActivityRecord a = activities.get(activityNdx);
5141 if (a.info.packageName.equals(packageName)) {
5142 a.forceNewConfig = true;
5143 if (starting != null && a == starting && a.visible) {
5144 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08005145 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08005146 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005147 }
5148 }
5149 }
5150
5151 return starting;
5152 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005153
Craig Mautner299f9602015-01-26 09:47:33 -08005154 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005155 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005156 }
5157
Wale Ogunwale000957c2015-04-03 08:19:12 -07005158 /**
5159 * Removes the input task from this stack.
5160 * @param task to remove.
5161 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07005162 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
5163 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07005164 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07005165 void removeTask(TaskRecord task, String reason, int mode) {
5166 if (mode == REMOVE_TASK_MODE_DESTROYING) {
Craig Mautner15df08a2015-04-01 12:17:18 -07005167 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005168 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08005169 if (!StackId.persistTaskBounds(mStackId)) {
5170 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
5171 // default configuration the next time it launches.
5172 task.updateOverrideConfiguration(null);
5173 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005174 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07005175
Craig Mautner04a0ea62014-01-13 12:51:26 -08005176 final ActivityRecord r = mResumedActivity;
5177 if (r != null && r.task == task) {
5178 mResumedActivity = null;
5179 }
5180
Craig Mautnerae7ecab2013-09-18 11:48:14 -07005181 final int taskNdx = mTaskHistory.indexOf(task);
5182 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07005183 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
5184 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
5185 if (!nextTask.isOverHomeStack()) {
5186 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
5187 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07005188 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005189 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005190 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07005191
Wale Ogunwale06579d62016-04-30 15:29:06 -07005192 if (mode == REMOVE_TASK_MODE_DESTROYING && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005193 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07005194 final boolean isVoiceSession = task.voiceSession != null;
5195 if (isVoiceSession) {
5196 try {
5197 task.voiceSession.taskFinished(task.intent, task.taskId);
5198 } catch (RemoteException e) {
5199 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07005200 }
Craig Mautner41db4a72014-05-07 17:20:56 -07005201 if (task.autoRemoveFromRecents() || isVoiceSession) {
5202 // Task creator asked to remove this when done, or this task was a voice
5203 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08005204 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08005205 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07005206 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07005207 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005208
5209 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005210 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07005211 // We only need to adjust focused stack if this stack is in focus and we are not in the
5212 // process of moving the task to the top of the stack that will be focused.
5213 if (isOnHomeDisplay() && mode != REMOVE_TASK_MODE_MOVING_TO_TOP
5214 && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005215 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07005216 if (mFullscreen
5217 || !adjustFocusToNextFocusableStackLocked(
5218 task.getTaskToReturnTo(), myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005219 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005220 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005221 }
Craig Mautner593a4e62014-01-15 17:55:51 -08005222 if (mStacks != null) {
5223 mStacks.remove(this);
5224 mStacks.add(0, this);
5225 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005226 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005227 mActivityContainer.onTaskListEmptyLocked();
5228 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005229 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005230
5231 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08005232 }
5233
Dianne Hackborn91097de2014-04-04 18:02:06 -07005234 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5235 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
5236 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07005237 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
5238 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005239 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005240 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08005241 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Andrii Kulian2e751b82016-03-16 16:59:32 -07005242 if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08005243 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07005244 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005245 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005246 return task;
5247 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005248
Andrii Kulian2e751b82016-03-16 16:59:32 -07005249 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08005250 if (mTaskPositioner == null) {
5251 return false;
5252 }
Andrii Kulian2e751b82016-03-16 16:59:32 -07005253 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
Wale Ogunwale935e5022015-11-10 12:36:10 -08005254 return true;
5255 }
5256
Craig Mautnerc00204b2013-03-05 15:02:14 -08005257 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005258 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005259 }
5260
Wale Ogunwale5f986092015-12-04 15:35:38 -08005261 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005262 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005263
Craig Mautnerc00204b2013-03-05 15:02:14 -08005264 task.stack = this;
5265 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07005266 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005267 } else {
5268 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005269 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005270 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005271 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005272 }
5273
Wale Ogunwale5f986092015-12-04 15:35:38 -08005274 void positionTask(final TaskRecord task, int position) {
Jorim Jaggi023da532016-04-20 22:42:32 -07005275 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
5276 final boolean wasResumed = topRunningActivity == task.stack.mResumedActivity;
Wale Ogunwaleed811d72016-05-06 08:41:19 -07005277 final ActivityStack prevStack = preAddTask(task, "positionTask", !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005278 task.stack = this;
5279 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005280 postAddTask(task, prevStack);
Jorim Jaggi023da532016-04-20 22:42:32 -07005281 if (wasResumed) {
5282 if (mResumedActivity != null) {
5283 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5284 + " other stack to this stack mResumedActivity=" + mResumedActivity
5285 + " other mResumedActivity=" + topRunningActivity);
5286 }
5287 mResumedActivity = topRunningActivity;
5288 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005289 }
5290
Wale Ogunwale06579d62016-04-30 15:29:06 -07005291 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08005292 final ActivityStack prevStack = task.stack;
5293 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005294 prevStack.removeTask(task, reason,
5295 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005296 }
5297 return prevStack;
5298 }
5299
5300 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
5301 if (prevStack != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07005302 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005303 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005304 try {
5305 task.voiceSession.taskStarted(task.intent, task.taskId);
5306 } catch (RemoteException e) {
5307 }
5308 }
5309 }
5310
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005311 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005312 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005313 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07005314 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
5315 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08005316 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
5317 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Yorke Lee0e852472016-06-15 10:03:18 -07005318 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask(),
Jiaquan Hedd1e66f2016-06-15 15:15:12 -07005319 r.appInfo.targetSdkVersion, r.mRotationAnimationHint, task.isOnTopLauncher());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005320 r.taskConfigOverride = task.mOverrideConfig;
5321 }
5322
Wale Ogunwaled046a012015-12-24 13:05:59 -08005323 void moveToFrontAndResumeStateIfNeeded(
5324 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
5325 if (!moveToFront) {
5326 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005327 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005328
5329 // If the activity owns the last resumed activity, transfer that together,
5330 // so that we don't resume the same activity again in the new stack.
5331 // Apps may depend on onResume()/onPause() being called in pairs.
5332 if (setResume) {
5333 mResumedActivity = r;
5334 }
5335 // Move the stack in which we are placing the activity to the front. The call will also
5336 // make sure the activity focus is set.
5337 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005338 }
5339
5340 /**
5341 * Moves the input activity from its current stack to this one.
5342 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
5343 * created on this stack which the activity is added to.
5344 * */
5345 void moveActivityToStack(ActivityRecord r) {
5346 final ActivityStack prevStack = r.task.stack;
5347 if (prevStack.mStackId == mStackId) {
5348 // You are already in the right stack silly...
5349 return;
5350 }
5351
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07005352 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07005353 && (mStackSupervisor.topRunningActivityLocked() == r);
5354 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
5355
5356 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08005357 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
5358 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005359 r.setTask(task, null);
5360 task.addActivityToTop(r);
5361 setAppTask(r, task);
Andrii Kulian933076d2016-03-29 17:04:42 -07005362 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005363 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
5364 if (wasResumed) {
5365 prevStack.mResumedActivity = null;
5366 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07005367 }
5368
Wale Ogunwale706ed792015-08-02 10:29:44 -07005369 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005370 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005371 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
Jiaquan Hedd1e66f2016-06-15 15:15:12 -07005372 task.mResizeMode, task.isHomeTask(), task.isOnTopLauncher());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005373 r.taskConfigOverride = task.mOverrideConfig;
5374 }
5375
Craig Mautnerc00204b2013-03-05 15:02:14 -08005376 public int getStackId() {
5377 return mStackId;
5378 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005379
5380 @Override
5381 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005382 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5383 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005384 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005385
Craig Mautner15df08a2015-04-01 12:17:18 -07005386 void onLockTaskPackagesUpdatedLocked() {
5387 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5388 mTaskHistory.get(taskNdx).setLockTaskAuth();
5389 }
5390 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005391}