blob: 837a1c1f1a2135767b17f2d38f50f4b33b19c6a9 [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 Ogunwale040b4702015-08-06 18:10:50 -070076import static com.android.server.am.ActivityStackSupervisor.MOVING;
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;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -0700120import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700122import com.android.internal.app.IVoiceInteractor;
123import com.android.internal.content.ReferrerIntent;
124import com.android.internal.os.BatteryStatsImpl;
125import com.android.server.Watchdog;
126import com.android.server.am.ActivityManagerService.ItemMatcher;
127import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
128import com.android.server.wm.TaskGroup;
129import com.android.server.wm.WindowManagerService;
130
Craig Mautnercae015f2013-02-08 14:31:27 -0800131import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800132import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133import java.lang.ref.WeakReference;
134import java.util.ArrayList;
135import java.util.Iterator;
136import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800137import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700138import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700139
140/**
141 * State and management of a single stack of activities.
142 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700143final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800145 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700146 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
147 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800148 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700149 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700150 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700151 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700152 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700153 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
155 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700156 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700157 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700158 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
159 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
160 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
161 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
162 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700163
164 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800165
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700166 // Ticks during which we check progress while waiting for an app to launch.
167 static final int LAUNCH_TICK = 500;
168
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 // How long we wait until giving up on the last activity to pause. This
170 // is short because it directly impacts the responsiveness of starting the
171 // next activity.
172 static final int PAUSE_TIMEOUT = 500;
173
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700174 // How long we wait for the activity to tell us it has stopped before
175 // giving up. This is a good amount of time because we really need this
176 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700177 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179 // How long we wait until giving up on an activity telling us it has
180 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700181 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800182
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800184 // disabled.
185 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800186
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700187 // How long between activity launches that we consider safe to not warn
188 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700189 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700191 // Set to false to disable the preview that is shown while a new activity
192 // is being started.
193 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800194
Craig Mautner5eda9b32013-07-02 11:58:16 -0700195 // How long to wait for all background Activities to redraw following a call to
196 // convertToTranslucent().
197 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
198
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800199 // How many activities have to be scheduled to stop to force a stop pass.
200 private static final int MAX_STOPPING_TO_FORCE = 3;
201
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202 enum ActivityState {
203 INITIALIZING,
204 RESUMED,
205 PAUSING,
206 PAUSED,
207 STOPPING,
208 STOPPED,
209 FINISHING,
210 DESTROYING,
211 DESTROYED
212 }
213
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800214 // Stack is not considered visible.
215 static final int STACK_INVISIBLE = 0;
216 // Stack is considered visible
217 static final int STACK_VISIBLE = 1;
218 // Stack is considered visible, but only becuase it has activity that is visible behind other
219 // activities and there is a specific combination of stacks.
220 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
221
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700222 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700223 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800224 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800225
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700226 /**
227 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800228 * running) activities. It contains #TaskRecord objects.
229 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800230 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800231
232 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800233 * Used for validating app tokens with window manager.
234 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800235 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700237 /**
238 * List of running activities, sorted by recent usage.
239 * The first entry in the list is the least recently used.
240 * It contains HistoryRecord objects.
241 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800242 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700243
244 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700245 * Animations that for the current transition have requested not to
246 * be considered for the transition animation.
247 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800248 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249
250 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700251 * When we are in the process of pausing an activity, before starting the
252 * next one, this variable holds the activity that is currently being paused.
253 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800254 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700255
256 /**
257 * This is the last activity that we put into the paused state. This is
258 * used to determine if we need to do an activity transition while sleeping,
259 * when we normally hold the top activity paused.
260 */
261 ActivityRecord mLastPausedActivity = null;
262
263 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700264 * Activities that specify No History must be removed once the user navigates away from them.
265 * If the device goes to sleep with such an activity in the paused state then we save it here
266 * and finish it later if another activity replaces it on wakeup.
267 */
268 ActivityRecord mLastNoHistoryActivity = null;
269
270 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700271 * Current activity that is resumed, or null if there is none.
272 */
273 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700276 * This is the last activity that has been started. It is only used to
277 * identify when multiple activities are started at once so that the user
278 * can be warned they may not be in the activity they think they are.
279 */
280 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800281
Craig Mautner5eda9b32013-07-02 11:58:16 -0700282 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
283 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
284 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
285 // Activity in mTranslucentActivityWaiting is notified via
286 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
287 // background activity being drawn then the same call will be made with a true value.
288 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700289 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700290
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700291 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 * Set when we know we are going to be calling updateConfiguration()
293 * soon, so want to skip intermediate config checks.
294 */
295 boolean mConfigWillChange;
296
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700297 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800298 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700299 // Current bounds of the stack or null if fullscreen.
300 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800301
Jorim Jaggi192086e2016-03-11 17:17:03 +0100302 boolean mUpdateBoundsDeferred;
303 boolean mUpdateBoundsDeferredCalled;
304 final Rect mDeferredBounds = new Rect();
305 final Rect mDeferredTaskBounds = new Rect();
306 final Rect mDeferredTaskInsetBounds = new Rect();
307
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700308 long mLaunchStartTime = 0;
309 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800310
Craig Mautner858d8a62013-04-23 17:08:34 -0700311 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700312
Craig Mautnerc00204b2013-03-05 15:02:14 -0800313 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800314 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800315 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
316 ArrayList<ActivityStack> mStacks;
317 /** The attached Display's unique identifier, or -1 if detached */
318 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319
Craig Mautner27084302013-03-25 08:05:25 -0700320 /** Run all ActivityStacks through this */
321 final ActivityStackSupervisor mStackSupervisor;
322
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700323 private final LaunchingTaskPositioner mTaskPositioner;
324
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700325 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700326 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
327 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
328 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
329 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700330 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700331 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700332 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700333
334 static class ScheduleDestroyArgs {
335 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700336 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700337 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700338 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700339 mReason = reason;
340 }
341 }
342
Zoran Marcetaf958b322012-08-09 20:27:12 +0900343 final Handler mHandler;
344
345 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800346
Craig Mautnerc8143c62013-09-03 12:15:57 -0700347 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900348 super(looper);
349 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700350
Zoran Marcetaf958b322012-08-09 20:27:12 +0900351 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700352 public void handleMessage(Message msg) {
353 switch (msg.what) {
354 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800355 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700356 // We don't at this point know if the activity is fullscreen,
357 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800358 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700359 synchronized (mService) {
360 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700361 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700362 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700363 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800364 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700365 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700366 case LAUNCH_TICK_MSG: {
367 ActivityRecord r = (ActivityRecord)msg.obj;
368 synchronized (mService) {
369 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700370 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700371 }
372 }
373 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700374 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800375 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 // We don't at this point know if the activity is fullscreen,
377 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800378 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800379 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800380 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800381 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700382 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700383 case STOP_TIMEOUT_MSG: {
384 ActivityRecord r = (ActivityRecord)msg.obj;
385 // We don't at this point know if the activity is fullscreen,
386 // so we need to be conservative and assume it isn't.
387 Slog.w(TAG, "Activity stop timeout for " + r);
388 synchronized (mService) {
389 if (r.isInHistory()) {
390 activityStoppedLocked(r, null, null, null);
391 }
392 }
393 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700394 case DESTROY_ACTIVITIES_MSG: {
395 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
396 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700397 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700398 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700399 } break;
400 case TRANSLUCENT_TIMEOUT_MSG: {
401 synchronized (mService) {
402 notifyActivityDrawnLocked(null);
403 }
404 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700405 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700406 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700407 final ActivityRecord r = getVisibleBehindActivity();
408 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700409 if (r != null) {
410 mService.killAppAtUsersRequest(r.app, null);
411 }
412 }
413 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 }
415 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800416 }
417
Craig Mautner34b73df2014-01-12 21:11:08 -0800418 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800419 int count = 0;
420 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
421 count += mTaskHistory.get(taskNdx).mActivities.size();
422 }
423 return count;
424 }
425
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800426 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
427 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800428 mActivityContainer = activityContainer;
429 mStackSupervisor = activityContainer.getOuter();
430 mService = mStackSupervisor.mService;
431 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
432 mWindowManager = mService.mWindowManager;
433 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700434 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800435 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700436 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
437 ? new LaunchingTaskPositioner() : null;
438 }
439
440 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
441 mDisplayId = activityDisplay.mDisplayId;
442 mStacks = activityDisplay.mStacks;
443 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
444 mFullscreen = mBounds == null;
445 if (mTaskPositioner != null) {
446 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
447 mTaskPositioner.configure(mBounds);
448 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800449
450 if (mStackId == DOCKED_STACK_ID) {
451 // If we created a docked stack we want to resize it so it resizes all other stacks
452 // in the system.
453 mStackSupervisor.resizeDockedStackLocked(
454 mBounds, null, null, null, null, PRESERVE_WINDOWS);
455 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700456 }
457
458 void detachDisplay() {
459 mDisplayId = Display.INVALID_DISPLAY;
460 mStacks = null;
461 if (mTaskPositioner != null) {
462 mTaskPositioner.reset();
463 }
464 mWindowManager.detachStack(mStackId);
Jorim Jaggi899327f2016-02-25 20:44:18 -0500465 if (mStackId == DOCKED_STACK_ID) {
466 // If we removed a docked stack we want to resize it so it resizes all other stacks
467 // in the system to fullscreen.
468 mStackSupervisor.resizeDockedStackLocked(
469 null, null, null, null, null, PRESERVE_WINDOWS);
470 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700471 }
472
Winsonc809cbb2015-11-02 12:06:15 -0800473 public void getDisplaySize(Point out) {
474 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
475 }
476
Jorim Jaggi192086e2016-03-11 17:17:03 +0100477 /**
478 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
479 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
480 */
481 void deferUpdateBounds() {
482 if (!mUpdateBoundsDeferred) {
483 mUpdateBoundsDeferred = true;
484 mUpdateBoundsDeferredCalled = false;
485 }
486 }
487
488 /**
489 * Continues updating bounds after updates have been deferred. If there was a resize attempt
490 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
491 * be resized to that bounds.
492 */
493 void continueUpdateBounds() {
494 final boolean wasDeferred = mUpdateBoundsDeferred;
495 mUpdateBoundsDeferred = false;
496 if (wasDeferred && mUpdateBoundsDeferredCalled) {
497 mStackSupervisor.resizeStackUncheckedLocked(this,
498 mDeferredBounds.isEmpty() ? null : mDeferredBounds,
499 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
500 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
501 }
502 }
503
504 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
505 Rect tempTaskInsetBounds) {
506 if (!mUpdateBoundsDeferred) {
507 return true;
508 }
509 if (bounds != null) {
510 mDeferredBounds.set(bounds);
511 } else {
512 mDeferredBounds.setEmpty();
513 }
514 if (tempTaskBounds != null) {
515 mDeferredTaskBounds.set(tempTaskBounds);
516 } else {
517 mDeferredTaskBounds.setEmpty();
518 }
519 if (tempTaskInsetBounds != null) {
520 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
521 } else {
522 mDeferredTaskInsetBounds.setEmpty();
523 }
524 mUpdateBoundsDeferredCalled = true;
525 return false;
526 }
527
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700528 void setBounds(Rect bounds) {
529 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700530 if (mTaskPositioner != null) {
531 mTaskPositioner.configure(bounds);
532 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700533 }
Craig Mautner5962b122012-10-05 14:45:52 -0700534
Amith Yamasani734983f2014-03-04 16:48:05 -0800535 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700536 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700537 }
538
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700539 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800540 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700541 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700542 if (r != null) {
543 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800544 }
545 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700546 return null;
547 }
548
549 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800550 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
551 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800552 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800553 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
554 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800555 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800556 return r;
557 }
558 }
559 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700560 return null;
561 }
562
563 /**
564 * This is a simplified version of topRunningActivityLocked that provides a number of
565 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800566 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700567 * @param token If non-null, any history records matching this token will be skipped.
568 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800569 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700570 * @return Returns the HistoryRecord of the next activity on the stack.
571 */
572 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800573 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
574 TaskRecord task = mTaskHistory.get(taskNdx);
575 if (task.taskId == taskId) {
576 continue;
577 }
578 ArrayList<ActivityRecord> activities = task.mActivities;
579 for (int i = activities.size() - 1; i >= 0; --i) {
580 final ActivityRecord r = activities.get(i);
581 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800582 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800583 return r;
584 }
585 }
586 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700587 return null;
588 }
589
Craig Mautner8849a5e2013-04-02 16:41:03 -0700590 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700591 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
592 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700593 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
594 final ActivityRecord r = activities.get(activityNdx);
595 if (!r.finishing) {
596 return r;
597 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700598 }
599 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700600 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700601 }
602
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700603 final TaskRecord topTask() {
604 final int size = mTaskHistory.size();
605 if (size > 0) {
606 return mTaskHistory.get(size - 1);
607 }
608 return null;
609 }
610
Craig Mautnerd2328952013-03-05 12:46:26 -0800611 TaskRecord taskForIdLocked(int id) {
612 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
613 final TaskRecord task = mTaskHistory.get(taskNdx);
614 if (task.taskId == id) {
615 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800616 }
617 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700618 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700619 }
620
Craig Mautnerd2328952013-03-05 12:46:26 -0800621 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700622 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800623 return isInStackLocked(r);
624 }
625
626 ActivityRecord isInStackLocked(ActivityRecord r) {
627 if (r == null) {
628 return null;
629 }
630 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700631 if (task != null && task.stack != null
632 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800633 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800634 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800635 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800636 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800637 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800638 }
639
Craig Mautner2420ead2013-04-01 17:13:20 -0700640 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700641 final boolean hadit = mLRUActivities.remove(r);
642 mLRUActivities.add(r);
643 return hadit;
644 }
645
Craig Mautnerde4ef022013-04-07 19:01:33 -0700646 final boolean isHomeStack() {
647 return mStackId == HOME_STACK_ID;
648 }
649
Winson Chung0583d3d2015-12-18 10:03:12 -0500650 final boolean isDockedStack() {
651 return mStackId == DOCKED_STACK_ID;
652 }
653
654 final boolean isPinnedStack() {
655 return mStackId == PINNED_STACK_ID;
656 }
657
Craig Mautnere0a38842013-12-16 16:14:02 -0800658 final boolean isOnHomeDisplay() {
659 return isAttached() &&
660 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
661 }
662
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700663 void moveToFront(String reason) {
664 moveToFront(reason, null);
665 }
666
667 /**
668 * @param reason The reason for moving the stack to the front.
669 * @param task If non-null, the task will be moved to the top of the stack.
670 * */
671 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700672 if (!isAttached()) {
673 return;
674 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700675
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700676 mStacks.remove(this);
677 int addIndex = mStacks.size();
678
679 if (addIndex > 0) {
680 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800681 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
682 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700683 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800684 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700685 }
686
687 mStacks.add(addIndex, this);
688
689 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
690 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800691 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700692 }
693 if (task != null) {
694 insertTaskAtTop(task, null);
695 } else {
696 task = topTask();
697 }
698 if (task != null) {
699 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800700 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 }
702
Wale Ogunwaled046a012015-12-24 13:05:59 -0800703 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800704 if (StackId.canReceiveKeys(mStackId)) {
705 return true;
706 }
707 // The stack isn't focusable. See if its top activity is focusable to force focus on the
708 // stack.
709 final ActivityRecord r = topRunningActivityLocked();
710 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800711 }
712
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 final boolean isAttached() {
714 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800715 }
716
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700717 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800718 * Returns the top activity in any existing task matching the given Intent in the input result.
719 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700720 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800721 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700722 Intent intent = target.intent;
723 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700724 ComponentName cls = intent.getComponent();
725 if (info.targetActivity != null) {
726 cls = new ComponentName(info.packageName, info.targetActivity);
727 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700728 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700729 boolean isDocument = intent != null & intent.isDocument();
730 // If documentData is non-null then it must match the existing task data.
731 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800732
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700733 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800734 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
735 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700736 if (task.voiceSession != null) {
737 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700738 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700739 continue;
740 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700741 if (task.userId != userId) {
742 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700743 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700744 continue;
745 }
Craig Mautner000f0022013-02-26 15:04:29 -0800746 final ActivityRecord r = task.getTopActivity();
747 if (r == null || r.finishing || r.userId != userId ||
748 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700749 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800750 continue;
751 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700752 if (r.mActivityType != target.mActivityType) {
753 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
754 continue;
755 }
Craig Mautner000f0022013-02-26 15:04:29 -0800756
Craig Mautnerd00f4742014-03-12 14:17:26 -0700757 final Intent taskIntent = task.intent;
758 final Intent affinityIntent = task.affinityIntent;
759 final boolean taskIsDocument;
760 final Uri taskDocumentData;
761 if (taskIntent != null && taskIntent.isDocument()) {
762 taskIsDocument = true;
763 taskDocumentData = taskIntent.getData();
764 } else if (affinityIntent != null && affinityIntent.isDocument()) {
765 taskIsDocument = true;
766 taskDocumentData = affinityIntent.getData();
767 } else {
768 taskIsDocument = false;
769 taskDocumentData = null;
770 }
771
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700772 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700773 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700774 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700775 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800776 if (!isDocument && !taskIsDocument
777 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700778 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800779 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
780 // It is possible for multiple tasks to have the same root affinity especially
781 // if they are in separate stacks. We save off this candidate, but keep looking
782 // to see if there is a better candidate.
783 result.r = r;
784 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700785 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700786 } else if (taskIntent != null && taskIntent.getComponent() != null &&
787 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700788 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700789 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800790 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700791 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
792 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800793 result.r = r;
794 result.matchedByRootAffinity = false;
795 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700796 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
797 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700798 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700799 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800800 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700801 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
802 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800803 result.r = r;
804 result.matchedByRootAffinity = false;
805 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700806 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 }
809
810 /**
811 * Returns the first activity (starting from the top of the stack) that
812 * is the same as the given activity. Returns null if no such activity
813 * is found.
814 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700815 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 ComponentName cls = intent.getComponent();
817 if (info.targetActivity != null) {
818 cls = new ComponentName(info.packageName, info.targetActivity);
819 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700820 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821
Craig Mautner000f0022013-02-26 15:04:29 -0800822 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700823 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700824 final boolean notCurrentUserTask =
825 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700826 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700827
Craig Mautner000f0022013-02-26 15:04:29 -0800828 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
829 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700830 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700831 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700832 }
Craig Mautner000f0022013-02-26 15:04:29 -0800833 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834 return r;
835 }
836 }
837 }
838
839 return null;
840 }
841
Amith Yamasani742a6712011-05-04 14:49:28 -0700842 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700843 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700844 */
Craig Mautner93529a42013-10-04 15:03:13 -0700845 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800846 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700847 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800848 }
849 mCurrentUser = userId;
850
851 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800852 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800853 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700854 final TaskRecord task = mTaskHistory.get(i);
855
856 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
857 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700858 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700859 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700860 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700861 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800862 mTaskHistory.remove(i);
863 mTaskHistory.add(task);
864 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800865 // Use same value for i.
866 } else {
867 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800868 }
869 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700870 if (VALIDATE_TOKENS) {
871 validateAppTokensLocked();
872 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700873 }
874
Craig Mautner2420ead2013-04-01 17:13:20 -0700875 void minimalResumeActivityLocked(ActivityRecord r) {
876 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800877 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
878 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700879 mResumedActivity = r;
880 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800881 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700882 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700883 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700884 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700885 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
886 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700887 }
888
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800889 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700890 if (r != null) {
891 mRecentTasks.addLocked(r.task);
892 r.task.touchActiveTime();
893 }
894 }
895
Narayan Kamath7829c812015-06-08 17:39:43 +0100896 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700897 if (mFullyDrawnStartTime != 0) {
898 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
899 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100900 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700901 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
902 }
903
904 private void stopFullyDrawnTraceIfNeeded() {
905 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
906 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
907 mFullyDrawnStartTime = 0;
908 }
909 }
910
Craig Mautnere79d42682013-04-01 19:01:53 -0700911 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700912 if (r.displayStartTime == 0) {
913 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
914 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100915 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700916 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700918 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100919 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700920 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700921 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700922 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800923
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700924 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700925 // Make sure that there is no activity waiting for this to launch.
926 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
927 r.displayStartTime = r.fullyDrawnStartTime = 0;
928 } else {
929 mStackSupervisor.removeTimeoutsForActivityLocked(r);
930 mStackSupervisor.scheduleIdleTimeoutLocked(r);
931 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700932 }
933
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800934 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800935 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800936 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
937 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
938 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
939 activities.get(activityNdx).setSleeping(false);
940 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800941 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800942 if (mPausingActivity != null) {
943 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
944 activityPausedLocked(mPausingActivity.appToken, true);
945 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800946 }
947
Todd Kennedy39bfee52016-02-24 10:28:21 -0800948 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
949 final String packageName = aInfo.packageName;
950 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
951 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
952 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
953 if (packageName.equals(activities.get(activityNdx).packageName)) {
954 activities.get(activityNdx).info.applicationInfo = aInfo;
955 }
956 }
957 }
958 }
959
Craig Mautner0eea92c2013-05-16 13:35:39 -0700960 /**
961 * @return true if something must be done before going to sleep.
962 */
963 boolean checkReadyForSleepLocked() {
964 if (mResumedActivity != null) {
965 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700966 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
967 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
968 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700969 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700970 return true;
971 }
972 if (mPausingActivity != null) {
973 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700974 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700975 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800976 }
977
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700978 if (hasVisibleBehindActivity()) {
979 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800980 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700981 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800982 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700983 return true;
984 }
985
Craig Mautner0eea92c2013-05-16 13:35:39 -0700986 return false;
987 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800988
Craig Mautner0eea92c2013-05-16 13:35:39 -0700989 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700990 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800991
Craig Mautner0eea92c2013-05-16 13:35:39 -0700992 // Make sure any stopped but visible activities are now sleeping.
993 // This ensures that the activity's onStop() is called.
994 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
995 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
996 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
997 final ActivityRecord r = activities.get(activityNdx);
998 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
999 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001000 }
1001 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001002 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001003 }
Craig Mautner59c00972012-07-30 12:10:24 -07001004
Winson8b1871d2015-11-20 09:56:20 -08001005 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
1006 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001007 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001008 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001009 return null;
1010 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001011
Winson Chung083baf92014-07-11 10:32:42 -07001012 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -07001013 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001014 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001015 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -07001016 return null;
1017 }
1018
Winson Chung48a10a52014-08-27 14:36:51 -07001019 int w = mService.mThumbnailWidth;
1020 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -08001021 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001022 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001023 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -08001024
1025 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -08001026 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
1027 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -05001028 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -08001029 w = h = -1;
Winson21700932016-03-24 17:26:23 -07001030 scale = mService.mFullscreenThumbnailScale;
Winson8b1871d2015-11-20 09:56:20 -08001031 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001032 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -08001033 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001034 }
Winson Chung376543b2014-05-21 17:43:28 -07001035 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001036 return null;
1037 }
1038
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001039 /**
1040 * Start pausing the currently resumed activity. It is an error to call this if there
1041 * is already an activity being paused or there is no resumed activity.
1042 *
1043 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1044 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1045 * screen turning off).
1046 * @param resuming True if this is being called as part of resuming the top activity, so
1047 * we shouldn't try to instigate a resume here.
1048 * @param dontWait True if the caller does not want to wait for the pause to complete. If
1049 * set to true, we will immediately complete the pause here before returning.
1050 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1051 * it to tell us when it is done.
1052 */
1053 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
1054 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001055 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001056 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1057 + " state=" + mPausingActivity.state);
1058 if (!mService.isSleeping()) {
1059 // Avoid recursion among check for sleep and complete pause during sleeping.
1060 // Because activity will be paused immediately after resume, just let pause
1061 // be completed by the order of activity paused from clients.
1062 completePauseLocked(false);
1063 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001064 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065 ActivityRecord prev = mResumedActivity;
1066 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001067 if (!resuming) {
1068 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001069 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001070 }
1071 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001073
1074 if (mActivityContainer.mParentActivity == null) {
1075 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001076 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001077 }
1078
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001079 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001080 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001081 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001082 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001083 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001084 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1085 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001086 prev.state = ActivityState.PAUSING;
1087 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001088 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001089 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -07001090 if (mService.mHasRecents
1091 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001092 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001093 }
Dianne Hackborncee04b52013-07-03 17:01:28 -07001094 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001095
1096 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001097
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001098 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001099 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001100 try {
1101 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001102 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001103 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001104 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001105 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001106 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001107 } catch (Exception e) {
1108 // Ignore exception, if process died other code will cleanup.
1109 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001110 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001111 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001112 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001113 }
1114 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001115 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001117 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001118 }
1119
1120 // If we are not going to sleep, we want to ensure the device is
1121 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001122 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001123 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001124 }
1125
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001126 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127 // Have the window manager pause its key dispatching until the new
1128 // activity has started. If we're pausing the activity just because
1129 // the screen is being turned off and the UI is sleeping, don't interrupt
1130 // key dispatch; the same activity will pick it up again on wakeup.
1131 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001132 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001133 } else if (DEBUG_PAUSE) {
1134 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135 }
1136
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001137 if (dontWait) {
1138 // If the caller said they don't want to wait for the pause, then complete
1139 // the pause now.
1140 completePauseLocked(false);
1141 return false;
1142
1143 } else {
1144 // Schedule a pause timeout in case the app doesn't respond.
1145 // We don't give it much time because this directly impacts the
1146 // responsiveness seen by the user.
1147 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1148 msg.obj = prev;
1149 prev.pauseTime = SystemClock.uptimeMillis();
1150 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001151 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001152 return true;
1153 }
1154
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001155 } else {
1156 // This activity failed to schedule the
1157 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001158 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001159 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001160 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001161 }
1162 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001163 }
1164 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001165
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001166 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001167 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1168 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001169
Craig Mautnerd2328952013-03-05 12:46:26 -08001170 final ActivityRecord r = isInStackLocked(token);
1171 if (r != null) {
1172 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1173 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001174 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001175 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001176 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001177 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001178 } else {
1179 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1180 r.userId, System.identityHashCode(r), r.shortComponentName,
1181 mPausingActivity != null
1182 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001183 if (r.state == ActivityState.PAUSING) {
1184 r.state = ActivityState.PAUSED;
1185 if (r.finishing) {
1186 if (DEBUG_PAUSE) Slog.v(TAG,
1187 "Executing finish of failed to pause activity: " + r);
1188 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1189 }
louis_chang047dfd42015-04-08 16:35:55 +08001190 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 }
1192 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001193 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 }
1195
Chong Zhangd78ddb42016-03-02 17:01:14 -08001196 final void activityResumedLocked(IBinder token) {
1197 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1198 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1199 r.icicle = null;
1200 r.haveState = false;
1201 }
1202
Craig Mautnera0026042014-04-23 11:45:37 -07001203 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1204 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001205 if (r.state != ActivityState.STOPPING) {
1206 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1207 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1208 return;
1209 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001210 if (persistentState != null) {
1211 r.persistentState = persistentState;
1212 mService.notifyTaskPersisterLocked(r.task, false);
1213 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001214 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001215 if (icicle != null) {
1216 // If icicle is null, this is happening due to a timeout, so we
1217 // haven't really saved the state.
1218 r.icicle = icicle;
1219 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001220 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001221 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001222 }
1223 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001224 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001225 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1226 r.stopped = true;
1227 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001228
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08001229 mWindowManager.notifyAppStopped(r.appToken, true);
Robert Carre12aece2016-02-02 22:43:27 -08001230
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001231 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001232 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001233 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001234 if (r.finishing) {
1235 r.clearOptionsLocked();
1236 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001237 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001238 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001239 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001240 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001241 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001242 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001243 }
1244 }
1245 }
1246
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001247 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001248 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001249 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001250
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001251 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001252 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001253 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001254 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001255 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001256 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001257 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001258 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1259 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001260 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001261 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1262 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001263 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001264 if (prev.deferRelaunchUntilPaused) {
1265 // Complete the deferred relaunch that was waiting for pause to complete.
1266 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1267 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1268 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001269 } else if (wasStopping) {
1270 // We are also stopping, the stop request must have gone soon after the pause.
1271 // We can't clobber it, because the stop confirmation will not be handled.
1272 // We don't need to schedule another stop, we only need to let it happen.
1273 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001274 } else if ((!prev.visible && !hasVisibleBehindActivity())
1275 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001276 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001277 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001278 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001279 }
1280 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001281 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001282 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001283 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001284 // It is possible the activity was freezing the screen before it was paused.
1285 // In that case go ahead and remove the freeze this activity has on the screen
1286 // since it is no longer visible.
1287 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001288 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001289 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001290
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001291 if (resumeNext) {
1292 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1293 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001294 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001295 } else {
1296 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001297 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001298 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001299 // If there are no more activities available to run, do resume anyway to start
1300 // something. Also if the top activity on the stack is not the just paused
1301 // activity, we need to go ahead and resume it to ensure we complete an
1302 // in-flight app switch.
1303 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001304 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001305 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001306 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001307
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001308 if (prev != null) {
1309 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001310
Craig Mautner525f3d92013-05-07 14:01:50 -07001311 if (prev.app != null && prev.cpuTimeAtResume > 0
1312 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001313 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1314 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001315 if (diff > 0) {
1316 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1317 synchronized (bsi) {
1318 BatteryStatsImpl.Uid.Proc ps =
1319 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001320 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001321 if (ps != null) {
1322 ps.addForegroundTimeLocked(diff);
1323 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001324 }
1325 }
1326 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001327 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001328 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001329
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001330 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001331 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1332 mService.notifyTaskStackChangedLocked();
1333 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1334 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001335
1336 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001337 }
1338
Chong Zhang46b1ac62016-02-18 17:53:57 -08001339 private void addToStopping(ActivityRecord r, boolean immediate) {
1340 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1341 mStackSupervisor.mStoppingActivities.add(r);
1342 }
1343
1344 // If we already have a few activities waiting to stop, then give up
1345 // on things going idle and start clearing them out. Or if r is the
1346 // last of activity of the last task the stack will be empty and must
1347 // be cleared immediately.
1348 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1349 || (r.frontOfTask && mTaskHistory.size() <= 1);
1350
1351 if (immediate || forceIdle) {
1352 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1353 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001354 mStackSupervisor.scheduleIdleLocked();
1355 } else {
1356 mStackSupervisor.checkReadyForSleepLocked();
1357 }
1358 }
1359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360 /**
1361 * Once we know that we have asked an application to put an activity in
1362 * the resumed state (either by launching it or explicitly telling it),
1363 * this function updates the rest of our state to match that fact.
1364 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001365 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001366 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001367 next.idle = false;
1368 next.results = null;
1369 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001370 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001371
Chong Zhang85ee6542015-10-02 13:36:38 -07001372 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001373 ProcessRecord app = next.task.mActivities.get(0).app;
1374 if (app != null && app != mService.mHomeProcess) {
1375 mService.mHomeProcess = app;
1376 }
1377 }
1378
Craig Mautner07566322013-09-26 16:42:55 -07001379 if (next.nowVisible) {
1380 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001381 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001382 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001383
1384 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001385 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001386
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001387 mStackSupervisor.reportResumedActivityLocked(next);
1388
1389 next.resumeKeyDispatchingLocked();
1390 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001391
1392 // Mark the point when the activity is resuming
1393 // TODO: To be more accurate, the mark should be before the onCreate,
1394 // not after the onResume. But for subsequent starts, onResume is fine.
1395 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001396 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001397 } else {
1398 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1399 }
Winson Chung376543b2014-05-21 17:43:28 -07001400
George Mount6ba042b2014-07-28 11:12:28 -07001401 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001402
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001403 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001404 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001405 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001406 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 }
1408
Craig Mautner2568c3a2015-03-26 14:22:34 -07001409 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001410 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001411 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1412 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1413 r.mUpdateTaskThumbnailWhenHidden = false;
1414 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001415 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001416 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001417 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001418 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001419 container.setVisible(visible);
1420 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001421 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001422 }
1423
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001424 // Find the first visible activity above the passed activity and if it is translucent return it
1425 // otherwise return null;
1426 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1427 TaskRecord task = r.task;
1428 if (task == null) {
1429 return null;
1430 }
1431
1432 ActivityStack stack = task.stack;
1433 if (stack == null) {
1434 return null;
1435 }
1436
1437 int stackNdx = mStacks.indexOf(stack);
1438
1439 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1440 int taskNdx = tasks.indexOf(task);
1441
1442 ArrayList<ActivityRecord> activities = task.mActivities;
1443 int activityNdx = activities.indexOf(r) + 1;
1444
1445 final int numStacks = mStacks.size();
1446 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001447 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001448 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001449 final int numTasks = tasks.size();
1450 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001451 final TaskRecord currentTask = tasks.get(taskNdx);
1452 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001453 final int numActivities = activities.size();
1454 while (activityNdx < numActivities) {
1455 final ActivityRecord activity = activities.get(activityNdx);
1456 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001457 return historyStack.mFullscreen
1458 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001459 }
1460 ++activityNdx;
1461 }
1462 activityNdx = 0;
1463 ++taskNdx;
1464 }
1465 taskNdx = 0;
1466 ++stackNdx;
1467 }
1468
1469 return null;
1470 }
1471
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001472 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001473 ArrayList<ActivityStack> stacks = mStacks;
1474 final ActivityRecord parent = mActivityContainer.mParentActivity;
1475 if (parent != null) {
1476 stacks = parent.task.stack.mStacks;
1477 }
1478 if (stacks != null) {
1479 for (int i = stacks.size() - 1; i >= 0; --i) {
1480 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001481 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001482 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001483 return stack;
1484 }
1485 }
1486 }
1487 return null;
1488 }
1489
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001490 /** Returns true if the stack contains a fullscreen task. */
1491 private boolean hasFullscreenTask() {
1492 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1493 final TaskRecord task = mTaskHistory.get(i);
1494 if (task.mFullscreen) {
1495 return true;
1496 }
1497 }
1498 return false;
1499 }
1500
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001501 /**
1502 * Returns true if the stack is translucent and can have other contents visible behind it if
1503 * needed. A stack is considered translucent if it don't contain a visible or
1504 * starting (about to be visible) activity that is fullscreen (opaque).
1505 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001506 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001507 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001508 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001509 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1510 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001511 final ArrayList<ActivityRecord> activities = task.mActivities;
1512 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1513 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001514
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001515 if (r.finishing) {
1516 // We don't factor in finishing activities when determining translucency since
1517 // they will be gone soon.
1518 continue;
1519 }
1520
1521 if (!r.visible && r != starting) {
1522 // Also ignore invisible activities that are not the currently starting
1523 // activity (about to be visible).
1524 continue;
1525 }
1526
1527 if (r.fullscreen) {
1528 // Stack isn't translucent if it has at least one fullscreen activity
1529 // that is visible.
1530 return false;
1531 }
1532
1533 if (!isHomeStack() && r.frontOfTask
1534 && task.isOverHomeStack() && stackBehindId != HOME_STACK_ID) {
1535 // Stack isn't translucent if it's top activity should have the home stack
1536 // behind it and the stack currently behind it isn't the home stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001537 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001538 }
1539 }
1540 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001541 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001542 }
1543
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001544 /**
1545 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1546 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001547 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001548 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001549 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001550 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001551 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001552 }
1553
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001554 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001555 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001556 }
1557
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001558 final int stackIndex = mStacks.indexOf(this);
1559
1560 if (stackIndex == mStacks.size() - 1) {
1561 Slog.wtf(TAG,
1562 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001563 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001564 }
1565
Chong Zhang75b37202015-12-04 14:16:36 -08001566 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1567 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001568 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001569 }
1570
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001571 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1572 final int focusedStackId = focusedStack.mStackId;
1573
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001574 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001575 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1576 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001577 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001578 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001579 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001580 }
1581
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001582 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001583 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001584 // task in the focus stack doesn't support any form of resizing but we show it for the
1585 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001586 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001587 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001588 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1589 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001590 }
1591
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001592 // Find the first stack behind focused stack that actually got something visible.
1593 int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1594 while (stackBehindFocusedIndex >= 0 &&
1595 mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1596 stackBehindFocusedIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001597 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001598 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001599 && stackIndex == stackBehindFocusedIndex) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001600 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001601 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001602 }
1603
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001604 final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1605 ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1606
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001607 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001608 && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001609 // Stacks behind the fullscreen stack with a translucent activity are always
1610 // visible so they can act as a backdrop to the translucent activity.
1611 // For example, dialog activities
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001612 if (stackIndex == stackBehindFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001613 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001614 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001615 if (stackBehindFocusedIndex >= 0) {
1616 if ((stackBehindFocusedId == DOCKED_STACK_ID
1617 || stackBehindFocusedId == PINNED_STACK_ID)
1618 && stackIndex == (stackBehindFocusedIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001619 // The stack behind the docked or pinned stack is also visible so we can have a
1620 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001621 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001622 }
1623 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001624 }
1625
Wale Ogunwale3797c222015-10-27 14:21:58 -07001626 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001627 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001628 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001629 }
1630
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001631 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001632 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001633
1634 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1635 continue;
1636 }
1637
Wale Ogunwale3797c222015-10-27 14:21:58 -07001638 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001639 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001640 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001641 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001642
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001643 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001644 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001645 }
1646 }
1647
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001648 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001649 }
1650
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001651 final int rankTaskLayers(int baseLayer) {
1652 int layer = 0;
1653 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1654 final TaskRecord task = mTaskHistory.get(taskNdx);
1655 ActivityRecord r = task.topRunningActivityLocked();
1656 if (r == null || r.finishing || !r.visible) {
1657 task.mLayerRank = -1;
1658 } else {
1659 task.mLayerRank = baseLayer + layer++;
1660 }
1661 }
1662 return layer;
1663 }
1664
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 /**
1666 * Make sure that all activities that need to be visible (that is, they
1667 * currently can be seen by the user) actually are.
1668 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001669 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1670 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001671 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001672 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001674 if (top != null) {
1675 checkTranslucentActivityWaiting(top);
1676 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001677
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001678 // If the top activity is not fullscreen, then we need to
1679 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001680 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001681 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001682 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1683 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001684 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001685 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001686 boolean behindTranslucentActivity = false;
1687 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001688 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001689 final TaskRecord task = mTaskHistory.get(taskNdx);
1690 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001691 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1692 final ActivityRecord r = activities.get(activityNdx);
1693 if (r.finishing) {
Chong Zhang22bc8512016-04-07 13:47:18 -07001694 // Normally the screenshot will be taken in makeInvisible(). When an activity
1695 // is finishing, we no longer change its visibility, but we still need to take
1696 // the screenshots if startPausingLocked decided it should be taken.
1697 if (r.mUpdateTaskThumbnailWhenHidden) {
1698 r.updateThumbnailLocked(screenshotActivitiesLocked(r), null);
1699 r.mUpdateTaskThumbnailWhenHidden = false;
1700 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001701 continue;
1702 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001703 final boolean isTop = r == top;
1704 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001705 continue;
1706 }
1707 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001708 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1709 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001710 final boolean activityVisibleBehind =
1711 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1712 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1713 || activityVisibleBehind) && okToShowLocked(r);
1714 if (isVisible) {
1715 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1716 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001717 // First: if this is not the current activity being started, make
1718 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001719 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001720 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001722
1723 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001724 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001725 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001726 if (activityNdx >= activities.size()) {
1727 // Record may be removed if its process needs to restart.
1728 activityNdx = activities.size() - 1;
1729 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001730 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001731 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001732 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001733 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001734 // If this activity is already visible, then there is nothing to do here.
1735 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001736 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001737 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001738 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001739 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001741 // Aggregate current change flags.
1742 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001743 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1744 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001745 if (behindFullscreenActivity && !r.fullscreen) {
1746 behindTranslucentActivity = true;
1747 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001748 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001749 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001750 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1751 + stackInvisible + " behindFullscreenActivity="
1752 + behindFullscreenActivity + " mLaunchTaskBehind="
1753 + r.mLaunchTaskBehind);
1754 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 }
1756 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001757 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1758 // The visibility of tasks and the activities they contain in freeform stack are
1759 // determined individually unlike other stacks where the visibility or fullscreen
1760 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001761 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001762 } else if (mStackId == HOME_STACK_ID) {
1763 if (task.isHomeTask()) {
1764 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1765 + " stackInvisible=" + stackInvisible
1766 + " behindFullscreenActivity=" + behindFullscreenActivity);
1767 // No other task in the home stack should be visible behind the home activity.
1768 // Home activities is usually a translucent activity with the wallpaper behind
1769 // them. However, when they don't have the wallpaper behind them, we want to
1770 // show activities in the next application stack behind them vs. another
1771 // task in the home stack like recents.
1772 behindFullscreenActivity = true;
1773 } else if (task.isRecentsTask()
1774 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1775 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1776 "Recents task returning to app: at " + task
1777 + " stackInvisible=" + stackInvisible
1778 + " behindFullscreenActivity=" + behindFullscreenActivity);
1779 // We don't want any other tasks in the home stack visible if the recents
1780 // activity is going to be returning to an application activity type.
1781 // We do this to preserve the visible order the user used to get into the
1782 // recents activity. The recents activity is normally translucent and if it
1783 // doesn't have the wallpaper behind it the next activity in the home stack
1784 // shouldn't be visible when the home stack is brought to the front to display
1785 // the recents activity from an app.
1786 behindFullscreenActivity = true;
1787 }
1788
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001789 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001791
1792 if (mTranslucentActivityWaiting != null &&
1793 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1794 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1795 notifyActivityDrawnLocked(null);
1796 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 }
Craig Mautner58547802013-03-05 08:23:53 -08001798
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001799 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1800 if (mTranslucentActivityWaiting != top) {
1801 mUndrawnActivitiesBelowTopTranslucent.clear();
1802 if (mTranslucentActivityWaiting != null) {
1803 // Call the callback with a timeout indication.
1804 notifyActivityDrawnLocked(null);
1805 mTranslucentActivityWaiting = null;
1806 }
1807 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1808 }
1809 }
1810
1811 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001812 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001813 // We need to make sure the app is running if it's the top, or it is just made visible from
1814 // invisible. If the app is already visible, it must have died while it was visible. In this
1815 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1816 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001817 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001818 // This activity needs to be visible, but isn't even running...
1819 // get it started and resume if no other stack in this stack is resumed.
1820 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1821 if (r != starting) {
1822 r.startFreezingScreenLocked(r.app, configChanges);
1823 }
1824 if (!r.visible || r.mLaunchTaskBehind) {
1825 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1826 setVisible(r, true);
1827 }
1828 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001829 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001830 return true;
1831 }
1832 }
1833 return false;
1834 }
1835
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001836 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001837 if (!r.visible) {
1838 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1839 return;
1840 }
1841 // Now for any activities that aren't visible to the user, make sure they no longer are
1842 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001843 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001844 try {
1845 setVisible(r, false);
1846 switch (r.state) {
1847 case STOPPING:
1848 case STOPPED:
1849 if (r.app != null && r.app.thread != null) {
1850 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1851 "Scheduling invisibility: " + r);
1852 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1853 }
1854 break;
1855
1856 case INITIALIZING:
1857 case RESUMED:
1858 case PAUSING:
1859 case PAUSED:
1860 // This case created for transitioning activities from
1861 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001862 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001863 releaseBackgroundResources(r);
1864 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001865 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001866 }
1867 break;
1868
1869 default:
1870 break;
1871 }
1872 } catch (Exception e) {
1873 // Just skip on any failure; we'll make it visible when it next restarts.
1874 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1875 }
1876 }
1877
1878 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1879 TaskRecord task, ActivityRecord r) {
1880 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001881 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001882 + " stackInvisible=" + stackInvisible
1883 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001884 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001885 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001886 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001887 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1888 + " stackInvisible=" + stackInvisible
1889 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001890 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001891 }
1892 return behindFullscreenActivity;
1893 }
1894
Jorim Jaggie66edb12016-02-05 12:41:17 -08001895 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1896
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001897 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001898 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001899 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1900 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001901 return;
1902 }
1903
1904 // If this activity is paused, tell it to now show its window.
1905 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1906 "Making visible and scheduling visibility: " + r);
1907 try {
1908 if (mTranslucentActivityWaiting != null) {
1909 r.updateOptionsLocked(r.returningOptions);
1910 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001911 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001912 setVisible(r, true);
1913 r.sleeping = false;
1914 r.app.pendingUiClean = true;
1915 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1916 r.stopFreezingScreenLocked(false);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001917
1918 // The activity may be waiting for stop, but that is no longer
1919 // appropriate for it.
1920 mStackSupervisor.mStoppingActivities.remove(r);
1921 mStackSupervisor.mGoingToSleepActivities.remove(r);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001922 } catch (Exception e) {
1923 // Just skip on any failure; we'll make it
1924 // visible when it next restarts.
1925 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001926 }
1927 }
1928
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001929 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001930 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1931 r.stopFreezingScreenLocked(false);
1932 try {
1933 if (r.returningOptions != null) {
1934 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1935 }
1936 } catch(RemoteException e) {
1937 }
1938 return r.state == ActivityState.RESUMED;
1939 }
1940
Todd Kennedyaab56db2015-01-30 09:39:53 -08001941 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001942 mTranslucentActivityWaiting = r;
1943 mUndrawnActivitiesBelowTopTranslucent.clear();
1944 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1945 }
1946
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001947 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1948 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1949 final TaskRecord task = mTaskHistory.get(taskNdx);
1950 final ArrayList<ActivityRecord> activities = task.mActivities;
1951 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1952 final ActivityRecord r = activities.get(activityNdx);
1953 if ( r.appTimeTracker != except) {
1954 r.appTimeTracker = null;
1955 }
1956 }
1957 }
1958 }
1959
Craig Mautner5eda9b32013-07-02 11:58:16 -07001960 /**
1961 * Called as activities below the top translucent activity are redrawn. When the last one is
1962 * redrawn notify the top activity by calling
1963 * {@link Activity#onTranslucentConversionComplete}.
1964 *
1965 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1966 * occurred and the activity will be notified immediately.
1967 */
1968 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001969 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001970 if ((r == null)
1971 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1972 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1973 // The last undrawn activity below the top has just been drawn. If there is an
1974 // opaque activity at the top, notify it that it can become translucent safely now.
1975 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1976 mTranslucentActivityWaiting = null;
1977 mUndrawnActivitiesBelowTopTranslucent.clear();
1978 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1979
Craig Mautner71dd1b62014-02-18 15:48:52 -08001980 if (waitingActivity != null) {
1981 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1982 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1983 try {
1984 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1985 waitingActivity.appToken, r != null);
1986 } catch (RemoteException e) {
1987 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001988 }
1989 }
1990 }
1991 }
1992
Craig Mautnera61bc652013-10-28 15:43:18 -07001993 /** If any activities below the top running one are in the INITIALIZING state and they have a
1994 * starting window displayed then remove that starting window. It is possible that the activity
1995 * in this state will never resumed in which case that starting window will be orphaned. */
1996 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001997 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001998 boolean aboveTop = true;
1999 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2000 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2001 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2002 final ActivityRecord r = activities.get(activityNdx);
2003 if (aboveTop) {
2004 if (r == topActivity) {
2005 aboveTop = false;
2006 }
2007 continue;
2008 }
2009
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002010 if (r.state == ActivityState.INITIALIZING
2011 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002012 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
2013 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002014 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07002015 mWindowManager.removeAppStartingWindow(r.appToken);
2016 }
2017 }
2018 }
2019 }
2020
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002021 /**
2022 * Ensure that the top activity in the stack is resumed.
2023 *
2024 * @param prev The previously resumed activity, for when in the process
2025 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002026 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027 *
2028 * @return Returns true if something is being resumed, or false if
2029 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002030 *
2031 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2032 * non-focused stack to be resumed.
2033 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2034 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08002036 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002037 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002038 // Don't even start recursing.
2039 return false;
2040 }
2041
2042 boolean result = false;
2043 try {
2044 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002045 mStackSupervisor.inResumeTopActivity = true;
2046 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
2047 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08002048 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08002049 }
Craig Mautner544efa72014-09-04 16:41:20 -07002050 result = resumeTopActivityInnerLocked(prev, options);
2051 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002052 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002053 }
2054 return result;
2055 }
2056
Chong Zhang280d3322015-11-03 17:27:26 -08002057 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002058 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07002059
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002060 if (!mService.mBooting && !mService.mBooted) {
2061 // Not ready yet!
2062 return false;
2063 }
2064
Craig Mautnerdf88d732014-01-27 09:21:32 -08002065 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002066 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07002067 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002068 // Do not resume this stack if its parent is not resumed.
2069 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
2070 return false;
2071 }
2072
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002073 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002074
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002076 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077
2078 // Remember how we'll process this pause/resume situation, and ensure
2079 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002080 final boolean userLeaving = mStackSupervisor.mUserLeaving;
2081 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082
Craig Mautner84984fa2014-06-19 11:19:20 -07002083 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002085 // There are no more activities!
2086 final String reason = "noMoreActivities";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07002087 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack()
2088 ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2089 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(returnTaskType, reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002090 // Try to move focus to the next visible stack with a running activity if this
2091 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002092 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2093 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002094 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002095
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002096 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07002097 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002098 if (DEBUG_STATES) Slog.d(TAG_STATES,
2099 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07002100 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08002101 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07002102 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002103 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
2105
2106 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002109 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2110 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 // Make sure we have executed any pending transitions, since there
2112 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002113 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002115 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002116 if (DEBUG_STATES) Slog.d(TAG_STATES,
2117 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002118 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 return false;
2120 }
2121
Craig Mautner525f3d92013-05-07 14:01:50 -07002122 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07002123 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002124 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002125 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002126 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002127 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002128 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002129 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002130 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002131 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002132 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002133 } else if (!isOnHomeDisplay()) {
2134 return false;
2135 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002136 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002137 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002138 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2139 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2140 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002141 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002142 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002143 }
2144
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145 // If we are sleeping, and there is no resumed activity, and the top
2146 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002147 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002148 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002149 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002150 // Make sure we have executed any pending transitions, since there
2151 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002152 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002154 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002155 if (DEBUG_STATES) Slog.d(TAG_STATES,
2156 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002157 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002158 return false;
2159 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002160
2161 // Make sure that the user who owns this activity is started. If not,
2162 // we will just leave it as is because someone should be bringing
2163 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002164 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002165 Slog.w(TAG, "Skipping resume of top activity " + next
2166 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002167 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002168 return false;
2169 }
2170
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 // The activity may be waiting for stop, but that is no longer
2172 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002173 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002174 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002175 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002176 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002178 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002180 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002181 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002182 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002183 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002184 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002185 return false;
2186 }
2187
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002188 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2189
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002190 // We need to start pausing the current activity so the top one can be resumed...
2191 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002192 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002193 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002194 if (DEBUG_STATES) Slog.d(TAG_STATES,
2195 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002196 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002197 }
2198 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002199 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002200 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002201 // At this point we want to put the upcoming activity's process
2202 // at the top of the LRU list, since we know we will be needing it
2203 // very soon and it would be a waste to let it get killed if it
2204 // happens to be sitting towards the end.
2205 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002206 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002207 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002208 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 return true;
2210 }
2211
Christopher Tated3f175c2012-06-14 14:16:54 -07002212 // If the most recent activity was noHistory but was only stopped rather
2213 // than stopped+finished because the device went to sleep, we need to make
2214 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002215 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002216 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002217 if (DEBUG_STATES) Slog.d(TAG_STATES,
2218 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002219 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002220 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002221 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002222 }
2223
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002224 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002225 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2226 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002227 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002228 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2229 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002230 } else {
2231 // The next activity is already visible, so hide the previous
2232 // activity's windows right now so we can show the new one ASAP.
2233 // We only do this if the previous is finishing, which should mean
2234 // it is on top of the one being resumed so hiding it quickly
2235 // is good. Otherwise, we want to do the normal route of allowing
2236 // the resumed activity to be shown so we can decide if the
2237 // previous should actually be hidden depending on whether the
2238 // new one is found to be full-screen or not.
2239 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002240 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002241 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2242 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002243 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002244 + ", nowVisible=" + next.nowVisible);
2245 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002246 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002247 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002248 + ", waitingVisible="
2249 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2250 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 }
2252 }
2253 }
2254
Dianne Hackborne7f97212011-02-24 14:40:20 -08002255 // Launching this app's activity, make sure the app is no longer
2256 // considered stopped.
2257 try {
2258 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002259 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002260 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002261 } catch (IllegalArgumentException e) {
2262 Slog.w(TAG, "Failed trying to unstop package "
2263 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002264 }
2265
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002266 // We are starting up the next activity, so tell the window manager
2267 // that the previous one will be hidden soon. This way it can know
2268 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002269 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002270 if (prev != null) {
2271 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002272 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002273 "Prepare close transition: prev=" + prev);
2274 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002275 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002276 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002278 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002279 ? TRANSIT_ACTIVITY_CLOSE
2280 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002281 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002282 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002284 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2285 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002286 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002287 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002288 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002289 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002290 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002291 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002292 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002293 ? TRANSIT_TASK_OPEN_BEHIND
2294 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 }
2296 }
Craig Mautner967212c2013-04-13 21:10:58 -07002297 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002298 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002299 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002300 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002301 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002302 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002303 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002304 }
2305 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002306
2307 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002308 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002309 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2310 if (opts != null) {
2311 resumeAnimOptions = opts.toBundle();
2312 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002313 next.applyOptionsLocked();
2314 } else {
2315 next.clearOptionsLocked();
2316 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317
Craig Mautnercf910b02013-04-23 11:23:27 -07002318 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002320 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2321 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322
2323 // This activity is now becoming visible.
Chong Zhangdea4bd92016-03-15 12:50:03 -07002324 if (!next.visible || next.stopped) {
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002325 mWindowManager.setAppVisibility(next.appToken, true);
2326 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002328 // schedule launch ticks to collect information about slow apps.
2329 next.startLaunchTickingLocked();
2330
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002331 ActivityRecord lastResumedActivity =
2332 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 ActivityState lastState = next.state;
2334
2335 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002336
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002337 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 next.state = ActivityState.RESUMED;
2339 mResumedActivity = next;
2340 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002341 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002342 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002344 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002345
2346 // Have the window manager re-evaluate the orientation of
2347 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002348 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002349 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002350 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002351 mService.mConfiguration,
2352 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2353 if (config != null) {
2354 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002356 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002357 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002358
Craig Mautner525f3d92013-05-07 14:01:50 -07002359 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 // The configuration update wasn't able to keep the existing
2361 // instance of the activity, and instead started a new one.
2362 // We should be all done, but let's just make sure our activity
2363 // is still at the top and schedule another run if something
2364 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002365 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002366 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002367 "Activity config changed during resume: " + next
2368 + ", new next: " + nextNext);
2369 if (nextNext != next) {
2370 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002371 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002373 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002374 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002375 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002376 return true;
2377 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002378 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002379 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002380 }
Craig Mautner58547802013-03-05 08:23:53 -08002381
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 try {
2383 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002384 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385 if (a != null) {
2386 final int N = a.size();
2387 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002388 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2389 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002390 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 }
2392 }
2393
2394 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002395 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002396 }
2397
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002398 // Well the app will no longer be stopped.
2399 // Clear app token stopped state in window manager if needed.
2400 mWindowManager.notifyAppStopped(next.appToken, false);
2401
Craig Mautner299f9602015-01-26 09:47:33 -08002402 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2403 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002404
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002405 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002406 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002407 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002408 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002409 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002410 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002411 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002412
Craig Mautner0eea92c2013-05-16 13:35:39 -07002413 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002414
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002415 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416 } catch (Exception e) {
2417 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002418 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002419 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002420 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002421 if (lastStack != null) {
2422 lastStack.mResumedActivity = lastResumedActivity;
2423 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 Slog.i(TAG, "Restarting because process died: " + next);
2425 if (!next.hasBeenLaunched) {
2426 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002427 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2428 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002429 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002430 next.appToken, next.packageName, next.theme,
2431 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002432 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2433 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002434 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 }
George Mount2c92c972014-03-20 09:38:23 -07002436 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002437 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 return true;
2439 }
2440
2441 // From this point on, if something goes wrong there is no way
2442 // to recover the activity.
2443 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 completeResumeLocked(next);
2445 } catch (Exception e) {
2446 // If any exception gets thrown, toss away this
2447 // activity and try the next one.
2448 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002449 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002450 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002451 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002452 return true;
2453 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002454 } else {
2455 // Whoops, need to restart this activity!
2456 if (!next.hasBeenLaunched) {
2457 next.hasBeenLaunched = true;
2458 } else {
2459 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002460 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002461 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002462 mService.compatibilityInfoForPackageLocked(
2463 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002464 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002465 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002466 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002467 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002468 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002469 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002470 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002471 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002472 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 }
2474
Craig Mautnercf910b02013-04-23 11:23:27 -07002475 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002476 return true;
2477 }
2478
riddle_hsuc215a4f2014-12-27 12:10:45 +08002479 private TaskRecord getNextTask(TaskRecord targetTask) {
2480 final int index = mTaskHistory.indexOf(targetTask);
2481 if (index >= 0) {
2482 final int numTasks = mTaskHistory.size();
2483 for (int i = index + 1; i < numTasks; ++i) {
2484 TaskRecord task = mTaskHistory.get(i);
2485 if (task.userId == targetTask.userId) {
2486 return task;
2487 }
2488 }
2489 }
2490 return null;
2491 }
2492
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002493 private void insertTaskAtPosition(TaskRecord task, int position) {
2494 if (position >= mTaskHistory.size()) {
2495 insertTaskAtTop(task, null);
2496 return;
2497 }
2498 // Calculate maximum possible position for this task.
2499 int maxPosition = mTaskHistory.size();
2500 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002501 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002502 // Put non-current user tasks below current user tasks.
2503 while (maxPosition > 0) {
2504 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2505 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002506 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002507 break;
2508 }
2509 maxPosition--;
2510 }
2511 }
2512 position = Math.min(position, maxPosition);
2513 mTaskHistory.remove(task);
2514 mTaskHistory.add(position, task);
2515 updateTaskMovement(task, true);
2516 }
2517
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002518 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002519 // If the moving task is over home stack, transfer its return type to next task
2520 if (task.isOverHomeStack()) {
2521 final TaskRecord nextTask = getNextTask(task);
2522 if (nextTask != null) {
2523 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2524 }
2525 }
2526
Craig Mautner9c85c202013-10-04 20:11:26 -07002527 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002528 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002529 if (isOnHomeDisplay()) {
2530 ActivityStack lastStack = mStackSupervisor.getLastStack();
2531 final boolean fromHome = lastStack.isHomeStack();
2532 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002533 int returnToType = APPLICATION_ACTIVITY_TYPE;
2534 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2535 returnToType = lastStack.topTask() == null
2536 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2537 }
2538 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002539 }
2540 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002541 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002542 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002543
Craig Mautnerac6f8432013-07-17 13:24:59 -07002544 mTaskHistory.remove(task);
2545 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002546 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002547 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002548 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002549 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002550 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002551 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002552 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002553 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002554 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002555 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002556 break;
2557 }
2558 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002559 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002560 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002561 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002562 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002563 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002564
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002565 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2566 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002567 TaskRecord rTask = r.task;
2568 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002569 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2570 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002571 // Last activity in task had been removed or ActivityManagerService is reusing task.
2572 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002573 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002574 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002575 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002576 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002577 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 if (!newTask) {
2579 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002580 boolean startIt = true;
2581 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2582 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002583 if (task.getTopActivity() == null) {
2584 // All activities in task are finishing.
2585 continue;
2586 }
Craig Mautner70a86932013-02-28 22:37:44 -08002587 if (task == r.task) {
2588 // Here it is! Now, if this is not yet visible to the
2589 // user, then just add it without starting; it will
2590 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002591 if (!startIt) {
2592 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2593 + task, new RuntimeException("here").fillInStackTrace());
2594 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002595 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002596 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002598 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002599 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002600 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002601 return;
2602 }
2603 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002604 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 startIt = false;
2606 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 }
2608 }
2609
2610 // Place a new activity at top of stack, so it is next to interact
2611 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002612
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002613 // If we are not placing the new activity frontmost, we do not want
2614 // to deliver the onUserLeaving callback to the actual frontmost
2615 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002616 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002617 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002618 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002619 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002620 }
Craig Mautner70a86932013-02-28 22:37:44 -08002621
2622 task = r.task;
2623
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002624 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002625 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002626 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002627 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002628 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002629
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002630 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002631 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002632 // We want to show the starting preview window if we are
2633 // switching to a new task, or the next activity's process is
2634 // not currently running.
2635 boolean showStartingIcon = newTask;
2636 ProcessRecord proc = r.app;
2637 if (proc == null) {
2638 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2639 }
2640 if (proc == null || proc.thread == null) {
2641 showStartingIcon = true;
2642 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002643 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002644 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002645 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002646 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002648 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002649 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002650 ? r.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002651 ? TRANSIT_TASK_OPEN_BEHIND
2652 : TRANSIT_TASK_OPEN
2653 : TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 mNoAnimActivities.remove(r);
2655 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002656 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002657 boolean doShow = true;
2658 if (newTask) {
2659 // Even though this activity is starting fresh, we still need
2660 // to reset it to make sure we apply affinities to move any
2661 // existing activities from other tasks in to it.
2662 // If the caller has requested that the target task be
2663 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002664 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002665 resetTaskIfNeededLocked(r, r);
2666 doShow = topRunningNonDelayedActivityLocked(null) == r;
2667 }
Chong Zhang280d3322015-11-03 17:27:26 -08002668 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002669 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2670 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002672 if (r.mLaunchTaskBehind) {
2673 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2674 // tell WindowManager that r is visible even though it is at the back of the stack.
2675 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002676 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002677 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002678 // Figure out if we are transitioning from another activity that is
2679 // "has the same starting icon" as the next one. This allows the
2680 // window manager to keep the previous window it had previously
2681 // created, if it still had one.
2682 ActivityRecord prev = mResumedActivity;
2683 if (prev != null) {
2684 // We don't want to reuse the previous starting preview if:
2685 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002686 if (prev.task != r.task) {
2687 prev = null;
2688 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002689 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002690 else if (prev.nowVisible) {
2691 prev = null;
2692 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002693 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002694 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002695 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002696 mService.compatibilityInfoForPackageLocked(
2697 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002698 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002699 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002700 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 }
2702 } else {
2703 // If this is the first activity, don't do any fancy animations,
2704 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002705 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002706 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002707 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002708 }
2709 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002710 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 }
2713
Dianne Hackbornbe707852011-11-11 14:32:10 -08002714 final void validateAppTokensLocked() {
2715 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002716 mValidateAppTokens.ensureCapacity(numActivities());
2717 final int numTasks = mTaskHistory.size();
2718 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2719 TaskRecord task = mTaskHistory.get(taskNdx);
2720 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002721 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002722 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002723 }
Craig Mautner000f0022013-02-26 15:04:29 -08002724 TaskGroup group = new TaskGroup();
2725 group.taskId = task.taskId;
2726 mValidateAppTokens.add(group);
2727 final int numActivities = activities.size();
2728 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2729 final ActivityRecord r = activities.get(activityNdx);
2730 group.tokens.add(r.appToken);
2731 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002732 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002733 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002734 }
2735
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 /**
2737 * Perform a reset of the given task, if needed as part of launching it.
2738 * Returns the new HistoryRecord at the top of the task.
2739 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002740 /**
2741 * Helper method for #resetTaskIfNeededLocked.
2742 * We are inside of the task being reset... we'll either finish this activity, push it out
2743 * for another task, or leave it as-is.
2744 * @param task The task containing the Activity (taskTop) that might be reset.
2745 * @param forceReset
2746 * @return An ActivityOptions that needs to be processed.
2747 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002748 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002749 ActivityOptions topOptions = null;
2750
2751 int replyChainEnd = -1;
2752 boolean canMoveOptions = true;
2753
2754 // We only do this for activities that are not the root of the task (since if we finish
2755 // the root, we may no longer have the task!).
2756 final ArrayList<ActivityRecord> activities = task.mActivities;
2757 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002758 final int rootActivityNdx = task.findEffectiveRootIndex();
2759 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002760 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002761 if (target.frontOfTask)
2762 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002763
2764 final int flags = target.info.flags;
2765 final boolean finishOnTaskLaunch =
2766 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2767 final boolean allowTaskReparenting =
2768 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2769 final boolean clearWhenTaskReset =
2770 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2771
2772 if (!finishOnTaskLaunch
2773 && !clearWhenTaskReset
2774 && target.resultTo != null) {
2775 // If this activity is sending a reply to a previous
2776 // activity, we can't do anything with it now until
2777 // we reach the start of the reply chain.
2778 // XXX note that we are assuming the result is always
2779 // to the previous activity, which is almost always
2780 // the case but we really shouldn't count on.
2781 if (replyChainEnd < 0) {
2782 replyChainEnd = i;
2783 }
2784 } else if (!finishOnTaskLaunch
2785 && !clearWhenTaskReset
2786 && allowTaskReparenting
2787 && target.taskAffinity != null
2788 && !target.taskAffinity.equals(task.affinity)) {
2789 // If this activity has an affinity for another
2790 // task, then we need to move it out of here. We will
2791 // move it as far out of the way as possible, to the
2792 // bottom of the activity stack. This also keeps it
2793 // correctly ordered with any activities we previously
2794 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002795 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002796 final ActivityRecord bottom =
2797 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002798 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002799 if (bottom != null && target.taskAffinity != null
2800 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002801 // If the activity currently at the bottom has the
2802 // same task affinity as the one we are moving,
2803 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002804 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002805 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002806 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002807 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002808 targetTask = createTaskRecord(
2809 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2810 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002811 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002812 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002813 + " out to new task " + target.task);
2814 }
2815
Wale Ogunwale706ed792015-08-02 10:29:44 -07002816 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002817
Craig Mautner525f3d92013-05-07 14:01:50 -07002818 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002819 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2820 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002821 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002822 if (p.finishing) {
2823 continue;
2824 }
2825
Craig Mautnere3a74d52013-02-22 14:14:58 -08002826 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002827 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002828 topOptions = p.takeOptionsLocked();
2829 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002830 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002831 }
2832 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002833 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2834 "Removing activity " + p + " from task=" + task + " adding to task="
2835 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002836 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2837 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002838 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002839 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002840
Wale Ogunwale706ed792015-08-02 10:29:44 -07002841 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002842 }
2843
Wale Ogunwale706ed792015-08-02 10:29:44 -07002844 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002845 if (VALIDATE_TOKENS) {
2846 validateAppTokensLocked();
2847 }
2848
2849 replyChainEnd = -1;
2850 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2851 // If the activity should just be removed -- either
2852 // because it asks for it, or the task should be
2853 // cleared -- then finish it and anything that is
2854 // part of its reply chain.
2855 int end;
2856 if (clearWhenTaskReset) {
2857 // In this case, we want to finish this activity
2858 // and everything above it, so be sneaky and pretend
2859 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002860 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002861 } else if (replyChainEnd < 0) {
2862 end = i;
2863 } else {
2864 end = replyChainEnd;
2865 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002866 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002867 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002868 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002869 if (p.finishing) {
2870 continue;
2871 }
2872 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002873 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002874 topOptions = p.takeOptionsLocked();
2875 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002876 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002877 }
2878 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002879 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002880 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002881 if (finishActivityLocked(
2882 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002883 end--;
2884 srcPos--;
2885 }
2886 }
2887 replyChainEnd = -1;
2888 } else {
2889 // If we were in the middle of a chain, well the
2890 // activity that started it all doesn't want anything
2891 // special, so leave it all as-is.
2892 replyChainEnd = -1;
2893 }
2894 }
2895
2896 return topOptions;
2897 }
2898
2899 /**
2900 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2901 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2902 * @param affinityTask The task we are looking for an affinity to.
2903 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2904 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2905 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2906 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002907 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002908 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002909 int replyChainEnd = -1;
2910 final int taskId = task.taskId;
2911 final String taskAffinity = task.affinity;
2912
2913 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2914 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002915 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2916
2917 // Do not operate on or below the effective root Activity.
2918 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002919 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002920 if (target.frontOfTask)
2921 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002922
2923 final int flags = target.info.flags;
2924 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2925 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2926
2927 if (target.resultTo != null) {
2928 // If this activity is sending a reply to a previous
2929 // activity, we can't do anything with it now until
2930 // we reach the start of the reply chain.
2931 // XXX note that we are assuming the result is always
2932 // to the previous activity, which is almost always
2933 // the case but we really shouldn't count on.
2934 if (replyChainEnd < 0) {
2935 replyChainEnd = i;
2936 }
2937 } else if (topTaskIsHigher
2938 && allowTaskReparenting
2939 && taskAffinity != null
2940 && taskAffinity.equals(target.taskAffinity)) {
2941 // This activity has an affinity for our task. Either remove it if we are
2942 // clearing or move it over to our task. Note that
2943 // we currently punt on the case where we are resetting a
2944 // task that is not at the top but who has activities above
2945 // with an affinity to it... this is really not a normal
2946 // case, and we will need to later pull that task to the front
2947 // and usually at that point we will do the reset and pick
2948 // up those remaining activities. (This only happens if
2949 // someone starts an activity in a new task from an activity
2950 // in a task that is not currently on top.)
2951 if (forceReset || finishOnTaskLaunch) {
2952 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002953 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2954 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002955 for (int srcPos = start; srcPos >= i; --srcPos) {
2956 final ActivityRecord p = activities.get(srcPos);
2957 if (p.finishing) {
2958 continue;
2959 }
Todd Kennedy539db512014-12-15 09:57:55 -08002960 finishActivityLocked(
2961 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002962 }
2963 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002964 if (taskInsertionPoint < 0) {
2965 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002966
Craig Mautner77878772013-03-04 19:46:24 -08002967 }
Craig Mautner77878772013-03-04 19:46:24 -08002968
2969 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002970 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2971 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2972 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002973 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002974 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002975 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002976 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002977
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002978 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2979 "Removing and adding activity " + p + " to stack at " + task
2980 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002981 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2982 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002983 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002984 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002985 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002986 if (VALIDATE_TOKENS) {
2987 validateAppTokensLocked();
2988 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002989
2990 // Now we've moved it in to place... but what if this is
2991 // a singleTop activity and we have put it on top of another
2992 // instance of the same activity? Then we drop the instance
2993 // below so it remains singleTop.
2994 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2995 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002996 int targetNdx = taskActivities.indexOf(target);
2997 if (targetNdx > 0) {
2998 ActivityRecord p = taskActivities.get(targetNdx - 1);
2999 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003000 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3001 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003002 }
3003 }
3004 }
3005 }
3006
3007 replyChainEnd = -1;
3008 }
3009 }
Craig Mautner77878772013-03-04 19:46:24 -08003010 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003011 }
3012
Craig Mautner8849a5e2013-04-02 16:41:03 -07003013 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003014 ActivityRecord newActivity) {
3015 boolean forceReset =
3016 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
3017 if (ACTIVITY_INACTIVE_RESET_TIME > 0
3018 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
3019 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
3020 forceReset = true;
3021 }
3022 }
3023
3024 final TaskRecord task = taskTop.task;
3025
3026 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3027 * for remaining tasks. Used for later tasks to reparent to task. */
3028 boolean taskFound = false;
3029
3030 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3031 ActivityOptions topOptions = null;
3032
Craig Mautner77878772013-03-04 19:46:24 -08003033 // Preserve the location for reparenting in the new task.
3034 int reparentInsertionPoint = -1;
3035
Craig Mautnere3a74d52013-02-22 14:14:58 -08003036 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3037 final TaskRecord targetTask = mTaskHistory.get(i);
3038
3039 if (targetTask == task) {
3040 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3041 taskFound = true;
3042 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003043 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3044 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003045 }
3046 }
3047
Craig Mautner70a86932013-02-28 22:37:44 -08003048 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003049 if (taskNdx >= 0) {
3050 do {
3051 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3052 } while (taskTop == null && taskNdx >= 0);
3053 }
Craig Mautner70a86932013-02-28 22:37:44 -08003054
Craig Mautnere3a74d52013-02-22 14:14:58 -08003055 if (topOptions != null) {
3056 // If we got some ActivityOptions from an activity on top that
3057 // was removed from the task, propagate them to the new real top.
3058 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003059 taskTop.updateOptionsLocked(topOptions);
3060 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003061 topOptions.abort();
3062 }
3063 }
3064
3065 return taskTop;
3066 }
3067
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003068 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3069 String resultWho, int requestCode, int resultCode, Intent data) {
3070
3071 if (callingUid > 0) {
3072 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003073 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003074 }
3075
3076 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3077 + " : who=" + resultWho + " req=" + requestCode
3078 + " res=" + resultCode + " data=" + data);
3079 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3080 try {
3081 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3082 list.add(new ResultInfo(resultWho, requestCode,
3083 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003084 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 return;
3086 } catch (Exception e) {
3087 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3088 }
3089 }
3090
3091 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3092 }
3093
Craig Mautner299f9602015-01-26 09:47:33 -08003094 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003095 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
3096 return;
3097 }
3098
3099 final ActivityRecord next = topRunningActivityLocked();
3100 final String myReason = reason + " adjustFocus";
3101 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003102 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003103 // For freeform, docked, and pinned stacks we always keep the focus within the
3104 // stack as long as there is a running activity in the stack that we can adjust
3105 // focus to.
3106 mService.setFocusedActivityLocked(next, myReason);
3107 return;
3108 } else {
3109 final TaskRecord task = r.task;
3110 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003111 final int taskToReturnTo = task.getTaskToReturnTo();
3112
Wale Ogunwaled045c822015-12-02 09:14:28 -08003113 // For non-fullscreen stack, we want to move the focus to the next visible
3114 // stack to prevent the home screen from moving to the top and obscuring
3115 // other visible stacks.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003116 if (!mFullscreen
3117 && adjustFocusToNextFocusableStackLocked(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003118 return;
3119 }
3120 // Move the home stack to the top if this stack is fullscreen or there is no
3121 // other visible stack.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003122 if (mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003123 // Activity focus was already adjusted. Nothing else to do...
3124 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003125 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003126 }
3127 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003128 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003129
Wale Ogunwaled046a012015-12-24 13:05:59 -08003130 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003131 }
3132
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003133 private boolean adjustFocusToNextFocusableStackLocked(int taskToReturnTo, String reason) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003134 final ActivityStack stack = getNextFocusableStackLocked();
3135 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003136 if (stack == null) {
3137 return false;
3138 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003139
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003140 final ActivityRecord top = stack.topRunningActivityLocked();
3141
3142 if (stack.isHomeStack() && (top == null || !top.visible)) {
3143 // If we will be focusing on the home stack next and its current top activity isn't
3144 // visible, then use the task return to value to determine the home task to display next.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003145 return mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, reason);
3146 }
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003147 return mService.setFocusedActivityLocked(top, myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003148 }
3149
Craig Mautnerf3333272013-04-22 10:55:53 -07003150 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003151 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3153 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3154 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07003155 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003156 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003157 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3158 "stop-no-history", false)) {
3159 // Activity was finished, no need to continue trying to schedule stop.
3160 adjustFocusedActivityLocked(r, "stopActivityFinished");
3161 r.resumeKeyDispatchingLocked();
3162 return;
3163 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003164 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003165 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003166 + " on stop because we're just sleeping");
3167 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003169 }
3170
3171 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003172 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003173 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 try {
3175 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003176 if (DEBUG_STATES) Slog.v(TAG_STATES,
3177 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003178 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003179 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3180 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003182 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003183 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003184 EventLogTags.writeAmStopActivity(
3185 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003186 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003187 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003188 r.setSleeping(true);
3189 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003190 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003191 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003192 } catch (Exception e) {
3193 // Maybe just ignore exceptions here... if the process
3194 // has crashed, our death notification will clean things
3195 // up.
3196 Slog.w(TAG, "Exception thrown during pause", e);
3197 // Just in case, assume it to be stopped.
3198 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003199 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003201 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003202 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003203 }
3204 }
3205 }
3206 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003207
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003208 /**
3209 * @return Returns true if the activity is being finished, false if for
3210 * some reason it is being left as-is.
3211 */
3212 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003213 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003214 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003215 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3216 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003217 + ", result=" + resultCode + ", data=" + resultData
3218 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003219 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 return false;
3221 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222
Craig Mautnerd44711d2013-02-23 11:24:36 -08003223 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003224 return true;
3225 }
3226
Craig Mautnerd2328952013-03-05 12:46:26 -08003227 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003228 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3229 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3230 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3231 ActivityRecord r = activities.get(activityNdx);
3232 if (r.resultTo == self && r.requestCode == requestCode) {
3233 if ((r.resultWho == null && resultWho == null) ||
3234 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3235 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3236 false);
3237 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003238 }
3239 }
3240 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003241 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003242 }
3243
Adrian Roos20d7df32016-01-12 18:59:43 +01003244 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003245 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003246 TaskRecord finishedTask = null;
3247 if (r == null || r.app != app) {
3248 return null;
3249 }
3250 Slog.w(TAG, " Force finishing activity "
3251 + r.intent.getComponent().flattenToShortString());
3252 int taskNdx = mTaskHistory.indexOf(r.task);
3253 int activityNdx = r.task.mActivities.indexOf(r);
3254 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3255 finishedTask = r.task;
3256 // Also terminate any activities below it that aren't yet
3257 // stopped, to avoid a situation where one will get
3258 // re-start our crashing activity once it gets resumed again.
3259 --activityNdx;
3260 if (activityNdx < 0) {
3261 do {
3262 --taskNdx;
3263 if (taskNdx < 0) {
3264 break;
3265 }
3266 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3267 } while (activityNdx < 0);
3268 }
3269 if (activityNdx >= 0) {
3270 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3271 if (r.state == ActivityState.RESUMED
3272 || r.state == ActivityState.PAUSING
3273 || r.state == ActivityState.PAUSED) {
3274 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3275 Slog.w(TAG, " Force finishing activity "
3276 + r.intent.getComponent().flattenToShortString());
3277 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003278 }
3279 }
3280 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003281 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003282 }
3283
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003284 final void finishVoiceTask(IVoiceInteractionSession session) {
3285 IBinder sessionBinder = session.asBinder();
3286 boolean didOne = false;
3287 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3288 TaskRecord tr = mTaskHistory.get(taskNdx);
3289 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3290 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3291 ActivityRecord r = tr.mActivities.get(activityNdx);
3292 if (!r.finishing) {
3293 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3294 false);
3295 didOne = true;
3296 }
3297 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003298 } else {
3299 // Check if any of the activities are using voice
3300 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3301 ActivityRecord r = tr.mActivities.get(activityNdx);
3302 if (r.voiceSession != null
3303 && r.voiceSession.asBinder() == sessionBinder) {
3304 // Inform of cancellation
3305 r.clearVoiceSessionLocked();
3306 try {
3307 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3308 null);
3309 } catch (RemoteException re) {
3310 // Ok
3311 }
3312 // TODO: VI This is redundant in some cases
3313 mService.finishRunningVoiceLocked();
3314 break;
3315 }
3316 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003317 }
3318 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003319 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3320
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003321 if (didOne) {
3322 mService.updateOomAdjLocked();
3323 }
3324 }
3325
Craig Mautnerd2328952013-03-05 12:46:26 -08003326 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003327 ArrayList<ActivityRecord> activities = r.task.mActivities;
3328 for (int index = activities.indexOf(r); index >= 0; --index) {
3329 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003330 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003331 break;
3332 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003333 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003334 }
3335 return true;
3336 }
3337
Dianne Hackborn5c607432012-02-28 14:44:19 -08003338 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3339 // send the result
3340 ActivityRecord resultTo = r.resultTo;
3341 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003342 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003343 + " who=" + r.resultWho + " req=" + r.requestCode
3344 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003345 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003346 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003347 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003348 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003349 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003350 if (r.info.applicationInfo.uid > 0) {
3351 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3352 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003353 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003354 }
3355 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3356 resultData);
3357 r.resultTo = null;
3358 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003359 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003360
3361 // Make sure this HistoryRecord is not holding on to other resources,
3362 // because clients have remote IPC references to this object so we
3363 // can't assume that will go away and want to avoid circular IPC refs.
3364 r.results = null;
3365 r.pendingResults = null;
3366 r.newIntents = null;
3367 r.icicle = null;
3368 }
3369
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003370 /**
3371 * @return Returns true if this activity has been removed from the history
3372 * list, or false if it is still in the list and will be removed later.
3373 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003374 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3375 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003376 if (r.finishing) {
3377 Slog.w(TAG, "Duplicate finish request for " + r);
3378 return false;
3379 }
3380
Wale Ogunwale7d701172015-03-11 15:36:30 -07003381 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003382 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003383 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003384 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003385 task.taskId, r.shortComponentName, reason);
3386 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003387 final int index = activities.indexOf(r);
3388 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003389 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003390 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003391 // If the caller asked that this activity (and all above it)
3392 // be cleared when the task is reset, don't lose that information,
3393 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003394 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003395 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396 }
3397 }
3398
3399 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003400
Craig Mautner299f9602015-01-26 09:47:33 -08003401 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003402
Dianne Hackborn5c607432012-02-28 14:44:19 -08003403 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003404
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003405 final boolean endTask = index <= 0;
3406 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003407 if (mResumedActivity == r) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003408
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003409 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003410 "Prepare close transition: finishing " + r);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003411 mWindowManager.prepareAppTransition(transit, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003412
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003413 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003414 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003415
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003416 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003417 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3418 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3419 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003420 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003421 }
3422
Craig Mautneraea74a52014-03-08 14:23:10 -08003423 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003424 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003425 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003426 } else if (r.state != ActivityState.PAUSING) {
3427 // If the activity is PAUSING, we will complete the finish once
3428 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003429 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003430 if (r.visible) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003431 mWindowManager.prepareAppTransition(transit, false);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003432 mWindowManager.setAppVisibility(r.appToken, false);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003433 mWindowManager.executeAppTransition();
Chong Zhang66ed4c62015-11-12 14:56:28 -08003434 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003435 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003436 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003437 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003438 }
3439
3440 return false;
3441 }
3442
Craig Mautnerf3333272013-04-22 10:55:53 -07003443 static final int FINISH_IMMEDIATELY = 0;
3444 static final int FINISH_AFTER_PAUSE = 1;
3445 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003446
Craig Mautnerf3333272013-04-22 10:55:53 -07003447 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003448 // First things first: if this activity is currently visible,
3449 // and the resumed activity is not yet visible, then hold off on
3450 // finishing until the resumed one becomes visible.
3451 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003452 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003453 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003454 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003455 if (DEBUG_STATES) Slog.v(TAG_STATES,
3456 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003458 if (oomAdj) {
3459 mService.updateOomAdjLocked();
3460 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003461 return r;
3462 }
3463
3464 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003465 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003466 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003467 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003468 if (mResumedActivity == r) {
3469 mResumedActivity = null;
3470 }
3471 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003472 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473 r.state = ActivityState.FINISHING;
3474
3475 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003476 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003477 || prevState == ActivityState.STOPPED
3478 || prevState == ActivityState.INITIALIZING) {
3479 // If this activity is already stopped, we can just finish
3480 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003481 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003482 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003483 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003484 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003485 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003486 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003487 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3488 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003489 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003490 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003491
3492 // Need to go through the full pause cycle to get this
3493 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003494 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003495 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003496 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003497 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003498 return r;
3499 }
3500
Craig Mautneree36c772014-07-16 14:56:05 -07003501 void finishAllActivitiesLocked(boolean immediately) {
3502 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003503 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3504 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3505 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3506 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003507 noActivitiesInStack = false;
3508 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003509 continue;
3510 }
Craig Mautneree36c772014-07-16 14:56:05 -07003511 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003512 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3513 }
3514 }
Craig Mautneree36c772014-07-16 14:56:05 -07003515 if (noActivitiesInStack) {
3516 mActivityContainer.onTaskListEmptyLocked();
3517 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003518 }
3519
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003520 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3521 // Basic case: for simple app-centric recents, we need to recreate
3522 // the task if the affinity has changed.
3523 if (srec == null || srec.task.affinity == null ||
3524 !srec.task.affinity.equals(destAffinity)) {
3525 return true;
3526 }
3527 // Document-centric case: an app may be split in to multiple documents;
3528 // they need to re-create their task if this current activity is the root
3529 // of a document, unless simply finishing it will return them to the the
3530 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003531 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3532 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003533 // Okay, this activity is at the root of its task. What to do, what to do...
3534 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3535 // Finishing won't return to an application, so we need to recreate.
3536 return true;
3537 }
3538 // We now need to get the task below it to determine what to do.
3539 int taskIdx = mTaskHistory.indexOf(srec.task);
3540 if (taskIdx <= 0) {
3541 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3542 return false;
3543 }
3544 if (taskIdx == 0) {
3545 // At the bottom of the stack, nothing to go back to.
3546 return true;
3547 }
3548 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3549 if (!srec.task.affinity.equals(prevTask.affinity)) {
3550 // These are different apps, so need to recreate.
3551 return true;
3552 }
3553 }
3554 return false;
3555 }
3556
Wale Ogunwale7d701172015-03-11 15:36:30 -07003557 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003558 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003559 final TaskRecord task = srec.task;
3560 final ArrayList<ActivityRecord> activities = task.mActivities;
3561 final int start = activities.indexOf(srec);
3562 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003563 return false;
3564 }
3565 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003566 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003567 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003568 final ComponentName dest = destIntent.getComponent();
3569 if (start > 0 && dest != null) {
3570 for (int i = finishTo; i >= 0; i--) {
3571 ActivityRecord r = activities.get(i);
3572 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003573 r.info.name.equals(dest.getClassName())) {
3574 finishTo = i;
3575 parent = r;
3576 foundParentInTask = true;
3577 break;
3578 }
3579 }
3580 }
3581
3582 IActivityController controller = mService.mController;
3583 if (controller != null) {
3584 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3585 if (next != null) {
3586 // ask watcher if this is allowed
3587 boolean resumeOK = true;
3588 try {
3589 resumeOK = controller.activityResuming(next.packageName);
3590 } catch (RemoteException e) {
3591 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003592 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003593 }
3594
3595 if (!resumeOK) {
3596 return false;
3597 }
3598 }
3599 }
3600 final long origId = Binder.clearCallingIdentity();
3601 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003602 ActivityRecord r = activities.get(i);
3603 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003604 // Only return the supplied result for the first activity finished
3605 resultCode = Activity.RESULT_CANCELED;
3606 resultData = null;
3607 }
3608
3609 if (parent != null && foundParentInTask) {
3610 final int parentLaunchMode = parent.info.launchMode;
3611 final int destIntentFlags = destIntent.getFlags();
3612 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3613 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3614 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3615 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003616 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3617 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003618 } else {
3619 try {
3620 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3621 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003622 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3623 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3624 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003625 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3626 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003627 foundParentInTask = res == ActivityManager.START_SUCCESS;
3628 } catch (RemoteException e) {
3629 foundParentInTask = false;
3630 }
3631 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003632 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003633 }
3634 }
3635 Binder.restoreCallingIdentity(origId);
3636 return foundParentInTask;
3637 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 /**
3639 * Perform the common clean-up of an activity record. This is called both
3640 * as part of destroyActivityLocked() (when destroying the client-side
3641 * representation) and cleaning things up as a result of its hosting
3642 * processing going away, in which case there is no remaining client-side
3643 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003644 *
3645 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003646 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003647 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3648 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003649 if (mResumedActivity == r) {
3650 mResumedActivity = null;
3651 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003652 if (mPausingActivity == r) {
3653 mPausingActivity = null;
3654 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003655 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003656
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003657 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003658 r.frozenBeforeDestroy = false;
3659
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003660 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003661 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003662 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003663 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003664 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003665 }
3666
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003667 // Make sure this record is no longer in the pending finishes list.
3668 // This could happen, for example, if we are trimming activities
3669 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003670 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003671 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003673 // Remove any pending results.
3674 if (r.finishing && r.pendingResults != null) {
3675 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3676 PendingIntentRecord rec = apr.get();
3677 if (rec != null) {
3678 mService.cancelIntentSenderLocked(rec, false);
3679 }
3680 }
3681 r.pendingResults = null;
3682 }
3683
3684 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003685 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003686 }
3687
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003688 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003689 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003690 if (getVisibleBehindActivity() == r) {
3691 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003692 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003693 }
3694
3695 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003696 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003697 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003698 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003699 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003700 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003701 }
3702
Craig Mautner299f9602015-01-26 09:47:33 -08003703 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003704 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003705 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003706 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003707 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3708 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3709
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003710 r.takeFromHistory();
3711 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003712 if (DEBUG_STATES) Slog.v(TAG_STATES,
3713 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003714 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003715 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003716 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003717 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003718 if (VALIDATE_TOKENS) {
3719 validateAppTokensLocked();
3720 }
Craig Mautner312ba862014-02-10 17:55:01 -08003721 final TaskRecord task = r.task;
3722 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003723 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003724 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003725 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003726 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003727 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003728 }
Craig Mautner299f9602015-01-26 09:47:33 -08003729 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003730 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003731 cleanUpActivityServicesLocked(r);
3732 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003734
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003735 /**
3736 * Perform clean-up of service connections in an activity record.
3737 */
3738 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3739 // Throw away any services that have been bound by this activity.
3740 if (r.connections != null) {
3741 Iterator<ConnectionRecord> it = r.connections.iterator();
3742 while (it.hasNext()) {
3743 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003744 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003745 }
3746 r.connections = null;
3747 }
3748 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003749
Craig Mautneree2e45a2014-06-27 12:10:03 -07003750 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003751 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003752 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003753 mHandler.sendMessage(msg);
3754 }
3755
Craig Mautneree2e45a2014-06-27 12:10:03 -07003756 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003757 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003758 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003759 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3760 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3761 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3762 final ActivityRecord r = activities.get(activityNdx);
3763 if (r.finishing) {
3764 continue;
3765 }
3766 if (r.fullscreen) {
3767 lastIsOpaque = true;
3768 }
3769 if (owner != null && r.app != owner) {
3770 continue;
3771 }
3772 if (!lastIsOpaque) {
3773 continue;
3774 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003775 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003776 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003777 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003778 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003779 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003780 activityRemoved = true;
3781 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003782 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003783 }
3784 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003785 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003786 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003787 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003788 }
3789
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003790 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3791 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003792 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3793 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003794 + " pausing=" + mPausingActivity + " for reason " + reason);
3795 return destroyActivityLocked(r, true, reason);
3796 }
3797 return false;
3798 }
3799
3800 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3801 String reason) {
3802 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003803 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003804 int maxTasks = tasks.size() / 4;
3805 if (maxTasks < 1) {
3806 maxTasks = 1;
3807 }
3808 int numReleased = 0;
3809 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3810 final TaskRecord task = mTaskHistory.get(taskNdx);
3811 if (!tasks.contains(task)) {
3812 continue;
3813 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003814 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003815 int curNum = 0;
3816 final ArrayList<ActivityRecord> activities = task.mActivities;
3817 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3818 final ActivityRecord activity = activities.get(actNdx);
3819 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003820 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003821 + " in state " + activity.state + " resumed=" + mResumedActivity
3822 + " pausing=" + mPausingActivity + " for reason " + reason);
3823 destroyActivityLocked(activity, true, reason);
3824 if (activities.get(actNdx) != activity) {
3825 // Was removed from list, back up so we don't miss the next one.
3826 actNdx--;
3827 }
3828 curNum++;
3829 }
3830 }
3831 if (curNum > 0) {
3832 numReleased += curNum;
3833 maxTasks--;
3834 if (mTaskHistory.get(taskNdx) != task) {
3835 // The entire task got removed, back up so we don't miss the next one.
3836 taskNdx--;
3837 }
3838 }
3839 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003840 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3841 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003842 return numReleased;
3843 }
3844
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003845 /**
3846 * Destroy the current CLIENT SIDE instance of an activity. This may be
3847 * called both when actually finishing an activity, or when performing
3848 * a configuration switch where we destroy the current client-side object
3849 * but then create a new client-side object for this same HistoryRecord.
3850 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003851 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003852 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3853 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003854 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003855 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003856 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003857 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003858
3859 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003860
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003861 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003862
3863 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003864
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003865 if (hadApp) {
3866 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003867 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003868 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3869 mService.mHeavyWeightProcess = null;
3870 mService.mHandler.sendEmptyMessage(
3871 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3872 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003873 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003874 // Update any services we are bound to that might care about whether
3875 // their client may have activities.
3876 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003877 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003878 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003879 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880 }
3881 }
3882
3883 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003884
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003885 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003886 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003887 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003888 r.configChangeFlags);
3889 } catch (Exception e) {
3890 // We can just ignore exceptions here... if the process
3891 // has crashed, our death notification will clean things
3892 // up.
3893 //Slog.w(TAG, "Exception thrown during finish", e);
3894 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003895 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003896 removedFromHistory = true;
3897 skipDestroy = true;
3898 }
3899 }
3900
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003901 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003902
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003903 // If the activity is finishing, we need to wait on removing it
3904 // from the list to give it a chance to do its cleanup. During
3905 // that time it may make calls back with its token so we need to
3906 // be able to find it on the list and so we don't want to remove
3907 // it from the list yet. Otherwise, we can just immediately put
3908 // it in the destroyed state since we are not removing it from the
3909 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003910 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003911 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003912 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003914 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003915 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3916 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003917 if (DEBUG_STATES) Slog.v(TAG_STATES,
3918 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003919 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003920 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003921 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003922 }
3923 } else {
3924 // remove this record from the history.
3925 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003926 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003927 removedFromHistory = true;
3928 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003929 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003930 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003931 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003932 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003933 }
3934 }
3935
3936 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003937
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003938 if (!mLRUActivities.remove(r) && hadApp) {
3939 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3940 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003941
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003942 return removedFromHistory;
3943 }
3944
Craig Mautner299f9602015-01-26 09:47:33 -08003945 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003946 final long origId = Binder.clearCallingIdentity();
3947 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003948 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003949 if (r != null) {
3950 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003951 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003952 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003953
Wale Ogunwale60454db2015-01-23 16:05:07 -08003954 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003955 if (r.state == ActivityState.DESTROYING) {
3956 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003957 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003958 }
3959 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003960 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003961 } finally {
3962 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003963 }
3964 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003965
Todd Kennedyaab56db2015-01-30 09:39:53 -08003966 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003967 if (hasVisibleBehindActivity() &&
3968 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08003969 if (r == topRunningActivityLocked()
3970 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003971 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003972 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003973 return;
3974 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003975 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003976 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003977 " thread=" + r.app.thread);
3978 if (r != null && r.app != null && r.app.thread != null) {
3979 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003980 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003981 } catch (RemoteException e) {
3982 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003983 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003984 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003985 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003986 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003987 }
3988 }
3989 }
3990
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003991 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003992 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3993 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003994 if (r != null) {
3995 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003996 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003997 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003998 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003999 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004000 }
4001
Jose Lima4b6c6692014-08-12 17:41:12 -07004002 boolean hasVisibleBehindActivity() {
4003 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004004 }
4005
Jose Lima4b6c6692014-08-12 17:41:12 -07004006 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07004007 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004008 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004009 }
4010 }
4011
Jose Lima4b6c6692014-08-12 17:41:12 -07004012 ActivityRecord getVisibleBehindActivity() {
4013 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004014 }
4015
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004016 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4017 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004018 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004019 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4020 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004021 while (i > 0) {
4022 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004023 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004024 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004025 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004026 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004027 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004028 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004029 }
4030 }
4031 }
4032
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004033 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
4034 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004035 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4036 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004037 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4038 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004039 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004040 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07004041 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4042 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004043
4044 boolean hasVisibleActivities = false;
4045
4046 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004047 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004048 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4049 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004050 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4051 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4052 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4053 final ActivityRecord r = activities.get(activityNdx);
4054 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004055 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4056 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08004057 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004058 if (r.visible) {
4059 hasVisibleActivities = true;
4060 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004061 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004062 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4063 // Don't currently have state for the activity, or
4064 // it is finishing -- always remove it.
4065 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004066 } else if (!r.visible && r.launchCount > 2 &&
4067 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004068 // We have launched this activity too many times since it was
4069 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004070 // (Note if the activity is visible, we don't remove the record.
4071 // We leave the dead window on the screen but the process will
4072 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004073 remove = true;
4074 } else {
4075 // The process may be gone, but the activity lives on!
4076 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004077 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004078 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004079 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4080 "Removing activity " + r + " from stack at " + i
4081 + ": haveState=" + r.haveState
4082 + " stateNotNeeded=" + r.stateNotNeeded
4083 + " finishing=" + r.finishing
4084 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004085 if (!r.finishing) {
4086 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4087 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4088 r.userId, System.identityHashCode(r),
4089 r.task.taskId, r.shortComponentName,
4090 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07004091 if (r.state == ActivityState.RESUMED) {
4092 mService.updateUsageStats(r, false);
4093 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004094 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004095 } else {
4096 // We have the current state for this activity, so
4097 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004098 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4099 if (DEBUG_APP) Slog.v(TAG_APP,
4100 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004101 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004102 // Set nowVisible to previous visible state. If the app was visible while
4103 // it died, we leave the dead window on screen so it's basically visible.
4104 // This is needed when user later tap on the dead window, we need to stop
4105 // other apps when user transfers focus to the restarted activity.
4106 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004107 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004108 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004109 "App died, clearing saved state of " + r);
4110 r.icicle = null;
4111 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004112 }
Craig Mautnerd2328952013-03-05 12:46:26 -08004113 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004114 if (remove) {
4115 removeActivityFromHistoryLocked(r, "appDied");
4116 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004117 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004118 }
4119 }
4120
4121 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004122 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004123
Chong Zhang280d3322015-11-03 17:27:26 -08004124 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004125 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004126 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004127 if (r != null && r.state != ActivityState.RESUMED) {
4128 r.updateOptionsLocked(options);
4129 } else {
4130 ActivityOptions.abort(options);
4131 }
4132 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004133 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004134 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004135
Craig Mautner21d24a22014-04-23 11:45:37 -07004136 void updateTaskMovement(TaskRecord task, boolean toFront) {
4137 if (task.isPersistable) {
4138 task.mLastTimeMoved = System.currentTimeMillis();
4139 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4140 // recently will be most negative, tasks sent to the bottom before that will be less
4141 // negative. Similarly for recent tasks moved to the top which will be most positive.
4142 if (!toFront) {
4143 task.mLastTimeMoved *= -1;
4144 }
4145 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004146 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004147 }
4148
Craig Mautner84984fa2014-06-19 11:19:20 -07004149 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004150 final int top = mTaskHistory.size() - 1;
4151 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4152 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004153 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004154 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4155 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004156 mTaskHistory.remove(taskNdx);
4157 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004158 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004159 return;
4160 }
4161 }
4162 }
4163
Chong Zhang280d3322015-11-03 17:27:26 -08004164 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004165 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004166 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004167
Craig Mautner11bf9a52013-02-19 14:08:51 -08004168 final int numTasks = mTaskHistory.size();
4169 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004170 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004171 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004172 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004173 ActivityOptions.abort(options);
4174 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004175 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004176 }
4177 return;
4178 }
4179
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004180 if (timeTracker != null) {
4181 // The caller wants a time tracker associated with this task.
4182 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4183 tr.mActivities.get(i).appTimeTracker = timeTracker;
4184 }
4185 }
4186
Craig Mautner11bf9a52013-02-19 14:08:51 -08004187 // Shift all activities with this task up to the top
4188 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004189 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004190
Chong Zhang45c25ce2015-08-10 22:18:26 -07004191 // Don't refocus if invisible to current user
4192 ActivityRecord top = tr.getTopActivity();
4193 if (!okToShowLocked(top)) {
4194 addRecentActivityLocked(top);
4195 ActivityOptions.abort(options);
4196 return;
4197 }
4198
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004199 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004200 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004201 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004202
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004203 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004204 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004205 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004206 if (r != null) {
4207 mNoAnimActivities.add(r);
4208 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004209 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004210 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004211 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004212 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004213
Wale Ogunwaled046a012015-12-24 13:05:59 -08004214 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004215 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004216
4217 if (VALIDATE_TOKENS) {
4218 validateAppTokensLocked();
4219 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004220 }
4221
4222 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004223 * Worker method for rearranging history stack. Implements the function of moving all
4224 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004225 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004226 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004227 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4228 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004229 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004230 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004231 * @return Returns true if the move completed, false if not.
4232 */
Craig Mautner299f9602015-01-26 09:47:33 -08004233 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004234 final TaskRecord tr = taskForIdLocked(taskId);
4235 if (tr == null) {
4236 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4237 return false;
4238 }
4239
4240 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004241 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004242
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004243 // If we have a watcher, preflight the move before committing to it. First check
4244 // for *other* available tasks, but if none are available, then try again allowing the
4245 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004246 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004247 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004248 if (next == null) {
4249 next = topRunningActivityLocked(null, 0);
4250 }
4251 if (next != null) {
4252 // ask watcher if this is allowed
4253 boolean moveOK = true;
4254 try {
4255 moveOK = mService.mController.activityResuming(next.packageName);
4256 } catch (RemoteException e) {
4257 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004258 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004259 }
4260 if (!moveOK) {
4261 return false;
4262 }
4263 }
4264 }
4265
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004266 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004267
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004268 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4269 // For the case where we are moving the home task back and there is an activity visible
4270 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4271 // activity to maintain order with what the user is seeing.
4272 final ActivityStack fullscreenStack =
4273 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4274 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4275 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4276 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004277 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004278 return true;
4279 }
4280 }
4281
riddle_hsuc215a4f2014-12-27 12:10:45 +08004282 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004283
4284 // If true, we should resume the home activity next if the task we are moving to the
4285 // back is over the home stack. We force to false if the task we are moving to back
4286 // is the home task and we don't want it resumed after moving to the back.
4287 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4288 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004289 final TaskRecord nextTask = getNextTask(tr);
4290 if (nextTask != null) {
4291 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4292 } else {
4293 prevIsHome = true;
4294 }
4295 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004296 mTaskHistory.remove(tr);
4297 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004298 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299
Craig Mautnerc8143c62013-09-03 12:15:57 -07004300 // There is an assumption that moving a task to the back moves it behind the home activity.
4301 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004302 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004303 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4304 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004305 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004306 break;
4307 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004308 if (taskNdx == 1) {
4309 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004310 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004311 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004312 }
4313
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004314 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004315 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004316
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004317 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004318 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004319 }
4320
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004321 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004322 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004323 if (!mService.mBooting && !mService.mBooted) {
4324 // Not ready yet!
4325 return false;
4326 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004327 final int taskToReturnTo = tr.getTaskToReturnTo();
4328 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004329 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004330 }
4331
Wale Ogunwaled046a012015-12-24 13:05:59 -08004332 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004333 return true;
4334 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004335
Craig Mautner8849a5e2013-04-02 16:41:03 -07004336 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004337 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004338 final Uri data = r.intent.getData();
4339 final String strData = data != null ? data.toSafeString() : null;
4340
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004341 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004342 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004343 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004344 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004345 }
4346
4347 /**
4348 * Make sure the given activity matches the current configuration. Returns
4349 * false if the activity had to be destroyed. Returns true if the
4350 * configuration is the same, or the activity will remain running as-is
4351 * for whatever reason. Ensures the HistoryRecord is updated with the
4352 * correct configuration and all other bookkeeping is handled.
4353 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004354 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4355 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004356 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004357 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004358 "Skipping config check (will change): " + r);
4359 return true;
4360 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004361
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004362 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004363 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004365 // Short circuit: if the two configurations are the exact same
4366 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004367 final Configuration newConfig = mService.mConfiguration;
4368 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004369 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004370 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004371 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004372 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004373 "Configuration unchanged in " + r);
4374 return true;
4375 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004376
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004377 // We don't worry about activities that are finishing.
4378 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004379 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004380 "Configuration doesn't matter in finishing " + r);
4381 r.stopFreezingScreenLocked(false);
4382 return true;
4383 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004384
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004385 // Okay we now are going to make this activity have the new config.
4386 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004387 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004388 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004389 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004390 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004391
Filip Gruszczynski23493322015-07-29 17:02:59 -07004392 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004393 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004394 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004395 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004396 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004397 // There are no significant differences, so we won't relaunch but should still deliver
4398 // the new configuration to the client process.
4399 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004400 return true;
4401 }
4402
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004403 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4404 "Configuration changes for " + r + " ; taskChanges="
4405 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4406 + Configuration.configurationDiffToString(changes));
4407
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004408 // If the activity isn't currently running, just leave the new
4409 // configuration and it will pick that up next time it starts.
4410 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004411 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004412 "Configuration doesn't matter not running " + r);
4413 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004414 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004415 return true;
4416 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004417
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004418 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004419 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4420 "Checking to restart " + r.info.name + ": changed=0x"
4421 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004422 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4423 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004424
Dianne Hackborne6676352011-06-01 16:51:20 -07004425 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004426 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4427 r.configChangeFlags |= changes;
4428 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004429 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004430 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004431 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004432 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004433 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004434 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004435 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004436 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4437 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004438 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004439 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004440 r.deferRelaunchUntilPaused = true;
4441 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004442 return true;
4443 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004444 // Try to optimize this case: the configuration is changing and we need to restart
4445 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004446 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004447 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004448 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004449
4450 if (DEBUG_STATES && !r.visible) {
4451 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4452 + " called by " + Debug.getCallers(4));
4453 }
4454
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004455 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004456 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004457 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004458 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004459 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004460 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004461
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004462 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004463 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004464 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004465
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004466 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004467 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004468 // changes is always sent to all processes when they happen so it can just use whatever
4469 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004470 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004471 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004472
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004473 return true;
4474 }
4475
Filip Gruszczynski23493322015-07-29 17:02:59 -07004476 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4477 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004478
4479 // If we went from full-screen to non-full-screen, make sure to use the correct
4480 // configuration task diff, so the diff stays as small as possible.
4481 if (Configuration.EMPTY.equals(oldTaskOverride)
4482 && !Configuration.EMPTY.equals(taskConfig)) {
4483 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4484 }
4485
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004486 // Conversely, do the same when going the other direction.
4487 if (Configuration.EMPTY.equals(taskConfig)
4488 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4489 taskConfig = record.task.extractOverrideConfig(record.configuration);
4490 }
4491
Filip Gruszczynski23493322015-07-29 17:02:59 -07004492 // Determine what has changed. May be nothing, if this is a config
4493 // that has come back from the app after going idle. In that case
4494 // we just want to leave the official config object now in the
4495 // activity and do nothing else.
4496 int taskChanges = oldTaskOverride.diff(taskConfig);
4497 // We don't want to use size changes if they don't cross boundaries that are important to
4498 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004499 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004500 final boolean crosses = record.crossesHorizontalSizeThreshold(
4501 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4502 || record.crossesVerticalSizeThreshold(
4503 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4504 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004505 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004506 }
4507 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004508 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004509 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4510 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004511 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004512 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004513 }
4514 }
4515 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4516 }
4517
4518 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4519 Configuration oldTaskOverride, int taskChanges) {
4520 if (taskChanges == 0) {
4521 // {@link Configuration#diff} doesn't catch changes from unset values.
4522 // Check for changes we care about.
4523 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004524 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004525 }
4526 // We want to explicitly track situations where the size configuration goes from
4527 // undefined to defined. We don't care about crossing the threshold in that case,
4528 // because there is no threshold.
4529 final int oldHeight = oldTaskOverride.screenHeightDp;
4530 final int newHeight = taskConfig.screenHeightDp;
4531 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4532 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4533 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004534 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004535 }
4536 final int oldWidth = oldTaskOverride.screenWidthDp;
4537 final int newWidth = taskConfig.screenWidthDp;
4538 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4539 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4540 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004541 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004542 }
4543 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4544 final int newSmallest = taskConfig.smallestScreenWidthDp;
4545 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4546 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4547 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004548 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4549 }
4550 final int oldLayout = oldTaskOverride.screenLayout;
4551 final int newLayout = taskConfig.screenLayout;
4552 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4553 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4554 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004555 }
4556 }
4557 return taskChanges;
4558 }
4559
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004560 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004561 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4562 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004563 }
4564
Wale Ogunwale83301a92015-09-24 15:54:08 -07004565 private void relaunchActivityLocked(
4566 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4567 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004568 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004569 return;
4570 }
4571
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004572 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004573 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004574 if (andResume) {
4575 results = r.results;
4576 newIntents = r.newIntents;
4577 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004578 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4579 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004580 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004581 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004582 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004583 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004584
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004585 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004586
Craig Mautner34b73df2014-01-12 21:11:08 -08004587 mStackSupervisor.removeChildActivityContainers(r);
4588
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004589 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004590 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004591 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4592 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004593 r.forceNewConfig = false;
Chong Zhangd78ddb42016-03-02 17:01:14 -08004594 mStackSupervisor.activityRelaunchingLocked(r);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004595 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4596 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004597 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004598 // Note: don't need to call pauseIfSleepingLocked() here, because
4599 // the caller will only pass in 'andResume' if this activity is
4600 // currently resumed, which implies we aren't sleeping.
4601 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004602 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004603 }
4604
4605 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004606 if (DEBUG_STATES) {
4607 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4608 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004609 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004610 // Relaunch-resume could happen either when the app is already in the front,
4611 // or while it's being brought to front. In the latter case, it's marked RESUMED
4612 // but not yet visible (or stopped). We need to complete the resume here as the
4613 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4614 if (!r.visible || r.stopped) {
4615 mWindowManager.setAppVisibility(r.appToken, true);
4616 completeResumeLocked(r);
4617 } else {
4618 r.results = null;
4619 r.newIntents = null;
4620 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004621 } else {
4622 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4623 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004624 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004625
4626 r.configChangeFlags = 0;
Chong Zhang225d1b62016-03-31 14:09:33 -07004627 r.deferRelaunchUntilPaused = false;
4628 r.preserveWindowOnDeferredRelaunch = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004629 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004630
4631 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004632 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4633 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4634 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4635 final ActivityRecord r = activities.get(activityNdx);
4636 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004637 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004638 }
4639 if (r.fullscreen && !r.finishing) {
4640 return false;
4641 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004642 }
4643 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004644 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004645 if (r == null) {
4646 return false;
4647 }
4648 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4649 + " would have returned true for r=" + r);
4650 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004651 }
4652
4653 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004654 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4655 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4656 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4657 final ActivityRecord r = activities.get(activityNdx);
4658 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004659 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004660 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004661 }
4662 }
4663 }
4664
Wale Ogunwale540e1232015-05-01 15:35:39 -07004665 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4666 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004667 boolean didSomething = false;
4668 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004669 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004670 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4671 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4672 int numActivities = activities.size();
4673 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4674 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004675 final boolean sameComponent =
4676 (r.packageName.equals(packageName) && (filterByClasses == null
4677 || filterByClasses.contains(r.realActivity.getClassName())))
4678 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004679 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004680 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004681 && (r.app == null || evenPersistent || !r.app.persistent)) {
4682 if (!doit) {
4683 if (r.finishing) {
4684 // If this activity is just finishing, then it is not
4685 // interesting as far as something to stop.
4686 continue;
4687 }
4688 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004689 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004690 if (r.isHomeActivity()) {
4691 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4692 Slog.i(TAG, "Skip force-stop again " + r);
4693 continue;
4694 } else {
4695 homeActivity = r.realActivity;
4696 }
4697 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004698 didSomething = true;
4699 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004700 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004701 if (r.app != null) {
4702 r.app.removed = true;
4703 }
4704 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004705 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004706 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004707 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4708 true)) {
4709 // r has been deleted from mActivities, accommodate.
4710 --numActivities;
4711 --activityNdx;
4712 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004713 }
4714 }
4715 }
4716 return didSomething;
4717 }
4718
Dianne Hackborn09233282014-04-30 11:33:59 -07004719 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004720 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4721 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004722 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004723 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004724 if (task.getTopActivity() == null) {
4725 continue;
4726 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004727 ActivityRecord r = null;
4728 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004729 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004730 int numActivities = 0;
4731 int numRunning = 0;
4732 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004733 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004734 continue;
4735 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004736 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004737 tmp = activities.get(activityNdx);
4738 if (tmp.finishing) {
4739 continue;
4740 }
4741 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004742
Craig Mautneraab647e2013-02-28 16:31:36 -08004743 // Initialize state for next task if needed.
4744 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4745 top = r;
4746 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004747 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004748
4749 // Add 'r' into the current task.
4750 numActivities++;
4751 if (r.app != null && r.app.thread != null) {
4752 numRunning++;
4753 }
4754
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004755 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004756 TAG, r.intent.getComponent().flattenToShortString()
4757 + ": task=" + r.task);
4758 }
4759
4760 RunningTaskInfo ci = new RunningTaskInfo();
4761 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004762 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004763 ci.baseActivity = r.intent.getComponent();
4764 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004765 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004766 if (focusedStack && topTask) {
4767 // Give the latest time to ensure foreground task can be sorted
4768 // at the first, because lastActiveTime of creating task is 0.
4769 ci.lastActiveTime = System.currentTimeMillis();
4770 topTask = false;
4771 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004772
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004773 if (top.task != null) {
4774 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004775 }
4776 ci.numActivities = numActivities;
4777 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004778 ci.isDockable = task.canGoInDockedStack();
Jorim Jaggi29379ec2016-04-11 23:43:42 -07004779 ci.resizeMode = task.mResizeMode;
Craig Mautneraab647e2013-02-28 16:31:36 -08004780 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004781 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004782 }
4783
4784 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004785 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004786 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004787 if (top >= 0) {
4788 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4789 int activityTop = activities.size() - 1;
4790 if (activityTop > 0) {
4791 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4792 "unhandled-back", true);
4793 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004794 }
4795 }
4796
Craig Mautner6b74cb52013-09-27 17:02:21 -07004797 /**
4798 * Reset local parameters because an app's activity died.
4799 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004800 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004801 */
4802 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004803 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004804 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004805 "App died while pausing: " + mPausingActivity);
4806 mPausingActivity = null;
4807 }
4808 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4809 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004810 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004811 }
4812
Craig Mautner19091252013-10-05 00:03:53 -07004813 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004814 }
4815
Craig Mautnercae015f2013-02-08 14:31:27 -08004816 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004817 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4818 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4819 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4820 final ActivityRecord r = activities.get(activityNdx);
4821 if (r.app == app) {
4822 Slog.w(TAG, " Force finishing activity "
4823 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004824 // Force the destroy to skip right to removal.
4825 r.app = null;
4826 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004827 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004828 }
4829 }
4830 }
4831
Dianne Hackborn390517b2013-05-30 15:03:32 -07004832 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004833 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004834 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004835 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4836 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004837 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4838 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004839 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004840 " Task id #" + task.taskId + "\n" +
4841 " mFullscreen=" + task.mFullscreen + "\n" +
4842 " mBounds=" + task.mBounds + "\n" +
Andrii Kulian771f9e02016-03-28 19:08:58 -07004843 " mMinimalWidth=" + task.mMinimalWidth + "\n" +
4844 " mMinimalHeight=" + task.mMinimalHeight + "\n" +
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004845 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004846 if (printed) {
4847 header = null;
4848 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004849 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004850 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004851 }
4852
4853 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4854 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4855
4856 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004857 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4858 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004859 }
4860 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004861 final int top = mTaskHistory.size() - 1;
4862 if (top >= 0) {
4863 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4864 int listTop = list.size() - 1;
4865 if (listTop >= 0) {
4866 activities.add(list.get(listTop));
4867 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004868 }
4869 } else {
4870 ItemMatcher matcher = new ItemMatcher();
4871 matcher.build(name);
4872
Craig Mautneraab647e2013-02-28 16:31:36 -08004873 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4874 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4875 if (matcher.match(r1, r1.intent.getComponent())) {
4876 activities.add(r1);
4877 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004878 }
4879 }
4880 }
4881
4882 return activities;
4883 }
4884
4885 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004886 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004887
4888 // All activities that came from the package must be
4889 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004890 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4891 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4892 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4893 final ActivityRecord a = activities.get(activityNdx);
4894 if (a.info.packageName.equals(packageName)) {
4895 a.forceNewConfig = true;
4896 if (starting != null && a == starting && a.visible) {
4897 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004898 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004899 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004900 }
4901 }
4902 }
4903
4904 return starting;
4905 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004906
Craig Mautner299f9602015-01-26 09:47:33 -08004907 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004908 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004909 }
4910
Wale Ogunwale000957c2015-04-03 08:19:12 -07004911 /**
4912 * Removes the input task from this stack.
4913 * @param task to remove.
4914 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004915 * @param moving task to another stack. In the case we are moving we don't want to perform
4916 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004917 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004918 void removeTask(TaskRecord task, String reason, boolean moving) {
4919 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004920 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004921 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004922 if (!StackId.persistTaskBounds(mStackId)) {
4923 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4924 // default configuration the next time it launches.
4925 task.updateOverrideConfiguration(null);
4926 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004927 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004928
Craig Mautner04a0ea62014-01-13 12:51:26 -08004929 final ActivityRecord r = mResumedActivity;
4930 if (r != null && r.task == task) {
4931 mResumedActivity = null;
4932 }
4933
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004934 final int taskNdx = mTaskHistory.indexOf(task);
4935 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004936 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4937 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4938 if (!nextTask.isOverHomeStack()) {
4939 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4940 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004941 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004942 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004943 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004944
Wale Ogunwale040b4702015-08-06 18:10:50 -07004945 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004946 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004947 final boolean isVoiceSession = task.voiceSession != null;
4948 if (isVoiceSession) {
4949 try {
4950 task.voiceSession.taskFinished(task.intent, task.taskId);
4951 } catch (RemoteException e) {
4952 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004953 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004954 if (task.autoRemoveFromRecents() || isVoiceSession) {
4955 // Task creator asked to remove this when done, or this task was a voice
4956 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004957 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004958 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004959 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004960 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004961
4962 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004963 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004964 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004965 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004966 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07004967 if (mFullscreen
4968 || !adjustFocusToNextFocusableStackLocked(
4969 task.getTaskToReturnTo(), myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004970 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004971 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004972 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004973 if (mStacks != null) {
4974 mStacks.remove(this);
4975 mStacks.add(0, this);
4976 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004977 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004978 mActivityContainer.onTaskListEmptyLocked();
4979 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004980 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004981
4982 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004983 }
4984
Dianne Hackborn91097de2014-04-04 18:02:06 -07004985 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4986 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4987 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004988 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4989 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004990 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004991 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004992 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Andrii Kulian2e751b82016-03-16 16:59:32 -07004993 if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004994 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004995 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004996 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004997 return task;
4998 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004999
Andrii Kulian2e751b82016-03-16 16:59:32 -07005000 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08005001 if (mTaskPositioner == null) {
5002 return false;
5003 }
Andrii Kulian2e751b82016-03-16 16:59:32 -07005004 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
Wale Ogunwale935e5022015-11-10 12:36:10 -08005005 return true;
5006 }
5007
Craig Mautnerc00204b2013-03-05 15:02:14 -08005008 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005009 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005010 }
5011
Wale Ogunwale5f986092015-12-04 15:35:38 -08005012 void addTask(final TaskRecord task, final boolean toTop, String reason) {
5013 final ActivityStack prevStack = preAddTask(task, reason);
5014
Craig Mautnerc00204b2013-03-05 15:02:14 -08005015 task.stack = this;
5016 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07005017 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005018 } else {
5019 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005020 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005021 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005022 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005023 }
5024
Wale Ogunwale5f986092015-12-04 15:35:38 -08005025 void positionTask(final TaskRecord task, int position) {
5026 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005027 task.stack = this;
5028 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005029 postAddTask(task, prevStack);
5030 }
5031
5032 private ActivityStack preAddTask(TaskRecord task, String reason) {
5033 final ActivityStack prevStack = task.stack;
5034 if (prevStack != null && prevStack != this) {
5035 prevStack.removeTask(task, reason, MOVING);
5036 }
5037 return prevStack;
5038 }
5039
5040 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
5041 if (prevStack != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07005042 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005043 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005044 try {
5045 task.voiceSession.taskStarted(task.intent, task.taskId);
5046 } catch (RemoteException e) {
5047 }
5048 }
5049 }
5050
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005051 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005052 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005053 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07005054 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
5055 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08005056 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
5057 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005058 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005059 r.taskConfigOverride = task.mOverrideConfig;
5060 }
5061
Wale Ogunwaled046a012015-12-24 13:05:59 -08005062 void moveToFrontAndResumeStateIfNeeded(
5063 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
5064 if (!moveToFront) {
5065 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005066 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005067
5068 // If the activity owns the last resumed activity, transfer that together,
5069 // so that we don't resume the same activity again in the new stack.
5070 // Apps may depend on onResume()/onPause() being called in pairs.
5071 if (setResume) {
5072 mResumedActivity = r;
5073 }
5074 // Move the stack in which we are placing the activity to the front. The call will also
5075 // make sure the activity focus is set.
5076 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005077 }
5078
5079 /**
5080 * Moves the input activity from its current stack to this one.
5081 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
5082 * created on this stack which the activity is added to.
5083 * */
5084 void moveActivityToStack(ActivityRecord r) {
5085 final ActivityStack prevStack = r.task.stack;
5086 if (prevStack.mStackId == mStackId) {
5087 // You are already in the right stack silly...
5088 return;
5089 }
5090
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07005091 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07005092 && (mStackSupervisor.topRunningActivityLocked() == r);
5093 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
5094
5095 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08005096 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
5097 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005098 r.setTask(task, null);
5099 task.addActivityToTop(r);
5100 setAppTask(r, task);
Andrii Kulian933076d2016-03-29 17:04:42 -07005101 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005102 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
5103 if (wasResumed) {
5104 prevStack.mResumedActivity = null;
5105 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07005106 }
5107
Wale Ogunwale706ed792015-08-02 10:29:44 -07005108 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005109 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005110 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
5111 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005112 r.taskConfigOverride = task.mOverrideConfig;
5113 }
5114
Craig Mautnerc00204b2013-03-05 15:02:14 -08005115 public int getStackId() {
5116 return mStackId;
5117 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005118
5119 @Override
5120 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005121 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5122 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005123 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005124
Craig Mautner15df08a2015-04-01 12:17:18 -07005125 void onLockTaskPackagesUpdatedLocked() {
5126 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5127 mTaskHistory.get(taskNdx).setLockTaskAuth();
5128 }
5129 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005130}