blob: 3db3a2609c42d5261b498ff56ef7b8063237c692 [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
Craig Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
33import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
34import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
35import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
36import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070037import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
38
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070039import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070040import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080041import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080042import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080043import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080044import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046
47import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070048import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070049import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080052import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080054import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080060import android.content.res.Resources;
61import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.RemoteException;
71import android.os.SystemClock;
Winson Chung303e1ff2014-03-07 15:06:19 -080072import android.os.SystemProperties;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070077import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import java.lang.ref.WeakReference;
82import java.util.ArrayList;
83import java.util.Iterator;
84import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080085import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
87/**
88 * State and management of a single stack of activities.
89 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070090final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080091
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070092 // Ticks during which we check progress while waiting for an app to launch.
93 static final int LAUNCH_TICK = 500;
94
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070095 // How long we wait until giving up on the last activity to pause. This
96 // is short because it directly impacts the responsiveness of starting the
97 // next activity.
98 static final int PAUSE_TIMEOUT = 500;
99
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700100 // How long we wait for the activity to tell us it has stopped before
101 // giving up. This is a good amount of time because we really need this
102 // from the application in order to get its saved state.
103 static final int STOP_TIMEOUT = 10*1000;
104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105 // How long we wait until giving up on an activity telling us it has
106 // finished destroying itself.
107 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800110 // disabled.
111 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800112
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700113 // How long between activity launches that we consider safe to not warn
114 // the user about an unexpected activity being launched on top.
115 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // Set to false to disable the preview that is shown while a new activity
118 // is being started.
119 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800120
Craig Mautner5eda9b32013-07-02 11:58:16 -0700121 // How long to wait for all background Activities to redraw following a call to
122 // convertToTranslucent().
123 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
124
Craig Mautnera61bc652013-10-28 15:43:18 -0700125 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 enum ActivityState {
128 INITIALIZING,
129 RESUMED,
130 PAUSING,
131 PAUSED,
132 STOPPING,
133 STOPPED,
134 FINISHING,
135 DESTROYING,
136 DESTROYED
137 }
138
139 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700140 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 /**
143 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144 * running) activities. It contains #TaskRecord objects.
145 */
146 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
147
148 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800149 * Used for validating app tokens with window manager.
150 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800151 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700153 /**
154 * List of running activities, sorted by recent usage.
155 * The first entry in the list is the least recently used.
156 * It contains HistoryRecord objects.
157 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700158 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159
160 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 * Animations that for the current transition have requested not to
162 * be considered for the transition animation.
163 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700164 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165
166 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167 * When we are in the process of pausing an activity, before starting the
168 * next one, this variable holds the activity that is currently being paused.
169 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800170 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171
172 /**
173 * This is the last activity that we put into the paused state. This is
174 * used to determine if we need to do an activity transition while sleeping,
175 * when we normally hold the top activity paused.
176 */
177 ActivityRecord mLastPausedActivity = null;
178
179 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700180 * Activities that specify No History must be removed once the user navigates away from them.
181 * If the device goes to sleep with such an activity in the paused state then we save it here
182 * and finish it later if another activity replaces it on wakeup.
183 */
184 ActivityRecord mLastNoHistoryActivity = null;
185
186 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 * Current activity that is resumed, or null if there is none.
188 */
189 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700191 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700192 * This is the last activity that has been started. It is only used to
193 * identify when multiple activities are started at once so that the user
194 * can be warned they may not be in the activity they think they are.
195 */
196 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800197
Craig Mautner5eda9b32013-07-02 11:58:16 -0700198 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
199 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
200 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
201 // Activity in mTranslucentActivityWaiting is notified via
202 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
203 // background activity being drawn then the same call will be made with a true value.
204 ActivityRecord mTranslucentActivityWaiting = null;
205 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
206 new ArrayList<ActivityRecord>();
207
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Set when we know we are going to be calling updateConfiguration()
210 * soon, so want to skip intermediate config checks.
211 */
212 boolean mConfigWillChange;
213
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700214 long mLaunchStartTime = 0;
215 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800216
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800217 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800218 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
219 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
220 */
221 private ActivityRecord mLastScreenshotActivity = null;
222 private Bitmap mLastScreenshotBitmap = null;
223
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800224 int mThumbnailWidth = -1;
225 int mThumbnailHeight = -1;
226
Craig Mautner858d8a62013-04-23 17:08:34 -0700227 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700228
Craig Mautnerc00204b2013-03-05 15:02:14 -0800229 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800230 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800231 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
232 ArrayList<ActivityStack> mStacks;
233 /** The attached Display's unique identifier, or -1 if detached */
234 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235
Craig Mautner27084302013-03-25 08:05:25 -0700236 /** Run all ActivityStacks through this */
237 final ActivityStackSupervisor mStackSupervisor;
238
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700239 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700240 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
241 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
242 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
243 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700244 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700245
246 static class ScheduleDestroyArgs {
247 final ProcessRecord mOwner;
248 final boolean mOomAdj;
249 final String mReason;
250 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
251 mOwner = owner;
252 mOomAdj = oomAdj;
253 mReason = reason;
254 }
255 }
256
Zoran Marcetaf958b322012-08-09 20:27:12 +0900257 final Handler mHandler;
258
259 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700260 //public Handler() {
261 // if (localLOGV) Slog.v(TAG, "Handler started!");
262 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700263 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 super(looper);
265 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700266
Zoran Marcetaf958b322012-08-09 20:27:12 +0900267 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268 public void handleMessage(Message msg) {
269 switch (msg.what) {
270 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800271 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700272 // We don't at this point know if the activity is fullscreen,
273 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800274 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700275 synchronized (mService) {
276 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700277 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700278 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700279 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800280 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700281 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700282 case LAUNCH_TICK_MSG: {
283 ActivityRecord r = (ActivityRecord)msg.obj;
284 synchronized (mService) {
285 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700286 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700287 }
288 }
289 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700290 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800291 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 // We don't at this point know if the activity is fullscreen,
293 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800294 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800295 synchronized (mService) {
296 activityDestroyedLocked(r != null ? r.appToken : null);
297 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700299 case STOP_TIMEOUT_MSG: {
300 ActivityRecord r = (ActivityRecord)msg.obj;
301 // We don't at this point know if the activity is fullscreen,
302 // so we need to be conservative and assume it isn't.
303 Slog.w(TAG, "Activity stop timeout for " + r);
304 synchronized (mService) {
305 if (r.isInHistory()) {
306 activityStoppedLocked(r, null, null, null);
307 }
308 }
309 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700310 case DESTROY_ACTIVITIES_MSG: {
311 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
312 synchronized (mService) {
313 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
314 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700315 } break;
316 case TRANSLUCENT_TIMEOUT_MSG: {
317 synchronized (mService) {
318 notifyActivityDrawnLocked(null);
319 }
320 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700321 }
322 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800323 }
324
Craig Mautner34b73df2014-01-12 21:11:08 -0800325 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800326 int count = 0;
327 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
328 count += mTaskHistory.get(taskNdx).mActivities.size();
329 }
330 return count;
331 }
332
Craig Mautner4a1cb222013-12-04 16:14:06 -0800333 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
334 mActivityContainer = activityContainer;
335 mStackSupervisor = activityContainer.getOuter();
336 mService = mStackSupervisor.mService;
337 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
338 mWindowManager = mService.mWindowManager;
339 mStackId = activityContainer.mStackId;
340 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700341 }
Craig Mautner5962b122012-10-05 14:45:52 -0700342
Amith Yamasani734983f2014-03-04 16:48:05 -0800343 /**
344 * Checks whether the userid is either the current user or a related user.
345 */
346 private boolean isRelatedToOrCurrentUserLocked(int userId) {
347 if (mCurrentUser == userId) return true;
348 for (int i = 0; i < mService.mRelatedUserIds.length; i++) {
349 if (mService.mRelatedUserIds[i] == userId) return true;
350 }
351 return false;
352 }
353
354 boolean okToShowLocked(ActivityRecord r) {
355 return isRelatedToOrCurrentUserLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700356 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
357 }
358
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700359 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800360 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700361 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
362 if (r != null) {
363 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800364 }
365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 return null;
367 }
368
369 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800370 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
371 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800372 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800373 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
374 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800375 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800376 return r;
377 }
378 }
379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 return null;
381 }
382
383 /**
384 * This is a simplified version of topRunningActivityLocked that provides a number of
385 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800386 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 * @param token If non-null, any history records matching this token will be skipped.
388 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800389 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700390 * @return Returns the HistoryRecord of the next activity on the stack.
391 */
392 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800393 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
394 TaskRecord task = mTaskHistory.get(taskNdx);
395 if (task.taskId == taskId) {
396 continue;
397 }
398 ArrayList<ActivityRecord> activities = task.mActivities;
399 for (int i = activities.size() - 1; i >= 0; --i) {
400 final ActivityRecord r = activities.get(i);
401 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800402 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800403 return r;
404 }
405 }
406 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700407 return null;
408 }
409
Craig Mautner8849a5e2013-04-02 16:41:03 -0700410 final ActivityRecord topActivity() {
411 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700412 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700413 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
414 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700415 final int topActivityNdx = activities.size() - 1;
416 if (topActivityNdx >= 0) {
417 return activities.get(topActivityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700418 }
419 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700420 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700421 }
422
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700423 final TaskRecord topTask() {
424 final int size = mTaskHistory.size();
425 if (size > 0) {
426 return mTaskHistory.get(size - 1);
427 }
428 return null;
429 }
430
Craig Mautnerd2328952013-03-05 12:46:26 -0800431 TaskRecord taskForIdLocked(int id) {
432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
433 final TaskRecord task = mTaskHistory.get(taskNdx);
434 if (task.taskId == id) {
435 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800436 }
437 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700438 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700439 }
440
Craig Mautnerd2328952013-03-05 12:46:26 -0800441 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800442 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800443 if (r != null) {
444 final TaskRecord task = r.task;
445 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
446 if (task.stack != this) Slog.w(TAG,
447 "Illegal state! task does not point to stack it is in.");
448 return r;
449 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800450 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800451 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800452 }
453
Craig Mautner2420ead2013-04-01 17:13:20 -0700454 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 final boolean hadit = mLRUActivities.remove(r);
456 mLRUActivities.add(r);
457 return hadit;
458 }
459
Craig Mautnerde4ef022013-04-07 19:01:33 -0700460 final boolean isHomeStack() {
461 return mStackId == HOME_STACK_ID;
462 }
463
Craig Mautnere0a38842013-12-16 16:14:02 -0800464 final boolean isOnHomeDisplay() {
465 return isAttached() &&
466 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
467 }
468
469 final void moveToFront() {
470 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800471 if (isOnHomeDisplay()) {
472 mStackSupervisor.moveHomeStack(isHomeStack());
473 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800474 mStacks.remove(this);
475 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800476 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800477 }
478
479 final boolean isAttached() {
480 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800481 }
482
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700483 /**
484 * Returns the top activity in any existing task matching the given
485 * Intent. Returns null if no such task is found.
486 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700487 ActivityRecord findTaskLocked(ActivityRecord target) {
488 Intent intent = target.intent;
489 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700490 ComponentName cls = intent.getComponent();
491 if (info.targetActivity != null) {
492 cls = new ComponentName(info.packageName, info.targetActivity);
493 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700494 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700495 boolean isDocument = intent != null & intent.isDocument();
496 // If documentData is non-null then it must match the existing task data.
497 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800498
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700499 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800500 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
501 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700502 if (task.userId != userId) {
503 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700504 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700505 continue;
506 }
Craig Mautner000f0022013-02-26 15:04:29 -0800507 final ActivityRecord r = task.getTopActivity();
508 if (r == null || r.finishing || r.userId != userId ||
509 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700510 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800511 continue;
512 }
513
Craig Mautnerd00f4742014-03-12 14:17:26 -0700514 final Intent taskIntent = task.intent;
515 final Intent affinityIntent = task.affinityIntent;
516 final boolean taskIsDocument;
517 final Uri taskDocumentData;
518 if (taskIntent != null && taskIntent.isDocument()) {
519 taskIsDocument = true;
520 taskDocumentData = taskIntent.getData();
521 } else if (affinityIntent != null && affinityIntent.isDocument()) {
522 taskIsDocument = true;
523 taskDocumentData = affinityIntent.getData();
524 } else {
525 taskIsDocument = false;
526 taskDocumentData = null;
527 }
528
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700529 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700530 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700531 + "/aff=" + r.task.affinity + " to new cls="
532 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700533 if (!isDocument && !taskIsDocument && task.affinity != null) {
534 if (task.affinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700535 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700536 return r;
537 }
Craig Mautnerd00f4742014-03-12 14:17:26 -0700538 } else if (taskIntent != null && taskIntent.getComponent().equals(cls) &&
539 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700540 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800541 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700542 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
543 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800544 return r;
Craig Mautnerd00f4742014-03-12 14:17:26 -0700545 } else if (affinityIntent != null && affinityIntent.getComponent().equals(cls) &&
546 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700547 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800548 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700549 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
550 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800551 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700552 } else if (DEBUG_TASKS) {
553 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700554 }
555 }
556
557 return null;
558 }
559
560 /**
561 * Returns the first activity (starting from the top of the stack) that
562 * is the same as the given activity. Returns null if no such activity
563 * is found.
564 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700565 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700566 ComponentName cls = intent.getComponent();
567 if (info.targetActivity != null) {
568 cls = new ComponentName(info.packageName, info.targetActivity);
569 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700570 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700571
Craig Mautner000f0022013-02-26 15:04:29 -0800572 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700573 TaskRecord task = mTaskHistory.get(taskNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800574 if (!isRelatedToOrCurrentUserLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700575 return null;
576 }
577 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800578 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
579 ActivityRecord r = activities.get(activityNdx);
580 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700581 //Slog.i(TAG, "Found matching class!");
582 //dump();
583 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
584 return r;
585 }
586 }
587 }
588
589 return null;
590 }
591
Amith Yamasani742a6712011-05-04 14:49:28 -0700592 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700593 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700594 */
Craig Mautner93529a42013-10-04 15:03:13 -0700595 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800596 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700597 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800598 }
599 mCurrentUser = userId;
600
601 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800602 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800603 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700604 TaskRecord task = mTaskHistory.get(i);
Amith Yamasani734983f2014-03-04 16:48:05 -0800605 if (isRelatedToOrCurrentUserLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700606 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
607 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800608 mTaskHistory.remove(i);
609 mTaskHistory.add(task);
610 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800611 // Use same value for i.
612 } else {
613 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800614 }
615 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700616 if (VALIDATE_TOKENS) {
617 validateAppTokensLocked();
618 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700619 }
620
Craig Mautner2420ead2013-04-01 17:13:20 -0700621 void minimalResumeActivityLocked(ActivityRecord r) {
622 r.state = ActivityState.RESUMED;
623 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
624 + " (starting new instance)");
625 r.stopped = false;
626 mResumedActivity = r;
627 r.task.touchActiveTime();
628 mService.addRecentTaskLocked(r.task);
629 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700630 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700631 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700632 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700633 }
634
Dianne Hackborncee04b52013-07-03 17:01:28 -0700635 private void startLaunchTraces() {
636 if (mFullyDrawnStartTime != 0) {
637 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
638 }
639 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
640 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
641 }
642
643 private void stopFullyDrawnTraceIfNeeded() {
644 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
645 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
646 mFullyDrawnStartTime = 0;
647 }
648 }
649
Craig Mautnere79d42682013-04-01 19:01:53 -0700650 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700651 if (r.displayStartTime == 0) {
652 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
653 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700654 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700655 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700656 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700657 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700658 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700659 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700660 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700661 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800662
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700663 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700664 // Make sure that there is no activity waiting for this to launch.
665 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
666 r.displayStartTime = r.fullyDrawnStartTime = 0;
667 } else {
668 mStackSupervisor.removeTimeoutsForActivityLocked(r);
669 mStackSupervisor.scheduleIdleTimeoutLocked(r);
670 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700671 }
672
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800673 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800674 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800675 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
676 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
677 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
678 activities.get(activityNdx).setSleeping(false);
679 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800680 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800681 }
682
Craig Mautner0eea92c2013-05-16 13:35:39 -0700683 /**
684 * @return true if something must be done before going to sleep.
685 */
686 boolean checkReadyForSleepLocked() {
687 if (mResumedActivity != null) {
688 // Still have something resumed; can't sleep until it is paused.
689 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
690 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
691 startPausingLocked(false, true);
692 return true;
693 }
694 if (mPausingActivity != null) {
695 // Still waiting for something to pause; can't sleep yet.
696 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
697 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800698 }
699
Craig Mautner0eea92c2013-05-16 13:35:39 -0700700 return false;
701 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800702
Craig Mautner0eea92c2013-05-16 13:35:39 -0700703 void goToSleep() {
704 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800705
Craig Mautner0eea92c2013-05-16 13:35:39 -0700706 // Make sure any stopped but visible activities are now sleeping.
707 // This ensures that the activity's onStop() is called.
708 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
709 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
710 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
711 final ActivityRecord r = activities.get(activityNdx);
712 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
713 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800714 }
715 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800716 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700717 }
Craig Mautner59c00972012-07-30 12:10:24 -0700718
Dianne Hackbornd2835932010-12-13 16:28:46 -0800719 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800720 if (who.noDisplay) {
721 return null;
722 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800723
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700724 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700725 if (mService.getMostRecentTask() != tr && tr.intent != null &&
726 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700727 // If this task is being excluded from recents, we don't want to take
728 // the expense of capturing a thumbnail, since we will never show it.
729 return null;
730 }
731
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800732 Resources res = mService.mContext.getResources();
733 int w = mThumbnailWidth;
734 int h = mThumbnailHeight;
735 if (w < 0) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800736 if (SystemProperties.getBoolean("persist.recents.use_alternate", false)) {
737 mThumbnailWidth = w =
738 res.getDimensionPixelSize(com.android.internal.R.dimen.recents_thumbnail_width);
739 mThumbnailHeight = h =
740 res.getDimensionPixelSize(com.android.internal.R.dimen.recents_thumbnail_height);
741 } else {
742 mThumbnailWidth = w =
743 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
744 mThumbnailHeight = h =
745 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
746 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800747 }
748
749 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800750 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700751 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800752 || mLastScreenshotBitmap.getWidth() != w
753 || mLastScreenshotBitmap.getHeight() != h) {
754 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700755 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700756 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800757 }
758 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700759 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800760 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800761 }
762 return null;
763 }
764
Craig Mautnercf910b02013-04-23 11:23:27 -0700765 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800766 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800767 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700768 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700770 ActivityRecord prev = mResumedActivity;
771 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700772 Slog.e(TAG, "Trying to pause when nothing is resumed",
773 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700774 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700775 return;
776 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800777
778 if (mActivityContainer.mParentActivity == null) {
779 // Top level stack, not a child. Look for child stacks.
780 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
781 }
782
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700783 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
784 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700785 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800786 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700787 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700788 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
789 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700790 prev.state = ActivityState.PAUSING;
791 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700792 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700793 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
794 if (next == null || next.task != prev.task) {
795 prev.updateThumbnail(screenshotActivities(prev), null);
796 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700797 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798
799 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800800
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700801 if (prev.app != null && prev.app.thread != null) {
802 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
803 try {
804 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700805 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700806 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700807 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800808 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
809 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 } catch (Exception e) {
811 // Ignore exception, if process died other code will cleanup.
812 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800813 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700815 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 }
817 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800818 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700820 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 }
822
823 // If we are not going to sleep, we want to ensure the device is
824 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700825 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700826 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 }
828
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800829 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700830 // Have the window manager pause its key dispatching until the new
831 // activity has started. If we're pausing the activity just because
832 // the screen is being turned off and the UI is sleeping, don't interrupt
833 // key dispatch; the same activity will pick it up again on wakeup.
834 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800835 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 } else {
837 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
838 }
839
840 // Schedule a pause timeout in case the app doesn't respond.
841 // We don't give it much time because this directly impacts the
842 // responsiveness seen by the user.
843 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
844 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700845 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
847 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
848 } else {
849 // This activity failed to schedule the
850 // pause, so just treat it as being paused now.
851 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700852 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 }
854 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700855
Craig Mautnerd2328952013-03-05 12:46:26 -0800856 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800858 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700859
Craig Mautnerd2328952013-03-05 12:46:26 -0800860 final ActivityRecord r = isInStackLocked(token);
861 if (r != null) {
862 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
863 if (mPausingActivity == r) {
864 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
865 + (timeout ? " (due to timeout)" : " (pause complete)"));
866 r.state = ActivityState.PAUSED;
867 completePauseLocked();
868 } else {
869 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
870 r.userId, System.identityHashCode(r), r.shortComponentName,
871 mPausingActivity != null
872 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700873 }
874 }
875 }
876
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700877 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
878 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700879 if (r.state != ActivityState.STOPPING) {
880 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
881 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
882 return;
883 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700884 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700885 if (icicle != null) {
886 // If icicle is null, this is happening due to a timeout, so we
887 // haven't really saved the state.
888 r.icicle = icicle;
889 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800890 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700891 r.updateThumbnail(thumbnail, description);
892 }
893 if (!r.stopped) {
894 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
895 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
896 r.stopped = true;
897 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700898 if (r.finishing) {
899 r.clearOptionsLocked();
900 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700901 if (r.configDestroy) {
902 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700903 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700904 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700905 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800906 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700907 }
908 }
909 }
910
Craig Mautnerc8143c62013-09-03 12:15:57 -0700911 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800912 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800914
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800915 if (prev != null) {
916 if (prev.finishing) {
917 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700918 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800919 } else if (prev.app != null) {
920 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
921 if (prev.waitingVisible) {
922 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700923 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800924 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
925 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800926 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800927 if (prev.configDestroy) {
928 // The previous is being paused because the configuration
929 // is changing, which means it is actually stopping...
930 // To juggle the fact that we are also starting a new
931 // instance right now, we need to first completely stop
932 // the current instance before starting the new one.
933 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
934 destroyActivityLocked(prev, true, false, "pause-config");
935 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700936 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700937 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
938 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800939 // If we already have a few activities waiting to stop,
940 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700941 // them out. Or if r is the last of activity of the last task the stack
942 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800943 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700944 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800945 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700946 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800947 }
948 }
949 } else {
950 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
951 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800952 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800953 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700954 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800955
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700956 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700957 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700958 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700960 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700961 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700962 if (top == null || (prev != null && top != prev)) {
963 // If there are no more activities available to run,
964 // do resume anyway to start something. Also if the top
965 // activity on the stack is not the just paused activity,
966 // we need to go ahead and resume it to ensure we complete
967 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700968 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700969 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700970 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800971
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800972 if (prev != null) {
973 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700974
Craig Mautner525f3d92013-05-07 14:01:50 -0700975 if (prev.app != null && prev.cpuTimeAtResume > 0
976 && mService.mBatteryStatsService.isOnBattery()) {
977 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700978 synchronized (mService.mProcessCpuThread) {
979 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700980 - prev.cpuTimeAtResume;
981 }
982 if (diff > 0) {
983 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
984 synchronized (bsi) {
985 BatteryStatsImpl.Uid.Proc ps =
986 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700987 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700988 if (ps != null) {
989 ps.addForegroundTimeLocked(diff);
990 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700991 }
992 }
993 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700994 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700995 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996 }
997
998 /**
999 * Once we know that we have asked an application to put an activity in
1000 * the resumed state (either by launching it or explicitly telling it),
1001 * this function updates the rest of our state to match that fact.
1002 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001003 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001004 next.idle = false;
1005 next.results = null;
1006 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -07001007 if (next.nowVisible) {
1008 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1009 mStackSupervisor.dismissKeyguard();
1010 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001011
1012 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001013 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001015 mStackSupervisor.reportResumedActivityLocked(next);
1016
1017 next.resumeKeyDispatchingLocked();
1018 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001019
1020 // Mark the point when the activity is resuming
1021 // TODO: To be more accurate, the mark should be before the onCreate,
1022 // not after the onResume. But for subsequent starts, onResume is fine.
1023 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -07001024 synchronized (mService.mProcessCpuThread) {
1025 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
1027 } else {
1028 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1029 }
1030 }
1031
Craig Mautner580ea812013-04-25 12:58:38 -07001032 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001033 * Determine if home should be visible below the passed record.
1034 * @param record activity we are querying for.
1035 * @return true if home is visible below the passed activity, false otherwise.
1036 */
1037 boolean isActivityOverHome(ActivityRecord record) {
1038 // Start at record and go down, look for either home or a visible fullscreen activity.
1039 final TaskRecord recordTask = record.task;
1040 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
1041 TaskRecord task = mTaskHistory.get(taskNdx);
1042 final ArrayList<ActivityRecord> activities = task.mActivities;
1043 final int startNdx =
1044 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
1045 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
1046 final ActivityRecord r = activities.get(activityNdx);
1047 if (r.isHomeActivity()) {
1048 return true;
1049 }
Craig Mautner76be9d22013-11-04 16:01:22 -08001050 if (!r.finishing && r.fullscreen) {
1051 // Passed activity is over a fullscreen activity.
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001052 return false;
1053 }
1054 }
1055 if (task.mOnTopOfHome) {
1056 // Got to the bottom of a task on top of home without finding a visible fullscreen
1057 // activity. Home is visible.
1058 return true;
1059 }
1060 }
1061 // Got to the bottom of this stack and still don't know. If this is over the home stack
1062 // then record is over home. May not work if we ever get more than two layers.
1063 return mStackSupervisor.isFrontStack(this);
1064 }
1065
1066 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001067 * Version of ensureActivitiesVisible that can easily be called anywhere.
1068 */
1069 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1070 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1071 }
1072
1073 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1074 boolean forceHomeShown) {
1075 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001076 return r != null &&
1077 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001078 }
1079
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001080 /**
1081 * Make sure that all activities that need to be visible (that is, they
1082 * currently can be seen by the user) actually are.
1083 */
Craig Mautner580ea812013-04-25 12:58:38 -07001084 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1085 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001086 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 TAG, "ensureActivitiesVisible behind " + top
1088 + " configChanges=0x" + Integer.toHexString(configChanges));
1089
Craig Mautner5eda9b32013-07-02 11:58:16 -07001090 if (mTranslucentActivityWaiting != top) {
1091 mUndrawnActivitiesBelowTopTranslucent.clear();
1092 if (mTranslucentActivityWaiting != null) {
1093 // Call the callback with a timeout indication.
1094 notifyActivityDrawnLocked(null);
1095 mTranslucentActivityWaiting = null;
1096 }
1097 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1098 }
1099
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001100 // If the top activity is not fullscreen, then we need to
1101 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001102 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001103 boolean showHomeBehindStack = false;
1104 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1105 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001106 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001107 final TaskRecord task = mTaskHistory.get(taskNdx);
1108 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001109 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1110 final ActivityRecord r = activities.get(activityNdx);
1111 if (r.finishing) {
1112 continue;
1113 }
1114 if (aboveTop && r != top) {
1115 continue;
1116 }
1117 aboveTop = false;
1118 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001119 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001120 TAG, "Make visible? " + r + " finishing=" + r.finishing
1121 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001122
Craig Mautnerd44711d2013-02-23 11:24:36 -08001123 final boolean doThisProcess = onlyThisProcess == null
1124 || onlyThisProcess.equals(r.processName);
1125
1126 // First: if this is not the current activity being started, make
1127 // sure it matches the current configuration.
1128 if (r != starting && doThisProcess) {
1129 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001130 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001131
1132 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001133 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001134 // This activity needs to be visible, but isn't even
1135 // running... get it started, but don't resume it
1136 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001137 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001138 if (r != starting) {
1139 r.startFreezingScreenLocked(r.app, configChanges);
1140 }
1141 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001142 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001143 TAG, "Starting and making visible: " + r);
louis_changd5c91ec2014-01-28 18:38:06 +08001144 r.visible = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001145 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001146 }
1147 if (r != starting) {
George Mount2c92c972014-03-20 09:38:23 -07001148 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001149 }
1150 }
1151
1152 } else if (r.visible) {
1153 // If this activity is already visible, then there is nothing
1154 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001155 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001157
1158 } else if (onlyThisProcess == null) {
1159 // This activity is not currently visible, but is running.
1160 // Tell it to become visible.
1161 r.visible = true;
1162 if (r.state != ActivityState.RESUMED && r != starting) {
1163 // If this activity is paused, tell it
1164 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001165 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001166 TAG, "Making visible and scheduling visibility: " + r);
1167 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001168 if (mTranslucentActivityWaiting != null) {
1169 mUndrawnActivitiesBelowTopTranslucent.add(r);
1170 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001171 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001172 r.sleeping = false;
1173 r.app.pendingUiClean = true;
1174 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1175 r.stopFreezingScreenLocked(false);
1176 } catch (Exception e) {
1177 // Just skip on any failure; we'll make it
1178 // visible when it next restarts.
1179 Slog.w(TAG, "Exception thrown making visibile: "
1180 + r.intent.getComponent(), e);
1181 }
1182 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184
Craig Mautnerd44711d2013-02-23 11:24:36 -08001185 // Aggregate current change flags.
1186 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187
Craig Mautnerd44711d2013-02-23 11:24:36 -08001188 if (r.fullscreen) {
1189 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001190 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1191 behindFullscreen = true;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001192 } else if (isActivityOverHome(r)) {
1193 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1194 showHomeBehindStack = true;
riddle_hsu446ef1d2014-01-09 20:24:34 +08001195 behindFullscreen = !isHomeStack() && r.frontOfTask && task.mOnTopOfHome;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001196 }
1197 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001198 if (DEBUG_VISBILITY) Slog.v(
1199 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1200 + " state=" + r.state
1201 + " behindFullscreen=" + behindFullscreen);
1202 // Now for any activities that aren't visible to the user, make
1203 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001204 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001205 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001206 r.visible = false;
1207 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001208 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001209 switch (r.state) {
1210 case STOPPING:
1211 case STOPPED:
1212 if (r.app != null && r.app.thread != null) {
1213 if (DEBUG_VISBILITY) Slog.v(
1214 TAG, "Scheduling invisibility: " + r);
1215 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1216 }
1217 break;
1218
1219 case INITIALIZING:
1220 case RESUMED:
1221 case PAUSING:
1222 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001223 // This case created for transitioning activities from
1224 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001225 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1226 mStackSupervisor.mStoppingActivities.add(r);
1227 }
1228 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001229 break;
1230
1231 default:
1232 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001233 }
1234 } catch (Exception e) {
1235 // Just skip on any failure; we'll make it
1236 // visible when it next restarts.
1237 Slog.w(TAG, "Exception thrown making hidden: "
1238 + r.intent.getComponent(), e);
1239 }
1240 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001241 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001242 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001243 }
1244 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001245 }
Craig Mautner580ea812013-04-25 12:58:38 -07001246 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001247 }
Craig Mautner58547802013-03-05 08:23:53 -08001248
Craig Mautner5eda9b32013-07-02 11:58:16 -07001249 void convertToTranslucent(ActivityRecord r) {
1250 mTranslucentActivityWaiting = r;
1251 mUndrawnActivitiesBelowTopTranslucent.clear();
1252 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1253 }
1254
1255 /**
1256 * Called as activities below the top translucent activity are redrawn. When the last one is
1257 * redrawn notify the top activity by calling
1258 * {@link Activity#onTranslucentConversionComplete}.
1259 *
1260 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1261 * occurred and the activity will be notified immediately.
1262 */
1263 void notifyActivityDrawnLocked(ActivityRecord r) {
1264 if ((r == null)
1265 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1266 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1267 // The last undrawn activity below the top has just been drawn. If there is an
1268 // opaque activity at the top, notify it that it can become translucent safely now.
1269 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1270 mTranslucentActivityWaiting = null;
1271 mUndrawnActivitiesBelowTopTranslucent.clear();
1272 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1273
Craig Mautner71dd1b62014-02-18 15:48:52 -08001274 if (waitingActivity != null) {
1275 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1276 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1277 try {
1278 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1279 waitingActivity.appToken, r != null);
1280 } catch (RemoteException e) {
1281 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001282 }
1283 }
1284 }
1285 }
1286
Craig Mautnera61bc652013-10-28 15:43:18 -07001287 /** If any activities below the top running one are in the INITIALIZING state and they have a
1288 * starting window displayed then remove that starting window. It is possible that the activity
1289 * in this state will never resumed in which case that starting window will be orphaned. */
1290 void cancelInitializingActivities() {
1291 final ActivityRecord topActivity = topRunningActivityLocked(null);
1292 boolean aboveTop = true;
1293 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1294 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1295 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1296 final ActivityRecord r = activities.get(activityNdx);
1297 if (aboveTop) {
1298 if (r == topActivity) {
1299 aboveTop = false;
1300 }
1301 continue;
1302 }
1303
1304 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1305 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1306 r.mStartingWindowShown = false;
1307 mWindowManager.removeAppStartingWindow(r.appToken);
1308 }
1309 }
1310 }
1311 }
1312
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 /**
1314 * Ensure that the top activity in the stack is resumed.
1315 *
1316 * @param prev The previously resumed activity, for when in the process
1317 * of pausing; can be null to call from elsewhere.
1318 *
1319 * @return Returns true if something is being resumed, or false if
1320 * nothing happened.
1321 */
1322 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001323 return resumeTopActivityLocked(prev, null);
1324 }
1325
1326 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001327 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1328
Craig Mautnerdf88d732014-01-27 09:21:32 -08001329 ActivityRecord parent = mActivityContainer.mParentActivity;
1330 if (parent != null && parent.state != ActivityState.RESUMED) {
1331 // Do not resume this stack if its parent is not resumed.
1332 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1333 return false;
1334 }
1335
Craig Mautnera61bc652013-10-28 15:43:18 -07001336 cancelInitializingActivities();
1337
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001338 // Find the first activity that is not finishing.
1339 ActivityRecord next = topRunningActivityLocked(null);
1340
1341 // Remember how we'll process this pause/resume situation, and ensure
1342 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001343 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1344 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345
1346 if (next == null) {
1347 // There are no more activities! Let's just start up the
1348 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001349 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001350 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001351 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001352 // Only resume home if on home display
1353 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001354 }
1355
1356 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001357
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001358 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001359 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1360 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001361 // Make sure we have executed any pending transitions, since there
1362 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001363 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001365 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001366 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001367 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001368 return false;
1369 }
1370
Craig Mautner525f3d92013-05-07 14:01:50 -07001371 final TaskRecord nextTask = next.task;
1372 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001373 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001374 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001375 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001376 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001377 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001378 // This task is going away but it was supposed to return to the home task.
1379 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001380 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001381 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001382 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001383 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1384 "resumeTopActivityLocked: Launching home next");
1385 // Only resume home if on home display
1386 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001387 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001388 }
1389
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 // If we are sleeping, and there is no resumed activity, and the top
1391 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001392 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001393 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001394 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001395 // Make sure we have executed any pending transitions, since there
1396 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001397 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001399 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001400 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001401 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 return false;
1403 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001404
1405 // Make sure that the user who owns this activity is started. If not,
1406 // we will just leave it as is because someone should be bringing
1407 // another user's activities to the top of the stack.
1408 if (mService.mStartedUsers.get(next.userId) == null) {
1409 Slog.w(TAG, "Skipping resume of top activity " + next
1410 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001411 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001412 return false;
1413 }
1414
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001415 // The activity may be waiting for stop, but that is no longer
1416 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001417 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001418 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001419 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001420 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421
Dianne Hackborn84375872012-06-01 19:03:50 -07001422 next.updateOptionsLocked(options);
1423
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1425
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001426 // If we are currently pausing an activity, then don't do anything
1427 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001428 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001429 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1430 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001431 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001432 return false;
1433 }
1434
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001435 // Okay we are now going to start a switch, to 'next'. We may first
1436 // have to pause the current activity, but this is an important point
1437 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001438 // XXX "App Redirected" dialog is getting too many false positives
1439 // at this point, so turn off for now.
1440 if (false) {
1441 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1442 long now = SystemClock.uptimeMillis();
1443 final boolean inTime = mLastStartedActivity.startTime != 0
1444 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1445 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1446 final int nextUid = next.info.applicationInfo.uid;
1447 if (inTime && lastUid != nextUid
1448 && lastUid != next.launchedFromUid
1449 && mService.checkPermission(
1450 android.Manifest.permission.STOP_APP_SWITCHES,
1451 -1, next.launchedFromUid)
1452 != PackageManager.PERMISSION_GRANTED) {
1453 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1454 } else {
1455 next.startTime = now;
1456 mLastStartedActivity = next;
1457 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001458 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001459 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001460 mLastStartedActivity = next;
1461 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001462 }
Craig Mautner58547802013-03-05 08:23:53 -08001463
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 // We need to start pausing the current activity so the top one
1465 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001466 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001467 if (mResumedActivity != null) {
1468 pausing = true;
1469 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001470 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001471 }
1472 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001473 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1474 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001475 // At this point we want to put the upcoming activity's process
1476 // at the top of the LRU list, since we know we will be needing it
1477 // very soon and it would be a waste to let it get killed if it
1478 // happens to be sitting towards the end.
1479 if (next.app != null && next.app.thread != null) {
1480 // No reason to do full oom adj update here; we'll let that
1481 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001482 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001483 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001484 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001485 return true;
1486 }
1487
Christopher Tated3f175c2012-06-14 14:16:54 -07001488 // If the most recent activity was noHistory but was only stopped rather
1489 // than stopped+finished because the device went to sleep, we need to make
1490 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001491 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1492 !mLastNoHistoryActivity.finishing) {
1493 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1494 " on new resume");
1495 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1496 null, "no-history", false);
1497 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001498 }
1499
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001500 if (prev != null && prev != next) {
1501 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1502 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001503 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504 if (DEBUG_SWITCH) Slog.v(
1505 TAG, "Resuming top, waiting visible to hide: " + prev);
1506 } else {
1507 // The next activity is already visible, so hide the previous
1508 // activity's windows right now so we can show the new one ASAP.
1509 // We only do this if the previous is finishing, which should mean
1510 // it is on top of the one being resumed so hiding it quickly
1511 // is good. Otherwise, we want to do the normal route of allowing
1512 // the resumed activity to be shown so we can decide if the
1513 // previous should actually be hidden depending on whether the
1514 // new one is found to be full-screen or not.
1515 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001516 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1518 + prev + ", waitingVisible="
1519 + (prev != null ? prev.waitingVisible : null)
1520 + ", nowVisible=" + next.nowVisible);
1521 } else {
1522 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1523 + prev + ", waitingVisible="
1524 + (prev != null ? prev.waitingVisible : null)
1525 + ", nowVisible=" + next.nowVisible);
1526 }
1527 }
1528 }
1529
Dianne Hackborne7f97212011-02-24 14:40:20 -08001530 // Launching this app's activity, make sure the app is no longer
1531 // considered stopped.
1532 try {
1533 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001534 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001535 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001536 } catch (IllegalArgumentException e) {
1537 Slog.w(TAG, "Failed trying to unstop package "
1538 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001539 }
1540
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541 // We are starting up the next activity, so tell the window manager
1542 // that the previous one will be hidden soon. This way it can know
1543 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001544 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 if (prev != null) {
1546 if (prev.finishing) {
1547 if (DEBUG_TRANSITION) Slog.v(TAG,
1548 "Prepare close transition: prev=" + prev);
1549 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001550 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001551 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001552 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001553 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001554 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1555 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001557 mWindowManager.setAppWillBeHidden(prev.appToken);
1558 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001560 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001562 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001563 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001564 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001565 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001566 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1567 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 }
1569 }
1570 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001571 mWindowManager.setAppWillBeHidden(prev.appToken);
1572 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001573 }
Craig Mautner967212c2013-04-13 21:10:58 -07001574 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001575 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001577 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001578 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001580 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 }
1582 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001583
1584 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001585 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001586 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1587 if (opts != null) {
1588 resumeAnimOptions = opts.toBundle();
1589 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001590 next.applyOptionsLocked();
1591 } else {
1592 next.clearOptionsLocked();
1593 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001594
Craig Mautnercf910b02013-04-23 11:23:27 -07001595 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 if (next.app != null && next.app.thread != null) {
1597 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1598
1599 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001600 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001602 // schedule launch ticks to collect information about slow apps.
1603 next.startLaunchTickingLocked();
1604
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001605 ActivityRecord lastResumedActivity =
1606 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 ActivityState lastState = next.state;
1608
1609 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001610
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001611 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 next.state = ActivityState.RESUMED;
1613 mResumedActivity = next;
1614 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001615 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001616 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001617 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001618 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619
1620 // Have the window manager re-evaluate the orientation of
1621 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001622 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001623 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001624 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001625 mService.mConfiguration,
1626 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1627 if (config != null) {
1628 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001629 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001630 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001631 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001632
Craig Mautner525f3d92013-05-07 14:01:50 -07001633 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 // The configuration update wasn't able to keep the existing
1635 // instance of the activity, and instead started a new one.
1636 // We should be all done, but let's just make sure our activity
1637 // is still at the top and schedule another run if something
1638 // weird happened.
1639 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001640 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001641 "Activity config changed during resume: " + next
1642 + ", new next: " + nextNext);
1643 if (nextNext != next) {
1644 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001645 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001646 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001647 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001648 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001649 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001650 return true;
1651 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001652 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001653 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001654 }
Craig Mautner58547802013-03-05 08:23:53 -08001655
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001656 try {
1657 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001658 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 if (a != null) {
1660 final int N = a.size();
1661 if (!next.finishing && N > 0) {
1662 if (DEBUG_RESULTS) Slog.v(
1663 TAG, "Delivering results to " + next
1664 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001665 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001666 }
1667 }
1668
1669 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001670 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 }
1672
1673 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001674 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001676
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001677 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001678 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001679 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001680 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001681 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001682 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001683 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001684
Craig Mautner0eea92c2013-05-16 13:35:39 -07001685 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001686
Craig Mautnerac6f8432013-07-17 13:24:59 -07001687 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001688 } catch (Exception e) {
1689 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001690 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1691 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001692 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001693 if (lastStack != null) {
1694 lastStack.mResumedActivity = lastResumedActivity;
1695 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 Slog.i(TAG, "Restarting because process died: " + next);
1697 if (!next.hasBeenLaunched) {
1698 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001699 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1700 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001701 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001702 next.appToken, next.packageName, next.theme,
1703 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001704 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1705 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001706 }
George Mount2c92c972014-03-20 09:38:23 -07001707 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001708 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001709 return true;
1710 }
1711
1712 // From this point on, if something goes wrong there is no way
1713 // to recover the activity.
1714 try {
1715 next.visible = true;
1716 completeResumeLocked(next);
1717 } catch (Exception e) {
1718 // If any exception gets thrown, toss away this
1719 // activity and try the next one.
1720 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001721 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001722 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001723 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 return true;
1725 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 next.stopped = false;
1727
1728 } else {
1729 // Whoops, need to restart this activity!
1730 if (!next.hasBeenLaunched) {
1731 next.hasBeenLaunched = true;
1732 } else {
1733 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001734 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001735 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001736 mService.compatibilityInfoForPackageLocked(
1737 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001739 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001740 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001741 }
1742 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1743 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001744 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001745 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001746 }
1747
Craig Mautnercf910b02013-04-23 11:23:27 -07001748 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 return true;
1750 }
1751
Craig Mautnerac6f8432013-07-17 13:24:59 -07001752 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001753 // If this is being moved to the top by another activity or being launched from the home
1754 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001755 if (isOnHomeDisplay()) {
1756 ActivityStack lastStack = mStackSupervisor.getLastStack();
1757 final boolean fromHome = lastStack.isHomeStack();
1758 if (!isHomeStack() && (fromHome || topTask() != task)) {
1759 task.mOnTopOfHome = fromHome;
1760 }
1761 } else {
1762 task.mOnTopOfHome = false;
Craig Mautner9c85c202013-10-04 20:11:26 -07001763 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001764
Craig Mautnerac6f8432013-07-17 13:24:59 -07001765 mTaskHistory.remove(task);
1766 // Now put task at top.
1767 int stackNdx = mTaskHistory.size();
Amith Yamasani734983f2014-03-04 16:48:05 -08001768 if (!isRelatedToOrCurrentUserLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001769 // Put non-current user tasks below current user tasks.
1770 while (--stackNdx >= 0) {
Amith Yamasani734983f2014-03-04 16:48:05 -08001771 if (!isRelatedToOrCurrentUserLocked(mTaskHistory.get(stackNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001772 break;
1773 }
1774 }
1775 ++stackNdx;
1776 }
1777 mTaskHistory.add(stackNdx, task);
1778 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001779
Craig Mautner8849a5e2013-04-02 16:41:03 -07001780 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001781 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001782 TaskRecord rTask = r.task;
1783 final int taskId = rTask.taskId;
1784 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001785 // Last activity in task had been removed or ActivityManagerService is reusing task.
1786 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001787 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001788 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001789 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001790 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001791 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001792 if (!newTask) {
1793 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001794 boolean startIt = true;
1795 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1796 task = mTaskHistory.get(taskNdx);
1797 if (task == r.task) {
1798 // Here it is! Now, if this is not yet visible to the
1799 // user, then just add it without starting; it will
1800 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001801 if (!startIt) {
1802 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1803 + task, new RuntimeException("here").fillInStackTrace());
1804 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001805 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001806 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1807 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001808 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001809 r.userId, r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001811 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001813 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 return;
1815 }
1816 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001817 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818 startIt = false;
1819 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 }
1821 }
1822
1823 // Place a new activity at top of stack, so it is next to interact
1824 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001825
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001826 // If we are not placing the new activity frontmost, we do not want
1827 // to deliver the onUserLeaving callback to the actual frontmost
1828 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001829 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001830 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001831 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1832 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001833 }
Craig Mautner70a86932013-02-28 22:37:44 -08001834
1835 task = r.task;
1836
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001838 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001839 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001840 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001841 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001842
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001843 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001844 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001845 // We want to show the starting preview window if we are
1846 // switching to a new task, or the next activity's process is
1847 // not currently running.
1848 boolean showStartingIcon = newTask;
1849 ProcessRecord proc = r.app;
1850 if (proc == null) {
1851 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1852 }
1853 if (proc == null || proc.thread == null) {
1854 showStartingIcon = true;
1855 }
1856 if (DEBUG_TRANSITION) Slog.v(TAG,
1857 "Prepare open transition: starting " + r);
1858 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001859 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001862 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001863 ? AppTransition.TRANSIT_TASK_OPEN
1864 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001865 mNoAnimActivities.remove(r);
1866 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001867 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001868 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001869 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001870 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1871 r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001872 boolean doShow = true;
1873 if (newTask) {
1874 // Even though this activity is starting fresh, we still need
1875 // to reset it to make sure we apply affinities to move any
1876 // existing activities from other tasks in to it.
1877 // If the caller has requested that the target task be
1878 // reset, then do so.
1879 if ((r.intent.getFlags()
Craig Mautnerd00f4742014-03-12 14:17:26 -07001880 & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881 resetTaskIfNeededLocked(r, r);
1882 doShow = topRunningNonDelayedActivityLocked(null) == r;
1883 }
1884 }
1885 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1886 // Figure out if we are transitioning from another activity that is
1887 // "has the same starting icon" as the next one. This allows the
1888 // window manager to keep the previous window it had previously
1889 // created, if it still had one.
1890 ActivityRecord prev = mResumedActivity;
1891 if (prev != null) {
1892 // We don't want to reuse the previous starting preview if:
1893 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001894 if (prev.task != r.task) {
1895 prev = null;
1896 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001898 else if (prev.nowVisible) {
1899 prev = null;
1900 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001902 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001903 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001904 mService.compatibilityInfoForPackageLocked(
1905 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001906 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001907 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07001908 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 }
1910 } else {
1911 // If this is the first activity, don't do any fancy animations,
1912 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001913 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001914 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001915 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1916 r.info.configChanges);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001917 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001918 }
1919 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001920 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001921 }
1922
1923 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001924 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001925 }
1926 }
1927
Dianne Hackbornbe707852011-11-11 14:32:10 -08001928 final void validateAppTokensLocked() {
1929 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001930 mValidateAppTokens.ensureCapacity(numActivities());
1931 final int numTasks = mTaskHistory.size();
1932 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1933 TaskRecord task = mTaskHistory.get(taskNdx);
1934 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001935 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001936 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001937 }
Craig Mautner000f0022013-02-26 15:04:29 -08001938 TaskGroup group = new TaskGroup();
1939 group.taskId = task.taskId;
1940 mValidateAppTokens.add(group);
1941 final int numActivities = activities.size();
1942 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1943 final ActivityRecord r = activities.get(activityNdx);
1944 group.tokens.add(r.appToken);
1945 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001946 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001947 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001948 }
1949
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001950 /**
1951 * Perform a reset of the given task, if needed as part of launching it.
1952 * Returns the new HistoryRecord at the top of the task.
1953 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001954 /**
1955 * Helper method for #resetTaskIfNeededLocked.
1956 * We are inside of the task being reset... we'll either finish this activity, push it out
1957 * for another task, or leave it as-is.
1958 * @param task The task containing the Activity (taskTop) that might be reset.
1959 * @param forceReset
1960 * @return An ActivityOptions that needs to be processed.
1961 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001962 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001963 ActivityOptions topOptions = null;
1964
1965 int replyChainEnd = -1;
1966 boolean canMoveOptions = true;
1967
1968 // We only do this for activities that are not the root of the task (since if we finish
1969 // the root, we may no longer have the task!).
1970 final ArrayList<ActivityRecord> activities = task.mActivities;
1971 final int numActivities = activities.size();
1972 for (int i = numActivities - 1; i > 0; --i ) {
1973 ActivityRecord target = activities.get(i);
1974
1975 final int flags = target.info.flags;
1976 final boolean finishOnTaskLaunch =
1977 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1978 final boolean allowTaskReparenting =
1979 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1980 final boolean clearWhenTaskReset =
1981 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1982
1983 if (!finishOnTaskLaunch
1984 && !clearWhenTaskReset
1985 && target.resultTo != null) {
1986 // If this activity is sending a reply to a previous
1987 // activity, we can't do anything with it now until
1988 // we reach the start of the reply chain.
1989 // XXX note that we are assuming the result is always
1990 // to the previous activity, which is almost always
1991 // the case but we really shouldn't count on.
1992 if (replyChainEnd < 0) {
1993 replyChainEnd = i;
1994 }
1995 } else if (!finishOnTaskLaunch
1996 && !clearWhenTaskReset
1997 && allowTaskReparenting
1998 && target.taskAffinity != null
1999 && !target.taskAffinity.equals(task.affinity)) {
2000 // If this activity has an affinity for another
2001 // task, then we need to move it out of here. We will
2002 // move it as far out of the way as possible, to the
2003 // bottom of the activity stack. This also keeps it
2004 // correctly ordered with any activities we previously
2005 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002006 final ThumbnailHolder newThumbHolder;
2007 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002008 final ActivityRecord bottom =
2009 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002010 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002011 if (bottom != null && target.taskAffinity != null
2012 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002013 // If the activity currently at the bottom has the
2014 // same task affinity as the one we are moving,
2015 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002016 targetTask = bottom.task;
2017 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002018 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002019 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002020 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002021 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
2022 null, false);
2023 newThumbHolder = targetTask;
2024 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002025 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2026 + " out to new task " + target.task);
2027 }
2028
Craig Mautnerd00f4742014-03-12 14:17:26 -07002029 target.thumbHolder = newThumbHolder;
Craig Mautner329f4122013-11-07 09:10:42 -08002030
Craig Mautnere3a74d52013-02-22 14:14:58 -08002031 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002032 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002033
Craig Mautner525f3d92013-05-07 14:01:50 -07002034 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002035 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2036 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002037 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002038 if (p.finishing) {
2039 continue;
2040 }
2041
Craig Mautner525f3d92013-05-07 14:01:50 -07002042 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002043 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002044 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002045 topOptions = p.takeOptionsLocked();
2046 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002047 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002048 }
2049 }
2050 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002051 + task + " adding to task=" + targetTask
2052 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002053 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2054 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002055 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002056 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002057
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002058 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002059 }
2060
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002061 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002062 if (VALIDATE_TOKENS) {
2063 validateAppTokensLocked();
2064 }
2065
2066 replyChainEnd = -1;
2067 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2068 // If the activity should just be removed -- either
2069 // because it asks for it, or the task should be
2070 // cleared -- then finish it and anything that is
2071 // part of its reply chain.
2072 int end;
2073 if (clearWhenTaskReset) {
2074 // In this case, we want to finish this activity
2075 // and everything above it, so be sneaky and pretend
2076 // like these are all in the reply chain.
2077 end = numActivities - 1;
2078 } else if (replyChainEnd < 0) {
2079 end = i;
2080 } else {
2081 end = replyChainEnd;
2082 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002083 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002084 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002085 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002086 if (p.finishing) {
2087 continue;
2088 }
2089 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002090 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002091 topOptions = p.takeOptionsLocked();
2092 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002093 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002094 }
2095 }
Craig Mautner58547802013-03-05 08:23:53 -08002096 if (DEBUG_TASKS) Slog.w(TAG,
2097 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002098 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002099 end--;
2100 srcPos--;
2101 }
2102 }
2103 replyChainEnd = -1;
2104 } else {
2105 // If we were in the middle of a chain, well the
2106 // activity that started it all doesn't want anything
2107 // special, so leave it all as-is.
2108 replyChainEnd = -1;
2109 }
2110 }
2111
2112 return topOptions;
2113 }
2114
2115 /**
2116 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2117 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2118 * @param affinityTask The task we are looking for an affinity to.
2119 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2120 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2121 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2122 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002123 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002124 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002125 int replyChainEnd = -1;
2126 final int taskId = task.taskId;
2127 final String taskAffinity = task.affinity;
2128
2129 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2130 final int numActivities = activities.size();
2131 // Do not operate on the root Activity.
2132 for (int i = numActivities - 1; i > 0; --i) {
2133 ActivityRecord target = activities.get(i);
2134
2135 final int flags = target.info.flags;
2136 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2137 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2138
2139 if (target.resultTo != null) {
2140 // If this activity is sending a reply to a previous
2141 // activity, we can't do anything with it now until
2142 // we reach the start of the reply chain.
2143 // XXX note that we are assuming the result is always
2144 // to the previous activity, which is almost always
2145 // the case but we really shouldn't count on.
2146 if (replyChainEnd < 0) {
2147 replyChainEnd = i;
2148 }
2149 } else if (topTaskIsHigher
2150 && allowTaskReparenting
2151 && taskAffinity != null
2152 && taskAffinity.equals(target.taskAffinity)) {
2153 // This activity has an affinity for our task. Either remove it if we are
2154 // clearing or move it over to our task. Note that
2155 // we currently punt on the case where we are resetting a
2156 // task that is not at the top but who has activities above
2157 // with an affinity to it... this is really not a normal
2158 // case, and we will need to later pull that task to the front
2159 // and usually at that point we will do the reset and pick
2160 // up those remaining activities. (This only happens if
2161 // someone starts an activity in a new task from an activity
2162 // in a task that is not currently on top.)
2163 if (forceReset || finishOnTaskLaunch) {
2164 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2165 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2166 for (int srcPos = start; srcPos >= i; --srcPos) {
2167 final ActivityRecord p = activities.get(srcPos);
2168 if (p.finishing) {
2169 continue;
2170 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002171 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002172 }
2173 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002174 if (taskInsertionPoint < 0) {
2175 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002176
Craig Mautner77878772013-03-04 19:46:24 -08002177 }
Craig Mautner77878772013-03-04 19:46:24 -08002178
2179 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2180 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2181 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2182 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002183 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002184 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002185 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002186
Craig Mautnere3a74d52013-02-22 14:14:58 -08002187 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2188 + " to stack at " + task,
2189 new RuntimeException("here").fillInStackTrace());
2190 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2191 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002192 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002193 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002194 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002195 if (VALIDATE_TOKENS) {
2196 validateAppTokensLocked();
2197 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002198
2199 // Now we've moved it in to place... but what if this is
2200 // a singleTop activity and we have put it on top of another
2201 // instance of the same activity? Then we drop the instance
2202 // below so it remains singleTop.
2203 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2204 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002205 int targetNdx = taskActivities.indexOf(target);
2206 if (targetNdx > 0) {
2207 ActivityRecord p = taskActivities.get(targetNdx - 1);
2208 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002209 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2210 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002211 }
2212 }
2213 }
2214 }
2215
2216 replyChainEnd = -1;
2217 }
2218 }
Craig Mautner77878772013-03-04 19:46:24 -08002219 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002220 }
2221
Craig Mautner8849a5e2013-04-02 16:41:03 -07002222 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002223 ActivityRecord newActivity) {
2224 boolean forceReset =
2225 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2226 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2227 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2228 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2229 forceReset = true;
2230 }
2231 }
2232
2233 final TaskRecord task = taskTop.task;
2234
2235 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2236 * for remaining tasks. Used for later tasks to reparent to task. */
2237 boolean taskFound = false;
2238
2239 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2240 ActivityOptions topOptions = null;
2241
Craig Mautner77878772013-03-04 19:46:24 -08002242 // Preserve the location for reparenting in the new task.
2243 int reparentInsertionPoint = -1;
2244
Craig Mautnere3a74d52013-02-22 14:14:58 -08002245 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2246 final TaskRecord targetTask = mTaskHistory.get(i);
2247
2248 if (targetTask == task) {
2249 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2250 taskFound = true;
2251 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002252 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2253 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002254 }
2255 }
2256
Craig Mautner70a86932013-02-28 22:37:44 -08002257 int taskNdx = mTaskHistory.indexOf(task);
2258 do {
2259 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2260 } while (taskTop == null && taskNdx >= 0);
2261
Craig Mautnere3a74d52013-02-22 14:14:58 -08002262 if (topOptions != null) {
2263 // If we got some ActivityOptions from an activity on top that
2264 // was removed from the task, propagate them to the new real top.
2265 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002266 taskTop.updateOptionsLocked(topOptions);
2267 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002268 topOptions.abort();
2269 }
2270 }
2271
2272 return taskTop;
2273 }
2274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002275 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2276 String resultWho, int requestCode, int resultCode, Intent data) {
2277
2278 if (callingUid > 0) {
2279 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002280 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002281 }
2282
2283 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2284 + " : who=" + resultWho + " req=" + requestCode
2285 + " res=" + resultCode + " data=" + data);
2286 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2287 try {
2288 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2289 list.add(new ResultInfo(resultWho, requestCode,
2290 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002291 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 return;
2293 } catch (Exception e) {
2294 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2295 }
2296 }
2297
2298 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2299 }
2300
Craig Mautner04f0b702013-10-22 12:31:01 -07002301 private void adjustFocusedActivityLocked(ActivityRecord r) {
2302 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2303 ActivityRecord next = topRunningActivityLocked(null);
2304 if (next != r) {
2305 final TaskRecord task = r.task;
2306 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2307 mStackSupervisor.moveHomeToTop();
2308 }
2309 }
2310 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
2311 }
2312 }
2313
Craig Mautnerf3333272013-04-22 10:55:53 -07002314 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002315 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2316 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2317 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2318 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002319 if (!mService.mSleeping) {
2320 if (DEBUG_STATES) {
2321 Slog.d(TAG, "no-history finish of " + r);
2322 }
2323 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002324 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002325 } else {
2326 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2327 + " on stop because we're just sleeping");
2328 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002330 }
2331
2332 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002333 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002334 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002335 try {
2336 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002337 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2338 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 r.state = ActivityState.STOPPING;
2340 if (DEBUG_VISBILITY) Slog.v(
2341 TAG, "Stopping visible=" + r.visible + " for " + r);
2342 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002343 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002344 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002345 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002346 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002347 r.setSleeping(true);
2348 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002349 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002350 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 } catch (Exception e) {
2352 // Maybe just ignore exceptions here... if the process
2353 // has crashed, our death notification will clean things
2354 // up.
2355 Slog.w(TAG, "Exception thrown during pause", e);
2356 // Just in case, assume it to be stopped.
2357 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002358 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002359 r.state = ActivityState.STOPPED;
2360 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002361 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002362 }
2363 }
2364 }
2365 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002366
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002367 /**
2368 * @return Returns true if the activity is being finished, false if for
2369 * some reason it is being left as-is.
2370 */
2371 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002372 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002373 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002374 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002375 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002376 + ", result=" + resultCode + ", data=" + resultData
2377 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002378 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 return false;
2380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381
Craig Mautnerd44711d2013-02-23 11:24:36 -08002382 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 return true;
2384 }
2385
Craig Mautnerd2328952013-03-05 12:46:26 -08002386 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002387 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2388 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2389 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2390 ActivityRecord r = activities.get(activityNdx);
2391 if (r.resultTo == self && r.requestCode == requestCode) {
2392 if ((r.resultWho == null && resultWho == null) ||
2393 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2394 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2395 false);
2396 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002397 }
2398 }
2399 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002400 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002401 }
2402
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002403 final void finishTopRunningActivityLocked(ProcessRecord app) {
2404 ActivityRecord r = topRunningActivityLocked(null);
2405 if (r != null && r.app == app) {
2406 // If the top running activity is from this crashing
2407 // process, then terminate it to avoid getting in a loop.
2408 Slog.w(TAG, " Force finishing activity "
2409 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002410 int taskNdx = mTaskHistory.indexOf(r.task);
2411 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002412 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002413 // Also terminate any activities below it that aren't yet
2414 // stopped, to avoid a situation where one will get
2415 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002416 --activityNdx;
2417 if (activityNdx < 0) {
2418 do {
2419 --taskNdx;
2420 if (taskNdx < 0) {
2421 break;
2422 }
2423 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2424 } while (activityNdx < 0);
2425 }
2426 if (activityNdx >= 0) {
2427 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002428 if (r.state == ActivityState.RESUMED
2429 || r.state == ActivityState.PAUSING
2430 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002431 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002432 Slog.w(TAG, " Force finishing activity "
2433 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002434 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002435 }
2436 }
2437 }
2438 }
2439 }
2440
Craig Mautnerd2328952013-03-05 12:46:26 -08002441 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002442 ArrayList<ActivityRecord> activities = r.task.mActivities;
2443 for (int index = activities.indexOf(r); index >= 0; --index) {
2444 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002445 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002446 break;
2447 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002448 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002449 }
2450 return true;
2451 }
2452
Dianne Hackborn5c607432012-02-28 14:44:19 -08002453 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2454 // send the result
2455 ActivityRecord resultTo = r.resultTo;
2456 if (resultTo != null) {
2457 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2458 + " who=" + r.resultWho + " req=" + r.requestCode
2459 + " res=" + resultCode + " data=" + resultData);
2460 if (r.info.applicationInfo.uid > 0) {
2461 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2462 resultTo.packageName, resultData,
2463 resultTo.getUriPermissionsLocked());
2464 }
2465 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2466 resultData);
2467 r.resultTo = null;
2468 }
2469 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2470
2471 // Make sure this HistoryRecord is not holding on to other resources,
2472 // because clients have remote IPC references to this object so we
2473 // can't assume that will go away and want to avoid circular IPC refs.
2474 r.results = null;
2475 r.pendingResults = null;
2476 r.newIntents = null;
2477 r.icicle = null;
2478 }
2479
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002480 /**
2481 * @return Returns true if this activity has been removed from the history
2482 * list, or false if it is still in the list and will be removed later.
2483 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002484 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2485 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002486 if (r.finishing) {
2487 Slog.w(TAG, "Duplicate finish request for " + r);
2488 return false;
2489 }
2490
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002491 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002492 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002493 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002494 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002495 task.taskId, r.shortComponentName, reason);
2496 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002497 final int index = activities.indexOf(r);
2498 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002499 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002500 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002501 // If the caller asked that this activity (and all above it)
2502 // be cleared when the task is reset, don't lose that information,
2503 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002504 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002505 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 }
2507 }
2508
2509 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002510
2511 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002512
Dianne Hackborn5c607432012-02-28 14:44:19 -08002513 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002514
Craig Mautnerc8143c62013-09-03 12:15:57 -07002515 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516 // There are clients waiting to receive thumbnails so, in case
2517 // this is an activity that someone is waiting for, add it
2518 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002519 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002520 }
2521
Craig Mautnerde4ef022013-04-07 19:01:33 -07002522 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002523 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002524 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002525 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002526 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002527 ? AppTransition.TRANSIT_TASK_CLOSE
2528 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002529
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002530 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002531 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002532
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002533 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2535 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2536 startPausingLocked(false, false);
2537 }
2538
Craig Mautneraea74a52014-03-08 14:23:10 -08002539 if (endTask) {
2540 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2541 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 } else if (r.state != ActivityState.PAUSING) {
2543 // If the activity is PAUSING, we will complete the finish once
2544 // it is done pausing; else we can just directly finish it here.
2545 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002546 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 } else {
2548 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2549 }
2550
2551 return false;
2552 }
2553
Craig Mautnerf3333272013-04-22 10:55:53 -07002554 static final int FINISH_IMMEDIATELY = 0;
2555 static final int FINISH_AFTER_PAUSE = 1;
2556 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002557
Craig Mautnerf3333272013-04-22 10:55:53 -07002558 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002559 // First things first: if this activity is currently visible,
2560 // and the resumed activity is not yet visible, then hold off on
2561 // finishing until the resumed one becomes visible.
2562 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002563 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2564 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002565 if (mStackSupervisor.mStoppingActivities.size() > 3
2566 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567 // If we already have a few activities waiting to stop,
2568 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002569 // them out. Or if r is the last of activity of the last task the stack
2570 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002571 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002572 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002573 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002574 }
2575 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002576 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2577 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002579 if (oomAdj) {
2580 mService.updateOomAdjLocked();
2581 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002582 return r;
2583 }
2584
2585 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002586 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002587 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002588 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002589 if (mResumedActivity == r) {
2590 mResumedActivity = null;
2591 }
2592 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002593 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002594 r.state = ActivityState.FINISHING;
2595
2596 if (mode == FINISH_IMMEDIATELY
2597 || prevState == ActivityState.STOPPED
2598 || prevState == ActivityState.INITIALIZING) {
2599 // If this activity is already stopped, we can just finish
2600 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002601 boolean activityRemoved = destroyActivityLocked(r, true,
2602 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002603 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002604 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002605 }
2606 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002608
2609 // Need to go through the full pause cycle to get this
2610 // activity into the stopped state and then finish it.
2611 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002612 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002613 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002614 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002615 return r;
2616 }
2617
Craig Mautnerd2328952013-03-05 12:46:26 -08002618 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002619 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002620 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002621 final TaskRecord task = srec.task;
2622 final ArrayList<ActivityRecord> activities = task.mActivities;
2623 final int start = activities.indexOf(srec);
2624 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002625 return false;
2626 }
2627 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002628 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002629 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002630 final ComponentName dest = destIntent.getComponent();
2631 if (start > 0 && dest != null) {
2632 for (int i = finishTo; i >= 0; i--) {
2633 ActivityRecord r = activities.get(i);
2634 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002635 r.info.name.equals(dest.getClassName())) {
2636 finishTo = i;
2637 parent = r;
2638 foundParentInTask = true;
2639 break;
2640 }
2641 }
2642 }
2643
2644 IActivityController controller = mService.mController;
2645 if (controller != null) {
2646 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2647 if (next != null) {
2648 // ask watcher if this is allowed
2649 boolean resumeOK = true;
2650 try {
2651 resumeOK = controller.activityResuming(next.packageName);
2652 } catch (RemoteException e) {
2653 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002654 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002655 }
2656
2657 if (!resumeOK) {
2658 return false;
2659 }
2660 }
2661 }
2662 final long origId = Binder.clearCallingIdentity();
2663 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002664 ActivityRecord r = activities.get(i);
2665 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002666 // Only return the supplied result for the first activity finished
2667 resultCode = Activity.RESULT_CANCELED;
2668 resultData = null;
2669 }
2670
2671 if (parent != null && foundParentInTask) {
2672 final int parentLaunchMode = parent.info.launchMode;
2673 final int destIntentFlags = destIntent.getFlags();
2674 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2675 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2676 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2677 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2678 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2679 } else {
2680 try {
2681 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2682 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002683 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002684 null, aInfo, parent.appToken, null,
2685 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002686 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002687 foundParentInTask = res == ActivityManager.START_SUCCESS;
2688 } catch (RemoteException e) {
2689 foundParentInTask = false;
2690 }
2691 requestFinishActivityLocked(parent.appToken, resultCode,
2692 resultData, "navigate-up", true);
2693 }
2694 }
2695 Binder.restoreCallingIdentity(origId);
2696 return foundParentInTask;
2697 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002698 /**
2699 * Perform the common clean-up of an activity record. This is called both
2700 * as part of destroyActivityLocked() (when destroying the client-side
2701 * representation) and cleaning things up as a result of its hosting
2702 * processing going away, in which case there is no remaining client-side
2703 * state to destroy so only the cleanup here is needed.
2704 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002705 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2706 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707 if (mResumedActivity == r) {
2708 mResumedActivity = null;
2709 }
2710 if (mService.mFocusedActivity == r) {
2711 mService.mFocusedActivity = null;
2712 }
2713
2714 r.configDestroy = false;
2715 r.frozenBeforeDestroy = false;
2716
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002717 if (setState) {
2718 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2719 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002720 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002721 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002722 }
2723
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002724 // Make sure this record is no longer in the pending finishes list.
2725 // This could happen, for example, if we are trimming activities
2726 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002727 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002728 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002729
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 // Remove any pending results.
2731 if (r.finishing && r.pendingResults != null) {
2732 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2733 PendingIntentRecord rec = apr.get();
2734 if (rec != null) {
2735 mService.cancelIntentSenderLocked(rec, false);
2736 }
2737 }
2738 r.pendingResults = null;
2739 }
2740
2741 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002742 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 }
2744
Craig Mautnerc8143c62013-09-03 12:15:57 -07002745 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002746 // There are clients waiting to receive thumbnails so, in case
2747 // this is an activity that someone is waiting for, add it
2748 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002749 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002750 }
2751
2752 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002753 removeTimeoutsForActivityLocked(r);
2754 }
2755
2756 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002757 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002758 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002759 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002760 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002761 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002762 }
2763
Craig Mautner04a0ea62014-01-13 12:51:26 -08002764 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002765 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002766 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2767 r.makeFinishing();
2768 if (DEBUG_ADD_REMOVE) {
2769 RuntimeException here = new RuntimeException("here");
2770 here.fillInStackTrace();
2771 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002773 r.takeFromHistory();
2774 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002775 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002776 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002777 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002778 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002779 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002780 if (VALIDATE_TOKENS) {
2781 validateAppTokensLocked();
2782 }
Craig Mautner312ba862014-02-10 17:55:01 -08002783 final TaskRecord task = r.task;
2784 if (task != null && task.removeActivity(r)) {
2785 if (DEBUG_STACK) Slog.i(TAG,
2786 "removeActivityFromHistoryLocked: last activity removed from " + this);
2787 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
2788 mStackSupervisor.moveHomeToTop();
2789 }
2790 removeTask(task);
2791 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002792 cleanUpActivityServicesLocked(r);
2793 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002794 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002795
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002796 /**
2797 * Perform clean-up of service connections in an activity record.
2798 */
2799 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2800 // Throw away any services that have been bound by this activity.
2801 if (r.connections != null) {
2802 Iterator<ConnectionRecord> it = r.connections.iterator();
2803 while (it.hasNext()) {
2804 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002805 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 }
2807 r.connections = null;
2808 }
2809 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002810
2811 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2812 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2813 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2814 mHandler.sendMessage(msg);
2815 }
2816
Dianne Hackborn28695e02011-11-02 21:59:51 -07002817 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002818 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002819 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002820 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2821 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2822 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2823 final ActivityRecord r = activities.get(activityNdx);
2824 if (r.finishing) {
2825 continue;
2826 }
2827 if (r.fullscreen) {
2828 lastIsOpaque = true;
2829 }
2830 if (owner != null && r.app != owner) {
2831 continue;
2832 }
2833 if (!lastIsOpaque) {
2834 continue;
2835 }
2836 // We can destroy this one if we have its icicle saved and
2837 // it is not in the process of pausing/stopping/finishing.
2838 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2839 && r.haveState && !r.visible && r.stopped
2840 && r.state != ActivityState.DESTROYING
2841 && r.state != ActivityState.DESTROYED) {
2842 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2843 + " resumed=" + mResumedActivity
2844 + " pausing=" + mPausingActivity);
2845 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2846 activityRemoved = true;
2847 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002848 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002849 }
2850 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002851 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002852 mStackSupervisor.resumeTopActivitiesLocked();
2853
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002854 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002855 }
2856
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002857 /**
2858 * Destroy the current CLIENT SIDE instance of an activity. This may be
2859 * called both when actually finishing an activity, or when performing
2860 * a configuration switch where we destroy the current client-side object
2861 * but then create a new client-side object for this same HistoryRecord.
2862 */
2863 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002864 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002865 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002866 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2868 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002869 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002870 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002871
2872 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002873
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002874 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875
2876 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002877
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878 if (hadApp) {
2879 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002880 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2882 mService.mHeavyWeightProcess = null;
2883 mService.mHandler.sendEmptyMessage(
2884 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2885 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002886 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002887 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002888 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002889 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002890 }
2891 }
2892
2893 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002894
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002895 try {
2896 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002897 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898 r.configChangeFlags);
2899 } catch (Exception e) {
2900 // We can just ignore exceptions here... if the process
2901 // has crashed, our death notification will clean things
2902 // up.
2903 //Slog.w(TAG, "Exception thrown during finish", e);
2904 if (r.finishing) {
2905 removeActivityFromHistoryLocked(r);
2906 removedFromHistory = true;
2907 skipDestroy = true;
2908 }
2909 }
2910
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002912
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002913 // If the activity is finishing, we need to wait on removing it
2914 // from the list to give it a chance to do its cleanup. During
2915 // that time it may make calls back with its token so we need to
2916 // be able to find it on the list and so we don't want to remove
2917 // it from the list yet. Otherwise, we can just immediately put
2918 // it in the destroyed state since we are not removing it from the
2919 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002920 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002921 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2922 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002924 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2926 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002927 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002928 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002929 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002930 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931 }
2932 } else {
2933 // remove this record from the history.
2934 if (r.finishing) {
2935 removeActivityFromHistoryLocked(r);
2936 removedFromHistory = true;
2937 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002938 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002939 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002940 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002941 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002942 }
2943 }
2944
2945 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002946
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 if (!mLRUActivities.remove(r) && hadApp) {
2948 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2949 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002950
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002951 return removedFromHistory;
2952 }
2953
Craig Mautnerd2328952013-03-05 12:46:26 -08002954 final void activityDestroyedLocked(IBinder token) {
2955 final long origId = Binder.clearCallingIdentity();
2956 try {
2957 ActivityRecord r = ActivityRecord.forToken(token);
2958 if (r != null) {
2959 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002961
2962 if (isInStackLocked(token) != null) {
2963 if (r.state == ActivityState.DESTROYING) {
2964 cleanUpActivityLocked(r, true, false);
2965 removeActivityFromHistoryLocked(r);
2966 }
2967 }
Craig Mautner05d29032013-05-03 13:40:13 -07002968 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002969 } finally {
2970 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971 }
2972 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002973
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002974 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2975 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002977 if (DEBUG_CLEANUP) Slog.v(
2978 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979 + " with " + i + " entries");
2980 while (i > 0) {
2981 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002982 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002983 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002984 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002985 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002986 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002987 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 }
2989 }
2990 }
2991
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002992 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2993 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002994 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2995 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002996 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2997 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002998 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002999 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003000 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3001 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003002
3003 boolean hasVisibleActivities = false;
3004
3005 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003006 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003007 if (DEBUG_CLEANUP) Slog.v(
3008 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003009 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3010 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3011 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3012 final ActivityRecord r = activities.get(activityNdx);
3013 --i;
3014 if (DEBUG_CLEANUP) Slog.v(
3015 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3016 if (r.app == app) {
3017 boolean remove;
3018 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3019 // Don't currently have state for the activity, or
3020 // it is finishing -- always remove it.
3021 remove = true;
3022 } else if (r.launchCount > 2 &&
3023 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3024 // We have launched this activity too many times since it was
3025 // able to run, so give up and remove it.
3026 remove = true;
3027 } else {
3028 // The process may be gone, but the activity lives on!
3029 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003030 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003031 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003032 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003033 RuntimeException here = new RuntimeException("here");
3034 here.fillInStackTrace();
3035 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3036 + ": haveState=" + r.haveState
3037 + " stateNotNeeded=" + r.stateNotNeeded
3038 + " finishing=" + r.finishing
3039 + " state=" + r.state, here);
3040 }
3041 if (!r.finishing) {
3042 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3043 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3044 r.userId, System.identityHashCode(r),
3045 r.task.taskId, r.shortComponentName,
3046 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003047 if (r.state == ActivityState.RESUMED) {
3048 mService.updateUsageStats(r, false);
3049 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003050 }
3051 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003052
Craig Mautner0247fc82013-02-28 14:32:06 -08003053 } else {
3054 // We have the current state for this activity, so
3055 // it can be restarted later when needed.
3056 if (localLOGV) Slog.v(
3057 TAG, "Keeping entry, setting app to null");
3058 if (r.visible) {
3059 hasVisibleActivities = true;
3060 }
3061 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3062 + r);
3063 r.app = null;
3064 r.nowVisible = false;
3065 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003066 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003067 "App died, clearing saved state of " + r);
3068 r.icicle = null;
3069 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003070 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003071
Craig Mautnerd2328952013-03-05 12:46:26 -08003072 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003073 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003074 }
3075 }
3076
3077 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003079
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003080 final void updateTransitLocked(int transit, Bundle options) {
3081 if (options != null) {
3082 ActivityRecord r = topRunningActivityLocked(null);
3083 if (r != null && r.state != ActivityState.RESUMED) {
3084 r.updateOptionsLocked(options);
3085 } else {
3086 ActivityOptions.abort(options);
3087 }
3088 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003089 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003090 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003091
Craig Mautnera82aa092013-09-13 15:34:08 -07003092 void moveHomeTaskToTop() {
3093 final int top = mTaskHistory.size() - 1;
3094 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3095 final TaskRecord task = mTaskHistory.get(taskNdx);
3096 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003097 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003098 mTaskHistory.remove(taskNdx);
3099 mTaskHistory.add(top, task);
3100 mWindowManager.moveTaskToTop(task.taskId);
3101 return;
3102 }
3103 }
3104 }
3105
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003106 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003107 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108
Craig Mautner11bf9a52013-02-19 14:08:51 -08003109 final int numTasks = mTaskHistory.size();
3110 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003111 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003112 // nothing to do!
3113 if (reason != null &&
3114 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3115 ActivityOptions.abort(options);
3116 } else {
3117 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3118 }
3119 return;
3120 }
3121
Craig Mautnere0a38842013-12-16 16:14:02 -08003122 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003123
Craig Mautner11bf9a52013-02-19 14:08:51 -08003124 // Shift all activities with this task up to the top
3125 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003126 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003127
3128 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 if (reason != null &&
3130 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003131 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 ActivityRecord r = topRunningActivityLocked(null);
3133 if (r != null) {
3134 mNoAnimActivities.add(r);
3135 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003136 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003137 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003138 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003139 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003140
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003141 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003142
Craig Mautner05d29032013-05-03 13:40:13 -07003143 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003144 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003145
3146 if (VALIDATE_TOKENS) {
3147 validateAppTokensLocked();
3148 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 }
3150
3151 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003152 * Worker method for rearranging history stack. Implements the function of moving all
3153 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003155 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003156 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3157 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003158 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003159 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 * @return Returns true if the move completed, false if not.
3161 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003162 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003163 final TaskRecord tr = taskForIdLocked(taskId);
3164 if (tr == null) {
3165 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3166 return false;
3167 }
3168
3169 Slog.i(TAG, "moveTaskToBack: " + tr);
3170
3171 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003172
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003173 // If we have a watcher, preflight the move before committing to it. First check
3174 // for *other* available tasks, but if none are available, then try again allowing the
3175 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003176 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003177 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003178 if (next == null) {
3179 next = topRunningActivityLocked(null, 0);
3180 }
3181 if (next != null) {
3182 // ask watcher if this is allowed
3183 boolean moveOK = true;
3184 try {
3185 moveOK = mService.mController.activityResuming(next.packageName);
3186 } catch (RemoteException e) {
3187 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003188 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003189 }
3190 if (!moveOK) {
3191 return false;
3192 }
3193 }
3194 }
3195
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003196 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003197 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003198
Craig Mautner11bf9a52013-02-19 14:08:51 -08003199 mTaskHistory.remove(tr);
3200 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201
Craig Mautnerc8143c62013-09-03 12:15:57 -07003202 // There is an assumption that moving a task to the back moves it behind the home activity.
3203 // We make sure here that some activity in the stack will launch home.
3204 ActivityRecord lastActivity = null;
3205 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003206 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3207 final TaskRecord task = mTaskHistory.get(taskNdx);
3208 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003209 break;
3210 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003211 if (taskNdx == 1) {
3212 // Set the last task before tr to go to home.
3213 task.mOnTopOfHome = true;
3214 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003215 }
3216
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003218 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3219 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 ActivityRecord r = topRunningActivityLocked(null);
3221 if (r != null) {
3222 mNoAnimActivities.add(r);
3223 }
3224 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003225 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003227 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003228
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003230 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003231 }
3232
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003233 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautnere0a38842013-12-16 16:14:02 -08003234 if (task == tr && tr.mOnTopOfHome || numTasks <= 1 && isOnHomeDisplay()) {
Daniel 2 Olofsson9cdf9e52013-12-16 13:24:25 +01003235 tr.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003236 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003237 }
3238
Craig Mautner05d29032013-05-03 13:40:13 -07003239 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003240 return true;
3241 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003242
Craig Mautner8849a5e2013-04-02 16:41:03 -07003243 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003244 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003245 final Uri data = r.intent.getData();
3246 final String strData = data != null ? data.toSafeString() : null;
3247
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003249 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003251 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003252 }
3253
3254 /**
3255 * Make sure the given activity matches the current configuration. Returns
3256 * false if the activity had to be destroyed. Returns true if the
3257 * configuration is the same, or the activity will remain running as-is
3258 * for whatever reason. Ensures the HistoryRecord is updated with the
3259 * correct configuration and all other bookkeeping is handled.
3260 */
3261 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3262 int globalChanges) {
3263 if (mConfigWillChange) {
3264 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3265 "Skipping config check (will change): " + r);
3266 return true;
3267 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003268
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003269 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3270 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003272 // Short circuit: if the two configurations are the exact same
3273 // object (the common case), then there is nothing to do.
3274 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003275 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003276 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3277 "Configuration unchanged in " + r);
3278 return true;
3279 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003280
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003281 // We don't worry about activities that are finishing.
3282 if (r.finishing) {
3283 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3284 "Configuration doesn't matter in finishing " + r);
3285 r.stopFreezingScreenLocked(false);
3286 return true;
3287 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003288
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003289 // Okay we now are going to make this activity have the new config.
3290 // But then we need to figure out how it needs to deal with that.
3291 Configuration oldConfig = r.configuration;
3292 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003293
3294 // Determine what has changed. May be nothing, if this is a config
3295 // that has come back from the app after going idle. In that case
3296 // we just want to leave the official config object now in the
3297 // activity and do nothing else.
3298 final int changes = oldConfig.diff(newConfig);
3299 if (changes == 0 && !r.forceNewConfig) {
3300 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3301 "Configuration no differences in " + r);
3302 return true;
3303 }
3304
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 // If the activity isn't currently running, just leave the new
3306 // configuration and it will pick that up next time it starts.
3307 if (r.app == null || r.app.thread == null) {
3308 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3309 "Configuration doesn't matter not running " + r);
3310 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003311 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 return true;
3313 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003314
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003315 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003316 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3317 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3318 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003319 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003320 + ", newConfig=" + newConfig);
3321 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003322 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003323 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3324 r.configChangeFlags |= changes;
3325 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003326 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003327 if (r.app == null || r.app.thread == null) {
3328 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003329 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003330 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003331 } else if (r.state == ActivityState.PAUSING) {
3332 // A little annoying: we are waiting for this activity to
3333 // finish pausing. Let's not do anything now, but just
3334 // flag that it needs to be restarted when done pausing.
3335 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003336 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003337 r.configDestroy = true;
3338 return true;
3339 } else if (r.state == ActivityState.RESUMED) {
3340 // Try to optimize this case: the configuration is changing
3341 // and we need to restart the top, resumed activity.
3342 // Instead of doing the normal handshaking, just say
3343 // "restart!".
3344 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003345 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003346 relaunchActivityLocked(r, r.configChangeFlags, true);
3347 r.configChangeFlags = 0;
3348 } else {
3349 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003350 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003351 relaunchActivityLocked(r, r.configChangeFlags, false);
3352 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003353 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003354
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003355 // All done... tell the caller we weren't able to keep this
3356 // activity around.
3357 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 // Default case: the activity can handle this new configuration, so
3361 // hand it over. Note that we don't need to give it the new
3362 // configuration, since we always send configuration changes to all
3363 // process when they happen so it can just use whatever configuration
3364 // it last got.
3365 if (r.app != null && r.app.thread != null) {
3366 try {
3367 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003368 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 } catch (RemoteException e) {
3370 // If process died, whatever.
3371 }
3372 }
3373 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003374
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 return true;
3376 }
3377
Craig Mautnerc8143c62013-09-03 12:15:57 -07003378 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003379 int changes, boolean andResume) {
3380 List<ResultInfo> results = null;
3381 List<Intent> newIntents = null;
3382 if (andResume) {
3383 results = r.results;
3384 newIntents = r.newIntents;
3385 }
3386 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3387 + " with results=" + results + " newIntents=" + newIntents
3388 + " andResume=" + andResume);
3389 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003390 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003391 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003392
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003393 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003394
Craig Mautner34b73df2014-01-12 21:11:08 -08003395 mStackSupervisor.removeChildActivityContainers(r);
3396
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003397 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003398 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3399 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3400 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003401 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003402 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003403 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003404 // Note: don't need to call pauseIfSleepingLocked() here, because
3405 // the caller will only pass in 'andResume' if this activity is
3406 // currently resumed, which implies we aren't sleeping.
3407 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003408 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003409 }
3410
3411 if (andResume) {
3412 r.results = null;
3413 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003414 r.state = ActivityState.RESUMED;
3415 } else {
3416 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3417 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003418 }
3419
3420 return true;
3421 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003422
3423 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3425 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3426 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3427 final ActivityRecord r = activities.get(activityNdx);
3428 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003429 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003430 }
3431 if (r.fullscreen && !r.finishing) {
3432 return false;
3433 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003434 }
3435 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003436 final ActivityRecord r = ActivityRecord.forToken(token);
3437 if (r == null) {
3438 return false;
3439 }
3440 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3441 + " would have returned true for r=" + r);
3442 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003443 }
3444
3445 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003446 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3447 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3448 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3449 final ActivityRecord r = activities.get(activityNdx);
3450 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003451 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003452 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003453 }
3454 }
3455 }
3456
3457 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3458 boolean didSomething = false;
3459 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003460 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3461 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3462 int numActivities = activities.size();
3463 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3464 ActivityRecord r = activities.get(activityNdx);
3465 final boolean samePackage = r.packageName.equals(name)
3466 || (name == null && r.userId == userId);
3467 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3468 && (samePackage || r.task == lastTask)
3469 && (r.app == null || evenPersistent || !r.app.persistent)) {
3470 if (!doit) {
3471 if (r.finishing) {
3472 // If this activity is just finishing, then it is not
3473 // interesting as far as something to stop.
3474 continue;
3475 }
3476 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003477 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003478 didSomething = true;
3479 Slog.i(TAG, " Force finishing activity " + r);
3480 if (samePackage) {
3481 if (r.app != null) {
3482 r.app.removed = true;
3483 }
3484 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003485 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003486 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003487 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3488 true)) {
3489 // r has been deleted from mActivities, accommodate.
3490 --numActivities;
3491 --activityNdx;
3492 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003493 }
3494 }
3495 }
3496 return didSomething;
3497 }
3498
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003499 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003500 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003501 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003502 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003503 final TaskRecord task = mTaskHistory.get(taskNdx);
3504 ActivityRecord r = null;
3505 ActivityRecord top = null;
3506 int numActivities = 0;
3507 int numRunning = 0;
3508 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003509 if (activities.isEmpty()) {
3510 continue;
3511 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003512 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3513 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003514
Craig Mautneraab647e2013-02-28 16:31:36 -08003515 // Initialize state for next task if needed.
3516 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3517 top = r;
3518 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003519 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003520
3521 // Add 'r' into the current task.
3522 numActivities++;
3523 if (r.app != null && r.app.thread != null) {
3524 numRunning++;
3525 }
3526
3527 if (localLOGV) Slog.v(
3528 TAG, r.intent.getComponent().flattenToShortString()
3529 + ": task=" + r.task);
3530 }
3531
3532 RunningTaskInfo ci = new RunningTaskInfo();
3533 ci.id = task.taskId;
3534 ci.baseActivity = r.intent.getComponent();
3535 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003536 ci.lastActiveTime = task.lastActiveTime;
3537
Craig Mautneraab647e2013-02-28 16:31:36 -08003538 if (top.thumbHolder != null) {
3539 ci.description = top.thumbHolder.lastDescription;
3540 }
3541 ci.numActivities = numActivities;
3542 ci.numRunning = numRunning;
3543 //System.out.println(
3544 // "#" + maxNum + ": " + " descr=" + ci.description);
3545 if (receiver != null) {
3546 if (localLOGV) Slog.v(
3547 TAG, "State=" + top.state + "Idle=" + top.idle
3548 + " app=" + top.app
3549 + " thr=" + (top.app != null ? top.app.thread : null));
3550 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3551 if (top.idle && top.app != null && top.app.thread != null) {
3552 topRecord = top;
3553 } else {
3554 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003555 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003556 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003557 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003558 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003559 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003560 }
3561 return topRecord;
3562 }
3563
3564 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003565 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003566 if (DEBUG_SWITCH) Slog.d(
3567 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003568 if (top >= 0) {
3569 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3570 int activityTop = activities.size() - 1;
3571 if (activityTop > 0) {
3572 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3573 "unhandled-back", true);
3574 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003575 }
3576 }
3577
Craig Mautner6b74cb52013-09-27 17:02:21 -07003578 /**
3579 * Reset local parameters because an app's activity died.
3580 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003581 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003582 */
3583 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003584 if (mPausingActivity != null && mPausingActivity.app == app) {
3585 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3586 "App died while pausing: " + mPausingActivity);
3587 mPausingActivity = null;
3588 }
3589 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3590 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003591 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003592 }
3593
Craig Mautner19091252013-10-05 00:03:53 -07003594 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003595 }
3596
Craig Mautnercae015f2013-02-08 14:31:27 -08003597 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003598 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3599 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3600 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3601 final ActivityRecord r = activities.get(activityNdx);
3602 if (r.app == app) {
3603 Slog.w(TAG, " Force finishing activity "
3604 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003605 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003606 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003607 }
3608 }
3609 }
3610
Dianne Hackborn390517b2013-05-30 15:03:32 -07003611 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003612 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003613 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003614 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3615 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003616 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3617 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003618 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003619 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003620 if (printed) {
3621 header = null;
3622 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003623 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003624 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003625 }
3626
3627 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3628 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3629
3630 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003631 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3632 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003633 }
3634 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003635 final int top = mTaskHistory.size() - 1;
3636 if (top >= 0) {
3637 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3638 int listTop = list.size() - 1;
3639 if (listTop >= 0) {
3640 activities.add(list.get(listTop));
3641 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003642 }
3643 } else {
3644 ItemMatcher matcher = new ItemMatcher();
3645 matcher.build(name);
3646
Craig Mautneraab647e2013-02-28 16:31:36 -08003647 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3648 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3649 if (matcher.match(r1, r1.intent.getComponent())) {
3650 activities.add(r1);
3651 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003652 }
3653 }
3654 }
3655
3656 return activities;
3657 }
3658
3659 ActivityRecord restartPackage(String packageName) {
3660 ActivityRecord starting = topRunningActivityLocked(null);
3661
3662 // All activities that came from the package must be
3663 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003664 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3665 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3666 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3667 final ActivityRecord a = activities.get(activityNdx);
3668 if (a.info.packageName.equals(packageName)) {
3669 a.forceNewConfig = true;
3670 if (starting != null && a == starting && a.visible) {
3671 a.startFreezingScreenLocked(starting.app,
3672 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3673 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003674 }
3675 }
3676 }
3677
3678 return starting;
3679 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003680
Craig Mautner04a0ea62014-01-13 12:51:26 -08003681 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003682 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003683 mWindowManager.removeTask(task.taskId);
3684 final ActivityRecord r = mResumedActivity;
3685 if (r != null && r.task == task) {
3686 mResumedActivity = null;
3687 }
3688
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003689 final int taskNdx = mTaskHistory.indexOf(task);
3690 final int topTaskNdx = mTaskHistory.size() - 1;
3691 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3692 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3693 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003694 mTaskHistory.remove(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003695
3696 if (mTaskHistory.isEmpty()) {
3697 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3698 if (isOnHomeDisplay()) {
3699 mStackSupervisor.moveHomeStack(!isHomeStack());
3700 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003701 if (mStacks != null) {
3702 mStacks.remove(this);
3703 mStacks.add(0, this);
3704 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003705 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003706 }
3707
Craig Mautnerde4ef022013-04-07 19:01:33 -07003708 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003709 TaskRecord task = new TaskRecord(taskId, info, intent);
3710 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003711 return task;
3712 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003713
3714 ArrayList<TaskRecord> getAllTasks() {
3715 return new ArrayList<TaskRecord>(mTaskHistory);
3716 }
3717
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003718 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003719 task.stack = this;
3720 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003721 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003722 } else {
3723 mTaskHistory.add(0, task);
3724 }
3725 }
3726
3727 public int getStackId() {
3728 return mStackId;
3729 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003730
3731 @Override
3732 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003733 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3734 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003735 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003736}