blob: 7486c721603f6727c0ede6198ef230e7178348be [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;
Jorim Jaggi023da532016-04-20 22:42:32 -0700119import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -0700121import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700122
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700123import com.android.internal.app.IVoiceInteractor;
124import com.android.internal.content.ReferrerIntent;
125import com.android.internal.os.BatteryStatsImpl;
126import com.android.server.Watchdog;
127import com.android.server.am.ActivityManagerService.ItemMatcher;
128import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
129import com.android.server.wm.TaskGroup;
130import com.android.server.wm.WindowManagerService;
131
Craig Mautnercae015f2013-02-08 14:31:27 -0800132import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800133import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134import java.lang.ref.WeakReference;
135import java.util.ArrayList;
136import java.util.Iterator;
137import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800138import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700139import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140
141/**
142 * State and management of a single stack of activities.
143 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700144final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800145
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800146 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700147 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
148 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800149 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700150 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700152 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700154 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700155 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
156 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700157 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
160 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
161 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
162 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
163 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700164
165 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800166
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700167 // Ticks during which we check progress while waiting for an app to launch.
168 static final int LAUNCH_TICK = 500;
169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 // How long we wait until giving up on the last activity to pause. This
171 // is short because it directly impacts the responsiveness of starting the
172 // next activity.
173 static final int PAUSE_TIMEOUT = 500;
174
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700175 // How long we wait for the activity to tell us it has stopped before
176 // giving up. This is a good amount of time because we really need this
177 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700178 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180 // How long we wait until giving up on an activity telling us it has
181 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700182 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800185 // disabled.
186 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800187
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700188 // How long between activity launches that we consider safe to not warn
189 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700190 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 // Set to false to disable the preview that is shown while a new activity
193 // is being started.
194 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800195
Craig Mautner5eda9b32013-07-02 11:58:16 -0700196 // How long to wait for all background Activities to redraw following a call to
197 // convertToTranslucent().
198 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
199
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800200 // How many activities have to be scheduled to stop to force a stop pass.
201 private static final int MAX_STOPPING_TO_FORCE = 3;
202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 enum ActivityState {
204 INITIALIZING,
205 RESUMED,
206 PAUSING,
207 PAUSED,
208 STOPPING,
209 STOPPED,
210 FINISHING,
211 DESTROYING,
212 DESTROYED
213 }
214
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800215 // Stack is not considered visible.
216 static final int STACK_INVISIBLE = 0;
217 // Stack is considered visible
218 static final int STACK_VISIBLE = 1;
219 // Stack is considered visible, but only becuase it has activity that is visible behind other
220 // activities and there is a specific combination of stacks.
221 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
222
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700223 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700224 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800225 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800226
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700227 /**
228 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800229 * running) activities. It contains #TaskRecord objects.
230 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800231 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800232
233 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800234 * Used for validating app tokens with window manager.
235 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800236 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800237
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700238 /**
239 * List of running activities, sorted by recent usage.
240 * The first entry in the list is the least recently used.
241 * It contains HistoryRecord objects.
242 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800243 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700244
245 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700246 * Animations that for the current transition have requested not to
247 * be considered for the transition animation.
248 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800249 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700250
251 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700252 * When we are in the process of pausing an activity, before starting the
253 * next one, this variable holds the activity that is currently being paused.
254 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800255 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700256
257 /**
258 * This is the last activity that we put into the paused state. This is
259 * used to determine if we need to do an activity transition while sleeping,
260 * when we normally hold the top activity paused.
261 */
262 ActivityRecord mLastPausedActivity = null;
263
264 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700265 * Activities that specify No History must be removed once the user navigates away from them.
266 * If the device goes to sleep with such an activity in the paused state then we save it here
267 * and finish it later if another activity replaces it on wakeup.
268 */
269 ActivityRecord mLastNoHistoryActivity = null;
270
271 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700272 * Current activity that is resumed, or null if there is none.
273 */
274 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800275
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700277 * This is the last activity that has been started. It is only used to
278 * identify when multiple activities are started at once so that the user
279 * can be warned they may not be in the activity they think they are.
280 */
281 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800282
Craig Mautner5eda9b32013-07-02 11:58:16 -0700283 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
284 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
285 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
286 // Activity in mTranslucentActivityWaiting is notified via
287 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
288 // background activity being drawn then the same call will be made with a true value.
289 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700290 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700291
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700292 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700293 * Set when we know we are going to be calling updateConfiguration()
294 * soon, so want to skip intermediate config checks.
295 */
296 boolean mConfigWillChange;
297
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700298 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800299 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700300 // Current bounds of the stack or null if fullscreen.
301 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800302
Jorim Jaggi192086e2016-03-11 17:17:03 +0100303 boolean mUpdateBoundsDeferred;
304 boolean mUpdateBoundsDeferredCalled;
305 final Rect mDeferredBounds = new Rect();
306 final Rect mDeferredTaskBounds = new Rect();
307 final Rect mDeferredTaskInsetBounds = new Rect();
308
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700309 long mLaunchStartTime = 0;
310 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800311
Craig Mautner858d8a62013-04-23 17:08:34 -0700312 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700313
Craig Mautnerc00204b2013-03-05 15:02:14 -0800314 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800316 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
317 ArrayList<ActivityStack> mStacks;
318 /** The attached Display's unique identifier, or -1 if detached */
319 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320
Craig Mautner27084302013-03-25 08:05:25 -0700321 /** Run all ActivityStacks through this */
322 final ActivityStackSupervisor mStackSupervisor;
323
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700324 private final LaunchingTaskPositioner mTaskPositioner;
325
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700326 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700327 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
328 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
329 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
330 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700331 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700332 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700333 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700334
335 static class ScheduleDestroyArgs {
336 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700337 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700338 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700339 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700340 mReason = reason;
341 }
342 }
343
Zoran Marcetaf958b322012-08-09 20:27:12 +0900344 final Handler mHandler;
345
346 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800347
Craig Mautnerc8143c62013-09-03 12:15:57 -0700348 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900349 super(looper);
350 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700351
Zoran Marcetaf958b322012-08-09 20:27:12 +0900352 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700353 public void handleMessage(Message msg) {
354 switch (msg.what) {
355 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800356 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700357 // We don't at this point know if the activity is fullscreen,
358 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800359 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700360 synchronized (mService) {
361 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700362 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700363 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700364 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700367 case LAUNCH_TICK_MSG: {
368 ActivityRecord r = (ActivityRecord)msg.obj;
369 synchronized (mService) {
370 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700371 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700372 }
373 }
374 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700375 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800376 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700377 // We don't at this point know if the activity is fullscreen,
378 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800379 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800380 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800381 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800382 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700383 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700384 case STOP_TIMEOUT_MSG: {
385 ActivityRecord r = (ActivityRecord)msg.obj;
386 // We don't at this point know if the activity is fullscreen,
387 // so we need to be conservative and assume it isn't.
388 Slog.w(TAG, "Activity stop timeout for " + r);
389 synchronized (mService) {
390 if (r.isInHistory()) {
391 activityStoppedLocked(r, null, null, null);
392 }
393 }
394 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700395 case DESTROY_ACTIVITIES_MSG: {
396 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
397 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700398 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700399 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700400 } break;
401 case TRANSLUCENT_TIMEOUT_MSG: {
402 synchronized (mService) {
403 notifyActivityDrawnLocked(null);
404 }
405 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700406 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700407 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700408 final ActivityRecord r = getVisibleBehindActivity();
409 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700410 if (r != null) {
411 mService.killAppAtUsersRequest(r.app, null);
412 }
413 }
414 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700415 }
416 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800417 }
418
Craig Mautner34b73df2014-01-12 21:11:08 -0800419 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800420 int count = 0;
421 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
422 count += mTaskHistory.get(taskNdx).mActivities.size();
423 }
424 return count;
425 }
426
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800427 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
428 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800429 mActivityContainer = activityContainer;
430 mStackSupervisor = activityContainer.getOuter();
431 mService = mStackSupervisor.mService;
432 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
433 mWindowManager = mService.mWindowManager;
434 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700435 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800436 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700437 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
438 ? new LaunchingTaskPositioner() : null;
439 }
440
441 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
442 mDisplayId = activityDisplay.mDisplayId;
443 mStacks = activityDisplay.mStacks;
444 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
445 mFullscreen = mBounds == null;
446 if (mTaskPositioner != null) {
447 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
448 mTaskPositioner.configure(mBounds);
449 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800450
451 if (mStackId == DOCKED_STACK_ID) {
452 // If we created a docked stack we want to resize it so it resizes all other stacks
453 // in the system.
454 mStackSupervisor.resizeDockedStackLocked(
455 mBounds, null, null, null, null, PRESERVE_WINDOWS);
456 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700457 }
458
459 void detachDisplay() {
460 mDisplayId = Display.INVALID_DISPLAY;
461 mStacks = null;
462 if (mTaskPositioner != null) {
463 mTaskPositioner.reset();
464 }
465 mWindowManager.detachStack(mStackId);
Jorim Jaggi899327f2016-02-25 20:44:18 -0500466 if (mStackId == DOCKED_STACK_ID) {
467 // If we removed a docked stack we want to resize it so it resizes all other stacks
468 // in the system to fullscreen.
469 mStackSupervisor.resizeDockedStackLocked(
470 null, null, null, null, null, PRESERVE_WINDOWS);
471 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700472 }
473
Winsonc809cbb2015-11-02 12:06:15 -0800474 public void getDisplaySize(Point out) {
475 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
476 }
477
Jorim Jaggi192086e2016-03-11 17:17:03 +0100478 /**
479 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
480 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
481 */
482 void deferUpdateBounds() {
483 if (!mUpdateBoundsDeferred) {
484 mUpdateBoundsDeferred = true;
485 mUpdateBoundsDeferredCalled = false;
486 }
487 }
488
489 /**
490 * Continues updating bounds after updates have been deferred. If there was a resize attempt
491 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
492 * be resized to that bounds.
493 */
494 void continueUpdateBounds() {
495 final boolean wasDeferred = mUpdateBoundsDeferred;
496 mUpdateBoundsDeferred = false;
497 if (wasDeferred && mUpdateBoundsDeferredCalled) {
498 mStackSupervisor.resizeStackUncheckedLocked(this,
499 mDeferredBounds.isEmpty() ? null : mDeferredBounds,
500 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
501 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
502 }
503 }
504
505 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
506 Rect tempTaskInsetBounds) {
507 if (!mUpdateBoundsDeferred) {
508 return true;
509 }
510 if (bounds != null) {
511 mDeferredBounds.set(bounds);
512 } else {
513 mDeferredBounds.setEmpty();
514 }
515 if (tempTaskBounds != null) {
516 mDeferredTaskBounds.set(tempTaskBounds);
517 } else {
518 mDeferredTaskBounds.setEmpty();
519 }
520 if (tempTaskInsetBounds != null) {
521 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
522 } else {
523 mDeferredTaskInsetBounds.setEmpty();
524 }
525 mUpdateBoundsDeferredCalled = true;
526 return false;
527 }
528
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700529 void setBounds(Rect bounds) {
530 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700531 if (mTaskPositioner != null) {
532 mTaskPositioner.configure(bounds);
533 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700534 }
Craig Mautner5962b122012-10-05 14:45:52 -0700535
Amith Yamasani734983f2014-03-04 16:48:05 -0800536 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700537 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700538 }
539
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700540 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800541 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700542 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700543 if (r != null) {
544 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800545 }
546 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700547 return null;
548 }
549
550 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800551 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
552 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800553 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800554 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
555 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800556 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800557 return r;
558 }
559 }
560 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700561 return null;
562 }
563
564 /**
565 * This is a simplified version of topRunningActivityLocked that provides a number of
566 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800567 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700568 * @param token If non-null, any history records matching this token will be skipped.
569 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800570 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700571 * @return Returns the HistoryRecord of the next activity on the stack.
572 */
573 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800574 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
575 TaskRecord task = mTaskHistory.get(taskNdx);
576 if (task.taskId == taskId) {
577 continue;
578 }
579 ArrayList<ActivityRecord> activities = task.mActivities;
580 for (int i = activities.size() - 1; i >= 0; --i) {
581 final ActivityRecord r = activities.get(i);
582 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800583 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800584 return r;
585 }
586 }
587 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700588 return null;
589 }
590
Craig Mautner8849a5e2013-04-02 16:41:03 -0700591 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700592 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
593 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700594 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
595 final ActivityRecord r = activities.get(activityNdx);
596 if (!r.finishing) {
597 return r;
598 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700599 }
600 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700601 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700602 }
603
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700604 final TaskRecord topTask() {
605 final int size = mTaskHistory.size();
606 if (size > 0) {
607 return mTaskHistory.get(size - 1);
608 }
609 return null;
610 }
611
Craig Mautnerd2328952013-03-05 12:46:26 -0800612 TaskRecord taskForIdLocked(int id) {
613 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
614 final TaskRecord task = mTaskHistory.get(taskNdx);
615 if (task.taskId == id) {
616 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800617 }
618 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700619 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620 }
621
Craig Mautnerd2328952013-03-05 12:46:26 -0800622 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700623 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800624 return isInStackLocked(r);
625 }
626
627 ActivityRecord isInStackLocked(ActivityRecord r) {
628 if (r == null) {
629 return null;
630 }
631 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700632 if (task != null && task.stack != null
633 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800634 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800635 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800636 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800637 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800638 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800639 }
640
Craig Mautner2420ead2013-04-01 17:13:20 -0700641 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700642 final boolean hadit = mLRUActivities.remove(r);
643 mLRUActivities.add(r);
644 return hadit;
645 }
646
Craig Mautnerde4ef022013-04-07 19:01:33 -0700647 final boolean isHomeStack() {
648 return mStackId == HOME_STACK_ID;
649 }
650
Winson Chung0583d3d2015-12-18 10:03:12 -0500651 final boolean isDockedStack() {
652 return mStackId == DOCKED_STACK_ID;
653 }
654
655 final boolean isPinnedStack() {
656 return mStackId == PINNED_STACK_ID;
657 }
658
Craig Mautnere0a38842013-12-16 16:14:02 -0800659 final boolean isOnHomeDisplay() {
660 return isAttached() &&
661 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
662 }
663
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700664 void moveToFront(String reason) {
665 moveToFront(reason, null);
666 }
667
668 /**
669 * @param reason The reason for moving the stack to the front.
670 * @param task If non-null, the task will be moved to the top of the stack.
671 * */
672 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700673 if (!isAttached()) {
674 return;
675 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700676
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700677 mStacks.remove(this);
678 int addIndex = mStacks.size();
679
680 if (addIndex > 0) {
681 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800682 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
683 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700684 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800685 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700686 }
687
688 mStacks.add(addIndex, this);
689
690 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
691 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800692 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700693 }
694 if (task != null) {
695 insertTaskAtTop(task, null);
696 } else {
697 task = topTask();
698 }
699 if (task != null) {
700 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800701 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800702 }
703
Wale Ogunwaled046a012015-12-24 13:05:59 -0800704 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800705 if (StackId.canReceiveKeys(mStackId)) {
706 return true;
707 }
708 // The stack isn't focusable. See if its top activity is focusable to force focus on the
709 // stack.
710 final ActivityRecord r = topRunningActivityLocked();
711 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800712 }
713
Craig Mautnere0a38842013-12-16 16:14:02 -0800714 final boolean isAttached() {
715 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800716 }
717
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700718 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800719 * Returns the top activity in any existing task matching the given Intent in the input result.
720 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700721 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800722 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700723 Intent intent = target.intent;
724 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 ComponentName cls = intent.getComponent();
726 if (info.targetActivity != null) {
727 cls = new ComponentName(info.packageName, info.targetActivity);
728 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700729 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700730 boolean isDocument = intent != null & intent.isDocument();
731 // If documentData is non-null then it must match the existing task data.
732 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800733
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700734 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800735 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
736 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700737 if (task.voiceSession != null) {
738 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700739 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700740 continue;
741 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700742 if (task.userId != userId) {
743 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700744 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700745 continue;
746 }
Craig Mautner000f0022013-02-26 15:04:29 -0800747 final ActivityRecord r = task.getTopActivity();
748 if (r == null || r.finishing || r.userId != userId ||
749 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700750 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800751 continue;
752 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700753 if (r.mActivityType != target.mActivityType) {
754 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
755 continue;
756 }
Craig Mautner000f0022013-02-26 15:04:29 -0800757
Craig Mautnerd00f4742014-03-12 14:17:26 -0700758 final Intent taskIntent = task.intent;
759 final Intent affinityIntent = task.affinityIntent;
760 final boolean taskIsDocument;
761 final Uri taskDocumentData;
762 if (taskIntent != null && taskIntent.isDocument()) {
763 taskIsDocument = true;
764 taskDocumentData = taskIntent.getData();
765 } else if (affinityIntent != null && affinityIntent.isDocument()) {
766 taskIsDocument = true;
767 taskDocumentData = affinityIntent.getData();
768 } else {
769 taskIsDocument = false;
770 taskDocumentData = null;
771 }
772
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700773 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700774 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700775 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700776 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800777 if (!isDocument && !taskIsDocument
778 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700779 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800780 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
781 // It is possible for multiple tasks to have the same root affinity especially
782 // if they are in separate stacks. We save off this candidate, but keep looking
783 // to see if there is a better candidate.
784 result.r = r;
785 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700786 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700787 } else if (taskIntent != null && taskIntent.getComponent() != null &&
788 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700789 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700790 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800791 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700792 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
793 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800794 result.r = r;
795 result.matchedByRootAffinity = false;
796 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700797 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
798 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700799 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700800 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800801 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700802 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
803 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800804 result.r = r;
805 result.matchedByRootAffinity = false;
806 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700807 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700809 }
810
811 /**
812 * Returns the first activity (starting from the top of the stack) that
813 * is the same as the given activity. Returns null if no such activity
814 * is found.
815 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700816 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 ComponentName cls = intent.getComponent();
818 if (info.targetActivity != null) {
819 cls = new ComponentName(info.packageName, info.targetActivity);
820 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700821 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822
Craig Mautner000f0022013-02-26 15:04:29 -0800823 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700824 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700825 final boolean notCurrentUserTask =
826 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700827 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700828
Craig Mautner000f0022013-02-26 15:04:29 -0800829 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
830 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700831 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700832 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700833 }
Craig Mautner000f0022013-02-26 15:04:29 -0800834 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 return r;
836 }
837 }
838 }
839
840 return null;
841 }
842
Amith Yamasani742a6712011-05-04 14:49:28 -0700843 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700844 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700845 */
Craig Mautner93529a42013-10-04 15:03:13 -0700846 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800847 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700848 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800849 }
850 mCurrentUser = userId;
851
852 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800853 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800854 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700855 final TaskRecord task = mTaskHistory.get(i);
856
857 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
858 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700859 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700860 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700861 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700862 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800863 mTaskHistory.remove(i);
864 mTaskHistory.add(task);
865 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800866 // Use same value for i.
867 } else {
868 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800869 }
870 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700871 if (VALIDATE_TOKENS) {
872 validateAppTokensLocked();
873 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700874 }
875
Craig Mautner2420ead2013-04-01 17:13:20 -0700876 void minimalResumeActivityLocked(ActivityRecord r) {
877 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800878 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
879 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700880 mResumedActivity = r;
881 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800882 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700883 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700884 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700885 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700886 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
887 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700888 }
889
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800890 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700891 if (r != null) {
892 mRecentTasks.addLocked(r.task);
893 r.task.touchActiveTime();
894 }
895 }
896
Narayan Kamath7829c812015-06-08 17:39:43 +0100897 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700898 if (mFullyDrawnStartTime != 0) {
899 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
900 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100901 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700902 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
903 }
904
905 private void stopFullyDrawnTraceIfNeeded() {
906 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
907 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
908 mFullyDrawnStartTime = 0;
909 }
910 }
911
Craig Mautnere79d42682013-04-01 19:01:53 -0700912 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700913 if (r.displayStartTime == 0) {
914 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
Chong Zhangafb776d2016-04-23 14:33:55 -0700915 if (r.task != null) {
916 r.task.isLaunching = true;
917 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700918 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100919 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700920 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700921 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700922 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100923 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700924 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700925 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800927
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700928 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700929 // Make sure that there is no activity waiting for this to launch.
930 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
931 r.displayStartTime = r.fullyDrawnStartTime = 0;
Chong Zhangafb776d2016-04-23 14:33:55 -0700932 if (r.task != null) {
933 r.task.isLaunching = false;
934 }
Craig Mautner5c494542013-09-06 11:59:38 -0700935 } else {
936 mStackSupervisor.removeTimeoutsForActivityLocked(r);
937 mStackSupervisor.scheduleIdleTimeoutLocked(r);
938 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700939 }
940
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800941 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800942 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800943 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
944 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
945 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
946 activities.get(activityNdx).setSleeping(false);
947 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800948 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800949 if (mPausingActivity != null) {
950 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
951 activityPausedLocked(mPausingActivity.appToken, true);
952 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800953 }
954
Todd Kennedy39bfee52016-02-24 10:28:21 -0800955 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
956 final String packageName = aInfo.packageName;
957 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
958 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
959 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
960 if (packageName.equals(activities.get(activityNdx).packageName)) {
961 activities.get(activityNdx).info.applicationInfo = aInfo;
962 }
963 }
964 }
965 }
966
Craig Mautner0eea92c2013-05-16 13:35:39 -0700967 /**
968 * @return true if something must be done before going to sleep.
969 */
970 boolean checkReadyForSleepLocked() {
971 if (mResumedActivity != null) {
972 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700973 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
974 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
975 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700976 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700977 return true;
978 }
979 if (mPausingActivity != null) {
980 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700981 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700982 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800983 }
984
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700985 if (hasVisibleBehindActivity()) {
986 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800987 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700988 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800989 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700990 return true;
991 }
992
Craig Mautner0eea92c2013-05-16 13:35:39 -0700993 return false;
994 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800995
Craig Mautner0eea92c2013-05-16 13:35:39 -0700996 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700997 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800998
Craig Mautner0eea92c2013-05-16 13:35:39 -0700999 // Make sure any stopped but visible activities are now sleeping.
1000 // This ensures that the activity's onStop() is called.
1001 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1002 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1003 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1004 final ActivityRecord r = activities.get(activityNdx);
1005 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
1006 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001007 }
1008 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001009 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001010 }
Craig Mautner59c00972012-07-30 12:10:24 -07001011
Winson8b1871d2015-11-20 09:56:20 -08001012 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
1013 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001014 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001015 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001016 return null;
1017 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001018
Winson Chung083baf92014-07-11 10:32:42 -07001019 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -07001020 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001021 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001022 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -07001023 return null;
1024 }
1025
Winson Chung48a10a52014-08-27 14:36:51 -07001026 int w = mService.mThumbnailWidth;
1027 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -08001028 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001029 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001030 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -08001031
1032 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -08001033 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
1034 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -05001035 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -08001036 w = h = -1;
Winson21700932016-03-24 17:26:23 -07001037 scale = mService.mFullscreenThumbnailScale;
Winson8b1871d2015-11-20 09:56:20 -08001038 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001039 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -08001040 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001041 }
Winson Chung376543b2014-05-21 17:43:28 -07001042 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001043 return null;
1044 }
1045
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001046 /**
1047 * Start pausing the currently resumed activity. It is an error to call this if there
1048 * is already an activity being paused or there is no resumed activity.
1049 *
1050 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1051 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1052 * screen turning off).
1053 * @param resuming True if this is being called as part of resuming the top activity, so
1054 * we shouldn't try to instigate a resume here.
1055 * @param dontWait True if the caller does not want to wait for the pause to complete. If
1056 * set to true, we will immediately complete the pause here before returning.
1057 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1058 * it to tell us when it is done.
1059 */
1060 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
1061 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001062 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001063 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1064 + " state=" + mPausingActivity.state);
1065 if (!mService.isSleeping()) {
1066 // Avoid recursion among check for sleep and complete pause during sleeping.
1067 // Because activity will be paused immediately after resume, just let pause
1068 // be completed by the order of activity paused from clients.
1069 completePauseLocked(false);
1070 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001071 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072 ActivityRecord prev = mResumedActivity;
1073 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001074 if (!resuming) {
1075 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001076 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001077 }
1078 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001079 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001080
1081 if (mActivityContainer.mParentActivity == null) {
1082 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001083 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001084 }
1085
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001086 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001087 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001088 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001089 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001091 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1092 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001093 prev.state = ActivityState.PAUSING;
1094 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001095 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001096 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -07001097 if (mService.mHasRecents
1098 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001099 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001100 }
Dianne Hackborncee04b52013-07-03 17:01:28 -07001101 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001102
1103 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001105 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001106 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001107 try {
1108 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001109 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001110 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001111 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001112 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001113 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001114 } catch (Exception e) {
1115 // Ignore exception, if process died other code will cleanup.
1116 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001117 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001118 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001119 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001120 }
1121 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001122 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001124 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001125 }
1126
1127 // If we are not going to sleep, we want to ensure the device is
1128 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001129 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001130 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131 }
1132
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001133 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 // Have the window manager pause its key dispatching until the new
1135 // activity has started. If we're pausing the activity just because
1136 // the screen is being turned off and the UI is sleeping, don't interrupt
1137 // key dispatch; the same activity will pick it up again on wakeup.
1138 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001139 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001140 } else if (DEBUG_PAUSE) {
1141 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001142 }
1143
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001144 if (dontWait) {
1145 // If the caller said they don't want to wait for the pause, then complete
1146 // the pause now.
1147 completePauseLocked(false);
1148 return false;
1149
1150 } else {
1151 // Schedule a pause timeout in case the app doesn't respond.
1152 // We don't give it much time because this directly impacts the
1153 // responsiveness seen by the user.
1154 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1155 msg.obj = prev;
1156 prev.pauseTime = SystemClock.uptimeMillis();
1157 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001158 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001159 return true;
1160 }
1161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001162 } else {
1163 // This activity failed to schedule the
1164 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001165 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001166 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001167 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001168 }
1169 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170 }
1171 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001172
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001173 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001174 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1175 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176
Craig Mautnerd2328952013-03-05 12:46:26 -08001177 final ActivityRecord r = isInStackLocked(token);
1178 if (r != null) {
1179 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1180 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001181 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001182 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001183 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001184 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001185 } else {
1186 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1187 r.userId, System.identityHashCode(r), r.shortComponentName,
1188 mPausingActivity != null
1189 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001190 if (r.state == ActivityState.PAUSING) {
1191 r.state = ActivityState.PAUSED;
1192 if (r.finishing) {
1193 if (DEBUG_PAUSE) Slog.v(TAG,
1194 "Executing finish of failed to pause activity: " + r);
1195 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1196 }
louis_chang047dfd42015-04-08 16:35:55 +08001197 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198 }
1199 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001200 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001201 }
1202
Chong Zhangd78ddb42016-03-02 17:01:14 -08001203 final void activityResumedLocked(IBinder token) {
1204 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1205 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1206 r.icicle = null;
1207 r.haveState = false;
1208 }
1209
Craig Mautnera0026042014-04-23 11:45:37 -07001210 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1211 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001212 if (r.state != ActivityState.STOPPING) {
1213 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1214 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1215 return;
1216 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001217 if (persistentState != null) {
1218 r.persistentState = persistentState;
1219 mService.notifyTaskPersisterLocked(r.task, false);
1220 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001221 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001222 if (icicle != null) {
1223 // If icicle is null, this is happening due to a timeout, so we
1224 // haven't really saved the state.
1225 r.icicle = icicle;
1226 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001227 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001228 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001229 }
1230 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001231 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001232 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1233 r.stopped = true;
1234 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001235
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08001236 mWindowManager.notifyAppStopped(r.appToken, true);
Robert Carre12aece2016-02-02 22:43:27 -08001237
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001238 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001239 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001240 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001241 if (r.finishing) {
1242 r.clearOptionsLocked();
1243 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001244 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001245 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001246 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001247 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001248 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001249 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001250 }
1251 }
1252 }
1253
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001254 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001255 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001256 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001257
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001258 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001259 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001260 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001261 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001262 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001263 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001264 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001265 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1266 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001267 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001268 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1269 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001270 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001271 if (prev.deferRelaunchUntilPaused) {
1272 // Complete the deferred relaunch that was waiting for pause to complete.
1273 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1274 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1275 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001276 } else if (wasStopping) {
1277 // We are also stopping, the stop request must have gone soon after the pause.
1278 // We can't clobber it, because the stop confirmation will not be handled.
1279 // We don't need to schedule another stop, we only need to let it happen.
1280 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001281 } else if ((!prev.visible && !hasVisibleBehindActivity())
1282 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001283 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001284 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001285 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001286 }
1287 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001288 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001289 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001290 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001291 // It is possible the activity was freezing the screen before it was paused.
1292 // In that case go ahead and remove the freeze this activity has on the screen
1293 // since it is no longer visible.
1294 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001295 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001297
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001298 if (resumeNext) {
1299 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1300 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001301 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001302 } else {
1303 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001304 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001305 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001306 // If there are no more activities available to run, do resume anyway to start
1307 // something. Also if the top activity on the stack is not the just paused
1308 // activity, we need to go ahead and resume it to ensure we complete an
1309 // in-flight app switch.
1310 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001311 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001312 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001314
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001315 if (prev != null) {
1316 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317
Craig Mautner525f3d92013-05-07 14:01:50 -07001318 if (prev.app != null && prev.cpuTimeAtResume > 0
1319 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001320 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1321 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001322 if (diff > 0) {
1323 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1324 synchronized (bsi) {
1325 BatteryStatsImpl.Uid.Proc ps =
1326 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001327 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001328 if (ps != null) {
1329 ps.addForegroundTimeLocked(diff);
1330 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001331 }
1332 }
1333 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001334 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001335 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001336
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001337 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001338 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1339 mService.notifyTaskStackChangedLocked();
1340 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1341 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001342
1343 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001344 }
1345
Chong Zhang46b1ac62016-02-18 17:53:57 -08001346 private void addToStopping(ActivityRecord r, boolean immediate) {
1347 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1348 mStackSupervisor.mStoppingActivities.add(r);
1349 }
1350
1351 // If we already have a few activities waiting to stop, then give up
1352 // on things going idle and start clearing them out. Or if r is the
1353 // last of activity of the last task the stack will be empty and must
1354 // be cleared immediately.
1355 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1356 || (r.frontOfTask && mTaskHistory.size() <= 1);
1357
1358 if (immediate || forceIdle) {
1359 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1360 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001361 mStackSupervisor.scheduleIdleLocked();
1362 } else {
1363 mStackSupervisor.checkReadyForSleepLocked();
1364 }
1365 }
1366
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001367 /**
1368 * Once we know that we have asked an application to put an activity in
1369 * the resumed state (either by launching it or explicitly telling it),
1370 * this function updates the rest of our state to match that fact.
1371 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001372 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001373 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001374 next.idle = false;
1375 next.results = null;
1376 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001377 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001378
Chong Zhang85ee6542015-10-02 13:36:38 -07001379 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001380 ProcessRecord app = next.task.mActivities.get(0).app;
1381 if (app != null && app != mService.mHomeProcess) {
1382 mService.mHomeProcess = app;
1383 }
1384 }
1385
Craig Mautner07566322013-09-26 16:42:55 -07001386 if (next.nowVisible) {
1387 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001388 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001389 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390
1391 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001392 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001393
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001394 mStackSupervisor.reportResumedActivityLocked(next);
1395
1396 next.resumeKeyDispatchingLocked();
1397 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398
1399 // Mark the point when the activity is resuming
1400 // TODO: To be more accurate, the mark should be before the onCreate,
1401 // not after the onResume. But for subsequent starts, onResume is fine.
1402 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001403 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 } else {
1405 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1406 }
Winson Chung376543b2014-05-21 17:43:28 -07001407
George Mount6ba042b2014-07-28 11:12:28 -07001408 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001409
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001410 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001411 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001412 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001414 }
1415
Craig Mautner2568c3a2015-03-26 14:22:34 -07001416 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001417 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001418 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1419 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1420 r.mUpdateTaskThumbnailWhenHidden = false;
1421 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001422 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001423 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001424 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001425 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001426 container.setVisible(visible);
1427 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001428 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001429 }
1430
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001431 // Find the first visible activity above the passed activity and if it is translucent return it
1432 // otherwise return null;
1433 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1434 TaskRecord task = r.task;
1435 if (task == null) {
1436 return null;
1437 }
1438
1439 ActivityStack stack = task.stack;
1440 if (stack == null) {
1441 return null;
1442 }
1443
1444 int stackNdx = mStacks.indexOf(stack);
1445
1446 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1447 int taskNdx = tasks.indexOf(task);
1448
1449 ArrayList<ActivityRecord> activities = task.mActivities;
1450 int activityNdx = activities.indexOf(r) + 1;
1451
1452 final int numStacks = mStacks.size();
1453 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001454 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001455 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001456 final int numTasks = tasks.size();
1457 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001458 final TaskRecord currentTask = tasks.get(taskNdx);
1459 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001460 final int numActivities = activities.size();
1461 while (activityNdx < numActivities) {
1462 final ActivityRecord activity = activities.get(activityNdx);
1463 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001464 return historyStack.mFullscreen
1465 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001466 }
1467 ++activityNdx;
1468 }
1469 activityNdx = 0;
1470 ++taskNdx;
1471 }
1472 taskNdx = 0;
1473 ++stackNdx;
1474 }
1475
1476 return null;
1477 }
1478
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001479 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001480 ArrayList<ActivityStack> stacks = mStacks;
1481 final ActivityRecord parent = mActivityContainer.mParentActivity;
1482 if (parent != null) {
1483 stacks = parent.task.stack.mStacks;
1484 }
1485 if (stacks != null) {
1486 for (int i = stacks.size() - 1; i >= 0; --i) {
1487 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001488 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001489 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001490 return stack;
1491 }
1492 }
1493 }
1494 return null;
1495 }
1496
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001497 /** Returns true if the stack contains a fullscreen task. */
1498 private boolean hasFullscreenTask() {
1499 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1500 final TaskRecord task = mTaskHistory.get(i);
1501 if (task.mFullscreen) {
1502 return true;
1503 }
1504 }
1505 return false;
1506 }
1507
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001508 /**
1509 * Returns true if the stack is translucent and can have other contents visible behind it if
1510 * needed. A stack is considered translucent if it don't contain a visible or
1511 * starting (about to be visible) activity that is fullscreen (opaque).
1512 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001513 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001514 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001515 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001516 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1517 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001518 final ArrayList<ActivityRecord> activities = task.mActivities;
1519 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1520 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001521
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001522 if (r.finishing) {
1523 // We don't factor in finishing activities when determining translucency since
1524 // they will be gone soon.
1525 continue;
1526 }
1527
1528 if (!r.visible && r != starting) {
1529 // Also ignore invisible activities that are not the currently starting
1530 // activity (about to be visible).
1531 continue;
1532 }
1533
1534 if (r.fullscreen) {
1535 // Stack isn't translucent if it has at least one fullscreen activity
1536 // that is visible.
1537 return false;
1538 }
1539
1540 if (!isHomeStack() && r.frontOfTask
1541 && task.isOverHomeStack() && stackBehindId != HOME_STACK_ID) {
1542 // Stack isn't translucent if it's top activity should have the home stack
1543 // behind it and the stack currently behind it isn't the home stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001544 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001545 }
1546 }
1547 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001548 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001549 }
1550
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001551 /**
1552 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1553 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001554 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001555 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001556 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001557 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001558 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001559 }
1560
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001561 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001562 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001563 }
1564
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001565 final int stackIndex = mStacks.indexOf(this);
1566
1567 if (stackIndex == mStacks.size() - 1) {
1568 Slog.wtf(TAG,
1569 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001570 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001571 }
1572
Chong Zhang75b37202015-12-04 14:16:36 -08001573 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1574 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001575 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001576 }
1577
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001578 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1579 final int focusedStackId = focusedStack.mStackId;
1580
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001581 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001582 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1583 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001584 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001585 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001586 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001587 }
1588
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001589 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001590 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001591 // task in the focus stack doesn't support any form of resizing but we show it for the
1592 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001593 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001594 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001595 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1596 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001597 }
1598
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001599 // Find the first stack behind focused stack that actually got something visible.
1600 int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1601 while (stackBehindFocusedIndex >= 0 &&
1602 mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1603 stackBehindFocusedIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001604 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001605 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001606 && stackIndex == stackBehindFocusedIndex) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001607 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001608 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001609 }
1610
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001611 final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1612 ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1613
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001614 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001615 && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001616 // Stacks behind the fullscreen stack with a translucent activity are always
1617 // visible so they can act as a backdrop to the translucent activity.
1618 // For example, dialog activities
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001619 if (stackIndex == stackBehindFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001620 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001621 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001622 if (stackBehindFocusedIndex >= 0) {
1623 if ((stackBehindFocusedId == DOCKED_STACK_ID
1624 || stackBehindFocusedId == PINNED_STACK_ID)
1625 && stackIndex == (stackBehindFocusedIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001626 // The stack behind the docked or pinned stack is also visible so we can have a
1627 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001628 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001629 }
1630 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001631 }
1632
Wale Ogunwale3797c222015-10-27 14:21:58 -07001633 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001634 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001635 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001636 }
1637
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001638 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001639 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001640
1641 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1642 continue;
1643 }
1644
Wale Ogunwale3797c222015-10-27 14:21:58 -07001645 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001646 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001647 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001648 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001649
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001650 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001651 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001652 }
1653 }
1654
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001655 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001656 }
1657
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001658 final int rankTaskLayers(int baseLayer) {
1659 int layer = 0;
1660 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1661 final TaskRecord task = mTaskHistory.get(taskNdx);
1662 ActivityRecord r = task.topRunningActivityLocked();
1663 if (r == null || r.finishing || !r.visible) {
1664 task.mLayerRank = -1;
1665 } else {
1666 task.mLayerRank = baseLayer + layer++;
1667 }
1668 }
1669 return layer;
1670 }
1671
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001672 /**
1673 * Make sure that all activities that need to be visible (that is, they
1674 * currently can be seen by the user) actually are.
1675 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001676 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1677 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001678 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001679 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001681 if (top != null) {
1682 checkTranslucentActivityWaiting(top);
1683 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 // If the top activity is not fullscreen, then we need to
1686 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001687 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001688 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001689 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1690 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001691 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001692 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001693 boolean behindTranslucentActivity = false;
1694 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001695 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001696 final TaskRecord task = mTaskHistory.get(taskNdx);
1697 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001698 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1699 final ActivityRecord r = activities.get(activityNdx);
1700 if (r.finishing) {
Chong Zhang22bc8512016-04-07 13:47:18 -07001701 // Normally the screenshot will be taken in makeInvisible(). When an activity
1702 // is finishing, we no longer change its visibility, but we still need to take
1703 // the screenshots if startPausingLocked decided it should be taken.
1704 if (r.mUpdateTaskThumbnailWhenHidden) {
1705 r.updateThumbnailLocked(screenshotActivitiesLocked(r), null);
1706 r.mUpdateTaskThumbnailWhenHidden = false;
1707 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001708 continue;
1709 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001710 final boolean isTop = r == top;
1711 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001712 continue;
1713 }
1714 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001715 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1716 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001717 final boolean activityVisibleBehind =
1718 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1719 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1720 || activityVisibleBehind) && okToShowLocked(r);
1721 if (isVisible) {
1722 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1723 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001724 // First: if this is not the current activity being started, make
1725 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001726 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001727 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001729
1730 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001731 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001732 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001733 if (activityNdx >= activities.size()) {
1734 // Record may be removed if its process needs to restart.
1735 activityNdx = activities.size() - 1;
1736 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001737 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001738 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001739 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001740 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001741 // If this activity is already visible, then there is nothing to do here.
1742 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001743 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001744 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001745 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001746 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001748 // Aggregate current change flags.
1749 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001750 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1751 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001752 if (behindFullscreenActivity && !r.fullscreen) {
1753 behindTranslucentActivity = true;
1754 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001755 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001756 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001757 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1758 + stackInvisible + " behindFullscreenActivity="
1759 + behindFullscreenActivity + " mLaunchTaskBehind="
1760 + r.mLaunchTaskBehind);
1761 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001762 }
1763 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001764 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1765 // The visibility of tasks and the activities they contain in freeform stack are
1766 // determined individually unlike other stacks where the visibility or fullscreen
1767 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001768 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001769 } else if (mStackId == HOME_STACK_ID) {
1770 if (task.isHomeTask()) {
1771 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1772 + " stackInvisible=" + stackInvisible
1773 + " behindFullscreenActivity=" + behindFullscreenActivity);
1774 // No other task in the home stack should be visible behind the home activity.
1775 // Home activities is usually a translucent activity with the wallpaper behind
1776 // them. However, when they don't have the wallpaper behind them, we want to
1777 // show activities in the next application stack behind them vs. another
1778 // task in the home stack like recents.
1779 behindFullscreenActivity = true;
1780 } else if (task.isRecentsTask()
1781 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1782 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1783 "Recents task returning to app: at " + task
1784 + " stackInvisible=" + stackInvisible
1785 + " behindFullscreenActivity=" + behindFullscreenActivity);
1786 // We don't want any other tasks in the home stack visible if the recents
1787 // activity is going to be returning to an application activity type.
1788 // We do this to preserve the visible order the user used to get into the
1789 // recents activity. The recents activity is normally translucent and if it
1790 // doesn't have the wallpaper behind it the next activity in the home stack
1791 // shouldn't be visible when the home stack is brought to the front to display
1792 // the recents activity from an app.
1793 behindFullscreenActivity = true;
1794 }
1795
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001796 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001798
1799 if (mTranslucentActivityWaiting != null &&
1800 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1801 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1802 notifyActivityDrawnLocked(null);
1803 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 }
Craig Mautner58547802013-03-05 08:23:53 -08001805
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001806 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1807 if (mTranslucentActivityWaiting != top) {
1808 mUndrawnActivitiesBelowTopTranslucent.clear();
1809 if (mTranslucentActivityWaiting != null) {
1810 // Call the callback with a timeout indication.
1811 notifyActivityDrawnLocked(null);
1812 mTranslucentActivityWaiting = null;
1813 }
1814 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1815 }
1816 }
1817
1818 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001819 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001820 // We need to make sure the app is running if it's the top, or it is just made visible from
1821 // invisible. If the app is already visible, it must have died while it was visible. In this
1822 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1823 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001824 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001825 // This activity needs to be visible, but isn't even running...
1826 // get it started and resume if no other stack in this stack is resumed.
1827 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1828 if (r != starting) {
1829 r.startFreezingScreenLocked(r.app, configChanges);
1830 }
1831 if (!r.visible || r.mLaunchTaskBehind) {
1832 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1833 setVisible(r, true);
1834 }
1835 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001836 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001837 return true;
1838 }
1839 }
1840 return false;
1841 }
1842
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001843 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001844 if (!r.visible) {
1845 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1846 return;
1847 }
1848 // Now for any activities that aren't visible to the user, make sure they no longer are
1849 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001850 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001851 try {
1852 setVisible(r, false);
1853 switch (r.state) {
1854 case STOPPING:
1855 case STOPPED:
1856 if (r.app != null && r.app.thread != null) {
1857 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1858 "Scheduling invisibility: " + r);
1859 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1860 }
1861 break;
1862
1863 case INITIALIZING:
1864 case RESUMED:
1865 case PAUSING:
1866 case PAUSED:
1867 // This case created for transitioning activities from
1868 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001869 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001870 releaseBackgroundResources(r);
1871 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001872 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001873 }
1874 break;
1875
1876 default:
1877 break;
1878 }
1879 } catch (Exception e) {
1880 // Just skip on any failure; we'll make it visible when it next restarts.
1881 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1882 }
1883 }
1884
1885 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1886 TaskRecord task, ActivityRecord r) {
1887 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001888 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001889 + " stackInvisible=" + stackInvisible
1890 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001891 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001892 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001893 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001894 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1895 + " stackInvisible=" + stackInvisible
1896 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001897 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001898 }
1899 return behindFullscreenActivity;
1900 }
1901
Jorim Jaggie66edb12016-02-05 12:41:17 -08001902 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1903
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001904 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001905 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001906 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1907 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001908 return;
1909 }
1910
1911 // If this activity is paused, tell it to now show its window.
1912 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1913 "Making visible and scheduling visibility: " + r);
1914 try {
1915 if (mTranslucentActivityWaiting != null) {
1916 r.updateOptionsLocked(r.returningOptions);
1917 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001918 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001919 setVisible(r, true);
1920 r.sleeping = false;
1921 r.app.pendingUiClean = true;
1922 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1923 r.stopFreezingScreenLocked(false);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001924
1925 // The activity may be waiting for stop, but that is no longer
1926 // appropriate for it.
1927 mStackSupervisor.mStoppingActivities.remove(r);
1928 mStackSupervisor.mGoingToSleepActivities.remove(r);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001929 } catch (Exception e) {
1930 // Just skip on any failure; we'll make it
1931 // visible when it next restarts.
1932 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001933 }
1934 }
1935
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001936 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001937 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1938 r.stopFreezingScreenLocked(false);
1939 try {
1940 if (r.returningOptions != null) {
1941 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1942 }
1943 } catch(RemoteException e) {
1944 }
1945 return r.state == ActivityState.RESUMED;
1946 }
1947
Todd Kennedyaab56db2015-01-30 09:39:53 -08001948 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001949 mTranslucentActivityWaiting = r;
1950 mUndrawnActivitiesBelowTopTranslucent.clear();
1951 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1952 }
1953
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001954 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1955 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1956 final TaskRecord task = mTaskHistory.get(taskNdx);
1957 final ArrayList<ActivityRecord> activities = task.mActivities;
1958 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1959 final ActivityRecord r = activities.get(activityNdx);
1960 if ( r.appTimeTracker != except) {
1961 r.appTimeTracker = null;
1962 }
1963 }
1964 }
1965 }
1966
Craig Mautner5eda9b32013-07-02 11:58:16 -07001967 /**
1968 * Called as activities below the top translucent activity are redrawn. When the last one is
1969 * redrawn notify the top activity by calling
1970 * {@link Activity#onTranslucentConversionComplete}.
1971 *
1972 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1973 * occurred and the activity will be notified immediately.
1974 */
1975 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001976 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001977 if ((r == null)
1978 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1979 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1980 // The last undrawn activity below the top has just been drawn. If there is an
1981 // opaque activity at the top, notify it that it can become translucent safely now.
1982 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1983 mTranslucentActivityWaiting = null;
1984 mUndrawnActivitiesBelowTopTranslucent.clear();
1985 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1986
Craig Mautner71dd1b62014-02-18 15:48:52 -08001987 if (waitingActivity != null) {
1988 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1989 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1990 try {
1991 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1992 waitingActivity.appToken, r != null);
1993 } catch (RemoteException e) {
1994 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001995 }
1996 }
1997 }
1998 }
1999
Craig Mautnera61bc652013-10-28 15:43:18 -07002000 /** If any activities below the top running one are in the INITIALIZING state and they have a
2001 * starting window displayed then remove that starting window. It is possible that the activity
2002 * in this state will never resumed in which case that starting window will be orphaned. */
2003 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002004 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002005 boolean aboveTop = true;
2006 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2007 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2008 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2009 final ActivityRecord r = activities.get(activityNdx);
2010 if (aboveTop) {
2011 if (r == topActivity) {
2012 aboveTop = false;
2013 }
2014 continue;
2015 }
2016
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002017 if (r.state == ActivityState.INITIALIZING
2018 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002019 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
2020 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002021 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07002022 mWindowManager.removeAppStartingWindow(r.appToken);
2023 }
2024 }
2025 }
2026 }
2027
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002028 /**
2029 * Ensure that the top activity in the stack is resumed.
2030 *
2031 * @param prev The previously resumed activity, for when in the process
2032 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002033 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002034 *
2035 * @return Returns true if something is being resumed, or false if
2036 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002037 *
2038 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2039 * non-focused stack to be resumed.
2040 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2041 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002042 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08002043 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002044 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002045 // Don't even start recursing.
2046 return false;
2047 }
2048
2049 boolean result = false;
2050 try {
2051 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002052 mStackSupervisor.inResumeTopActivity = true;
2053 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
2054 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08002055 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08002056 }
Craig Mautner544efa72014-09-04 16:41:20 -07002057 result = resumeTopActivityInnerLocked(prev, options);
2058 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002059 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002060 }
2061 return result;
2062 }
2063
Chong Zhang280d3322015-11-03 17:27:26 -08002064 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002065 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07002066
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002067 if (!mService.mBooting && !mService.mBooted) {
2068 // Not ready yet!
2069 return false;
2070 }
2071
Craig Mautnerdf88d732014-01-27 09:21:32 -08002072 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002073 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07002074 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002075 // Do not resume this stack if its parent is not resumed.
2076 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
2077 return false;
2078 }
2079
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002080 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002081
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002083 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084
2085 // Remember how we'll process this pause/resume situation, and ensure
2086 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002087 final boolean userLeaving = mStackSupervisor.mUserLeaving;
2088 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089
Craig Mautner84984fa2014-06-19 11:19:20 -07002090 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002091 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002092 // There are no more activities!
2093 final String reason = "noMoreActivities";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07002094 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack()
2095 ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2096 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(returnTaskType, reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002097 // Try to move focus to the next visible stack with a running activity if this
2098 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002099 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2100 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002101 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002102
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002103 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07002104 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002105 if (DEBUG_STATES) Slog.d(TAG_STATES,
2106 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07002107 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08002108 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07002109 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002110 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 }
2112
2113 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002116 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2117 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 // Make sure we have executed any pending transitions, since there
2119 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002120 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002122 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002123 if (DEBUG_STATES) Slog.d(TAG_STATES,
2124 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002125 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 return false;
2127 }
2128
Craig Mautner525f3d92013-05-07 14:01:50 -07002129 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07002130 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002131 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002132 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002133 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002134 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002135 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002136 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002137 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002138 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002139 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002140 } else if (!isOnHomeDisplay()) {
2141 return false;
2142 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002143 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002144 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002145 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2146 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2147 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002148 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002149 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002150 }
2151
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 // If we are sleeping, and there is no resumed activity, and the top
2153 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002154 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002155 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002156 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 // Make sure we have executed any pending transitions, since there
2158 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002159 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002161 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002162 if (DEBUG_STATES) Slog.d(TAG_STATES,
2163 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002164 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 return false;
2166 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002167
2168 // Make sure that the user who owns this activity is started. If not,
2169 // we will just leave it as is because someone should be bringing
2170 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002171 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002172 Slog.w(TAG, "Skipping resume of top activity " + next
2173 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002174 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002175 return false;
2176 }
2177
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002178 // The activity may be waiting for stop, but that is no longer
2179 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002180 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002181 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002182 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002183 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002184
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002185 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002186
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002187 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002188 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002189 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002190 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002191 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002192 return false;
2193 }
2194
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002195 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2196
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002197 // We need to start pausing the current activity so the top one can be resumed...
2198 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002199 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002200 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002201 if (DEBUG_STATES) Slog.d(TAG_STATES,
2202 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002203 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002204 }
2205 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002206 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002207 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002208 // At this point we want to put the upcoming activity's process
2209 // at the top of the LRU list, since we know we will be needing it
2210 // very soon and it would be a waste to let it get killed if it
2211 // happens to be sitting towards the end.
2212 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002213 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002214 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002215 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002216 return true;
2217 }
2218
Christopher Tated3f175c2012-06-14 14:16:54 -07002219 // If the most recent activity was noHistory but was only stopped rather
2220 // than stopped+finished because the device went to sleep, we need to make
2221 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002222 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002223 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002224 if (DEBUG_STATES) Slog.d(TAG_STATES,
2225 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002226 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002227 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002228 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002229 }
2230
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002232 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2233 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002234 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002235 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2236 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 } else {
2238 // The next activity is already visible, so hide the previous
2239 // activity's windows right now so we can show the new one ASAP.
2240 // We only do this if the previous is finishing, which should mean
2241 // it is on top of the one being resumed so hiding it quickly
2242 // is good. Otherwise, we want to do the normal route of allowing
2243 // the resumed activity to be shown so we can decide if the
2244 // previous should actually be hidden depending on whether the
2245 // new one is found to be full-screen or not.
2246 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002247 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002248 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2249 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002250 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 + ", nowVisible=" + next.nowVisible);
2252 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002253 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002254 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002255 + ", waitingVisible="
2256 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2257 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002258 }
2259 }
2260 }
2261
Dianne Hackborne7f97212011-02-24 14:40:20 -08002262 // Launching this app's activity, make sure the app is no longer
2263 // considered stopped.
2264 try {
2265 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002266 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002267 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002268 } catch (IllegalArgumentException e) {
2269 Slog.w(TAG, "Failed trying to unstop package "
2270 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002271 }
2272
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002273 // We are starting up the next activity, so tell the window manager
2274 // that the previous one will be hidden soon. This way it can know
2275 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002276 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 if (prev != null) {
2278 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002279 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002280 "Prepare close transition: prev=" + prev);
2281 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002282 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002283 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002284 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002285 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002286 ? TRANSIT_ACTIVITY_CLOSE
2287 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002288 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002289 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002290 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002291 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2292 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002294 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002295 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002296 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002297 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002298 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002299 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002300 ? TRANSIT_TASK_OPEN_BEHIND
2301 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002302 }
2303 }
Craig Mautner967212c2013-04-13 21:10:58 -07002304 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002305 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002307 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002308 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002310 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002311 }
2312 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002313
2314 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002315 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002316 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2317 if (opts != null) {
2318 resumeAnimOptions = opts.toBundle();
2319 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002320 next.applyOptionsLocked();
2321 } else {
2322 next.clearOptionsLocked();
2323 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002324
Craig Mautnercf910b02013-04-23 11:23:27 -07002325 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002327 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2328 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329
Chong Zhangd127c6d2016-05-02 16:36:41 -07002330 // If the previous activity is translucent, force a visibility update of
2331 // the next activity, so that it's added to WM's opening app list, and
2332 // transition animation can be set up properly.
2333 // For example, pressing Home button with a translucent activity in focus.
2334 // Launcher is already visible in this case. If we don't add it to opening
2335 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2336 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2337 final boolean lastActivityTranslucent = lastStack != null
2338 && (!lastStack.mFullscreen
2339 || (lastStack.mLastPausedActivity != null
2340 && !lastStack.mLastPausedActivity.fullscreen));
2341
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002342 // This activity is now becoming visible.
Chong Zhangd127c6d2016-05-02 16:36:41 -07002343 if (!next.visible || next.stopped || lastActivityTranslucent) {
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002344 mWindowManager.setAppVisibility(next.appToken, true);
2345 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002346
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002347 // schedule launch ticks to collect information about slow apps.
2348 next.startLaunchTickingLocked();
2349
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002350 ActivityRecord lastResumedActivity =
2351 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 ActivityState lastState = next.state;
2353
2354 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002355
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002356 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002357 next.state = ActivityState.RESUMED;
2358 mResumedActivity = next;
2359 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002360 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002361 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002362 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002363 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364
2365 // Have the window manager re-evaluate the orientation of
2366 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002367 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002368 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002369 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002370 mService.mConfiguration,
2371 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2372 if (config != null) {
2373 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002375 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002377
Craig Mautner525f3d92013-05-07 14:01:50 -07002378 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 // The configuration update wasn't able to keep the existing
2380 // instance of the activity, and instead started a new one.
2381 // We should be all done, but let's just make sure our activity
2382 // is still at the top and schedule another run if something
2383 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002384 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002385 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 "Activity config changed during resume: " + next
2387 + ", new next: " + nextNext);
2388 if (nextNext != next) {
2389 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002390 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002392 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002393 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002394 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002395 return true;
2396 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002397 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002398 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 }
Craig Mautner58547802013-03-05 08:23:53 -08002400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 try {
2402 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002403 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 if (a != null) {
2405 final int N = a.size();
2406 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002407 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2408 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002409 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002410 }
2411 }
2412
2413 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002414 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002415 }
2416
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002417 // Well the app will no longer be stopped.
2418 // Clear app token stopped state in window manager if needed.
2419 mWindowManager.notifyAppStopped(next.appToken, false);
2420
Craig Mautner299f9602015-01-26 09:47:33 -08002421 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2422 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002423
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002424 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002425 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002426 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002427 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002428 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002429 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002430 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002431
Craig Mautner0eea92c2013-05-16 13:35:39 -07002432 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002434 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 } catch (Exception e) {
2436 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002437 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002438 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002440 if (lastStack != null) {
2441 lastStack.mResumedActivity = lastResumedActivity;
2442 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002443 Slog.i(TAG, "Restarting because process died: " + next);
2444 if (!next.hasBeenLaunched) {
2445 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002446 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2447 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002448 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002449 next.appToken, next.packageName, next.theme,
2450 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002451 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2452 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002453 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002454 }
George Mount2c92c972014-03-20 09:38:23 -07002455 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002456 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002457 return true;
2458 }
2459
2460 // From this point on, if something goes wrong there is no way
2461 // to recover the activity.
2462 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 completeResumeLocked(next);
2464 } catch (Exception e) {
2465 // If any exception gets thrown, toss away this
2466 // activity and try the next one.
2467 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002468 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002469 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002470 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002471 return true;
2472 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 } else {
2474 // Whoops, need to restart this activity!
2475 if (!next.hasBeenLaunched) {
2476 next.hasBeenLaunched = true;
2477 } else {
2478 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002479 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002480 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002481 mService.compatibilityInfoForPackageLocked(
2482 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002484 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002485 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002486 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002487 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002488 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002489 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002490 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002491 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002492 }
2493
Craig Mautnercf910b02013-04-23 11:23:27 -07002494 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002495 return true;
2496 }
2497
riddle_hsuc215a4f2014-12-27 12:10:45 +08002498 private TaskRecord getNextTask(TaskRecord targetTask) {
2499 final int index = mTaskHistory.indexOf(targetTask);
2500 if (index >= 0) {
2501 final int numTasks = mTaskHistory.size();
2502 for (int i = index + 1; i < numTasks; ++i) {
2503 TaskRecord task = mTaskHistory.get(i);
2504 if (task.userId == targetTask.userId) {
2505 return task;
2506 }
2507 }
2508 }
2509 return null;
2510 }
2511
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002512 private void insertTaskAtPosition(TaskRecord task, int position) {
2513 if (position >= mTaskHistory.size()) {
2514 insertTaskAtTop(task, null);
2515 return;
2516 }
2517 // Calculate maximum possible position for this task.
2518 int maxPosition = mTaskHistory.size();
2519 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002520 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002521 // Put non-current user tasks below current user tasks.
2522 while (maxPosition > 0) {
2523 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2524 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002525 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002526 break;
2527 }
2528 maxPosition--;
2529 }
2530 }
2531 position = Math.min(position, maxPosition);
2532 mTaskHistory.remove(task);
2533 mTaskHistory.add(position, task);
2534 updateTaskMovement(task, true);
2535 }
2536
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002537 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002538 // If the moving task is over home stack, transfer its return type to next task
2539 if (task.isOverHomeStack()) {
2540 final TaskRecord nextTask = getNextTask(task);
2541 if (nextTask != null) {
2542 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2543 }
2544 }
2545
Craig Mautner9c85c202013-10-04 20:11:26 -07002546 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002547 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002548 if (isOnHomeDisplay()) {
2549 ActivityStack lastStack = mStackSupervisor.getLastStack();
2550 final boolean fromHome = lastStack.isHomeStack();
2551 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002552 int returnToType = APPLICATION_ACTIVITY_TYPE;
2553 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2554 returnToType = lastStack.topTask() == null
2555 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2556 }
2557 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002558 }
2559 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002560 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002561 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002562
Craig Mautnerac6f8432013-07-17 13:24:59 -07002563 mTaskHistory.remove(task);
2564 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002565 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002566 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002567 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002568 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002569 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002570 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002571 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002572 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002573 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002574 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002575 break;
2576 }
2577 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002578 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002579 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002580 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002581 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002582 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002583
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002584 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2585 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002586 TaskRecord rTask = r.task;
2587 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002588 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2589 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002590 // Last activity in task had been removed or ActivityManagerService is reusing task.
2591 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002592 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002593 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002594 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002595 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002596 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 if (!newTask) {
2598 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002599 boolean startIt = true;
2600 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2601 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002602 if (task.getTopActivity() == null) {
2603 // All activities in task are finishing.
2604 continue;
2605 }
Craig Mautner70a86932013-02-28 22:37:44 -08002606 if (task == r.task) {
2607 // Here it is! Now, if this is not yet visible to the
2608 // user, then just add it without starting; it will
2609 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002610 if (!startIt) {
2611 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2612 + task, new RuntimeException("here").fillInStackTrace());
2613 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002614 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002615 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002616 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002617 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002618 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002619 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002620 return;
2621 }
2622 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002623 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002624 startIt = false;
2625 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002626 }
2627 }
2628
2629 // Place a new activity at top of stack, so it is next to interact
2630 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002632 // If we are not placing the new activity frontmost, we do not want
2633 // to deliver the onUserLeaving callback to the actual frontmost
2634 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002635 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002636 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002637 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002638 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002639 }
Craig Mautner70a86932013-02-28 22:37:44 -08002640
2641 task = r.task;
2642
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002644 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002645 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002646 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002647 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002648
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002649 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002650 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002651 // We want to show the starting preview window if we are
2652 // switching to a new task, or the next activity's process is
2653 // not currently running.
2654 boolean showStartingIcon = newTask;
2655 ProcessRecord proc = r.app;
2656 if (proc == null) {
2657 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2658 }
2659 if (proc == null || proc.thread == null) {
2660 showStartingIcon = true;
2661 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002662 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002664 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002665 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002667 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002668 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002669 ? r.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002670 ? TRANSIT_TASK_OPEN_BEHIND
2671 : TRANSIT_TASK_OPEN
2672 : TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673 mNoAnimActivities.remove(r);
2674 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002675 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002676 boolean doShow = true;
2677 if (newTask) {
2678 // Even though this activity is starting fresh, we still need
2679 // to reset it to make sure we apply affinities to move any
2680 // existing activities from other tasks in to it.
2681 // If the caller has requested that the target task be
2682 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002683 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002684 resetTaskIfNeededLocked(r, r);
2685 doShow = topRunningNonDelayedActivityLocked(null) == r;
2686 }
Chong Zhang280d3322015-11-03 17:27:26 -08002687 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002688 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2689 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002691 if (r.mLaunchTaskBehind) {
2692 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2693 // tell WindowManager that r is visible even though it is at the back of the stack.
2694 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002695 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002696 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002697 // Figure out if we are transitioning from another activity that is
2698 // "has the same starting icon" as the next one. This allows the
2699 // window manager to keep the previous window it had previously
2700 // created, if it still had one.
2701 ActivityRecord prev = mResumedActivity;
2702 if (prev != null) {
2703 // We don't want to reuse the previous starting preview if:
2704 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002705 if (prev.task != r.task) {
2706 prev = null;
2707 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002708 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002709 else if (prev.nowVisible) {
2710 prev = null;
2711 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002713 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002714 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002715 mService.compatibilityInfoForPackageLocked(
2716 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002717 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002718 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002719 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002720 }
2721 } else {
2722 // If this is the first activity, don't do any fancy animations,
2723 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002724 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002725 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002726 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002727 }
2728 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002729 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731 }
2732
Dianne Hackbornbe707852011-11-11 14:32:10 -08002733 final void validateAppTokensLocked() {
2734 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002735 mValidateAppTokens.ensureCapacity(numActivities());
2736 final int numTasks = mTaskHistory.size();
2737 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2738 TaskRecord task = mTaskHistory.get(taskNdx);
2739 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002740 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002741 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002742 }
Craig Mautner000f0022013-02-26 15:04:29 -08002743 TaskGroup group = new TaskGroup();
2744 group.taskId = task.taskId;
2745 mValidateAppTokens.add(group);
2746 final int numActivities = activities.size();
2747 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2748 final ActivityRecord r = activities.get(activityNdx);
2749 group.tokens.add(r.appToken);
2750 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002751 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002752 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002753 }
2754
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 /**
2756 * Perform a reset of the given task, if needed as part of launching it.
2757 * Returns the new HistoryRecord at the top of the task.
2758 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002759 /**
2760 * Helper method for #resetTaskIfNeededLocked.
2761 * We are inside of the task being reset... we'll either finish this activity, push it out
2762 * for another task, or leave it as-is.
2763 * @param task The task containing the Activity (taskTop) that might be reset.
2764 * @param forceReset
2765 * @return An ActivityOptions that needs to be processed.
2766 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002767 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002768 ActivityOptions topOptions = null;
2769
2770 int replyChainEnd = -1;
2771 boolean canMoveOptions = true;
2772
2773 // We only do this for activities that are not the root of the task (since if we finish
2774 // the root, we may no longer have the task!).
2775 final ArrayList<ActivityRecord> activities = task.mActivities;
2776 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002777 final int rootActivityNdx = task.findEffectiveRootIndex();
2778 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002779 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002780 if (target.frontOfTask)
2781 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002782
2783 final int flags = target.info.flags;
2784 final boolean finishOnTaskLaunch =
2785 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2786 final boolean allowTaskReparenting =
2787 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2788 final boolean clearWhenTaskReset =
2789 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2790
2791 if (!finishOnTaskLaunch
2792 && !clearWhenTaskReset
2793 && target.resultTo != null) {
2794 // If this activity is sending a reply to a previous
2795 // activity, we can't do anything with it now until
2796 // we reach the start of the reply chain.
2797 // XXX note that we are assuming the result is always
2798 // to the previous activity, which is almost always
2799 // the case but we really shouldn't count on.
2800 if (replyChainEnd < 0) {
2801 replyChainEnd = i;
2802 }
2803 } else if (!finishOnTaskLaunch
2804 && !clearWhenTaskReset
2805 && allowTaskReparenting
2806 && target.taskAffinity != null
2807 && !target.taskAffinity.equals(task.affinity)) {
2808 // If this activity has an affinity for another
2809 // task, then we need to move it out of here. We will
2810 // move it as far out of the way as possible, to the
2811 // bottom of the activity stack. This also keeps it
2812 // correctly ordered with any activities we previously
2813 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002814 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002815 final ActivityRecord bottom =
2816 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002817 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002818 if (bottom != null && target.taskAffinity != null
2819 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002820 // If the activity currently at the bottom has the
2821 // same task affinity as the one we are moving,
2822 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002823 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002824 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002825 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002826 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002827 targetTask = createTaskRecord(
2828 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2829 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002830 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002831 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002832 + " out to new task " + target.task);
2833 }
2834
Wale Ogunwale706ed792015-08-02 10:29:44 -07002835 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002836
Craig Mautner525f3d92013-05-07 14:01:50 -07002837 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002838 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2839 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002840 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002841 if (p.finishing) {
2842 continue;
2843 }
2844
Craig Mautnere3a74d52013-02-22 14:14:58 -08002845 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002846 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002847 topOptions = p.takeOptionsLocked();
2848 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002849 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002850 }
2851 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002852 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2853 "Removing activity " + p + " from task=" + task + " adding to task="
2854 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002855 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2856 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002857 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002858 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002859
Wale Ogunwale706ed792015-08-02 10:29:44 -07002860 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002861 }
2862
Wale Ogunwale706ed792015-08-02 10:29:44 -07002863 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002864 if (VALIDATE_TOKENS) {
2865 validateAppTokensLocked();
2866 }
2867
2868 replyChainEnd = -1;
2869 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2870 // If the activity should just be removed -- either
2871 // because it asks for it, or the task should be
2872 // cleared -- then finish it and anything that is
2873 // part of its reply chain.
2874 int end;
2875 if (clearWhenTaskReset) {
2876 // In this case, we want to finish this activity
2877 // and everything above it, so be sneaky and pretend
2878 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002879 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002880 } else if (replyChainEnd < 0) {
2881 end = i;
2882 } else {
2883 end = replyChainEnd;
2884 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002885 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002886 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002887 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002888 if (p.finishing) {
2889 continue;
2890 }
2891 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002892 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002893 topOptions = p.takeOptionsLocked();
2894 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002895 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002896 }
2897 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002898 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002899 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002900 if (finishActivityLocked(
2901 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002902 end--;
2903 srcPos--;
2904 }
2905 }
2906 replyChainEnd = -1;
2907 } else {
2908 // If we were in the middle of a chain, well the
2909 // activity that started it all doesn't want anything
2910 // special, so leave it all as-is.
2911 replyChainEnd = -1;
2912 }
2913 }
2914
2915 return topOptions;
2916 }
2917
2918 /**
2919 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2920 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2921 * @param affinityTask The task we are looking for an affinity to.
2922 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2923 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2924 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2925 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002926 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002927 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002928 int replyChainEnd = -1;
2929 final int taskId = task.taskId;
2930 final String taskAffinity = task.affinity;
2931
2932 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2933 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002934 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2935
2936 // Do not operate on or below the effective root Activity.
2937 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002938 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002939 if (target.frontOfTask)
2940 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002941
2942 final int flags = target.info.flags;
2943 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2944 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2945
2946 if (target.resultTo != null) {
2947 // If this activity is sending a reply to a previous
2948 // activity, we can't do anything with it now until
2949 // we reach the start of the reply chain.
2950 // XXX note that we are assuming the result is always
2951 // to the previous activity, which is almost always
2952 // the case but we really shouldn't count on.
2953 if (replyChainEnd < 0) {
2954 replyChainEnd = i;
2955 }
2956 } else if (topTaskIsHigher
2957 && allowTaskReparenting
2958 && taskAffinity != null
2959 && taskAffinity.equals(target.taskAffinity)) {
2960 // This activity has an affinity for our task. Either remove it if we are
2961 // clearing or move it over to our task. Note that
2962 // we currently punt on the case where we are resetting a
2963 // task that is not at the top but who has activities above
2964 // with an affinity to it... this is really not a normal
2965 // case, and we will need to later pull that task to the front
2966 // and usually at that point we will do the reset and pick
2967 // up those remaining activities. (This only happens if
2968 // someone starts an activity in a new task from an activity
2969 // in a task that is not currently on top.)
2970 if (forceReset || finishOnTaskLaunch) {
2971 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002972 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2973 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002974 for (int srcPos = start; srcPos >= i; --srcPos) {
2975 final ActivityRecord p = activities.get(srcPos);
2976 if (p.finishing) {
2977 continue;
2978 }
Todd Kennedy539db512014-12-15 09:57:55 -08002979 finishActivityLocked(
2980 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002981 }
2982 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002983 if (taskInsertionPoint < 0) {
2984 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002985
Craig Mautner77878772013-03-04 19:46:24 -08002986 }
Craig Mautner77878772013-03-04 19:46:24 -08002987
2988 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002989 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2990 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2991 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002992 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002993 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002994 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002995 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002996
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002997 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2998 "Removing and adding activity " + p + " to stack at " + task
2999 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003000 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3001 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003002 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003003 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003004 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08003005 if (VALIDATE_TOKENS) {
3006 validateAppTokensLocked();
3007 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08003008
3009 // Now we've moved it in to place... but what if this is
3010 // a singleTop activity and we have put it on top of another
3011 // instance of the same activity? Then we drop the instance
3012 // below so it remains singleTop.
3013 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3014 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003015 int targetNdx = taskActivities.indexOf(target);
3016 if (targetNdx > 0) {
3017 ActivityRecord p = taskActivities.get(targetNdx - 1);
3018 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003019 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3020 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003021 }
3022 }
3023 }
3024 }
3025
3026 replyChainEnd = -1;
3027 }
3028 }
Craig Mautner77878772013-03-04 19:46:24 -08003029 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003030 }
3031
Craig Mautner8849a5e2013-04-02 16:41:03 -07003032 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003033 ActivityRecord newActivity) {
3034 boolean forceReset =
3035 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
3036 if (ACTIVITY_INACTIVE_RESET_TIME > 0
3037 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
3038 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
3039 forceReset = true;
3040 }
3041 }
3042
3043 final TaskRecord task = taskTop.task;
3044
3045 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3046 * for remaining tasks. Used for later tasks to reparent to task. */
3047 boolean taskFound = false;
3048
3049 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3050 ActivityOptions topOptions = null;
3051
Craig Mautner77878772013-03-04 19:46:24 -08003052 // Preserve the location for reparenting in the new task.
3053 int reparentInsertionPoint = -1;
3054
Craig Mautnere3a74d52013-02-22 14:14:58 -08003055 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3056 final TaskRecord targetTask = mTaskHistory.get(i);
3057
3058 if (targetTask == task) {
3059 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3060 taskFound = true;
3061 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003062 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3063 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003064 }
3065 }
3066
Craig Mautner70a86932013-02-28 22:37:44 -08003067 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003068 if (taskNdx >= 0) {
3069 do {
3070 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3071 } while (taskTop == null && taskNdx >= 0);
3072 }
Craig Mautner70a86932013-02-28 22:37:44 -08003073
Craig Mautnere3a74d52013-02-22 14:14:58 -08003074 if (topOptions != null) {
3075 // If we got some ActivityOptions from an activity on top that
3076 // was removed from the task, propagate them to the new real top.
3077 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003078 taskTop.updateOptionsLocked(topOptions);
3079 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003080 topOptions.abort();
3081 }
3082 }
3083
3084 return taskTop;
3085 }
3086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3088 String resultWho, int requestCode, int resultCode, Intent data) {
3089
3090 if (callingUid > 0) {
3091 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003092 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003093 }
3094
3095 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3096 + " : who=" + resultWho + " req=" + requestCode
3097 + " res=" + resultCode + " data=" + data);
3098 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3099 try {
3100 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3101 list.add(new ResultInfo(resultWho, requestCode,
3102 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003103 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 return;
3105 } catch (Exception e) {
3106 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3107 }
3108 }
3109
3110 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3111 }
3112
Craig Mautner299f9602015-01-26 09:47:33 -08003113 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003114 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
3115 return;
3116 }
3117
3118 final ActivityRecord next = topRunningActivityLocked();
3119 final String myReason = reason + " adjustFocus";
3120 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003121 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003122 // For freeform, docked, and pinned stacks we always keep the focus within the
3123 // stack as long as there is a running activity in the stack that we can adjust
3124 // focus to.
3125 mService.setFocusedActivityLocked(next, myReason);
3126 return;
3127 } else {
3128 final TaskRecord task = r.task;
3129 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003130 final int taskToReturnTo = task.getTaskToReturnTo();
3131
Wale Ogunwaled045c822015-12-02 09:14:28 -08003132 // For non-fullscreen stack, we want to move the focus to the next visible
3133 // stack to prevent the home screen from moving to the top and obscuring
3134 // other visible stacks.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003135 if (!mFullscreen
3136 && adjustFocusToNextFocusableStackLocked(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003137 return;
3138 }
3139 // Move the home stack to the top if this stack is fullscreen or there is no
3140 // other visible stack.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003141 if (mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003142 // Activity focus was already adjusted. Nothing else to do...
3143 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003144 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003145 }
3146 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003147 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003148
Wale Ogunwaled046a012015-12-24 13:05:59 -08003149 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003150 }
3151
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003152 private boolean adjustFocusToNextFocusableStackLocked(int taskToReturnTo, String reason) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003153 final ActivityStack stack = getNextFocusableStackLocked();
3154 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003155 if (stack == null) {
3156 return false;
3157 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003158
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003159 final ActivityRecord top = stack.topRunningActivityLocked();
3160
3161 if (stack.isHomeStack() && (top == null || !top.visible)) {
3162 // If we will be focusing on the home stack next and its current top activity isn't
3163 // visible, then use the task return to value to determine the home task to display next.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003164 return mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, reason);
3165 }
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003166 return mService.setFocusedActivityLocked(top, myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003167 }
3168
Craig Mautnerf3333272013-04-22 10:55:53 -07003169 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003170 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003171 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3172 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3173 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07003174 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003175 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003176 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3177 "stop-no-history", false)) {
3178 // Activity was finished, no need to continue trying to schedule stop.
3179 adjustFocusedActivityLocked(r, "stopActivityFinished");
3180 r.resumeKeyDispatchingLocked();
3181 return;
3182 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003183 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003184 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003185 + " on stop because we're just sleeping");
3186 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003187 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003188 }
3189
3190 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003191 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003192 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193 try {
3194 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003195 if (DEBUG_STATES) Slog.v(TAG_STATES,
3196 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003197 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003198 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3199 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003201 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003202 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003203 EventLogTags.writeAmStopActivity(
3204 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003205 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003206 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003207 r.setSleeping(true);
3208 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003209 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003210 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 } catch (Exception e) {
3212 // Maybe just ignore exceptions here... if the process
3213 // has crashed, our death notification will clean things
3214 // up.
3215 Slog.w(TAG, "Exception thrown during pause", e);
3216 // Just in case, assume it to be stopped.
3217 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003218 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003219 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003220 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003221 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 }
3223 }
3224 }
3225 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003226
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003227 /**
3228 * @return Returns true if the activity is being finished, false if for
3229 * some reason it is being left as-is.
3230 */
3231 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003232 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003233 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003234 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3235 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003236 + ", result=" + resultCode + ", data=" + resultData
3237 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003238 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003239 return false;
3240 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003241
Craig Mautnerd44711d2013-02-23 11:24:36 -08003242 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003243 return true;
3244 }
3245
Craig Mautnerd2328952013-03-05 12:46:26 -08003246 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003247 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3248 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3249 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3250 ActivityRecord r = activities.get(activityNdx);
3251 if (r.resultTo == self && r.requestCode == requestCode) {
3252 if ((r.resultWho == null && resultWho == null) ||
3253 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3254 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3255 false);
3256 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003257 }
3258 }
3259 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003260 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003261 }
3262
Adrian Roos20d7df32016-01-12 18:59:43 +01003263 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003264 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003265 TaskRecord finishedTask = null;
3266 if (r == null || r.app != app) {
3267 return null;
3268 }
3269 Slog.w(TAG, " Force finishing activity "
3270 + r.intent.getComponent().flattenToShortString());
3271 int taskNdx = mTaskHistory.indexOf(r.task);
3272 int activityNdx = r.task.mActivities.indexOf(r);
3273 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3274 finishedTask = r.task;
3275 // Also terminate any activities below it that aren't yet
3276 // stopped, to avoid a situation where one will get
3277 // re-start our crashing activity once it gets resumed again.
3278 --activityNdx;
3279 if (activityNdx < 0) {
3280 do {
3281 --taskNdx;
3282 if (taskNdx < 0) {
3283 break;
3284 }
3285 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3286 } while (activityNdx < 0);
3287 }
3288 if (activityNdx >= 0) {
3289 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3290 if (r.state == ActivityState.RESUMED
3291 || r.state == ActivityState.PAUSING
3292 || r.state == ActivityState.PAUSED) {
3293 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3294 Slog.w(TAG, " Force finishing activity "
3295 + r.intent.getComponent().flattenToShortString());
3296 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003297 }
3298 }
3299 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003300 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003301 }
3302
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003303 final void finishVoiceTask(IVoiceInteractionSession session) {
3304 IBinder sessionBinder = session.asBinder();
3305 boolean didOne = false;
3306 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3307 TaskRecord tr = mTaskHistory.get(taskNdx);
3308 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3309 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3310 ActivityRecord r = tr.mActivities.get(activityNdx);
3311 if (!r.finishing) {
3312 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3313 false);
3314 didOne = true;
3315 }
3316 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003317 } else {
3318 // Check if any of the activities are using voice
3319 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3320 ActivityRecord r = tr.mActivities.get(activityNdx);
3321 if (r.voiceSession != null
3322 && r.voiceSession.asBinder() == sessionBinder) {
3323 // Inform of cancellation
3324 r.clearVoiceSessionLocked();
3325 try {
3326 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3327 null);
3328 } catch (RemoteException re) {
3329 // Ok
3330 }
3331 // TODO: VI This is redundant in some cases
3332 mService.finishRunningVoiceLocked();
3333 break;
3334 }
3335 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003336 }
3337 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003338 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3339
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003340 if (didOne) {
3341 mService.updateOomAdjLocked();
3342 }
3343 }
3344
Craig Mautnerd2328952013-03-05 12:46:26 -08003345 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003346 ArrayList<ActivityRecord> activities = r.task.mActivities;
3347 for (int index = activities.indexOf(r); index >= 0; --index) {
3348 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003349 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003350 break;
3351 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003352 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003353 }
3354 return true;
3355 }
3356
Dianne Hackborn5c607432012-02-28 14:44:19 -08003357 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3358 // send the result
3359 ActivityRecord resultTo = r.resultTo;
3360 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003361 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003362 + " who=" + r.resultWho + " req=" + r.requestCode
3363 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003364 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003365 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003366 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003367 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003368 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003369 if (r.info.applicationInfo.uid > 0) {
3370 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3371 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003372 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003373 }
3374 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3375 resultData);
3376 r.resultTo = null;
3377 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003378 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003379
3380 // Make sure this HistoryRecord is not holding on to other resources,
3381 // because clients have remote IPC references to this object so we
3382 // can't assume that will go away and want to avoid circular IPC refs.
3383 r.results = null;
3384 r.pendingResults = null;
3385 r.newIntents = null;
3386 r.icicle = null;
3387 }
3388
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003389 /**
3390 * @return Returns true if this activity has been removed from the history
3391 * list, or false if it is still in the list and will be removed later.
3392 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003393 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3394 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003395 if (r.finishing) {
3396 Slog.w(TAG, "Duplicate finish request for " + r);
3397 return false;
3398 }
3399
Wale Ogunwale7d701172015-03-11 15:36:30 -07003400 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003401 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003402 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003403 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003404 task.taskId, r.shortComponentName, reason);
3405 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003406 final int index = activities.indexOf(r);
3407 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003408 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003409 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003410 // If the caller asked that this activity (and all above it)
3411 // be cleared when the task is reset, don't lose that information,
3412 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003413 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003414 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003415 }
3416 }
3417
3418 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003419
Craig Mautner299f9602015-01-26 09:47:33 -08003420 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003421
Dianne Hackborn5c607432012-02-28 14:44:19 -08003422 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003423
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003424 final boolean endTask = index <= 0;
3425 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003426 if (mResumedActivity == r) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003427
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003428 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003429 "Prepare close transition: finishing " + r);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003430 mWindowManager.prepareAppTransition(transit, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003431
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003432 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003433 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003434
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003435 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003436 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3437 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3438 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003439 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003440 }
3441
Craig Mautneraea74a52014-03-08 14:23:10 -08003442 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003443 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003444 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003445 } else if (r.state != ActivityState.PAUSING) {
3446 // If the activity is PAUSING, we will complete the finish once
3447 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003448 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003449 if (r.visible) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003450 mWindowManager.prepareAppTransition(transit, false);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003451 mWindowManager.setAppVisibility(r.appToken, false);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003452 mWindowManager.executeAppTransition();
Chong Zhangb8bd0692016-04-27 17:29:28 -07003453 if (!mStackSupervisor.mWaitingVisibleActivities.contains(r)) {
3454 mStackSupervisor.mWaitingVisibleActivities.add(r);
3455 }
Chong Zhang66ed4c62015-11-12 14:56:28 -08003456 }
Chong Zhang4c5f7ff2016-04-22 11:31:51 -07003457 return finishCurrentActivityLocked(r, (r.visible || r.nowVisible) ?
3458 FINISH_AFTER_VISIBLE : FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003460 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003461 }
3462
3463 return false;
3464 }
3465
Craig Mautnerf3333272013-04-22 10:55:53 -07003466 static final int FINISH_IMMEDIATELY = 0;
3467 static final int FINISH_AFTER_PAUSE = 1;
3468 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003469
Craig Mautnerf3333272013-04-22 10:55:53 -07003470 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003471 // First things first: if this activity is currently visible,
3472 // and the resumed activity is not yet visible, then hold off on
3473 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003474
3475 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
3476
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003477 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003478 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003479 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003480 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003481 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003482 if (DEBUG_STATES) Slog.v(TAG_STATES,
3483 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003484 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003485 if (oomAdj) {
3486 mService.updateOomAdjLocked();
3487 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003488 return r;
3489 }
3490
3491 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003492 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003493 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003494 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003495 if (mResumedActivity == r) {
3496 mResumedActivity = null;
3497 }
3498 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003499 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003500 r.state = ActivityState.FINISHING;
3501
3502 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003503 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003504 || prevState == ActivityState.STOPPED
3505 || prevState == ActivityState.INITIALIZING) {
3506 // If this activity is already stopped, we can just finish
3507 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003508 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003509 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003510 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003511 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003512 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003513 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003514 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3515 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003516 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003517 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003518
3519 // Need to go through the full pause cycle to get this
3520 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003521 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003522 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003523 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003524 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003525 return r;
3526 }
3527
Craig Mautneree36c772014-07-16 14:56:05 -07003528 void finishAllActivitiesLocked(boolean immediately) {
3529 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003530 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3531 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3532 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3533 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003534 noActivitiesInStack = false;
3535 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003536 continue;
3537 }
Craig Mautneree36c772014-07-16 14:56:05 -07003538 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003539 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3540 }
3541 }
Craig Mautneree36c772014-07-16 14:56:05 -07003542 if (noActivitiesInStack) {
3543 mActivityContainer.onTaskListEmptyLocked();
3544 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003545 }
3546
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003547 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3548 // Basic case: for simple app-centric recents, we need to recreate
3549 // the task if the affinity has changed.
3550 if (srec == null || srec.task.affinity == null ||
3551 !srec.task.affinity.equals(destAffinity)) {
3552 return true;
3553 }
3554 // Document-centric case: an app may be split in to multiple documents;
3555 // they need to re-create their task if this current activity is the root
3556 // of a document, unless simply finishing it will return them to the the
3557 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003558 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3559 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003560 // Okay, this activity is at the root of its task. What to do, what to do...
3561 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3562 // Finishing won't return to an application, so we need to recreate.
3563 return true;
3564 }
3565 // We now need to get the task below it to determine what to do.
3566 int taskIdx = mTaskHistory.indexOf(srec.task);
3567 if (taskIdx <= 0) {
3568 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3569 return false;
3570 }
3571 if (taskIdx == 0) {
3572 // At the bottom of the stack, nothing to go back to.
3573 return true;
3574 }
3575 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3576 if (!srec.task.affinity.equals(prevTask.affinity)) {
3577 // These are different apps, so need to recreate.
3578 return true;
3579 }
3580 }
3581 return false;
3582 }
3583
Wale Ogunwale7d701172015-03-11 15:36:30 -07003584 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003585 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003586 final TaskRecord task = srec.task;
3587 final ArrayList<ActivityRecord> activities = task.mActivities;
3588 final int start = activities.indexOf(srec);
3589 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003590 return false;
3591 }
3592 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003593 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003594 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003595 final ComponentName dest = destIntent.getComponent();
3596 if (start > 0 && dest != null) {
3597 for (int i = finishTo; i >= 0; i--) {
3598 ActivityRecord r = activities.get(i);
3599 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003600 r.info.name.equals(dest.getClassName())) {
3601 finishTo = i;
3602 parent = r;
3603 foundParentInTask = true;
3604 break;
3605 }
3606 }
3607 }
3608
3609 IActivityController controller = mService.mController;
3610 if (controller != null) {
3611 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3612 if (next != null) {
3613 // ask watcher if this is allowed
3614 boolean resumeOK = true;
3615 try {
3616 resumeOK = controller.activityResuming(next.packageName);
3617 } catch (RemoteException e) {
3618 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003619 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003620 }
3621
3622 if (!resumeOK) {
3623 return false;
3624 }
3625 }
3626 }
3627 final long origId = Binder.clearCallingIdentity();
3628 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003629 ActivityRecord r = activities.get(i);
3630 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003631 // Only return the supplied result for the first activity finished
3632 resultCode = Activity.RESULT_CANCELED;
3633 resultData = null;
3634 }
3635
3636 if (parent != null && foundParentInTask) {
3637 final int parentLaunchMode = parent.info.launchMode;
3638 final int destIntentFlags = destIntent.getFlags();
3639 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3640 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3641 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3642 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003643 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3644 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003645 } else {
3646 try {
3647 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3648 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003649 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3650 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3651 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003652 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3653 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003654 foundParentInTask = res == ActivityManager.START_SUCCESS;
3655 } catch (RemoteException e) {
3656 foundParentInTask = false;
3657 }
3658 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003659 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003660 }
3661 }
3662 Binder.restoreCallingIdentity(origId);
3663 return foundParentInTask;
3664 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003665 /**
3666 * Perform the common clean-up of an activity record. This is called both
3667 * as part of destroyActivityLocked() (when destroying the client-side
3668 * representation) and cleaning things up as a result of its hosting
3669 * processing going away, in which case there is no remaining client-side
3670 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003671 *
3672 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003673 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003674 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3675 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 if (mResumedActivity == r) {
3677 mResumedActivity = null;
3678 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003679 if (mPausingActivity == r) {
3680 mPausingActivity = null;
3681 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003682 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003683
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003684 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003685 r.frozenBeforeDestroy = false;
3686
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003687 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003688 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003689 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003690 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003691 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003692 }
3693
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003694 // Make sure this record is no longer in the pending finishes list.
3695 // This could happen, for example, if we are trimming activities
3696 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003697 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003698 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003699
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003700 // Remove any pending results.
3701 if (r.finishing && r.pendingResults != null) {
3702 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3703 PendingIntentRecord rec = apr.get();
3704 if (rec != null) {
3705 mService.cancelIntentSenderLocked(rec, false);
3706 }
3707 }
3708 r.pendingResults = null;
3709 }
3710
3711 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003712 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713 }
3714
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003715 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003716 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003717 if (getVisibleBehindActivity() == r) {
3718 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003719 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003720 }
3721
3722 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003723 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003724 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003725 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003726 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003727 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003728 }
3729
Craig Mautner299f9602015-01-26 09:47:33 -08003730 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003731 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003732 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003733 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003734 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3735 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3736
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003737 r.takeFromHistory();
3738 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003739 if (DEBUG_STATES) Slog.v(TAG_STATES,
3740 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003741 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003742 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003743 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003744 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003745 if (VALIDATE_TOKENS) {
3746 validateAppTokensLocked();
3747 }
Craig Mautner312ba862014-02-10 17:55:01 -08003748 final TaskRecord task = r.task;
3749 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003750 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003751 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003752 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003753 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003754 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003755 }
Craig Mautner299f9602015-01-26 09:47:33 -08003756 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003757 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003758 cleanUpActivityServicesLocked(r);
3759 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003760 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003761
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003762 /**
3763 * Perform clean-up of service connections in an activity record.
3764 */
3765 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3766 // Throw away any services that have been bound by this activity.
3767 if (r.connections != null) {
3768 Iterator<ConnectionRecord> it = r.connections.iterator();
3769 while (it.hasNext()) {
3770 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003771 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003772 }
3773 r.connections = null;
3774 }
3775 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003776
Craig Mautneree2e45a2014-06-27 12:10:03 -07003777 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003778 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003779 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003780 mHandler.sendMessage(msg);
3781 }
3782
Craig Mautneree2e45a2014-06-27 12:10:03 -07003783 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003784 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003785 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003786 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3787 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3788 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3789 final ActivityRecord r = activities.get(activityNdx);
3790 if (r.finishing) {
3791 continue;
3792 }
3793 if (r.fullscreen) {
3794 lastIsOpaque = true;
3795 }
3796 if (owner != null && r.app != owner) {
3797 continue;
3798 }
3799 if (!lastIsOpaque) {
3800 continue;
3801 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003802 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003803 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003804 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003805 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003806 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003807 activityRemoved = true;
3808 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003809 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003810 }
3811 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003812 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003813 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003814 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003815 }
3816
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003817 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3818 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003819 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3820 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003821 + " pausing=" + mPausingActivity + " for reason " + reason);
3822 return destroyActivityLocked(r, true, reason);
3823 }
3824 return false;
3825 }
3826
3827 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3828 String reason) {
3829 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003830 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003831 int maxTasks = tasks.size() / 4;
3832 if (maxTasks < 1) {
3833 maxTasks = 1;
3834 }
3835 int numReleased = 0;
3836 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3837 final TaskRecord task = mTaskHistory.get(taskNdx);
3838 if (!tasks.contains(task)) {
3839 continue;
3840 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003841 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003842 int curNum = 0;
3843 final ArrayList<ActivityRecord> activities = task.mActivities;
3844 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3845 final ActivityRecord activity = activities.get(actNdx);
3846 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003847 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003848 + " in state " + activity.state + " resumed=" + mResumedActivity
3849 + " pausing=" + mPausingActivity + " for reason " + reason);
3850 destroyActivityLocked(activity, true, reason);
3851 if (activities.get(actNdx) != activity) {
3852 // Was removed from list, back up so we don't miss the next one.
3853 actNdx--;
3854 }
3855 curNum++;
3856 }
3857 }
3858 if (curNum > 0) {
3859 numReleased += curNum;
3860 maxTasks--;
3861 if (mTaskHistory.get(taskNdx) != task) {
3862 // The entire task got removed, back up so we don't miss the next one.
3863 taskNdx--;
3864 }
3865 }
3866 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003867 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3868 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003869 return numReleased;
3870 }
3871
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003872 /**
3873 * Destroy the current CLIENT SIDE instance of an activity. This may be
3874 * called both when actually finishing an activity, or when performing
3875 * a configuration switch where we destroy the current client-side object
3876 * but then create a new client-side object for this same HistoryRecord.
3877 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003878 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003879 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3880 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003881 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003882 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003883 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003884 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003885
3886 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003887
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003888 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003889
3890 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003891
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003892 if (hadApp) {
3893 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003894 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003895 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3896 mService.mHeavyWeightProcess = null;
3897 mService.mHandler.sendEmptyMessage(
3898 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3899 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003900 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003901 // Update any services we are bound to that might care about whether
3902 // their client may have activities.
3903 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003904 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003905 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003906 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003907 }
3908 }
3909
3910 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003911
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003912 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003913 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003914 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003915 r.configChangeFlags);
3916 } catch (Exception e) {
3917 // We can just ignore exceptions here... if the process
3918 // has crashed, our death notification will clean things
3919 // up.
3920 //Slog.w(TAG, "Exception thrown during finish", e);
3921 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003922 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003923 removedFromHistory = true;
3924 skipDestroy = true;
3925 }
3926 }
3927
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003928 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003929
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003930 // If the activity is finishing, we need to wait on removing it
3931 // from the list to give it a chance to do its cleanup. During
3932 // that time it may make calls back with its token so we need to
3933 // be able to find it on the list and so we don't want to remove
3934 // it from the list yet. Otherwise, we can just immediately put
3935 // it in the destroyed state since we are not removing it from the
3936 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003937 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003938 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003939 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003940 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003941 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003942 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3943 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003944 if (DEBUG_STATES) Slog.v(TAG_STATES,
3945 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003946 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003947 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003948 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003949 }
3950 } else {
3951 // remove this record from the history.
3952 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003953 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003954 removedFromHistory = true;
3955 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003956 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003957 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003958 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003959 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 }
3961 }
3962
3963 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003964
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003965 if (!mLRUActivities.remove(r) && hadApp) {
3966 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3967 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003968
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003969 return removedFromHistory;
3970 }
3971
Craig Mautner299f9602015-01-26 09:47:33 -08003972 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003973 final long origId = Binder.clearCallingIdentity();
3974 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003975 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003976 if (r != null) {
3977 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003978 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003979 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003980
Wale Ogunwale60454db2015-01-23 16:05:07 -08003981 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003982 if (r.state == ActivityState.DESTROYING) {
3983 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003984 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003985 }
3986 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003987 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003988 } finally {
3989 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003990 }
3991 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003992
Todd Kennedyaab56db2015-01-30 09:39:53 -08003993 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003994 if (hasVisibleBehindActivity() &&
3995 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08003996 if (r == topRunningActivityLocked()
3997 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003998 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003999 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07004000 return;
4001 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004002 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07004003 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07004004 " thread=" + r.app.thread);
4005 if (r != null && r.app != null && r.app.thread != null) {
4006 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07004007 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004008 } catch (RemoteException e) {
4009 }
Jose Lima4b6c6692014-08-12 17:41:12 -07004010 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004011 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07004012 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004013 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004014 }
4015 }
4016 }
4017
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004018 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07004019 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
4020 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004021 if (r != null) {
4022 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07004023 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004024 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004025 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004026 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004027 }
4028
Jose Lima4b6c6692014-08-12 17:41:12 -07004029 boolean hasVisibleBehindActivity() {
4030 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004031 }
4032
Jose Lima4b6c6692014-08-12 17:41:12 -07004033 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07004034 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004035 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004036 }
4037 }
4038
Jose Lima4b6c6692014-08-12 17:41:12 -07004039 ActivityRecord getVisibleBehindActivity() {
4040 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004041 }
4042
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004043 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4044 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004045 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004046 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4047 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004048 while (i > 0) {
4049 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004050 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004051 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004052 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004053 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004054 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004055 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004056 }
4057 }
4058 }
4059
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004060 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
4061 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004062 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4063 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004064 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4065 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004066 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004067 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07004068 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4069 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004070
4071 boolean hasVisibleActivities = false;
4072
4073 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004074 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004075 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4076 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004077 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4078 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4079 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4080 final ActivityRecord r = activities.get(activityNdx);
4081 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004082 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4083 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08004084 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004085 if (r.visible) {
4086 hasVisibleActivities = true;
4087 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004088 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004089 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4090 // Don't currently have state for the activity, or
4091 // it is finishing -- always remove it.
4092 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004093 } else if (!r.visible && r.launchCount > 2 &&
4094 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004095 // We have launched this activity too many times since it was
4096 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004097 // (Note if the activity is visible, we don't remove the record.
4098 // We leave the dead window on the screen but the process will
4099 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004100 remove = true;
4101 } else {
4102 // The process may be gone, but the activity lives on!
4103 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004104 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004105 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004106 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4107 "Removing activity " + r + " from stack at " + i
4108 + ": haveState=" + r.haveState
4109 + " stateNotNeeded=" + r.stateNotNeeded
4110 + " finishing=" + r.finishing
4111 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004112 if (!r.finishing) {
4113 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4114 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4115 r.userId, System.identityHashCode(r),
4116 r.task.taskId, r.shortComponentName,
4117 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07004118 if (r.state == ActivityState.RESUMED) {
4119 mService.updateUsageStats(r, false);
4120 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004121 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004122 } else {
4123 // We have the current state for this activity, so
4124 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004125 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4126 if (DEBUG_APP) Slog.v(TAG_APP,
4127 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004128 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004129 // Set nowVisible to previous visible state. If the app was visible while
4130 // it died, we leave the dead window on screen so it's basically visible.
4131 // This is needed when user later tap on the dead window, we need to stop
4132 // other apps when user transfers focus to the restarted activity.
4133 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004134 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004135 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004136 "App died, clearing saved state of " + r);
4137 r.icicle = null;
4138 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004139 }
Craig Mautnerd2328952013-03-05 12:46:26 -08004140 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004141 if (remove) {
4142 removeActivityFromHistoryLocked(r, "appDied");
4143 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004144 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004145 }
4146 }
4147
4148 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004149 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004150
Chong Zhang280d3322015-11-03 17:27:26 -08004151 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004152 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004153 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004154 if (r != null && r.state != ActivityState.RESUMED) {
4155 r.updateOptionsLocked(options);
4156 } else {
4157 ActivityOptions.abort(options);
4158 }
4159 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004160 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004161 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004162
Craig Mautner21d24a22014-04-23 11:45:37 -07004163 void updateTaskMovement(TaskRecord task, boolean toFront) {
4164 if (task.isPersistable) {
4165 task.mLastTimeMoved = System.currentTimeMillis();
4166 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4167 // recently will be most negative, tasks sent to the bottom before that will be less
4168 // negative. Similarly for recent tasks moved to the top which will be most positive.
4169 if (!toFront) {
4170 task.mLastTimeMoved *= -1;
4171 }
4172 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004173 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004174 }
4175
Craig Mautner84984fa2014-06-19 11:19:20 -07004176 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004177 final int top = mTaskHistory.size() - 1;
4178 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4179 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004180 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004181 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4182 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004183 mTaskHistory.remove(taskNdx);
4184 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004185 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004186 return;
4187 }
4188 }
4189 }
4190
Chong Zhang280d3322015-11-03 17:27:26 -08004191 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004192 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004193 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004194
Craig Mautner11bf9a52013-02-19 14:08:51 -08004195 final int numTasks = mTaskHistory.size();
4196 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004197 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004198 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004199 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004200 ActivityOptions.abort(options);
4201 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004202 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004203 }
4204 return;
4205 }
4206
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004207 if (timeTracker != null) {
4208 // The caller wants a time tracker associated with this task.
4209 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4210 tr.mActivities.get(i).appTimeTracker = timeTracker;
4211 }
4212 }
4213
Craig Mautner11bf9a52013-02-19 14:08:51 -08004214 // Shift all activities with this task up to the top
4215 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004216 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004217
Chong Zhang45c25ce2015-08-10 22:18:26 -07004218 // Don't refocus if invisible to current user
4219 ActivityRecord top = tr.getTopActivity();
4220 if (!okToShowLocked(top)) {
4221 addRecentActivityLocked(top);
4222 ActivityOptions.abort(options);
4223 return;
4224 }
4225
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004226 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004227 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004228 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004229
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004230 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004231 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004232 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004233 if (r != null) {
4234 mNoAnimActivities.add(r);
4235 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004236 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004237 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004238 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004239 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004240
Wale Ogunwaled046a012015-12-24 13:05:59 -08004241 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004242 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004243
4244 if (VALIDATE_TOKENS) {
4245 validateAppTokensLocked();
4246 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004247 }
4248
4249 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004250 * Worker method for rearranging history stack. Implements the function of moving all
4251 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004252 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004253 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004254 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4255 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004256 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004257 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004258 * @return Returns true if the move completed, false if not.
4259 */
Craig Mautner299f9602015-01-26 09:47:33 -08004260 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004261 final TaskRecord tr = taskForIdLocked(taskId);
4262 if (tr == null) {
4263 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4264 return false;
4265 }
4266
4267 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004268 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004269
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004270 // If we have a watcher, preflight the move before committing to it. First check
4271 // for *other* available tasks, but if none are available, then try again allowing the
4272 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004273 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004274 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004275 if (next == null) {
4276 next = topRunningActivityLocked(null, 0);
4277 }
4278 if (next != null) {
4279 // ask watcher if this is allowed
4280 boolean moveOK = true;
4281 try {
4282 moveOK = mService.mController.activityResuming(next.packageName);
4283 } catch (RemoteException e) {
4284 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004285 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004286 }
4287 if (!moveOK) {
4288 return false;
4289 }
4290 }
4291 }
4292
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004293 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004294
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004295 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4296 // For the case where we are moving the home task back and there is an activity visible
4297 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4298 // activity to maintain order with what the user is seeing.
4299 final ActivityStack fullscreenStack =
4300 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4301 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4302 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4303 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004304 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004305 return true;
4306 }
4307 }
4308
riddle_hsuc215a4f2014-12-27 12:10:45 +08004309 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004310
4311 // If true, we should resume the home activity next if the task we are moving to the
4312 // back is over the home stack. We force to false if the task we are moving to back
4313 // is the home task and we don't want it resumed after moving to the back.
4314 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4315 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004316 final TaskRecord nextTask = getNextTask(tr);
4317 if (nextTask != null) {
4318 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4319 } else {
4320 prevIsHome = true;
4321 }
4322 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004323 mTaskHistory.remove(tr);
4324 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004325 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004326
Craig Mautnerc8143c62013-09-03 12:15:57 -07004327 // There is an assumption that moving a task to the back moves it behind the home activity.
4328 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004329 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004330 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4331 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004332 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004333 break;
4334 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004335 if (taskNdx == 1) {
4336 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004337 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004338 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004339 }
4340
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004341 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004342 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004343
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004344 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004345 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004346 }
4347
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004348 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004349 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004350 if (!mService.mBooting && !mService.mBooted) {
4351 // Not ready yet!
4352 return false;
4353 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004354 final int taskToReturnTo = tr.getTaskToReturnTo();
4355 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004356 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004357 }
4358
Wale Ogunwaled046a012015-12-24 13:05:59 -08004359 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004360 return true;
4361 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004362
Craig Mautner8849a5e2013-04-02 16:41:03 -07004363 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004364 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004365 final Uri data = r.intent.getData();
4366 final String strData = data != null ? data.toSafeString() : null;
4367
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004368 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004369 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004370 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004371 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004372 }
4373
4374 /**
4375 * Make sure the given activity matches the current configuration. Returns
4376 * false if the activity had to be destroyed. Returns true if the
4377 * configuration is the same, or the activity will remain running as-is
4378 * for whatever reason. Ensures the HistoryRecord is updated with the
4379 * correct configuration and all other bookkeeping is handled.
4380 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004381 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4382 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004383 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004384 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004385 "Skipping config check (will change): " + r);
4386 return true;
4387 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004388
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004389 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004390 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004391
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004392 // Short circuit: if the two configurations are equal (the common case), then there is
4393 // nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004394 final Configuration newConfig = mService.mConfiguration;
4395 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004396 if (r.configuration.equals(newConfig)
4397 && r.taskConfigOverride.equals(taskConfig)
Wale Ogunwale60454db2015-01-23 16:05:07 -08004398 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004399 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004400 "Configuration unchanged in " + r);
4401 return true;
4402 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004403
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004404 // We don't worry about activities that are finishing.
4405 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004406 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004407 "Configuration doesn't matter in finishing " + r);
4408 r.stopFreezingScreenLocked(false);
4409 return true;
4410 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004411
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004412 // Okay we now are going to make this activity have the new config.
4413 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004414 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004415 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004416 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004417 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004418
Filip Gruszczynski23493322015-07-29 17:02:59 -07004419 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004420 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004421 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004422 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004423 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004424 // There are no significant differences, so we won't relaunch but should still deliver
4425 // the new configuration to the client process.
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004426 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004427 return true;
4428 }
4429
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004430 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4431 "Configuration changes for " + r + " ; taskChanges="
4432 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4433 + Configuration.configurationDiffToString(changes));
4434
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004435 // If the activity isn't currently running, just leave the new
4436 // configuration and it will pick that up next time it starts.
4437 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004438 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004439 "Configuration doesn't matter not running " + r);
4440 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004441 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004442 return true;
4443 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004444
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004445 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004446 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4447 "Checking to restart " + r.info.name + ": changed=0x"
4448 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004449 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4450 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004451
Dianne Hackborne6676352011-06-01 16:51:20 -07004452 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004453 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4454 r.configChangeFlags |= changes;
4455 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004456 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004457 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004458 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004459 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004460 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004461 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004462 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004463 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4464 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004465 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004466 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004467 r.deferRelaunchUntilPaused = true;
4468 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004469 return true;
4470 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004471 // Try to optimize this case: the configuration is changing and we need to restart
4472 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004473 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004474 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004475 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004476
4477 if (DEBUG_STATES && !r.visible) {
4478 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4479 + " called by " + Debug.getCallers(4));
4480 }
4481
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004482 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004483 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004484 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004485 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004486 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004487 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004488
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004489 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004490 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004491 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004492
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004493 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004494 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004495 // changes is always sent to all processes when they happen so it can just use whatever
4496 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004497 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004498 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004499
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004500 return true;
4501 }
4502
Filip Gruszczynski23493322015-07-29 17:02:59 -07004503 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4504 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004505
4506 // If we went from full-screen to non-full-screen, make sure to use the correct
4507 // configuration task diff, so the diff stays as small as possible.
4508 if (Configuration.EMPTY.equals(oldTaskOverride)
4509 && !Configuration.EMPTY.equals(taskConfig)) {
4510 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4511 }
4512
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004513 // Conversely, do the same when going the other direction.
4514 if (Configuration.EMPTY.equals(taskConfig)
4515 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4516 taskConfig = record.task.extractOverrideConfig(record.configuration);
4517 }
4518
Filip Gruszczynski23493322015-07-29 17:02:59 -07004519 // Determine what has changed. May be nothing, if this is a config
4520 // that has come back from the app after going idle. In that case
4521 // we just want to leave the official config object now in the
4522 // activity and do nothing else.
4523 int taskChanges = oldTaskOverride.diff(taskConfig);
4524 // We don't want to use size changes if they don't cross boundaries that are important to
4525 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004526 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004527 final boolean crosses = record.crossesHorizontalSizeThreshold(
4528 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4529 || record.crossesVerticalSizeThreshold(
4530 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4531 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004532 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004533 }
4534 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004535 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004536 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4537 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004538 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004539 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004540 }
4541 }
4542 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4543 }
4544
4545 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4546 Configuration oldTaskOverride, int taskChanges) {
4547 if (taskChanges == 0) {
4548 // {@link Configuration#diff} doesn't catch changes from unset values.
4549 // Check for changes we care about.
4550 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004551 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004552 }
4553 // We want to explicitly track situations where the size configuration goes from
4554 // undefined to defined. We don't care about crossing the threshold in that case,
4555 // because there is no threshold.
4556 final int oldHeight = oldTaskOverride.screenHeightDp;
4557 final int newHeight = taskConfig.screenHeightDp;
4558 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4559 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4560 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004561 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004562 }
4563 final int oldWidth = oldTaskOverride.screenWidthDp;
4564 final int newWidth = taskConfig.screenWidthDp;
4565 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4566 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4567 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004568 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004569 }
4570 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4571 final int newSmallest = taskConfig.smallestScreenWidthDp;
4572 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4573 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4574 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004575 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4576 }
4577 final int oldLayout = oldTaskOverride.screenLayout;
4578 final int newLayout = taskConfig.screenLayout;
4579 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4580 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4581 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004582 }
4583 }
4584 return taskChanges;
4585 }
4586
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004587 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004588 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4589 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004590 }
4591
Wale Ogunwale83301a92015-09-24 15:54:08 -07004592 private void relaunchActivityLocked(
4593 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4594 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004595 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004596 return;
4597 }
4598
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004599 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004600 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004601 if (andResume) {
4602 results = r.results;
4603 newIntents = r.newIntents;
4604 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004605 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4606 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004607 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004608 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004609 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004610 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004612 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004613
Craig Mautner34b73df2014-01-12 21:11:08 -08004614 mStackSupervisor.removeChildActivityContainers(r);
4615
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004616 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004617 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004618 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4619 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004620 r.forceNewConfig = false;
Chong Zhangd78ddb42016-03-02 17:01:14 -08004621 mStackSupervisor.activityRelaunchingLocked(r);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004622 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4623 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004624 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004625 // Note: don't need to call pauseIfSleepingLocked() here, because
4626 // the caller will only pass in 'andResume' if this activity is
4627 // currently resumed, which implies we aren't sleeping.
4628 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004629 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004630 }
4631
4632 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004633 if (DEBUG_STATES) {
4634 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4635 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004636 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004637 // Relaunch-resume could happen either when the app is already in the front,
4638 // or while it's being brought to front. In the latter case, it's marked RESUMED
4639 // but not yet visible (or stopped). We need to complete the resume here as the
4640 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4641 if (!r.visible || r.stopped) {
4642 mWindowManager.setAppVisibility(r.appToken, true);
4643 completeResumeLocked(r);
4644 } else {
4645 r.results = null;
4646 r.newIntents = null;
4647 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004648 } else {
4649 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4650 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004651 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004652
4653 r.configChangeFlags = 0;
Chong Zhang225d1b62016-03-31 14:09:33 -07004654 r.deferRelaunchUntilPaused = false;
4655 r.preserveWindowOnDeferredRelaunch = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004656 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004657
4658 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004659 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4660 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4661 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4662 final ActivityRecord r = activities.get(activityNdx);
4663 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004664 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004665 }
4666 if (r.fullscreen && !r.finishing) {
4667 return false;
4668 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004669 }
4670 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004671 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004672 if (r == null) {
4673 return false;
4674 }
4675 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4676 + " would have returned true for r=" + r);
4677 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004678 }
4679
4680 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004681 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4682 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4683 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4684 final ActivityRecord r = activities.get(activityNdx);
4685 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004686 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004687 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004688 }
4689 }
4690 }
4691
Wale Ogunwale540e1232015-05-01 15:35:39 -07004692 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4693 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004694 boolean didSomething = false;
4695 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004696 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004697 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4698 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4699 int numActivities = activities.size();
4700 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4701 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004702 final boolean sameComponent =
4703 (r.packageName.equals(packageName) && (filterByClasses == null
4704 || filterByClasses.contains(r.realActivity.getClassName())))
4705 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004706 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004707 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004708 && (r.app == null || evenPersistent || !r.app.persistent)) {
4709 if (!doit) {
4710 if (r.finishing) {
4711 // If this activity is just finishing, then it is not
4712 // interesting as far as something to stop.
4713 continue;
4714 }
4715 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004716 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004717 if (r.isHomeActivity()) {
4718 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4719 Slog.i(TAG, "Skip force-stop again " + r);
4720 continue;
4721 } else {
4722 homeActivity = r.realActivity;
4723 }
4724 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004725 didSomething = true;
4726 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004727 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004728 if (r.app != null) {
4729 r.app.removed = true;
4730 }
4731 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004732 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004733 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004734 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4735 true)) {
4736 // r has been deleted from mActivities, accommodate.
4737 --numActivities;
4738 --activityNdx;
4739 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004740 }
4741 }
4742 }
4743 return didSomething;
4744 }
4745
Dianne Hackborn09233282014-04-30 11:33:59 -07004746 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004747 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4748 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004749 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004750 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004751 if (task.getTopActivity() == null) {
4752 continue;
4753 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004754 ActivityRecord r = null;
4755 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004756 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004757 int numActivities = 0;
4758 int numRunning = 0;
4759 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004760 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004761 continue;
4762 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004763 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004764 tmp = activities.get(activityNdx);
4765 if (tmp.finishing) {
4766 continue;
4767 }
4768 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004769
Craig Mautneraab647e2013-02-28 16:31:36 -08004770 // Initialize state for next task if needed.
4771 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4772 top = r;
4773 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004774 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004775
4776 // Add 'r' into the current task.
4777 numActivities++;
4778 if (r.app != null && r.app.thread != null) {
4779 numRunning++;
4780 }
4781
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004782 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004783 TAG, r.intent.getComponent().flattenToShortString()
4784 + ": task=" + r.task);
4785 }
4786
4787 RunningTaskInfo ci = new RunningTaskInfo();
4788 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004789 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004790 ci.baseActivity = r.intent.getComponent();
4791 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004792 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004793 if (focusedStack && topTask) {
4794 // Give the latest time to ensure foreground task can be sorted
4795 // at the first, because lastActiveTime of creating task is 0.
4796 ci.lastActiveTime = System.currentTimeMillis();
4797 topTask = false;
4798 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004799
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004800 if (top.task != null) {
4801 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004802 }
4803 ci.numActivities = numActivities;
4804 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004805 ci.isDockable = task.canGoInDockedStack();
Jorim Jaggi29379ec2016-04-11 23:43:42 -07004806 ci.resizeMode = task.mResizeMode;
Craig Mautneraab647e2013-02-28 16:31:36 -08004807 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004808 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004809 }
4810
4811 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004812 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004813 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004814 if (top >= 0) {
4815 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4816 int activityTop = activities.size() - 1;
4817 if (activityTop > 0) {
4818 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4819 "unhandled-back", true);
4820 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004821 }
4822 }
4823
Craig Mautner6b74cb52013-09-27 17:02:21 -07004824 /**
4825 * Reset local parameters because an app's activity died.
4826 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004827 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004828 */
4829 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004830 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004831 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004832 "App died while pausing: " + mPausingActivity);
4833 mPausingActivity = null;
4834 }
4835 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4836 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004837 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004838 }
4839
Craig Mautner19091252013-10-05 00:03:53 -07004840 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004841 }
4842
Craig Mautnercae015f2013-02-08 14:31:27 -08004843 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004844 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4845 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4846 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4847 final ActivityRecord r = activities.get(activityNdx);
4848 if (r.app == app) {
4849 Slog.w(TAG, " Force finishing activity "
4850 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004851 // Force the destroy to skip right to removal.
4852 r.app = null;
4853 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004854 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004855 }
4856 }
4857 }
4858
Dianne Hackborn390517b2013-05-30 15:03:32 -07004859 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004860 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004861 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004862 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4863 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004864 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4865 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004866 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004867 " Task id #" + task.taskId + "\n" +
4868 " mFullscreen=" + task.mFullscreen + "\n" +
4869 " mBounds=" + task.mBounds + "\n" +
Andrii Kulian771f9e02016-03-28 19:08:58 -07004870 " mMinimalWidth=" + task.mMinimalWidth + "\n" +
4871 " mMinimalHeight=" + task.mMinimalHeight + "\n" +
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004872 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004873 if (printed) {
4874 header = null;
4875 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004876 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004877 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004878 }
4879
4880 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4881 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4882
4883 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004884 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4885 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004886 }
4887 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004888 final int top = mTaskHistory.size() - 1;
4889 if (top >= 0) {
4890 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4891 int listTop = list.size() - 1;
4892 if (listTop >= 0) {
4893 activities.add(list.get(listTop));
4894 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004895 }
4896 } else {
4897 ItemMatcher matcher = new ItemMatcher();
4898 matcher.build(name);
4899
Craig Mautneraab647e2013-02-28 16:31:36 -08004900 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4901 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4902 if (matcher.match(r1, r1.intent.getComponent())) {
4903 activities.add(r1);
4904 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004905 }
4906 }
4907 }
4908
4909 return activities;
4910 }
4911
4912 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004913 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004914
4915 // All activities that came from the package must be
4916 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004917 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4918 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4919 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4920 final ActivityRecord a = activities.get(activityNdx);
4921 if (a.info.packageName.equals(packageName)) {
4922 a.forceNewConfig = true;
4923 if (starting != null && a == starting && a.visible) {
4924 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004925 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004926 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004927 }
4928 }
4929 }
4930
4931 return starting;
4932 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004933
Craig Mautner299f9602015-01-26 09:47:33 -08004934 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004935 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004936 }
4937
Wale Ogunwale000957c2015-04-03 08:19:12 -07004938 /**
4939 * Removes the input task from this stack.
4940 * @param task to remove.
4941 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004942 * @param moving task to another stack. In the case we are moving we don't want to perform
4943 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004944 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004945 void removeTask(TaskRecord task, String reason, boolean moving) {
4946 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004947 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004948 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004949 if (!StackId.persistTaskBounds(mStackId)) {
4950 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4951 // default configuration the next time it launches.
4952 task.updateOverrideConfiguration(null);
4953 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004954 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004955
Craig Mautner04a0ea62014-01-13 12:51:26 -08004956 final ActivityRecord r = mResumedActivity;
4957 if (r != null && r.task == task) {
4958 mResumedActivity = null;
4959 }
4960
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004961 final int taskNdx = mTaskHistory.indexOf(task);
4962 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004963 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4964 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4965 if (!nextTask.isOverHomeStack()) {
4966 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4967 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004968 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004969 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004970 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004971
Wale Ogunwale040b4702015-08-06 18:10:50 -07004972 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004973 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004974 final boolean isVoiceSession = task.voiceSession != null;
4975 if (isVoiceSession) {
4976 try {
4977 task.voiceSession.taskFinished(task.intent, task.taskId);
4978 } catch (RemoteException e) {
4979 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004980 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004981 if (task.autoRemoveFromRecents() || isVoiceSession) {
4982 // Task creator asked to remove this when done, or this task was a voice
4983 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004984 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004985 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004986 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004987 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004988
4989 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004990 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004991 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004992 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004993 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07004994 if (mFullscreen
4995 || !adjustFocusToNextFocusableStackLocked(
4996 task.getTaskToReturnTo(), myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004997 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004998 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004999 }
Craig Mautner593a4e62014-01-15 17:55:51 -08005000 if (mStacks != null) {
5001 mStacks.remove(this);
5002 mStacks.add(0, this);
5003 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005004 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005005 mActivityContainer.onTaskListEmptyLocked();
5006 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005007 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005008
5009 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08005010 }
5011
Dianne Hackborn91097de2014-04-04 18:02:06 -07005012 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5013 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
5014 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07005015 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
5016 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005017 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005018 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08005019 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Andrii Kulian2e751b82016-03-16 16:59:32 -07005020 if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08005021 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07005022 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005023 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005024 return task;
5025 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005026
Andrii Kulian2e751b82016-03-16 16:59:32 -07005027 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08005028 if (mTaskPositioner == null) {
5029 return false;
5030 }
Andrii Kulian2e751b82016-03-16 16:59:32 -07005031 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
Wale Ogunwale935e5022015-11-10 12:36:10 -08005032 return true;
5033 }
5034
Craig Mautnerc00204b2013-03-05 15:02:14 -08005035 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005036 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005037 }
5038
Wale Ogunwale5f986092015-12-04 15:35:38 -08005039 void addTask(final TaskRecord task, final boolean toTop, String reason) {
5040 final ActivityStack prevStack = preAddTask(task, reason);
5041
Craig Mautnerc00204b2013-03-05 15:02:14 -08005042 task.stack = this;
5043 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07005044 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005045 } else {
5046 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005047 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005048 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005049 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005050 }
5051
Wale Ogunwale5f986092015-12-04 15:35:38 -08005052 void positionTask(final TaskRecord task, int position) {
Jorim Jaggi023da532016-04-20 22:42:32 -07005053 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
5054 final boolean wasResumed = topRunningActivity == task.stack.mResumedActivity;
Wale Ogunwale5f986092015-12-04 15:35:38 -08005055 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005056 task.stack = this;
5057 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005058 postAddTask(task, prevStack);
Jorim Jaggi023da532016-04-20 22:42:32 -07005059 if (wasResumed) {
5060 if (mResumedActivity != null) {
5061 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5062 + " other stack to this stack mResumedActivity=" + mResumedActivity
5063 + " other mResumedActivity=" + topRunningActivity);
5064 }
5065 mResumedActivity = topRunningActivity;
5066 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005067 }
5068
5069 private ActivityStack preAddTask(TaskRecord task, String reason) {
5070 final ActivityStack prevStack = task.stack;
5071 if (prevStack != null && prevStack != this) {
5072 prevStack.removeTask(task, reason, MOVING);
5073 }
5074 return prevStack;
5075 }
5076
5077 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
5078 if (prevStack != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07005079 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005080 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005081 try {
5082 task.voiceSession.taskStarted(task.intent, task.taskId);
5083 } catch (RemoteException e) {
5084 }
5085 }
5086 }
5087
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005088 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005089 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005090 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07005091 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
5092 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08005093 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
5094 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005095 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005096 r.taskConfigOverride = task.mOverrideConfig;
5097 }
5098
Wale Ogunwaled046a012015-12-24 13:05:59 -08005099 void moveToFrontAndResumeStateIfNeeded(
5100 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
5101 if (!moveToFront) {
5102 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005103 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005104
5105 // If the activity owns the last resumed activity, transfer that together,
5106 // so that we don't resume the same activity again in the new stack.
5107 // Apps may depend on onResume()/onPause() being called in pairs.
5108 if (setResume) {
5109 mResumedActivity = r;
5110 }
5111 // Move the stack in which we are placing the activity to the front. The call will also
5112 // make sure the activity focus is set.
5113 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005114 }
5115
5116 /**
5117 * Moves the input activity from its current stack to this one.
5118 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
5119 * created on this stack which the activity is added to.
5120 * */
5121 void moveActivityToStack(ActivityRecord r) {
5122 final ActivityStack prevStack = r.task.stack;
5123 if (prevStack.mStackId == mStackId) {
5124 // You are already in the right stack silly...
5125 return;
5126 }
5127
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07005128 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07005129 && (mStackSupervisor.topRunningActivityLocked() == r);
5130 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
5131
5132 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08005133 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
5134 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005135 r.setTask(task, null);
5136 task.addActivityToTop(r);
5137 setAppTask(r, task);
Andrii Kulian933076d2016-03-29 17:04:42 -07005138 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005139 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
5140 if (wasResumed) {
5141 prevStack.mResumedActivity = null;
5142 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07005143 }
5144
Wale Ogunwale706ed792015-08-02 10:29:44 -07005145 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005146 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005147 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
5148 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005149 r.taskConfigOverride = task.mOverrideConfig;
5150 }
5151
Craig Mautnerc00204b2013-03-05 15:02:14 -08005152 public int getStackId() {
5153 return mStackId;
5154 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005155
5156 @Override
5157 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005158 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5159 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005160 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005161
Craig Mautner15df08a2015-04-01 12:17:18 -07005162 void onLockTaskPackagesUpdatedLocked() {
5163 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5164 mTaskHistory.get(taskNdx).setLockTaskAuth();
5165 }
5166 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005167}