blob: a154b9c59d223ae6dffd5e285854199c9c725617 [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 Hackborncee04b52013-07-03 17:01:28 -070039import android.os.Trace;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080041import com.android.internal.util.Objects;
Kenny Rootadd58212013-05-07 09:47:34 -070042import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080043import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070050import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080052import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080053import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
57import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080062import android.content.res.Resources;
63import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080064import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080065import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070067import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Handler;
69import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090070import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Message;
72import android.os.PowerManager;
73import android.os.RemoteException;
74import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070075import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
86
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
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 enum ActivityState {
126 INITIALIZING,
127 RESUMED,
128 PAUSING,
129 PAUSED,
130 STOPPING,
131 STOPPED,
132 FINISHING,
133 DESTROYING,
134 DESTROYED
135 }
136
137 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700138 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -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 /**
180 * Current activity that is resumed, or null if there is none.
181 */
182 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700185 * This is the last activity that has been started. It is only used to
186 * identify when multiple activities are started at once so that the user
187 * can be warned they may not be in the activity they think they are.
188 */
189 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800190
Craig Mautner5eda9b32013-07-02 11:58:16 -0700191 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
192 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
193 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
194 // Activity in mTranslucentActivityWaiting is notified via
195 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
196 // background activity being drawn then the same call will be made with a true value.
197 ActivityRecord mTranslucentActivityWaiting = null;
198 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
199 new ArrayList<ActivityRecord>();
200
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700201 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202 * Set when we know we are going to be calling updateConfiguration()
203 * soon, so want to skip intermediate config checks.
204 */
205 boolean mConfigWillChange;
206
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700207 long mLaunchStartTime = 0;
208 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800209
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800210 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800211 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
212 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
213 */
214 private ActivityRecord mLastScreenshotActivity = null;
215 private Bitmap mLastScreenshotBitmap = null;
216
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800217 int mThumbnailWidth = -1;
218 int mThumbnailHeight = -1;
219
Craig Mautner858d8a62013-04-23 17:08:34 -0700220 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700221
Craig Mautnerc00204b2013-03-05 15:02:14 -0800222 final int mStackId;
223
Craig Mautner27084302013-03-25 08:05:25 -0700224 /** Run all ActivityStacks through this */
225 final ActivityStackSupervisor mStackSupervisor;
226
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700227 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700228 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
229 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
230 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
231 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700232 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700233
234 static class ScheduleDestroyArgs {
235 final ProcessRecord mOwner;
236 final boolean mOomAdj;
237 final String mReason;
238 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
239 mOwner = owner;
240 mOomAdj = oomAdj;
241 mReason = reason;
242 }
243 }
244
Zoran Marcetaf958b322012-08-09 20:27:12 +0900245 final Handler mHandler;
246
247 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700248 //public Handler() {
249 // if (localLOGV) Slog.v(TAG, "Handler started!");
250 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900251 public ActivityStackHandler(Looper looper) {
252 super(looper);
253 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700254
Zoran Marcetaf958b322012-08-09 20:27:12 +0900255 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700256 public void handleMessage(Message msg) {
257 switch (msg.what) {
258 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800259 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700260 // We don't at this point know if the activity is fullscreen,
261 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800262 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700263 synchronized (mService) {
264 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700265 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700266 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700267 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800268 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700269 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700270 case LAUNCH_TICK_MSG: {
271 ActivityRecord r = (ActivityRecord)msg.obj;
272 synchronized (mService) {
273 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700274 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700275 }
276 }
277 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800279 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700280 // We don't at this point know if the activity is fullscreen,
281 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800282 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800283 synchronized (mService) {
284 activityDestroyedLocked(r != null ? r.appToken : null);
285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700287 case STOP_TIMEOUT_MSG: {
288 ActivityRecord r = (ActivityRecord)msg.obj;
289 // We don't at this point know if the activity is fullscreen,
290 // so we need to be conservative and assume it isn't.
291 Slog.w(TAG, "Activity stop timeout for " + r);
292 synchronized (mService) {
293 if (r.isInHistory()) {
294 activityStoppedLocked(r, null, null, null);
295 }
296 }
297 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700298 case DESTROY_ACTIVITIES_MSG: {
299 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
300 synchronized (mService) {
301 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
302 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700303 } break;
304 case TRANSLUCENT_TIMEOUT_MSG: {
305 synchronized (mService) {
306 notifyActivityDrawnLocked(null);
307 }
308 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 }
310 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800311 }
312
Craig Mautner000f0022013-02-26 15:04:29 -0800313 private int numActivities() {
314 int count = 0;
315 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
316 count += mTaskHistory.get(taskNdx).mActivities.size();
317 }
318 return count;
319 }
320
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900322 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700323 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324 mWindowManager = service.mWindowManager;
325 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700326 mContext = context;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700327 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800328 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700330 }
Craig Mautner5962b122012-10-05 14:45:52 -0700331
332 private boolean okToShow(ActivityRecord r) {
333 return r.userId == mCurrentUser
334 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
335 }
336
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700337 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800338 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
339 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800340 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800341 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
342 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800343 if (!r.finishing && r != notTop && okToShow(r)) {
344 return r;
345 }
346 }
347 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700348 return null;
349 }
350
351 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800352 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
353 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800354 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800355 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
356 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800357 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
358 return r;
359 }
360 }
361 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362 return null;
363 }
364
365 /**
366 * This is a simplified version of topRunningActivityLocked that provides a number of
367 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800368 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700369 * @param token If non-null, any history records matching this token will be skipped.
370 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800371 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 * @return Returns the HistoryRecord of the next activity on the stack.
373 */
374 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800375 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
376 TaskRecord task = mTaskHistory.get(taskNdx);
377 if (task.taskId == taskId) {
378 continue;
379 }
380 ArrayList<ActivityRecord> activities = task.mActivities;
381 for (int i = activities.size() - 1; i >= 0; --i) {
382 final ActivityRecord r = activities.get(i);
383 // Note: the taskId check depends on real taskId fields being non-zero
384 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
385 return r;
386 }
387 }
388 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700389 return null;
390 }
391
Craig Mautner8849a5e2013-04-02 16:41:03 -0700392 final ActivityRecord topActivity() {
393 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700394 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700395 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
396 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
397 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700398 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700399 }
400 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700401 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700402 }
403
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700404 final TaskRecord topTask() {
405 final int size = mTaskHistory.size();
406 if (size > 0) {
407 return mTaskHistory.get(size - 1);
408 }
409 return null;
410 }
411
Craig Mautnerd2328952013-03-05 12:46:26 -0800412 TaskRecord taskForIdLocked(int id) {
413 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
414 final TaskRecord task = mTaskHistory.get(taskNdx);
415 if (task.taskId == id) {
416 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800417 }
418 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700419 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700420 }
421
Craig Mautnerd2328952013-03-05 12:46:26 -0800422 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800423 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800424 if (r != null) {
425 final TaskRecord task = r.task;
426 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
427 if (task.stack != this) Slog.w(TAG,
428 "Illegal state! task does not point to stack it is in.");
429 return r;
430 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800431 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800432 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800433 }
434
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700435 boolean containsApp(ProcessRecord app) {
436 if (app == null) {
437 return false;
438 }
439 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
440 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
441 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
442 final ActivityRecord r = activities.get(activityNdx);
443 if (r.finishing) {
444 continue;
445 }
446 if (r.app == app) {
447 return true;
448 }
449 }
450 }
451 return false;
452 }
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
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700464 /**
465 * Returns the top activity in any existing task matching the given
466 * Intent. Returns null if no such task is found.
467 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700468 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700469 ComponentName cls = intent.getComponent();
470 if (info.targetActivity != null) {
471 cls = new ComponentName(info.packageName, info.targetActivity);
472 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700473 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800474
475 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
476 final TaskRecord task = mTaskHistory.get(taskNdx);
477 final ActivityRecord r = task.getTopActivity();
478 if (r == null || r.finishing || r.userId != userId ||
479 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
480 continue;
481 }
482
483 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
484 // + "/aff=" + r.task.affinity + " to new cls="
485 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
486 if (task.affinity != null) {
487 if (task.affinity.equals(info.taskAffinity)) {
488 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700489 return r;
490 }
Craig Mautner000f0022013-02-26 15:04:29 -0800491 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
492 //Slog.i(TAG, "Found matching class!");
493 //dump();
494 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
495 return r;
496 } else if (task.affinityIntent != null
497 && task.affinityIntent.getComponent().equals(cls)) {
498 //Slog.i(TAG, "Found matching class!");
499 //dump();
500 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
501 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700502 }
503 }
504
505 return null;
506 }
507
508 /**
509 * Returns the first activity (starting from the top of the stack) that
510 * is the same as the given activity. Returns null if no such activity
511 * is found.
512 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700513 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700514 ComponentName cls = intent.getComponent();
515 if (info.targetActivity != null) {
516 cls = new ComponentName(info.packageName, info.targetActivity);
517 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700518 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700519
Craig Mautner000f0022013-02-26 15:04:29 -0800520 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
521 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
522 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
523 ActivityRecord r = activities.get(activityNdx);
524 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700525 //Slog.i(TAG, "Found matching class!");
526 //dump();
527 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
528 return r;
529 }
530 }
531 }
532
533 return null;
534 }
535
Amith Yamasani742a6712011-05-04 14:49:28 -0700536 /*
Craig Mautner858d8a62013-04-23 17:08:34 -0700537 * Move the activities around in the stack to bring a user to the foreground. This only
538 * matters on the home stack. All other stacks are single user.
Amith Yamasani742a6712011-05-04 14:49:28 -0700539 * @return whether there are any activities for the specified user.
540 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700541 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800542 if (VALIDATE_TOKENS) {
543 validateAppTokensLocked();
544 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800545 if (mCurrentUser == userId) {
546 return true;
547 }
548 mCurrentUser = userId;
549
550 // Move userId's tasks to the top.
551 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800552 int index = mTaskHistory.size();
553 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700554 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800555 if (task.userId == userId) {
556 haveActivities = true;
557 mTaskHistory.remove(i);
558 mTaskHistory.add(task);
559 --index;
560 }
561 }
562
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700563 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700564 }
565
Craig Mautner2420ead2013-04-01 17:13:20 -0700566 void minimalResumeActivityLocked(ActivityRecord r) {
567 r.state = ActivityState.RESUMED;
568 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
569 + " (starting new instance)");
570 r.stopped = false;
571 mResumedActivity = r;
572 r.task.touchActiveTime();
573 mService.addRecentTaskLocked(r.task);
574 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700575 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700576 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700577 }
578
Dianne Hackborncee04b52013-07-03 17:01:28 -0700579 private void startLaunchTraces() {
580 if (mFullyDrawnStartTime != 0) {
581 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
582 }
583 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
584 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
585 }
586
587 private void stopFullyDrawnTraceIfNeeded() {
588 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
589 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
590 mFullyDrawnStartTime = 0;
591 }
592 }
593
Craig Mautnere79d42682013-04-01 19:01:53 -0700594 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700595 if (r.displayStartTime == 0) {
596 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
597 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700598 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700599 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700600 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700601 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700602 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700603 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700604 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800606
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700607 void clearLaunchTime(ActivityRecord r) {
608 r.displayStartTime = r.fullyDrawnStartTime = 0;
609 }
610
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800611 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800612 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800613 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
614 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
615 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
616 activities.get(activityNdx).setSleeping(false);
617 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800618 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800619 }
620
Craig Mautner0eea92c2013-05-16 13:35:39 -0700621 /**
622 * @return true if something must be done before going to sleep.
623 */
624 boolean checkReadyForSleepLocked() {
625 if (mResumedActivity != null) {
626 // Still have something resumed; can't sleep until it is paused.
627 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
628 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
629 startPausingLocked(false, true);
630 return true;
631 }
632 if (mPausingActivity != null) {
633 // Still waiting for something to pause; can't sleep yet.
634 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
635 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800636 }
637
Craig Mautner0eea92c2013-05-16 13:35:39 -0700638 return false;
639 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800640
Craig Mautner0eea92c2013-05-16 13:35:39 -0700641 void goToSleep() {
642 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800643
Craig Mautner0eea92c2013-05-16 13:35:39 -0700644 // Make sure any stopped but visible activities are now sleeping.
645 // This ensures that the activity's onStop() is called.
646 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
647 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
648 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
649 final ActivityRecord r = activities.get(activityNdx);
650 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
651 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800652 }
653 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800654 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700655 }
Craig Mautner59c00972012-07-30 12:10:24 -0700656
Dianne Hackbornd2835932010-12-13 16:28:46 -0800657 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800658 if (who.noDisplay) {
659 return null;
660 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800661
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800662 Resources res = mService.mContext.getResources();
663 int w = mThumbnailWidth;
664 int h = mThumbnailHeight;
665 if (w < 0) {
666 mThumbnailWidth = w =
667 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
668 mThumbnailHeight = h =
669 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
670 }
671
672 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800673 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
674 || mLastScreenshotBitmap.getWidth() != w
675 || mLastScreenshotBitmap.getHeight() != h) {
676 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700677 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800678 who.appToken, Display.DEFAULT_DISPLAY, w, h);
679 }
680 if (mLastScreenshotBitmap != null) {
681 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
682 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800683 }
684 return null;
685 }
686
Craig Mautnercf910b02013-04-23 11:23:27 -0700687 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800688 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800689 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700690 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800691 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700692 ActivityRecord prev = mResumedActivity;
693 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700694 Slog.e(TAG, "Trying to pause when nothing is resumed",
695 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700696 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700697 return;
698 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700699 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
700 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700701 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800702 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700703 mLastPausedActivity = prev;
704 prev.state = ActivityState.PAUSING;
705 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700706 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700707 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700708 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700709
710 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800711
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700712 if (prev.app != null && prev.app.thread != null) {
713 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
714 try {
715 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700716 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700717 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700718 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800719 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
720 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700721 } catch (Exception e) {
722 // Ignore exception, if process died other code will cleanup.
723 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800724 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 mLastPausedActivity = null;
726 }
727 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800728 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 mLastPausedActivity = null;
730 }
731
732 // If we are not going to sleep, we want to ensure the device is
733 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700734 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700735 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700736 }
737
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800738 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739 // Have the window manager pause its key dispatching until the new
740 // activity has started. If we're pausing the activity just because
741 // the screen is being turned off and the UI is sleeping, don't interrupt
742 // key dispatch; the same activity will pick it up again on wakeup.
743 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800744 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700745 } else {
746 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
747 }
748
749 // Schedule a pause timeout in case the app doesn't respond.
750 // We don't give it much time because this directly impacts the
751 // responsiveness seen by the user.
752 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
753 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700754 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
756 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
757 } else {
758 // This activity failed to schedule the
759 // pause, so just treat it as being paused now.
760 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700761 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 }
763 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700764
Craig Mautnerd2328952013-03-05 12:46:26 -0800765 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800767 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768
Craig Mautnerd2328952013-03-05 12:46:26 -0800769 final ActivityRecord r = isInStackLocked(token);
770 if (r != null) {
771 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
772 if (mPausingActivity == r) {
773 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
774 + (timeout ? " (due to timeout)" : " (pause complete)"));
775 r.state = ActivityState.PAUSED;
776 completePauseLocked();
777 } else {
778 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
779 r.userId, System.identityHashCode(r), r.shortComponentName,
780 mPausingActivity != null
781 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 }
783 }
784 }
785
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700786 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
787 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700788 if (r.state != ActivityState.STOPPING) {
789 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
790 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
791 return;
792 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700793 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700794 if (icicle != null) {
795 // If icicle is null, this is happening due to a timeout, so we
796 // haven't really saved the state.
797 r.icicle = icicle;
798 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800799 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700800 r.updateThumbnail(thumbnail, description);
801 }
802 if (!r.stopped) {
803 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
804 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
805 r.stopped = true;
806 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700807 if (r.finishing) {
808 r.clearOptionsLocked();
809 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700810 if (r.configDestroy) {
811 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700812 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700813 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700814 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800815 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700816 }
817 }
818 }
819
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800820 private final void completePauseLocked() {
821 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800823
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800824 if (prev != null) {
825 if (prev.finishing) {
826 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700827 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800828 } else if (prev.app != null) {
829 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
830 if (prev.waitingVisible) {
831 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700832 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800833 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
834 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800835 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800836 if (prev.configDestroy) {
837 // The previous is being paused because the configuration
838 // is changing, which means it is actually stopping...
839 // To juggle the fact that we are also starting a new
840 // instance right now, we need to first completely stop
841 // the current instance before starting the new one.
842 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
843 destroyActivityLocked(prev, true, false, "pause-config");
844 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700845 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700846 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
847 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800848 // If we already have a few activities waiting to stop,
849 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700850 // them out. Or if r is the last of activity of the last task the stack
851 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800852 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700853 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800854 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700855 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800856 }
857 }
858 } else {
859 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
860 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800861 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800862 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700863 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800864
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700865 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700866 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700867 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700869 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700870 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700871 if (top == null || (prev != null && top != prev)) {
872 // If there are no more activities available to run,
873 // do resume anyway to start something. Also if the top
874 // activity on the stack is not the just paused activity,
875 // we need to go ahead and resume it to ensure we complete
876 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700877 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700878 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800880
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800881 if (prev != null) {
882 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883
Craig Mautner525f3d92013-05-07 14:01:50 -0700884 if (prev.app != null && prev.cpuTimeAtResume > 0
885 && mService.mBatteryStatsService.isOnBattery()) {
886 long diff;
887 synchronized (mService.mProcessStatsThread) {
888 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
889 - prev.cpuTimeAtResume;
890 }
891 if (diff > 0) {
892 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
893 synchronized (bsi) {
894 BatteryStatsImpl.Uid.Proc ps =
895 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
896 prev.info.packageName);
897 if (ps != null) {
898 ps.addForegroundTimeLocked(diff);
899 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 }
901 }
902 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700903 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700904 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700905 }
906
907 /**
908 * Once we know that we have asked an application to put an activity in
909 * the resumed state (either by launching it or explicitly telling it),
910 * this function updates the rest of our state to match that fact.
911 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700912 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 next.idle = false;
914 next.results = null;
915 next.newIntents = null;
916
917 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700918 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700920 mStackSupervisor.reportResumedActivityLocked(next);
921
922 next.resumeKeyDispatchingLocked();
923 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700924
925 // Mark the point when the activity is resuming
926 // TODO: To be more accurate, the mark should be before the onCreate,
927 // not after the onResume. But for subsequent starts, onResume is fine.
928 if (next.app != null) {
929 synchronized (mService.mProcessStatsThread) {
930 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
931 }
932 } else {
933 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
934 }
935 }
936
Craig Mautner580ea812013-04-25 12:58:38 -0700937 /**
938 * Version of ensureActivitiesVisible that can easily be called anywhere.
939 */
940 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
941 return ensureActivitiesVisibleLocked(starting, configChanges, false);
942 }
943
944 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
945 boolean forceHomeShown) {
946 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700947 return r != null &&
948 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700949 }
950
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700951 /**
952 * Make sure that all activities that need to be visible (that is, they
953 * currently can be seen by the user) actually are.
954 */
Craig Mautner580ea812013-04-25 12:58:38 -0700955 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
956 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700957 if (DEBUG_VISBILITY) Slog.v(
958 TAG, "ensureActivitiesVisible behind " + top
959 + " configChanges=0x" + Integer.toHexString(configChanges));
960
Craig Mautner5eda9b32013-07-02 11:58:16 -0700961 if (mTranslucentActivityWaiting != top) {
962 mUndrawnActivitiesBelowTopTranslucent.clear();
963 if (mTranslucentActivityWaiting != null) {
964 // Call the callback with a timeout indication.
965 notifyActivityDrawnLocked(null);
966 mTranslucentActivityWaiting = null;
967 }
968 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
969 }
970
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700971 // If the top activity is not fullscreen, then we need to
972 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800973 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -0700974 boolean showHomeBehindStack = false;
975 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
976 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700977 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -0800978 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
979 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
980 final ActivityRecord r = activities.get(activityNdx);
981 if (r.finishing) {
982 continue;
983 }
984 if (aboveTop && r != top) {
985 continue;
986 }
987 aboveTop = false;
988 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -0800990 TAG, "Make visible? " + r + " finishing=" + r.finishing
991 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -0800992
Craig Mautnerd44711d2013-02-23 11:24:36 -0800993 final boolean doThisProcess = onlyThisProcess == null
994 || onlyThisProcess.equals(r.processName);
995
996 // First: if this is not the current activity being started, make
997 // sure it matches the current configuration.
998 if (r != starting && doThisProcess) {
999 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001000 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001001
1002 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001003 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001004 // This activity needs to be visible, but isn't even
1005 // running... get it started, but don't resume it
1006 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001007 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001008 if (r != starting) {
1009 r.startFreezingScreenLocked(r.app, configChanges);
1010 }
1011 if (!r.visible) {
1012 if (DEBUG_VISBILITY) Slog.v(
1013 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001014 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001015 }
1016 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001017 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001018 }
1019 }
1020
1021 } else if (r.visible) {
1022 // If this activity is already visible, then there is nothing
1023 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001024 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001025 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001026
1027 } else if (onlyThisProcess == null) {
1028 // This activity is not currently visible, but is running.
1029 // Tell it to become visible.
1030 r.visible = true;
1031 if (r.state != ActivityState.RESUMED && r != starting) {
1032 // If this activity is paused, tell it
1033 // to now show its window.
1034 if (DEBUG_VISBILITY) Slog.v(
1035 TAG, "Making visible and scheduling visibility: " + r);
1036 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001037 if (mTranslucentActivityWaiting != null) {
1038 mUndrawnActivitiesBelowTopTranslucent.add(r);
1039 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001041 r.sleeping = false;
1042 r.app.pendingUiClean = true;
1043 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1044 r.stopFreezingScreenLocked(false);
1045 } catch (Exception e) {
1046 // Just skip on any failure; we'll make it
1047 // visible when it next restarts.
1048 Slog.w(TAG, "Exception thrown making visibile: "
1049 + r.intent.getComponent(), e);
1050 }
1051 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001052 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001053
Craig Mautnerd44711d2013-02-23 11:24:36 -08001054 // Aggregate current change flags.
1055 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001056
Craig Mautnerd44711d2013-02-23 11:24:36 -08001057 if (r.fullscreen) {
1058 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001059 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1060 behindFullscreen = true;
1061 } else if (r.mLaunchHomeTaskNext) {
1062 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1063 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001064 behindFullscreen = true;
1065 }
1066 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001067 if (DEBUG_VISBILITY) Slog.v(
1068 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1069 + " state=" + r.state
1070 + " behindFullscreen=" + behindFullscreen);
1071 // Now for any activities that aren't visible to the user, make
1072 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001073 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001074 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001075 r.visible = false;
1076 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001077 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001078 switch (r.state) {
1079 case STOPPING:
1080 case STOPPED:
1081 if (r.app != null && r.app.thread != null) {
1082 if (DEBUG_VISBILITY) Slog.v(
1083 TAG, "Scheduling invisibility: " + r);
1084 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1085 }
1086 break;
1087
1088 case INITIALIZING:
1089 case RESUMED:
1090 case PAUSING:
1091 case PAUSED:
1092 stopActivityLocked(r);
1093 break;
1094
1095 default:
1096 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001097 }
1098 } catch (Exception e) {
1099 // Just skip on any failure; we'll make it
1100 // visible when it next restarts.
1101 Slog.w(TAG, "Exception thrown making hidden: "
1102 + r.intent.getComponent(), e);
1103 }
1104 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001105 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001106 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001107 }
1108 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001109 }
Craig Mautner580ea812013-04-25 12:58:38 -07001110 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001111 }
Craig Mautner58547802013-03-05 08:23:53 -08001112
Craig Mautner5eda9b32013-07-02 11:58:16 -07001113 void convertToTranslucent(ActivityRecord r) {
1114 mTranslucentActivityWaiting = r;
1115 mUndrawnActivitiesBelowTopTranslucent.clear();
1116 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1117 }
1118
1119 /**
1120 * Called as activities below the top translucent activity are redrawn. When the last one is
1121 * redrawn notify the top activity by calling
1122 * {@link Activity#onTranslucentConversionComplete}.
1123 *
1124 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1125 * occurred and the activity will be notified immediately.
1126 */
1127 void notifyActivityDrawnLocked(ActivityRecord r) {
1128 if ((r == null)
1129 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1130 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1131 // The last undrawn activity below the top has just been drawn. If there is an
1132 // opaque activity at the top, notify it that it can become translucent safely now.
1133 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1134 mTranslucentActivityWaiting = null;
1135 mUndrawnActivitiesBelowTopTranslucent.clear();
1136 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1137
1138 if (waitingActivity != null && waitingActivity.app != null &&
1139 waitingActivity.app.thread != null) {
1140 try {
1141 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1142 waitingActivity.appToken, r != null);
1143 } catch (RemoteException e) {
1144 }
1145 }
1146 }
1147 }
1148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001149 /**
1150 * Ensure that the top activity in the stack is resumed.
1151 *
1152 * @param prev The previously resumed activity, for when in the process
1153 * of pausing; can be null to call from elsewhere.
1154 *
1155 * @return Returns true if something is being resumed, or false if
1156 * nothing happened.
1157 */
1158 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001159 return resumeTopActivityLocked(prev, null);
1160 }
1161
1162 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001163 // Find the first activity that is not finishing.
1164 ActivityRecord next = topRunningActivityLocked(null);
1165
1166 // Remember how we'll process this pause/resume situation, and ensure
1167 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001168 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1169 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170
1171 if (next == null) {
1172 // There are no more activities! Let's just start up the
1173 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001174 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001175 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001176 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001177 }
1178
1179 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001181 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001182 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1183 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184 // Make sure we have executed any pending transitions, since there
1185 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001186 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001188 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001189 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001190 return false;
1191 }
1192
Craig Mautner525f3d92013-05-07 14:01:50 -07001193 final TaskRecord nextTask = next.task;
1194 final TaskRecord prevTask = prev != null ? prev.task : null;
1195 if (prevTask != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001196 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001197 if (prevTask == nextTask) {
1198 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1199 final int numActivities = activities.size();
1200 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1201 final ActivityRecord r = activities.get(activityNdx);
1202 // r is usually the same as next, but what if two activities were launched
1203 // before prev finished?
1204 if (!r.finishing) {
1205 r.mLaunchHomeTaskNext = true;
1206 r.frontOfTask = true;
1207 break;
1208 }
1209 }
1210 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001211 // This task is going away but it was supposed to return to the home task.
1212 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001213 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001214 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1215 } else {
1216 return mStackSupervisor.resumeHomeActivity(prev);
1217 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001218 }
1219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001220 // If we are sleeping, and there is no resumed activity, and the top
1221 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001222 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001223 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001224 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001225 // Make sure we have executed any pending transitions, since there
1226 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001227 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001228 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001229 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001230 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001231 return false;
1232 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001233
1234 // Make sure that the user who owns this activity is started. If not,
1235 // we will just leave it as is because someone should be bringing
1236 // another user's activities to the top of the stack.
1237 if (mService.mStartedUsers.get(next.userId) == null) {
1238 Slog.w(TAG, "Skipping resume of top activity " + next
1239 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001240 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001241 return false;
1242 }
1243
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001244 // The activity may be waiting for stop, but that is no longer
1245 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001246 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001247 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001248 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001249 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001250
Dianne Hackborn84375872012-06-01 19:03:50 -07001251 next.updateOptionsLocked(options);
1252
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1254
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001255 // If we are currently pausing an activity, then don't do anything
1256 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001257 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1258 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Craig Mautnercf910b02013-04-23 11:23:27 -07001259 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001260 return false;
1261 }
1262
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001263 // Okay we are now going to start a switch, to 'next'. We may first
1264 // have to pause the current activity, but this is an important point
1265 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001266 // XXX "App Redirected" dialog is getting too many false positives
1267 // at this point, so turn off for now.
1268 if (false) {
1269 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1270 long now = SystemClock.uptimeMillis();
1271 final boolean inTime = mLastStartedActivity.startTime != 0
1272 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1273 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1274 final int nextUid = next.info.applicationInfo.uid;
1275 if (inTime && lastUid != nextUid
1276 && lastUid != next.launchedFromUid
1277 && mService.checkPermission(
1278 android.Manifest.permission.STOP_APP_SWITCHES,
1279 -1, next.launchedFromUid)
1280 != PackageManager.PERMISSION_GRANTED) {
1281 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1282 } else {
1283 next.startTime = now;
1284 mLastStartedActivity = next;
1285 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001286 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001287 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001288 mLastStartedActivity = next;
1289 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001290 }
Craig Mautner58547802013-03-05 08:23:53 -08001291
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001292 // We need to start pausing the current activity so the top one
1293 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001294 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1295 if (mResumedActivity != null) {
1296 pausing = true;
1297 startPausingLocked(userLeaving, false);
1298 }
1299 if (pausing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001300 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001301 // At this point we want to put the upcoming activity's process
1302 // at the top of the LRU list, since we know we will be needing it
1303 // very soon and it would be a waste to let it get killed if it
1304 // happens to be sitting towards the end.
1305 if (next.app != null && next.app.thread != null) {
1306 // No reason to do full oom adj update here; we'll let that
1307 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001308 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001309 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001310 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001311 return true;
1312 }
1313
Christopher Tated3f175c2012-06-14 14:16:54 -07001314 // If the most recent activity was noHistory but was only stopped rather
1315 // than stopped+finished because the device went to sleep, we need to make
1316 // sure to finish it as we're making a new activity topmost.
1317 final ActivityRecord last = mLastPausedActivity;
1318 if (mService.mSleeping && last != null && !last.finishing) {
1319 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1320 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1321 if (DEBUG_STATES) {
1322 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1323 }
1324 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001325 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001326 }
1327 }
1328
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001329 if (prev != null && prev != next) {
1330 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1331 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001332 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001333 if (DEBUG_SWITCH) Slog.v(
1334 TAG, "Resuming top, waiting visible to hide: " + prev);
1335 } else {
1336 // The next activity is already visible, so hide the previous
1337 // activity's windows right now so we can show the new one ASAP.
1338 // We only do this if the previous is finishing, which should mean
1339 // it is on top of the one being resumed so hiding it quickly
1340 // is good. Otherwise, we want to do the normal route of allowing
1341 // the resumed activity to be shown so we can decide if the
1342 // previous should actually be hidden depending on whether the
1343 // new one is found to be full-screen or not.
1344 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001345 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001346 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1347 + prev + ", waitingVisible="
1348 + (prev != null ? prev.waitingVisible : null)
1349 + ", nowVisible=" + next.nowVisible);
1350 } else {
1351 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1352 + prev + ", waitingVisible="
1353 + (prev != null ? prev.waitingVisible : null)
1354 + ", nowVisible=" + next.nowVisible);
1355 }
1356 }
1357 }
1358
Dianne Hackborne7f97212011-02-24 14:40:20 -08001359 // Launching this app's activity, make sure the app is no longer
1360 // considered stopped.
1361 try {
1362 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001363 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001364 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001365 } catch (IllegalArgumentException e) {
1366 Slog.w(TAG, "Failed trying to unstop package "
1367 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001368 }
1369
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001370 // We are starting up the next activity, so tell the window manager
1371 // that the previous one will be hidden soon. This way it can know
1372 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001373 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001374 if (prev != null) {
1375 if (prev.finishing) {
1376 if (DEBUG_TRANSITION) Slog.v(TAG,
1377 "Prepare close transition: prev=" + prev);
1378 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001379 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001380 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001381 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001382 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001383 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1384 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001385 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001386 mWindowManager.setAppWillBeHidden(prev.appToken);
1387 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001388 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001389 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001391 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001392 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001393 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001394 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001395 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1396 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001397 }
1398 }
1399 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001400 mWindowManager.setAppWillBeHidden(prev.appToken);
1401 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 }
Craig Mautner967212c2013-04-13 21:10:58 -07001403 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001404 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001405 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001406 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001407 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001408 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001409 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001410 }
1411 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001412 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001413 next.applyOptionsLocked();
1414 } else {
1415 next.clearOptionsLocked();
1416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417
Craig Mautnercf910b02013-04-23 11:23:27 -07001418 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 if (next.app != null && next.app.thread != null) {
1420 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1421
1422 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001423 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001425 // schedule launch ticks to collect information about slow apps.
1426 next.startLaunchTickingLocked();
1427
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001428 ActivityRecord lastResumedActivity =
1429 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 ActivityState lastState = next.state;
1431
1432 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001433
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001434 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 next.state = ActivityState.RESUMED;
1436 mResumedActivity = next;
1437 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001438 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001439 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001440 updateLRUListLocked(next);
1441
1442 // Have the window manager re-evaluate the orientation of
1443 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001444 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001445 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001446 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001447 mService.mConfiguration,
1448 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1449 if (config != null) {
1450 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001452 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001453 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001454
Craig Mautner525f3d92013-05-07 14:01:50 -07001455 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001456 // The configuration update wasn't able to keep the existing
1457 // instance of the activity, and instead started a new one.
1458 // We should be all done, but let's just make sure our activity
1459 // is still at the top and schedule another run if something
1460 // weird happened.
1461 ActivityRecord nextNext = topRunningActivityLocked(null);
1462 if (DEBUG_SWITCH) Slog.i(TAG,
1463 "Activity config changed during resume: " + next
1464 + ", new next: " + nextNext);
1465 if (nextNext != next) {
1466 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001467 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001469 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001470 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001471 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001472 return true;
1473 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001474 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001475 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 }
Craig Mautner58547802013-03-05 08:23:53 -08001477
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478 try {
1479 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001480 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 if (a != null) {
1482 final int N = a.size();
1483 if (!next.finishing && N > 0) {
1484 if (DEBUG_RESULTS) Slog.v(
1485 TAG, "Delivering results to " + next
1486 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001487 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 }
1489 }
1490
1491 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001492 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001493 }
1494
1495 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001496 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001498
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001499 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001500 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001501 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001502 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1503 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001505
Craig Mautner0eea92c2013-05-16 13:35:39 -07001506 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507
1508 } catch (Exception e) {
1509 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001510 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1511 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001513 if (lastStack != null) {
1514 lastStack.mResumedActivity = lastResumedActivity;
1515 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001516 Slog.i(TAG, "Restarting because process died: " + next);
1517 if (!next.hasBeenLaunched) {
1518 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001519 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1520 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001521 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001522 next.appToken, next.packageName, next.theme,
1523 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001524 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1525 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001527 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001528 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 return true;
1530 }
1531
1532 // From this point on, if something goes wrong there is no way
1533 // to recover the activity.
1534 try {
1535 next.visible = true;
1536 completeResumeLocked(next);
1537 } catch (Exception e) {
1538 // If any exception gets thrown, toss away this
1539 // activity and try the next one.
1540 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001541 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001542 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001543 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 return true;
1545 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 next.stopped = false;
1547
1548 } else {
1549 // Whoops, need to restart this activity!
1550 if (!next.hasBeenLaunched) {
1551 next.hasBeenLaunched = true;
1552 } else {
1553 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001554 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001555 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001556 mService.compatibilityInfoForPackageLocked(
1557 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001559 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001560 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 }
1562 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1563 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001564 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 }
1566
Craig Mautnercf910b02013-04-23 11:23:27 -07001567 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 return true;
1569 }
1570
Craig Mautner11bf9a52013-02-19 14:08:51 -08001571
Craig Mautner8849a5e2013-04-02 16:41:03 -07001572 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001573 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001574 TaskRecord rTask = r.task;
1575 final int taskId = rTask.taskId;
1576 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001577 // Last activity in task had been removed or ActivityManagerService is reusing task.
1578 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001579 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001580 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001581 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001582 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001583 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001584 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001585 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001586 if (!newTask) {
1587 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001588 boolean startIt = true;
1589 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1590 task = mTaskHistory.get(taskNdx);
1591 if (task == r.task) {
1592 // Here it is! Now, if this is not yet visible to the
1593 // user, then just add it without starting; it will
1594 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001595 if (!startIt) {
1596 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1597 + task, new RuntimeException("here").fillInStackTrace());
1598 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001599 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001600 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1601 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001602 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001604 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001605 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001606 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 return;
1608 }
1609 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001610 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 startIt = false;
1612 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 }
1614 }
1615
1616 // Place a new activity at top of stack, so it is next to interact
1617 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 // If we are not placing the new activity frontmost, we do not want
1620 // to deliver the onUserLeaving callback to the actual frontmost
1621 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001622 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001623 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001624 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1625 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 }
Craig Mautner70a86932013-02-28 22:37:44 -08001627
1628 task = r.task;
1629
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001631 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001632 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001633 task.addActivityToTop(r);
1634
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001635 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001637 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638 // We want to show the starting preview window if we are
1639 // switching to a new task, or the next activity's process is
1640 // not currently running.
1641 boolean showStartingIcon = newTask;
1642 ProcessRecord proc = r.app;
1643 if (proc == null) {
1644 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1645 }
1646 if (proc == null || proc.thread == null) {
1647 showStartingIcon = true;
1648 }
1649 if (DEBUG_TRANSITION) Slog.v(TAG,
1650 "Prepare open transition: starting " + r);
1651 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001652 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001654 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001655 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001656 ? AppTransition.TRANSIT_TASK_OPEN
1657 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001658 mNoAnimActivities.remove(r);
1659 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001660 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001661 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001662 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001663 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001664 boolean doShow = true;
1665 if (newTask) {
1666 // Even though this activity is starting fresh, we still need
1667 // to reset it to make sure we apply affinities to move any
1668 // existing activities from other tasks in to it.
1669 // If the caller has requested that the target task be
1670 // reset, then do so.
1671 if ((r.intent.getFlags()
1672 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1673 resetTaskIfNeededLocked(r, r);
1674 doShow = topRunningNonDelayedActivityLocked(null) == r;
1675 }
1676 }
1677 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1678 // Figure out if we are transitioning from another activity that is
1679 // "has the same starting icon" as the next one. This allows the
1680 // window manager to keep the previous window it had previously
1681 // created, if it still had one.
1682 ActivityRecord prev = mResumedActivity;
1683 if (prev != null) {
1684 // We don't want to reuse the previous starting preview if:
1685 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001686 if (prev.task != r.task) {
1687 prev = null;
1688 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001689 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001690 else if (prev.nowVisible) {
1691 prev = null;
1692 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001694 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001695 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001696 mService.compatibilityInfoForPackageLocked(
1697 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001698 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001699 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 }
1701 } else {
1702 // If this is the first activity, don't do any fancy animations,
1703 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001704 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001705 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001706 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001707 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001708 }
1709 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001710 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711 }
1712
1713 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001714 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 }
1716 }
1717
Dianne Hackbornbe707852011-11-11 14:32:10 -08001718 final void validateAppTokensLocked() {
1719 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001720 mValidateAppTokens.ensureCapacity(numActivities());
1721 final int numTasks = mTaskHistory.size();
1722 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1723 TaskRecord task = mTaskHistory.get(taskNdx);
1724 final ArrayList<ActivityRecord> activities = task.mActivities;
1725 if (activities.size() == 0) {
1726 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001727 }
Craig Mautner000f0022013-02-26 15:04:29 -08001728 TaskGroup group = new TaskGroup();
1729 group.taskId = task.taskId;
1730 mValidateAppTokens.add(group);
1731 final int numActivities = activities.size();
1732 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1733 final ActivityRecord r = activities.get(activityNdx);
1734 group.tokens.add(r.appToken);
1735 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001736 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001737 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001738 }
1739
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 /**
1741 * Perform a reset of the given task, if needed as part of launching it.
1742 * Returns the new HistoryRecord at the top of the task.
1743 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001744 /**
1745 * Helper method for #resetTaskIfNeededLocked.
1746 * We are inside of the task being reset... we'll either finish this activity, push it out
1747 * for another task, or leave it as-is.
1748 * @param task The task containing the Activity (taskTop) that might be reset.
1749 * @param forceReset
1750 * @return An ActivityOptions that needs to be processed.
1751 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001752 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001753 ActivityOptions topOptions = null;
1754
1755 int replyChainEnd = -1;
1756 boolean canMoveOptions = true;
1757
1758 // We only do this for activities that are not the root of the task (since if we finish
1759 // the root, we may no longer have the task!).
1760 final ArrayList<ActivityRecord> activities = task.mActivities;
1761 final int numActivities = activities.size();
1762 for (int i = numActivities - 1; i > 0; --i ) {
1763 ActivityRecord target = activities.get(i);
1764
1765 final int flags = target.info.flags;
1766 final boolean finishOnTaskLaunch =
1767 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1768 final boolean allowTaskReparenting =
1769 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1770 final boolean clearWhenTaskReset =
1771 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1772
1773 if (!finishOnTaskLaunch
1774 && !clearWhenTaskReset
1775 && target.resultTo != null) {
1776 // If this activity is sending a reply to a previous
1777 // activity, we can't do anything with it now until
1778 // we reach the start of the reply chain.
1779 // XXX note that we are assuming the result is always
1780 // to the previous activity, which is almost always
1781 // the case but we really shouldn't count on.
1782 if (replyChainEnd < 0) {
1783 replyChainEnd = i;
1784 }
1785 } else if (!finishOnTaskLaunch
1786 && !clearWhenTaskReset
1787 && allowTaskReparenting
1788 && target.taskAffinity != null
1789 && !target.taskAffinity.equals(task.affinity)) {
1790 // If this activity has an affinity for another
1791 // task, then we need to move it out of here. We will
1792 // move it as far out of the way as possible, to the
1793 // bottom of the activity stack. This also keeps it
1794 // correctly ordered with any activities we previously
1795 // moved.
1796 TaskRecord bottomTask = mTaskHistory.get(0);
1797 ActivityRecord p = bottomTask.mActivities.get(0);
1798 if (target.taskAffinity != null
1799 && target.taskAffinity.equals(p.task.affinity)) {
1800 // If the activity currently at the bottom has the
1801 // same task affinity as the one we are moving,
1802 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001803 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001804 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1805 + " out to bottom task " + p.task);
1806 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001807 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001808 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001809 target.task.affinityIntent = target.intent;
1810 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1811 + " out to new task " + target.task);
1812 }
1813
1814 final TaskRecord targetTask = target.task;
1815 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001816 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001817
Craig Mautner525f3d92013-05-07 14:01:50 -07001818 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001819 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1820 for (int srcPos = start; srcPos >= i; --srcPos) {
1821 p = activities.get(srcPos);
1822 if (p.finishing) {
1823 continue;
1824 }
1825
Craig Mautner525f3d92013-05-07 14:01:50 -07001826 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001827 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001828 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001829 topOptions = p.takeOptionsLocked();
1830 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001831 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001832 }
1833 }
1834 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1835 + task + " adding to task=" + targetTask,
1836 new RuntimeException("here").fillInStackTrace());
1837 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1838 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001839 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001840 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001841
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001842 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001843 }
1844
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001845 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001846 if (VALIDATE_TOKENS) {
1847 validateAppTokensLocked();
1848 }
1849
1850 replyChainEnd = -1;
1851 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1852 // If the activity should just be removed -- either
1853 // because it asks for it, or the task should be
1854 // cleared -- then finish it and anything that is
1855 // part of its reply chain.
1856 int end;
1857 if (clearWhenTaskReset) {
1858 // In this case, we want to finish this activity
1859 // and everything above it, so be sneaky and pretend
1860 // like these are all in the reply chain.
1861 end = numActivities - 1;
1862 } else if (replyChainEnd < 0) {
1863 end = i;
1864 } else {
1865 end = replyChainEnd;
1866 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001867 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001868 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001869 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001870 if (p.finishing) {
1871 continue;
1872 }
1873 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001874 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001875 topOptions = p.takeOptionsLocked();
1876 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001877 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001878 }
1879 }
Craig Mautner58547802013-03-05 08:23:53 -08001880 if (DEBUG_TASKS) Slog.w(TAG,
1881 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001882 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001883 end--;
1884 srcPos--;
1885 }
1886 }
1887 replyChainEnd = -1;
1888 } else {
1889 // If we were in the middle of a chain, well the
1890 // activity that started it all doesn't want anything
1891 // special, so leave it all as-is.
1892 replyChainEnd = -1;
1893 }
1894 }
1895
1896 return topOptions;
1897 }
1898
1899 /**
1900 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1901 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1902 * @param affinityTask The task we are looking for an affinity to.
1903 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1904 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1905 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1906 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001907 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001908 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001909 int replyChainEnd = -1;
1910 final int taskId = task.taskId;
1911 final String taskAffinity = task.affinity;
1912
1913 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1914 final int numActivities = activities.size();
1915 // Do not operate on the root Activity.
1916 for (int i = numActivities - 1; i > 0; --i) {
1917 ActivityRecord target = activities.get(i);
1918
1919 final int flags = target.info.flags;
1920 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1921 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1922
1923 if (target.resultTo != null) {
1924 // If this activity is sending a reply to a previous
1925 // activity, we can't do anything with it now until
1926 // we reach the start of the reply chain.
1927 // XXX note that we are assuming the result is always
1928 // to the previous activity, which is almost always
1929 // the case but we really shouldn't count on.
1930 if (replyChainEnd < 0) {
1931 replyChainEnd = i;
1932 }
1933 } else if (topTaskIsHigher
1934 && allowTaskReparenting
1935 && taskAffinity != null
1936 && taskAffinity.equals(target.taskAffinity)) {
1937 // This activity has an affinity for our task. Either remove it if we are
1938 // clearing or move it over to our task. Note that
1939 // we currently punt on the case where we are resetting a
1940 // task that is not at the top but who has activities above
1941 // with an affinity to it... this is really not a normal
1942 // case, and we will need to later pull that task to the front
1943 // and usually at that point we will do the reset and pick
1944 // up those remaining activities. (This only happens if
1945 // someone starts an activity in a new task from an activity
1946 // in a task that is not currently on top.)
1947 if (forceReset || finishOnTaskLaunch) {
1948 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1949 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1950 for (int srcPos = start; srcPos >= i; --srcPos) {
1951 final ActivityRecord p = activities.get(srcPos);
1952 if (p.finishing) {
1953 continue;
1954 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001955 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001956 }
1957 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001958 if (taskInsertionPoint < 0) {
1959 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001960
Craig Mautner77878772013-03-04 19:46:24 -08001961 }
Craig Mautner77878772013-03-04 19:46:24 -08001962
1963 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1964 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1965 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1966 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001967 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001968 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08001969 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001970
Craig Mautnere3a74d52013-02-22 14:14:58 -08001971 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
1972 + " to stack at " + task,
1973 new RuntimeException("here").fillInStackTrace());
1974 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
1975 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001976 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001977 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001978 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08001979 if (VALIDATE_TOKENS) {
1980 validateAppTokensLocked();
1981 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08001982
1983 // Now we've moved it in to place... but what if this is
1984 // a singleTop activity and we have put it on top of another
1985 // instance of the same activity? Then we drop the instance
1986 // below so it remains singleTop.
1987 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
1988 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001989 int targetNdx = taskActivities.indexOf(target);
1990 if (targetNdx > 0) {
1991 ActivityRecord p = taskActivities.get(targetNdx - 1);
1992 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08001993 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
1994 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001995 }
1996 }
1997 }
1998 }
1999
2000 replyChainEnd = -1;
2001 }
2002 }
Craig Mautner77878772013-03-04 19:46:24 -08002003 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002004 }
2005
Craig Mautner8849a5e2013-04-02 16:41:03 -07002006 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002007 ActivityRecord newActivity) {
2008 boolean forceReset =
2009 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2010 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2011 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2012 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2013 forceReset = true;
2014 }
2015 }
2016
2017 final TaskRecord task = taskTop.task;
2018
2019 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2020 * for remaining tasks. Used for later tasks to reparent to task. */
2021 boolean taskFound = false;
2022
2023 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2024 ActivityOptions topOptions = null;
2025
Craig Mautner77878772013-03-04 19:46:24 -08002026 // Preserve the location for reparenting in the new task.
2027 int reparentInsertionPoint = -1;
2028
Craig Mautnere3a74d52013-02-22 14:14:58 -08002029 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2030 final TaskRecord targetTask = mTaskHistory.get(i);
2031
2032 if (targetTask == task) {
2033 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2034 taskFound = true;
2035 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002036 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2037 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002038 }
2039 }
2040
Craig Mautner70a86932013-02-28 22:37:44 -08002041 int taskNdx = mTaskHistory.indexOf(task);
2042 do {
2043 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2044 } while (taskTop == null && taskNdx >= 0);
2045
Craig Mautnere3a74d52013-02-22 14:14:58 -08002046 if (topOptions != null) {
2047 // If we got some ActivityOptions from an activity on top that
2048 // was removed from the task, propagate them to the new real top.
2049 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002050 taskTop.updateOptionsLocked(topOptions);
2051 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002052 topOptions.abort();
2053 }
2054 }
2055
2056 return taskTop;
2057 }
2058
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002059 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2060 String resultWho, int requestCode, int resultCode, Intent data) {
2061
2062 if (callingUid > 0) {
2063 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002064 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002065 }
2066
2067 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2068 + " : who=" + resultWho + " req=" + requestCode
2069 + " res=" + resultCode + " data=" + data);
2070 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2071 try {
2072 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2073 list.add(new ResultInfo(resultWho, requestCode,
2074 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002075 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002076 return;
2077 } catch (Exception e) {
2078 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2079 }
2080 }
2081
2082 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2083 }
2084
Craig Mautnerf3333272013-04-22 10:55:53 -07002085 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002086 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2087 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2088 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2089 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002090 if (!mService.mSleeping) {
2091 if (DEBUG_STATES) {
2092 Slog.d(TAG, "no-history finish of " + r);
2093 }
2094 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002095 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002096 } else {
2097 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2098 + " on stop because we're just sleeping");
2099 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002100 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002101 }
2102
2103 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002104 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002105 if (mService.mFocusedActivity == r) {
2106 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2107 }
2108 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002109 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 try {
2111 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002112 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2113 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 r.state = ActivityState.STOPPING;
2115 if (DEBUG_VISBILITY) Slog.v(
2116 TAG, "Stopping visible=" + r.visible + " for " + r);
2117 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002118 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002120 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002121 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002122 r.setSleeping(true);
2123 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002124 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002125 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 } catch (Exception e) {
2127 // Maybe just ignore exceptions here... if the process
2128 // has crashed, our death notification will clean things
2129 // up.
2130 Slog.w(TAG, "Exception thrown during pause", e);
2131 // Just in case, assume it to be stopped.
2132 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002133 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 r.state = ActivityState.STOPPED;
2135 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002136 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 }
2138 }
2139 }
2140 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 /**
2143 * @return Returns true if the activity is being finished, false if for
2144 * some reason it is being left as-is.
2145 */
2146 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002147 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002148 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002149 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002150 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002151 + ", result=" + resultCode + ", data=" + resultData
2152 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002153 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002154 return false;
2155 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156
Craig Mautnerd44711d2013-02-23 11:24:36 -08002157 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002158 return true;
2159 }
2160
Craig Mautnerd2328952013-03-05 12:46:26 -08002161 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002162 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2163 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2164 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2165 ActivityRecord r = activities.get(activityNdx);
2166 if (r.resultTo == self && r.requestCode == requestCode) {
2167 if ((r.resultWho == null && resultWho == null) ||
2168 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2169 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2170 false);
2171 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002172 }
2173 }
2174 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002175 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002176 }
2177
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002178 final void finishTopRunningActivityLocked(ProcessRecord app) {
2179 ActivityRecord r = topRunningActivityLocked(null);
2180 if (r != null && r.app == app) {
2181 // If the top running activity is from this crashing
2182 // process, then terminate it to avoid getting in a loop.
2183 Slog.w(TAG, " Force finishing activity "
2184 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002185 int taskNdx = mTaskHistory.indexOf(r.task);
2186 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002187 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002188 // Also terminate any activities below it that aren't yet
2189 // stopped, to avoid a situation where one will get
2190 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002191 --activityNdx;
2192 if (activityNdx < 0) {
2193 do {
2194 --taskNdx;
2195 if (taskNdx < 0) {
2196 break;
2197 }
2198 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2199 } while (activityNdx < 0);
2200 }
2201 if (activityNdx >= 0) {
2202 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002203 if (r.state == ActivityState.RESUMED
2204 || r.state == ActivityState.PAUSING
2205 || r.state == ActivityState.PAUSED) {
Craig Mautner86d67a42013-05-14 10:34:38 -07002206 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002207 Slog.w(TAG, " Force finishing activity "
2208 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002209 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002210 }
2211 }
2212 }
2213 }
2214 }
2215
Craig Mautnerd2328952013-03-05 12:46:26 -08002216 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002217 ArrayList<ActivityRecord> activities = r.task.mActivities;
2218 for (int index = activities.indexOf(r); index >= 0; --index) {
2219 ActivityRecord cur = activities.get(index);
2220 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002221 break;
2222 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002223 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002224 }
2225 return true;
2226 }
2227
Dianne Hackborn5c607432012-02-28 14:44:19 -08002228 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2229 // send the result
2230 ActivityRecord resultTo = r.resultTo;
2231 if (resultTo != null) {
2232 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2233 + " who=" + r.resultWho + " req=" + r.requestCode
2234 + " res=" + resultCode + " data=" + resultData);
2235 if (r.info.applicationInfo.uid > 0) {
2236 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2237 resultTo.packageName, resultData,
2238 resultTo.getUriPermissionsLocked());
2239 }
2240 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2241 resultData);
2242 r.resultTo = null;
2243 }
2244 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2245
2246 // Make sure this HistoryRecord is not holding on to other resources,
2247 // because clients have remote IPC references to this object so we
2248 // can't assume that will go away and want to avoid circular IPC refs.
2249 r.results = null;
2250 r.pendingResults = null;
2251 r.newIntents = null;
2252 r.icicle = null;
2253 }
2254
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002255 /**
2256 * @return Returns true if this activity has been removed from the history
2257 * list, or false if it is still in the list and will be removed later.
2258 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002259 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2260 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002261 if (r.finishing) {
2262 Slog.w(TAG, "Duplicate finish request for " + r);
2263 return false;
2264 }
2265
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002266 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002267 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002268 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002269 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002270 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2271 final int index = activities.indexOf(r);
2272 if (index < (activities.size() - 1)) {
2273 ActivityRecord next = activities.get(index+1);
2274 if (r.frontOfTask) {
2275 // The next activity is now the front of the task.
2276 next.frontOfTask = true;
2277 }
2278 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2279 // If the caller asked that this activity (and all above it)
2280 // be cleared when the task is reset, don't lose that information,
2281 // but propagate it up to the next activity.
2282 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283 }
2284 }
2285
2286 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002287 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002288 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002289 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002290 }
2291 }
2292
Dianne Hackborn5c607432012-02-28 14:44:19 -08002293 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002294
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 if (mService.mPendingThumbnails.size() > 0) {
2296 // There are clients waiting to receive thumbnails so, in case
2297 // this is an activity that someone is waiting for, add it
2298 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002299 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002300 }
2301
Craig Mautnerde4ef022013-04-07 19:01:33 -07002302 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002303 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002304 if (DEBUG_TRANSITION) Slog.v(TAG,
2305 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002306 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002307 ? AppTransition.TRANSIT_TASK_CLOSE
2308 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002309
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002310 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002311 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002312
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002313 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2315 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2316 startPausingLocked(false, false);
2317 }
2318
2319 } else if (r.state != ActivityState.PAUSING) {
2320 // If the activity is PAUSING, we will complete the finish once
2321 // it is done pausing; else we can just directly finish it here.
2322 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002323 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002324 } else {
2325 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2326 }
2327
2328 return false;
2329 }
2330
Craig Mautnerf3333272013-04-22 10:55:53 -07002331 static final int FINISH_IMMEDIATELY = 0;
2332 static final int FINISH_AFTER_PAUSE = 1;
2333 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334
Craig Mautnerf3333272013-04-22 10:55:53 -07002335 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 // First things first: if this activity is currently visible,
2337 // and the resumed activity is not yet visible, then hold off on
2338 // finishing until the resumed one becomes visible.
2339 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002340 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2341 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002342 if (mStackSupervisor.mStoppingActivities.size() > 3
2343 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002344 // If we already have a few activities waiting to stop,
2345 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002346 // them out. Or if r is the last of activity of the last task the stack
2347 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002348 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002349 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002350 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 }
2352 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002353 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2354 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002356 if (oomAdj) {
2357 mService.updateOomAdjLocked();
2358 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002359 return r;
2360 }
2361
2362 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002363 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002364 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002365 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366 if (mResumedActivity == r) {
2367 mResumedActivity = null;
2368 }
2369 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002370 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 r.state = ActivityState.FINISHING;
2372
2373 if (mode == FINISH_IMMEDIATELY
2374 || prevState == ActivityState.STOPPED
2375 || prevState == ActivityState.INITIALIZING) {
2376 // If this activity is already stopped, we can just finish
2377 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002378 boolean activityRemoved = destroyActivityLocked(r, true,
2379 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002380 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002381 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002382 }
2383 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002385
2386 // Need to go through the full pause cycle to get this
2387 // activity into the stopped state and then finish it.
2388 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002389 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002390 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 return r;
2392 }
2393
Craig Mautnerd2328952013-03-05 12:46:26 -08002394 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002395 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002396 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002397 final TaskRecord task = srec.task;
2398 final ArrayList<ActivityRecord> activities = task.mActivities;
2399 final int start = activities.indexOf(srec);
2400 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002401 return false;
2402 }
2403 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002404 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002405 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002406 final ComponentName dest = destIntent.getComponent();
2407 if (start > 0 && dest != null) {
2408 for (int i = finishTo; i >= 0; i--) {
2409 ActivityRecord r = activities.get(i);
2410 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002411 r.info.name.equals(dest.getClassName())) {
2412 finishTo = i;
2413 parent = r;
2414 foundParentInTask = true;
2415 break;
2416 }
2417 }
2418 }
2419
2420 IActivityController controller = mService.mController;
2421 if (controller != null) {
2422 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2423 if (next != null) {
2424 // ask watcher if this is allowed
2425 boolean resumeOK = true;
2426 try {
2427 resumeOK = controller.activityResuming(next.packageName);
2428 } catch (RemoteException e) {
2429 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002430 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002431 }
2432
2433 if (!resumeOK) {
2434 return false;
2435 }
2436 }
2437 }
2438 final long origId = Binder.clearCallingIdentity();
2439 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002440 ActivityRecord r = activities.get(i);
2441 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002442 // Only return the supplied result for the first activity finished
2443 resultCode = Activity.RESULT_CANCELED;
2444 resultData = null;
2445 }
2446
2447 if (parent != null && foundParentInTask) {
2448 final int parentLaunchMode = parent.info.launchMode;
2449 final int destIntentFlags = destIntent.getFlags();
2450 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2451 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2452 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2453 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2454 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2455 } else {
2456 try {
2457 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2458 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002459 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002460 null, aInfo, parent.appToken, null,
2461 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2462 0, null, true, null);
2463 foundParentInTask = res == ActivityManager.START_SUCCESS;
2464 } catch (RemoteException e) {
2465 foundParentInTask = false;
2466 }
2467 requestFinishActivityLocked(parent.appToken, resultCode,
2468 resultData, "navigate-up", true);
2469 }
2470 }
2471 Binder.restoreCallingIdentity(origId);
2472 return foundParentInTask;
2473 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002474 /**
2475 * Perform the common clean-up of an activity record. This is called both
2476 * as part of destroyActivityLocked() (when destroying the client-side
2477 * representation) and cleaning things up as a result of its hosting
2478 * processing going away, in which case there is no remaining client-side
2479 * state to destroy so only the cleanup here is needed.
2480 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002481 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2482 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 if (mResumedActivity == r) {
2484 mResumedActivity = null;
2485 }
2486 if (mService.mFocusedActivity == r) {
2487 mService.mFocusedActivity = null;
2488 }
2489
2490 r.configDestroy = false;
2491 r.frozenBeforeDestroy = false;
2492
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002493 if (setState) {
2494 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2495 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002496 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002497 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002498 }
2499
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002500 // Make sure this record is no longer in the pending finishes list.
2501 // This could happen, for example, if we are trimming activities
2502 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002503 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002504 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 // Remove any pending results.
2507 if (r.finishing && r.pendingResults != null) {
2508 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2509 PendingIntentRecord rec = apr.get();
2510 if (rec != null) {
2511 mService.cancelIntentSenderLocked(rec, false);
2512 }
2513 }
2514 r.pendingResults = null;
2515 }
2516
2517 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002518 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002519 }
2520
2521 if (mService.mPendingThumbnails.size() > 0) {
2522 // There are clients waiting to receive thumbnails so, in case
2523 // this is an activity that someone is waiting for, add it
2524 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002525 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002526 }
2527
2528 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002529 removeTimeoutsForActivityLocked(r);
2530 }
2531
2532 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002533 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002535 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002536 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002537 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 }
2539
Dianne Hackborn5c607432012-02-28 14:44:19 -08002540 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002541 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2542 r.makeFinishing();
2543 if (DEBUG_ADD_REMOVE) {
2544 RuntimeException here = new RuntimeException("here");
2545 here.fillInStackTrace();
2546 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002548 final TaskRecord task = r.task;
2549 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002550 if (DEBUG_STACK) Slog.i(TAG,
2551 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002552 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002553 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002554 r.takeFromHistory();
2555 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002556 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002557 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002558 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002559 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002560 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002561 if (VALIDATE_TOKENS) {
2562 validateAppTokensLocked();
2563 }
2564 cleanUpActivityServicesLocked(r);
2565 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002566 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002568 /**
2569 * Perform clean-up of service connections in an activity record.
2570 */
2571 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2572 // Throw away any services that have been bound by this activity.
2573 if (r.connections != null) {
2574 Iterator<ConnectionRecord> it = r.connections.iterator();
2575 while (it.hasNext()) {
2576 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002577 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 }
2579 r.connections = null;
2580 }
2581 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002582
2583 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2584 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2585 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2586 mHandler.sendMessage(msg);
2587 }
2588
Dianne Hackborn28695e02011-11-02 21:59:51 -07002589 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002590 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002591 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002592 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2593 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2594 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2595 final ActivityRecord r = activities.get(activityNdx);
2596 if (r.finishing) {
2597 continue;
2598 }
2599 if (r.fullscreen) {
2600 lastIsOpaque = true;
2601 }
2602 if (owner != null && r.app != owner) {
2603 continue;
2604 }
2605 if (!lastIsOpaque) {
2606 continue;
2607 }
2608 // We can destroy this one if we have its icicle saved and
2609 // it is not in the process of pausing/stopping/finishing.
2610 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2611 && r.haveState && !r.visible && r.stopped
2612 && r.state != ActivityState.DESTROYING
2613 && r.state != ActivityState.DESTROYED) {
2614 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2615 + " resumed=" + mResumedActivity
2616 + " pausing=" + mPausingActivity);
2617 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2618 activityRemoved = true;
2619 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002620 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002621 }
2622 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002623 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002624 mStackSupervisor.resumeTopActivitiesLocked();
2625
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002626 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002627 }
2628
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002629 /**
2630 * Destroy the current CLIENT SIDE instance of an activity. This may be
2631 * called both when actually finishing an activity, or when performing
2632 * a configuration switch where we destroy the current client-side object
2633 * but then create a new client-side object for this same HistoryRecord.
2634 */
2635 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002636 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002637 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002638 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002639 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2640 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002641 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002642 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643
2644 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002645
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002646 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647
2648 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002649
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002650 if (hadApp) {
2651 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002652 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2654 mService.mHeavyWeightProcess = null;
2655 mService.mHandler.sendEmptyMessage(
2656 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2657 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002658 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002659 // No longer have activities, so update oom adj.
2660 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 }
2662 }
2663
2664 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002665
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 try {
2667 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002668 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002669 r.configChangeFlags);
2670 } catch (Exception e) {
2671 // We can just ignore exceptions here... if the process
2672 // has crashed, our death notification will clean things
2673 // up.
2674 //Slog.w(TAG, "Exception thrown during finish", e);
2675 if (r.finishing) {
2676 removeActivityFromHistoryLocked(r);
2677 removedFromHistory = true;
2678 skipDestroy = true;
2679 }
2680 }
2681
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002683
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002684 // If the activity is finishing, we need to wait on removing it
2685 // from the list to give it a chance to do its cleanup. During
2686 // that time it may make calls back with its token so we need to
2687 // be able to find it on the list and so we don't want to remove
2688 // it from the list yet. Otherwise, we can just immediately put
2689 // it in the destroyed state since we are not removing it from the
2690 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002691 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002692 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2693 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002694 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002695 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2697 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002698 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002700 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002701 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002702 }
2703 } else {
2704 // remove this record from the history.
2705 if (r.finishing) {
2706 removeActivityFromHistoryLocked(r);
2707 removedFromHistory = true;
2708 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002709 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002710 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002711 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002712 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713 }
2714 }
2715
2716 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002717
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002718 if (!mLRUActivities.remove(r) && hadApp) {
2719 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2720 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002721
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002722 return removedFromHistory;
2723 }
2724
Craig Mautnerd2328952013-03-05 12:46:26 -08002725 final void activityDestroyedLocked(IBinder token) {
2726 final long origId = Binder.clearCallingIdentity();
2727 try {
2728 ActivityRecord r = ActivityRecord.forToken(token);
2729 if (r != null) {
2730 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002732
2733 if (isInStackLocked(token) != null) {
2734 if (r.state == ActivityState.DESTROYING) {
2735 cleanUpActivityLocked(r, true, false);
2736 removeActivityFromHistoryLocked(r);
2737 }
2738 }
Craig Mautner05d29032013-05-03 13:40:13 -07002739 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002740 } finally {
2741 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 }
2743 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002744
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002745 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2746 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002747 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002748 if (DEBUG_CLEANUP) Slog.v(
2749 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002750 + " with " + i + " entries");
2751 while (i > 0) {
2752 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002753 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002754 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002756 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002757 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002758 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002759 }
2760 }
2761 }
2762
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002763 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2764 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002765 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2766 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002767 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2768 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002769 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002770 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002771 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2772 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002773
2774 boolean hasVisibleActivities = false;
2775
2776 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002777 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002778 if (DEBUG_CLEANUP) Slog.v(
2779 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002780 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2781 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2782 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2783 final ActivityRecord r = activities.get(activityNdx);
2784 --i;
2785 if (DEBUG_CLEANUP) Slog.v(
2786 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2787 if (r.app == app) {
2788 boolean remove;
2789 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2790 // Don't currently have state for the activity, or
2791 // it is finishing -- always remove it.
2792 remove = true;
2793 } else if (r.launchCount > 2 &&
2794 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2795 // We have launched this activity too many times since it was
2796 // able to run, so give up and remove it.
2797 remove = true;
2798 } else {
2799 // The process may be gone, but the activity lives on!
2800 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002801 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002802 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002803 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002804 RuntimeException here = new RuntimeException("here");
2805 here.fillInStackTrace();
2806 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2807 + ": haveState=" + r.haveState
2808 + " stateNotNeeded=" + r.stateNotNeeded
2809 + " finishing=" + r.finishing
2810 + " state=" + r.state, here);
2811 }
2812 if (!r.finishing) {
2813 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2814 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2815 r.userId, System.identityHashCode(r),
2816 r.task.taskId, r.shortComponentName,
2817 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002818 if (r.state == ActivityState.RESUMED) {
2819 mService.updateUsageStats(r, false);
2820 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002821 }
2822 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002823
Craig Mautner0247fc82013-02-28 14:32:06 -08002824 } else {
2825 // We have the current state for this activity, so
2826 // it can be restarted later when needed.
2827 if (localLOGV) Slog.v(
2828 TAG, "Keeping entry, setting app to null");
2829 if (r.visible) {
2830 hasVisibleActivities = true;
2831 }
2832 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2833 + r);
2834 r.app = null;
2835 r.nowVisible = false;
2836 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002837 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002838 "App died, clearing saved state of " + r);
2839 r.icicle = null;
2840 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002841 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002842
Craig Mautnerd2328952013-03-05 12:46:26 -08002843 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002844 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002845 }
2846 }
2847
2848 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002850
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002851 final void updateTransitLocked(int transit, Bundle options) {
2852 if (options != null) {
2853 ActivityRecord r = topRunningActivityLocked(null);
2854 if (r != null && r.state != ActivityState.RESUMED) {
2855 r.updateOptionsLocked(options);
2856 } else {
2857 ActivityOptions.abort(options);
2858 }
2859 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002860 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002861 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002862
Craig Mautnercae015f2013-02-08 14:31:27 -08002863 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002864 final TaskRecord task = taskForIdLocked(taskId);
2865 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002866 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002867 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002868 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002869 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2870 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002871 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002872 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002873 }
2874 moveTaskToFrontLocked(task, null, options);
2875 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002876 }
2877 return false;
2878 }
2879
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002880 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002881 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002882
Craig Mautner11bf9a52013-02-19 14:08:51 -08002883 final int numTasks = mTaskHistory.size();
2884 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002885 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002886 // nothing to do!
2887 if (reason != null &&
2888 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2889 ActivityOptions.abort(options);
2890 } else {
2891 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2892 }
2893 return;
2894 }
2895
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002896 mStackSupervisor.moveHomeStack(isHomeStack());
2897
Craig Mautner11bf9a52013-02-19 14:08:51 -08002898 // Shift all activities with this task up to the top
2899 // of the stack, keeping them in the same internal order.
2900 mTaskHistory.remove(tr);
2901 mTaskHistory.add(tr);
2902
2903 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904 if (reason != null &&
2905 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002906 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002907 ActivityRecord r = topRunningActivityLocked(null);
2908 if (r != null) {
2909 mNoAnimActivities.add(r);
2910 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002911 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002912 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002913 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002914 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002915
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002916 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917
Craig Mautner05d29032013-05-03 13:40:13 -07002918 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002919 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002920
2921 if (VALIDATE_TOKENS) {
2922 validateAppTokensLocked();
2923 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924 }
2925
2926 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002927 * Worker method for rearranging history stack. Implements the function of moving all
2928 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002930 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2932 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002933 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002934 * @param task The taskId to collect and move to the bottom.
2935 * @return Returns true if the move completed, false if not.
2936 */
2937 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
2938 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002939
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002940 // If we have a watcher, preflight the move before committing to it. First check
2941 // for *other* available tasks, but if none are available, then try again allowing the
2942 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002943 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002944 ActivityRecord next = topRunningActivityLocked(null, task);
2945 if (next == null) {
2946 next = topRunningActivityLocked(null, 0);
2947 }
2948 if (next != null) {
2949 // ask watcher if this is allowed
2950 boolean moveOK = true;
2951 try {
2952 moveOK = mService.mController.activityResuming(next.packageName);
2953 } catch (RemoteException e) {
2954 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002955 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002956 }
2957 if (!moveOK) {
2958 return false;
2959 }
2960 }
2961 }
2962
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002963 if (DEBUG_TRANSITION) Slog.v(TAG,
2964 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965
Craig Mautnerd2328952013-03-05 12:46:26 -08002966 final TaskRecord tr = taskForIdLocked(task);
2967 if (tr == null) {
2968 return false;
2969 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002970 mTaskHistory.remove(tr);
2971 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972
2973 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002974 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2975 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 ActivityRecord r = topRunningActivityLocked(null);
2977 if (r != null) {
2978 mNoAnimActivities.add(r);
2979 }
2980 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002981 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08002982 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002983 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002984 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002985
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002986 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002987 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 }
2989
Craig Mautnerde4ef022013-04-07 19:01:33 -07002990 if (mResumedActivity != null && mResumedActivity.task == tr &&
2991 mResumedActivity.mLaunchHomeTaskNext) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07002992 // TODO: Can we skip the next line and just pass mResumedAct. to resumeHomeAct.()?
Craig Mautnerde4ef022013-04-07 19:01:33 -07002993 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07002994 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002995 }
2996
Craig Mautner05d29032013-05-03 13:40:13 -07002997 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 return true;
2999 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003000
Craig Mautner8849a5e2013-04-02 16:41:03 -07003001 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003003 final Uri data = r.intent.getData();
3004 final String strData = data != null ? data.toSafeString() : null;
3005
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003006 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003007 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003008 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003009 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003010 }
3011
3012 /**
3013 * Make sure the given activity matches the current configuration. Returns
3014 * false if the activity had to be destroyed. Returns true if the
3015 * configuration is the same, or the activity will remain running as-is
3016 * for whatever reason. Ensures the HistoryRecord is updated with the
3017 * correct configuration and all other bookkeeping is handled.
3018 */
3019 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3020 int globalChanges) {
3021 if (mConfigWillChange) {
3022 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3023 "Skipping config check (will change): " + r);
3024 return true;
3025 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003026
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3028 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003029
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003030 // Short circuit: if the two configurations are the exact same
3031 // object (the common case), then there is nothing to do.
3032 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003033 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003034 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3035 "Configuration unchanged in " + r);
3036 return true;
3037 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003038
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003039 // We don't worry about activities that are finishing.
3040 if (r.finishing) {
3041 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3042 "Configuration doesn't matter in finishing " + r);
3043 r.stopFreezingScreenLocked(false);
3044 return true;
3045 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003046
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 // Okay we now are going to make this activity have the new config.
3048 // But then we need to figure out how it needs to deal with that.
3049 Configuration oldConfig = r.configuration;
3050 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003051
3052 // Determine what has changed. May be nothing, if this is a config
3053 // that has come back from the app after going idle. In that case
3054 // we just want to leave the official config object now in the
3055 // activity and do nothing else.
3056 final int changes = oldConfig.diff(newConfig);
3057 if (changes == 0 && !r.forceNewConfig) {
3058 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3059 "Configuration no differences in " + r);
3060 return true;
3061 }
3062
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003063 // If the activity isn't currently running, just leave the new
3064 // configuration and it will pick that up next time it starts.
3065 if (r.app == null || r.app.thread == null) {
3066 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3067 "Configuration doesn't matter not running " + r);
3068 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003069 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 return true;
3071 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003072
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003073 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003074 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3075 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3076 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003077 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003078 + ", newConfig=" + newConfig);
3079 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003080 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003081 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3082 r.configChangeFlags |= changes;
3083 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003084 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003085 if (r.app == null || r.app.thread == null) {
3086 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003087 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003088 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003089 } else if (r.state == ActivityState.PAUSING) {
3090 // A little annoying: we are waiting for this activity to
3091 // finish pausing. Let's not do anything now, but just
3092 // flag that it needs to be restarted when done pausing.
3093 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003094 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003095 r.configDestroy = true;
3096 return true;
3097 } else if (r.state == ActivityState.RESUMED) {
3098 // Try to optimize this case: the configuration is changing
3099 // and we need to restart the top, resumed activity.
3100 // Instead of doing the normal handshaking, just say
3101 // "restart!".
3102 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003103 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003104 relaunchActivityLocked(r, r.configChangeFlags, true);
3105 r.configChangeFlags = 0;
3106 } else {
3107 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003108 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003109 relaunchActivityLocked(r, r.configChangeFlags, false);
3110 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003112
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003113 // All done... tell the caller we weren't able to keep this
3114 // activity around.
3115 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003116 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 // Default case: the activity can handle this new configuration, so
3119 // hand it over. Note that we don't need to give it the new
3120 // configuration, since we always send configuration changes to all
3121 // process when they happen so it can just use whatever configuration
3122 // it last got.
3123 if (r.app != null && r.app.thread != null) {
3124 try {
3125 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003126 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003127 } catch (RemoteException e) {
3128 // If process died, whatever.
3129 }
3130 }
3131 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 return true;
3134 }
3135
3136 private final boolean relaunchActivityLocked(ActivityRecord r,
3137 int changes, boolean andResume) {
3138 List<ResultInfo> results = null;
3139 List<Intent> newIntents = null;
3140 if (andResume) {
3141 results = r.results;
3142 newIntents = r.newIntents;
3143 }
3144 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3145 + " with results=" + results + " newIntents=" + newIntents
3146 + " andResume=" + andResume);
3147 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003148 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003154 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3155 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3156 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003157 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003158 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003159 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 // Note: don't need to call pauseIfSleepingLocked() here, because
3161 // the caller will only pass in 'andResume' if this activity is
3162 // currently resumed, which implies we aren't sleeping.
3163 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003164 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 }
3166
3167 if (andResume) {
3168 r.results = null;
3169 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003170 r.state = ActivityState.RESUMED;
3171 } else {
3172 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3173 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 }
3175
3176 return true;
3177 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003178
3179 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003180 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3181 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3182 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3183 final ActivityRecord r = activities.get(activityNdx);
3184 if (r.appToken == token) {
3185 return true;
3186 }
3187 if (r.fullscreen && !r.finishing) {
3188 return false;
3189 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003190 }
3191 }
3192 return true;
3193 }
3194
3195 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003196 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3197 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3198 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3199 final ActivityRecord r = activities.get(activityNdx);
3200 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003201 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003202 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003203 }
3204 }
3205 }
3206
3207 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3208 boolean didSomething = false;
3209 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003210 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3211 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3212 int numActivities = activities.size();
3213 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3214 ActivityRecord r = activities.get(activityNdx);
3215 final boolean samePackage = r.packageName.equals(name)
3216 || (name == null && r.userId == userId);
3217 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3218 && (samePackage || r.task == lastTask)
3219 && (r.app == null || evenPersistent || !r.app.persistent)) {
3220 if (!doit) {
3221 if (r.finishing) {
3222 // If this activity is just finishing, then it is not
3223 // interesting as far as something to stop.
3224 continue;
3225 }
3226 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003227 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003228 didSomething = true;
3229 Slog.i(TAG, " Force finishing activity " + r);
3230 if (samePackage) {
3231 if (r.app != null) {
3232 r.app.removed = true;
3233 }
3234 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003235 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003236 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003237 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3238 true)) {
3239 // r has been deleted from mActivities, accommodate.
3240 --numActivities;
3241 --activityNdx;
3242 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003243 }
3244 }
3245 }
3246 return didSomething;
3247 }
3248
3249 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003250 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003251 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003252 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3253 --maxNum, --taskNdx) {
3254 final TaskRecord task = mTaskHistory.get(taskNdx);
3255 ActivityRecord r = null;
3256 ActivityRecord top = null;
3257 int numActivities = 0;
3258 int numRunning = 0;
3259 final ArrayList<ActivityRecord> activities = task.mActivities;
3260 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3261 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003262
Craig Mautneraab647e2013-02-28 16:31:36 -08003263 // Initialize state for next task if needed.
3264 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3265 top = r;
3266 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003267 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003268
3269 // Add 'r' into the current task.
3270 numActivities++;
3271 if (r.app != null && r.app.thread != null) {
3272 numRunning++;
3273 }
3274
3275 if (localLOGV) Slog.v(
3276 TAG, r.intent.getComponent().flattenToShortString()
3277 + ": task=" + r.task);
3278 }
3279
3280 RunningTaskInfo ci = new RunningTaskInfo();
3281 ci.id = task.taskId;
3282 ci.baseActivity = r.intent.getComponent();
3283 ci.topActivity = top.intent.getComponent();
3284 if (top.thumbHolder != null) {
3285 ci.description = top.thumbHolder.lastDescription;
3286 }
3287 ci.numActivities = numActivities;
3288 ci.numRunning = numRunning;
3289 //System.out.println(
3290 // "#" + maxNum + ": " + " descr=" + ci.description);
3291 if (receiver != null) {
3292 if (localLOGV) Slog.v(
3293 TAG, "State=" + top.state + "Idle=" + top.idle
3294 + " app=" + top.app
3295 + " thr=" + (top.app != null ? top.app.thread : null));
3296 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3297 if (top.idle && top.app != null && top.app.thread != null) {
3298 topRecord = top;
3299 } else {
3300 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003301 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003302 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003303 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003304 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003305 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003306 }
3307 return topRecord;
3308 }
3309
3310 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003311 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003312 if (DEBUG_SWITCH) Slog.d(
3313 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003314 if (top >= 0) {
3315 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3316 int activityTop = activities.size() - 1;
3317 if (activityTop > 0) {
3318 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3319 "unhandled-back", true);
3320 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003321 }
3322 }
3323
Craig Mautnere79d42682013-04-01 19:01:53 -07003324 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003325 if (!containsApp(app)) {
3326 return;
3327 }
3328 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003329 if (mPausingActivity != null && mPausingActivity.app == app) {
3330 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3331 "App died while pausing: " + mPausingActivity);
3332 mPausingActivity = null;
3333 }
3334 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3335 mLastPausedActivity = null;
3336 }
Craig Mautner37f748d2013-06-24 16:19:55 -07003337 final ActivityRecord top = topRunningActivityLocked(null);
3338 final boolean launchHomeTaskNext =
3339 top != null && top.app == app && top.mLaunchHomeTaskNext;
Craig Mautnere79d42682013-04-01 19:01:53 -07003340
3341 // Remove this application's activities from active lists.
3342 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3343
3344 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003345 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautner37f748d2013-06-24 16:19:55 -07003346 if (stack == null || launchHomeTaskNext) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003347 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003348 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003349 // If there was nothing to resume, and we are not already
3350 // restarting this process, but there is a visible activity that
3351 // is hosted by the process... then make sure all visible
3352 // activities are running, taking care of restarting this
3353 // process.
3354 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003355 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003356 }
3357 }
3358 }
3359 }
3360
Craig Mautnercae015f2013-02-08 14:31:27 -08003361 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003362 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3363 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3364 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3365 final ActivityRecord r = activities.get(activityNdx);
3366 if (r.app == app) {
3367 Slog.w(TAG, " Force finishing activity "
3368 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003369 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003370 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003371 }
3372 }
3373 }
3374
Dianne Hackborn390517b2013-05-30 15:03:32 -07003375 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003376 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003377 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003378 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3379 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003380 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3381 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003382 dumpClient, dumpPackage, needSep, header,
3383 " Task " + taskNdx + ": id #" + task.taskId);
3384 if (printed) {
3385 header = null;
3386 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003387 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003388 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003389 }
3390
3391 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3392 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3393
3394 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003395 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3396 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003397 }
3398 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003399 final int top = mTaskHistory.size() - 1;
3400 if (top >= 0) {
3401 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3402 int listTop = list.size() - 1;
3403 if (listTop >= 0) {
3404 activities.add(list.get(listTop));
3405 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003406 }
3407 } else {
3408 ItemMatcher matcher = new ItemMatcher();
3409 matcher.build(name);
3410
Craig Mautneraab647e2013-02-28 16:31:36 -08003411 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3412 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3413 if (matcher.match(r1, r1.intent.getComponent())) {
3414 activities.add(r1);
3415 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003416 }
3417 }
3418 }
3419
3420 return activities;
3421 }
3422
3423 ActivityRecord restartPackage(String packageName) {
3424 ActivityRecord starting = topRunningActivityLocked(null);
3425
3426 // All activities that came from the package must be
3427 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003428 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3429 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3430 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3431 final ActivityRecord a = activities.get(activityNdx);
3432 if (a.info.packageName.equals(packageName)) {
3433 a.forceNewConfig = true;
3434 if (starting != null && a == starting && a.visible) {
3435 a.startFreezingScreenLocked(starting.app,
3436 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3437 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003438 }
3439 }
3440 }
3441
3442 return starting;
3443 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003444
Craig Mautnerde4ef022013-04-07 19:01:33 -07003445 boolean removeTask(TaskRecord task) {
3446 mTaskHistory.remove(task);
3447 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003448 }
3449
Craig Mautnerde4ef022013-04-07 19:01:33 -07003450 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003451 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003452 if (toTop) {
3453 mTaskHistory.add(task);
3454 } else {
3455 mTaskHistory.add(0, task);
3456 }
3457 return task;
3458 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003459
3460 ArrayList<TaskRecord> getAllTasks() {
3461 return new ArrayList<TaskRecord>(mTaskHistory);
3462 }
3463
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003464 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003465 task.stack = this;
3466 if (toTop) {
3467 mTaskHistory.add(task);
3468 } else {
3469 mTaskHistory.add(0, task);
3470 }
3471 }
3472
3473 public int getStackId() {
3474 return mStackId;
3475 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003476
3477 @Override
3478 public String toString() {
3479 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3480 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003481}