blob: 9013b4a48c191594127673ce3bd7ff333c249009 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
33import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
34import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
35import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
36import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070037import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
38
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070039import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080040import com.android.internal.util.Objects;
Kenny Rootadd58212013-05-07 09:47:34 -070041import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080042import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080043import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080044import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046
47import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070048import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070049import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080052import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080054import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.ComponentName;
56import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.content.res.Resources;
62import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080063import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080064import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070066import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.os.Handler;
68import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090069import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Message;
71import android.os.PowerManager;
Dianne Hackborn053f61d2013-06-26 18:07:43 -070072import android.os.Process;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import 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 can hold the launch wake lock before giving up.
106 static final int LAUNCH_TIMEOUT = 10*1000;
107
108 // How long we wait until giving up on an activity telling us it has
109 // finished destroying itself.
110 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800111
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800113 // disabled.
114 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700116 // How long between activity launches that we consider safe to not warn
117 // the user about an unexpected activity being launched on top.
118 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120 // Set to false to disable the preview that is shown while a new activity
121 // is being started.
122 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800123
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700124 // For debugging to make sure the caller when acquiring/releasing our
125 // wake lock is the system process.
126 static final boolean VALIDATE_WAKE_LOCK_CALLER = true;
127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 enum ActivityState {
129 INITIALIZING,
130 RESUMED,
131 PAUSING,
132 PAUSED,
133 STOPPING,
134 STOPPED,
135 FINISHING,
136 DESTROYING,
137 DESTROYED
138 }
139
140 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700141 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700143 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700145 /**
146 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800147 * running) activities. It contains #TaskRecord objects.
148 */
149 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
150
151 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152 * Used for validating app tokens with window manager.
153 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800154 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700156 /**
157 * List of running activities, sorted by recent usage.
158 * The first entry in the list is the least recently used.
159 * It contains HistoryRecord objects.
160 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700161 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162
163 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 * Animations that for the current transition have requested not to
165 * be considered for the transition animation.
166 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700167 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168
169 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 * We don't want to allow the device to go to sleep while in the process
171 * of launching an activity. This is primarily to allow alarm intent
172 * receivers to launch an activity and get that to run before the device
173 * goes back to sleep.
174 */
175 final PowerManager.WakeLock mLaunchingActivity;
176
177 /**
178 * When we are in the process of pausing an activity, before starting the
179 * next one, this variable holds the activity that is currently being paused.
180 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800181 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700182
183 /**
184 * This is the last activity that we put into the paused state. This is
185 * used to determine if we need to do an activity transition while sleeping,
186 * when we normally hold the top activity paused.
187 */
188 ActivityRecord mLastPausedActivity = null;
189
190 /**
191 * Current activity that is resumed, or null if there is none.
192 */
193 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800194
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700195 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700196 * This is the last activity that has been started. It is only used to
197 * identify when multiple activities are started at once so that the user
198 * can be warned they may not be in the activity they think they are.
199 */
200 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800201
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700202 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 * Set when we know we are going to be calling updateConfiguration()
204 * soon, so want to skip intermediate config checks.
205 */
206 boolean mConfigWillChange;
207
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700208 long mLaunchStartTime = 0;
209 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800210
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800211 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800212 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
213 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
214 */
215 private ActivityRecord mLastScreenshotActivity = null;
216 private Bitmap mLastScreenshotBitmap = null;
217
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800218 int mThumbnailWidth = -1;
219 int mThumbnailHeight = -1;
220
Craig Mautner858d8a62013-04-23 17:08:34 -0700221 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700222
Craig Mautnerc00204b2013-03-05 15:02:14 -0800223 final int mStackId;
224
Craig Mautner27084302013-03-25 08:05:25 -0700225 /** Run all ActivityStacks through this */
226 final ActivityStackSupervisor mStackSupervisor;
227
Craig Mautner0eea92c2013-05-16 13:35:39 -0700228 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
229 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
230 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
231 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
232 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
233 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700234
235 static class ScheduleDestroyArgs {
236 final ProcessRecord mOwner;
237 final boolean mOomAdj;
238 final String mReason;
239 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
240 mOwner = owner;
241 mOomAdj = oomAdj;
242 mReason = reason;
243 }
244 }
245
Zoran Marcetaf958b322012-08-09 20:27:12 +0900246 final Handler mHandler;
247
248 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249 //public Handler() {
250 // if (localLOGV) Slog.v(TAG, "Handler started!");
251 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900252 public ActivityStackHandler(Looper looper) {
253 super(looper);
254 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700255
Zoran Marcetaf958b322012-08-09 20:27:12 +0900256 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700257 public void handleMessage(Message msg) {
258 switch (msg.what) {
259 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800260 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700261 // We don't at this point know if the activity is fullscreen,
262 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800263 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700264 synchronized (mService) {
265 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700266 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700267 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700268 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800269 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700270 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700271 case LAUNCH_TICK_MSG: {
272 ActivityRecord r = (ActivityRecord)msg.obj;
273 synchronized (mService) {
274 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700275 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700276 }
277 }
278 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700279 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800280 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700281 // We don't at this point know if the activity is fullscreen,
282 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800283 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800284 synchronized (mService) {
285 activityDestroyedLocked(r != null ? r.appToken : null);
286 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 case LAUNCH_TIMEOUT_MSG: {
289 if (mService.mDidDexOpt) {
290 mService.mDidDexOpt = false;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700291 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 return;
293 }
294 synchronized (mService) {
295 if (mLaunchingActivity.isHeld()) {
296 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700297 if (VALIDATE_WAKE_LOCK_CALLER
298 && Binder.getCallingUid() != Process.myUid()) {
299 throw new IllegalStateException("Calling must be system uid");
300 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700301 mLaunchingActivity.release();
302 }
303 }
304 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700305 case STOP_TIMEOUT_MSG: {
306 ActivityRecord r = (ActivityRecord)msg.obj;
307 // We don't at this point know if the activity is fullscreen,
308 // so we need to be conservative and assume it isn't.
309 Slog.w(TAG, "Activity stop timeout for " + r);
310 synchronized (mService) {
311 if (r.isInHistory()) {
312 activityStoppedLocked(r, null, null, null);
313 }
314 }
315 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700316 case DESTROY_ACTIVITIES_MSG: {
317 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
318 synchronized (mService) {
319 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
320 }
321 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700322 }
323 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800324 }
325
Craig Mautner000f0022013-02-26 15:04:29 -0800326 private int numActivities() {
327 int count = 0;
328 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
329 count += mTaskHistory.get(taskNdx).mActivities.size();
330 }
331 return count;
332 }
333
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700334 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900335 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700337 mWindowManager = service.mWindowManager;
338 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700339 mContext = context;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700340 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700341 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
342 throw new IllegalStateException("Calling must be system uid");
343 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700344 mLaunchingActivity =
345 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700346 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800347 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700348 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700349 }
Craig Mautner5962b122012-10-05 14:45:52 -0700350
351 private boolean okToShow(ActivityRecord r) {
352 return r.userId == mCurrentUser
353 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
354 }
355
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700356 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800357 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
358 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800359 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800360 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
361 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800362 if (!r.finishing && r != notTop && okToShow(r)) {
363 return r;
364 }
365 }
366 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700367 return null;
368 }
369
370 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800371 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
372 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800373 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800374 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
375 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800376 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
377 return r;
378 }
379 }
380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700381 return null;
382 }
383
384 /**
385 * This is a simplified version of topRunningActivityLocked that provides a number of
386 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800387 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700388 * @param token If non-null, any history records matching this token will be skipped.
389 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800390 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700391 * @return Returns the HistoryRecord of the next activity on the stack.
392 */
393 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800394 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
395 TaskRecord task = mTaskHistory.get(taskNdx);
396 if (task.taskId == taskId) {
397 continue;
398 }
399 ArrayList<ActivityRecord> activities = task.mActivities;
400 for (int i = activities.size() - 1; i >= 0; --i) {
401 final ActivityRecord r = activities.get(i);
402 // Note: the taskId check depends on real taskId fields being non-zero
403 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
404 return r;
405 }
406 }
407 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700408 return null;
409 }
410
Craig Mautner8849a5e2013-04-02 16:41:03 -0700411 final ActivityRecord topActivity() {
412 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700413 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700414 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
415 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
416 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700417 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700418 }
419 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700420 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700421 }
422
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700423 final TaskRecord topTask() {
424 final int size = mTaskHistory.size();
425 if (size > 0) {
426 return mTaskHistory.get(size - 1);
427 }
428 return null;
429 }
430
Craig Mautnerd2328952013-03-05 12:46:26 -0800431 TaskRecord taskForIdLocked(int id) {
432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
433 final TaskRecord task = mTaskHistory.get(taskNdx);
434 if (task.taskId == id) {
435 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800436 }
437 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700438 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700439 }
440
Craig Mautnerd2328952013-03-05 12:46:26 -0800441 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800442 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800443 if (r != null) {
444 final TaskRecord task = r.task;
445 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
446 if (task.stack != this) Slog.w(TAG,
447 "Illegal state! task does not point to stack it is in.");
448 return r;
449 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800450 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800451 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800452 }
453
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700454 boolean containsApp(ProcessRecord app) {
455 if (app == null) {
456 return false;
457 }
458 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
459 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
460 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
461 final ActivityRecord r = activities.get(activityNdx);
462 if (r.finishing) {
463 continue;
464 }
465 if (r.app == app) {
466 return true;
467 }
468 }
469 }
470 return false;
471 }
472
Craig Mautner2420ead2013-04-01 17:13:20 -0700473 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700474 final boolean hadit = mLRUActivities.remove(r);
475 mLRUActivities.add(r);
476 return hadit;
477 }
478
Craig Mautnerde4ef022013-04-07 19:01:33 -0700479 final boolean isHomeStack() {
480 return mStackId == HOME_STACK_ID;
481 }
482
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700483 /**
484 * Returns the top activity in any existing task matching the given
485 * Intent. Returns null if no such task is found.
486 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700487 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700488 ComponentName cls = intent.getComponent();
489 if (info.targetActivity != null) {
490 cls = new ComponentName(info.packageName, info.targetActivity);
491 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700492 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800493
494 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
495 final TaskRecord task = mTaskHistory.get(taskNdx);
496 final ActivityRecord r = task.getTopActivity();
497 if (r == null || r.finishing || r.userId != userId ||
498 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
499 continue;
500 }
501
502 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
503 // + "/aff=" + r.task.affinity + " to new cls="
504 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
505 if (task.affinity != null) {
506 if (task.affinity.equals(info.taskAffinity)) {
507 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700508 return r;
509 }
Craig Mautner000f0022013-02-26 15:04:29 -0800510 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
511 //Slog.i(TAG, "Found matching class!");
512 //dump();
513 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
514 return r;
515 } else if (task.affinityIntent != null
516 && task.affinityIntent.getComponent().equals(cls)) {
517 //Slog.i(TAG, "Found matching class!");
518 //dump();
519 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
520 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700521 }
522 }
523
524 return null;
525 }
526
527 /**
528 * Returns the first activity (starting from the top of the stack) that
529 * is the same as the given activity. Returns null if no such activity
530 * is found.
531 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700532 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700533 ComponentName cls = intent.getComponent();
534 if (info.targetActivity != null) {
535 cls = new ComponentName(info.packageName, info.targetActivity);
536 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700537 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700538
Craig Mautner000f0022013-02-26 15:04:29 -0800539 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
540 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
541 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
542 ActivityRecord r = activities.get(activityNdx);
543 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700544 //Slog.i(TAG, "Found matching class!");
545 //dump();
546 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
547 return r;
548 }
549 }
550 }
551
552 return null;
553 }
554
Amith Yamasani742a6712011-05-04 14:49:28 -0700555 /*
Craig Mautner858d8a62013-04-23 17:08:34 -0700556 * Move the activities around in the stack to bring a user to the foreground. This only
557 * matters on the home stack. All other stacks are single user.
Amith Yamasani742a6712011-05-04 14:49:28 -0700558 * @return whether there are any activities for the specified user.
559 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700560 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800561 if (VALIDATE_TOKENS) {
562 validateAppTokensLocked();
563 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800564 if (mCurrentUser == userId) {
565 return true;
566 }
567 mCurrentUser = userId;
568
569 // Move userId's tasks to the top.
570 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800571 int index = mTaskHistory.size();
572 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700573 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800574 if (task.userId == userId) {
575 haveActivities = true;
576 mTaskHistory.remove(i);
577 mTaskHistory.add(task);
578 --index;
579 }
580 }
581
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700582 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700583 }
584
Craig Mautner2420ead2013-04-01 17:13:20 -0700585 void minimalResumeActivityLocked(ActivityRecord r) {
586 r.state = ActivityState.RESUMED;
587 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
588 + " (starting new instance)");
589 r.stopped = false;
590 mResumedActivity = r;
591 r.task.touchActiveTime();
592 mService.addRecentTaskLocked(r.task);
593 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700594 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700595 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700596 }
597
Craig Mautnere79d42682013-04-01 19:01:53 -0700598 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700599 if (r.displayStartTime == 0) {
600 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
601 if (mLaunchStartTime == 0) {
602 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700603 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700604 } else if (mLaunchStartTime == 0) {
605 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700606 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700607 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800608
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700609 void clearLaunchTime(ActivityRecord r) {
610 r.displayStartTime = r.fullyDrawnStartTime = 0;
611 }
612
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800613 void stopIfSleepingLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700614 if (mLaunchingActivity.isHeld()) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700615 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
616 throw new IllegalStateException("Calling must be system uid");
617 }
Craig Mautner0eea92c2013-05-16 13:35:39 -0700618 mLaunchingActivity.release();
619 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800620 }
621 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700622
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800623 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800624 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800625 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
626 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
627 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
628 activities.get(activityNdx).setSleeping(false);
629 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800630 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800631 }
632
Craig Mautner0eea92c2013-05-16 13:35:39 -0700633 /**
634 * @return true if something must be done before going to sleep.
635 */
636 boolean checkReadyForSleepLocked() {
637 if (mResumedActivity != null) {
638 // Still have something resumed; can't sleep until it is paused.
639 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
640 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
641 startPausingLocked(false, true);
642 return true;
643 }
644 if (mPausingActivity != null) {
645 // Still waiting for something to pause; can't sleep yet.
646 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
647 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800648 }
649
Craig Mautner0eea92c2013-05-16 13:35:39 -0700650 return false;
651 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800652
Craig Mautner0eea92c2013-05-16 13:35:39 -0700653 void goToSleep() {
654 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800655
Craig Mautner0eea92c2013-05-16 13:35:39 -0700656 // Make sure any stopped but visible activities are now sleeping.
657 // This ensures that the activity's onStop() is called.
658 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
659 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
660 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
661 final ActivityRecord r = activities.get(activityNdx);
662 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
663 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800664 }
665 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800666 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700667 }
Craig Mautner59c00972012-07-30 12:10:24 -0700668
Dianne Hackbornd2835932010-12-13 16:28:46 -0800669 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800670 if (who.noDisplay) {
671 return null;
672 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800673
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800674 Resources res = mService.mContext.getResources();
675 int w = mThumbnailWidth;
676 int h = mThumbnailHeight;
677 if (w < 0) {
678 mThumbnailWidth = w =
679 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
680 mThumbnailHeight = h =
681 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
682 }
683
684 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800685 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
686 || mLastScreenshotBitmap.getWidth() != w
687 || mLastScreenshotBitmap.getHeight() != h) {
688 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700689 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800690 who.appToken, Display.DEFAULT_DISPLAY, w, h);
691 }
692 if (mLastScreenshotBitmap != null) {
693 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
694 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800695 }
696 return null;
697 }
698
Craig Mautnercf910b02013-04-23 11:23:27 -0700699 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800700 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800701 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700702 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800703 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700704 ActivityRecord prev = mResumedActivity;
705 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700706 Slog.e(TAG, "Trying to pause when nothing is resumed",
707 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700708 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700709 return;
710 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700711 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
712 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700713 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800714 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700715 mLastPausedActivity = prev;
716 prev.state = ActivityState.PAUSING;
717 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700718 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700719 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700720
721 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800722
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700723 if (prev.app != null && prev.app.thread != null) {
724 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
725 try {
726 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700727 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700728 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700729 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800730 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
731 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700732 } catch (Exception e) {
733 // Ignore exception, if process died other code will cleanup.
734 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800735 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700736 mLastPausedActivity = null;
737 }
738 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800739 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700740 mLastPausedActivity = null;
741 }
742
743 // If we are not going to sleep, we want to ensure the device is
744 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700745 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700746 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
747 throw new IllegalStateException("Calling must be system uid");
748 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700749 mLaunchingActivity.acquire();
750 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
751 // To be safe, don't allow the wake lock to be held for too long.
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700752 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700753 }
754 }
755
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800756 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700757 // Have the window manager pause its key dispatching until the new
758 // activity has started. If we're pausing the activity just because
759 // the screen is being turned off and the UI is sleeping, don't interrupt
760 // key dispatch; the same activity will pick it up again on wakeup.
761 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800762 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700763 } else {
764 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
765 }
766
767 // Schedule a pause timeout in case the app doesn't respond.
768 // We don't give it much time because this directly impacts the
769 // responsiveness seen by the user.
770 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
771 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700772 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700773 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
774 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
775 } else {
776 // This activity failed to schedule the
777 // pause, so just treat it as being paused now.
778 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700779 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700780 }
781 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700782
Craig Mautnerd2328952013-03-05 12:46:26 -0800783 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700784 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800785 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700786
Craig Mautnerd2328952013-03-05 12:46:26 -0800787 final ActivityRecord r = isInStackLocked(token);
788 if (r != null) {
789 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
790 if (mPausingActivity == r) {
791 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
792 + (timeout ? " (due to timeout)" : " (pause complete)"));
793 r.state = ActivityState.PAUSED;
794 completePauseLocked();
795 } else {
796 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
797 r.userId, System.identityHashCode(r), r.shortComponentName,
798 mPausingActivity != null
799 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800 }
801 }
802 }
803
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700804 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
805 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700806 if (r.state != ActivityState.STOPPING) {
807 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
808 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
809 return;
810 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700811 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700812 if (icicle != null) {
813 // If icicle is null, this is happening due to a timeout, so we
814 // haven't really saved the state.
815 r.icicle = icicle;
816 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800817 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700818 r.updateThumbnail(thumbnail, description);
819 }
820 if (!r.stopped) {
821 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
822 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
823 r.stopped = true;
824 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700825 if (r.finishing) {
826 r.clearOptionsLocked();
827 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700828 if (r.configDestroy) {
829 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700830 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700831 } else {
832 // Now that this process has stopped, we may want to consider
833 // it to be the previous app to try to keep around in case
834 // the user wants to return to it.
835 ProcessRecord fgApp = null;
836 if (mResumedActivity != null) {
837 fgApp = mResumedActivity.app;
838 } else if (mPausingActivity != null) {
839 fgApp = mPausingActivity.app;
840 }
841 if (r.app != null && fgApp != null && r.app != fgApp
842 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
843 && r.app != mService.mHomeProcess) {
844 mService.mPreviousProcess = r.app;
845 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
846 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800847 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700848 }
849 }
850 }
851
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800852 private final void completePauseLocked() {
853 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800855
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800856 if (prev != null) {
857 if (prev.finishing) {
858 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700859 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800860 } else if (prev.app != null) {
861 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
862 if (prev.waitingVisible) {
863 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700864 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
866 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800867 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 if (prev.configDestroy) {
869 // The previous is being paused because the configuration
870 // is changing, which means it is actually stopping...
871 // To juggle the fact that we are also starting a new
872 // instance right now, we need to first completely stop
873 // the current instance before starting the new one.
874 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
875 destroyActivityLocked(prev, true, false, "pause-config");
876 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700877 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700878 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
879 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800880 // If we already have a few activities waiting to stop,
881 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700882 // them out. Or if r is the last of activity of the last task the stack
883 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800884 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700885 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800886 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700887 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 }
889 }
890 } else {
891 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
892 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800893 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800894 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700895 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800896
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700897 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700898 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700899 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800900 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700901 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700902 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700903 if (top == null || (prev != null && top != prev)) {
904 // If there are no more activities available to run,
905 // do resume anyway to start something. Also if the top
906 // activity on the stack is not the just paused activity,
907 // we need to go ahead and resume it to ensure we complete
908 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700909 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700910 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700911 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800912
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800913 if (prev != null) {
914 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700915
Craig Mautner525f3d92013-05-07 14:01:50 -0700916 if (prev.app != null && prev.cpuTimeAtResume > 0
917 && mService.mBatteryStatsService.isOnBattery()) {
918 long diff;
919 synchronized (mService.mProcessStatsThread) {
920 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
921 - prev.cpuTimeAtResume;
922 }
923 if (diff > 0) {
924 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
925 synchronized (bsi) {
926 BatteryStatsImpl.Uid.Proc ps =
927 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
928 prev.info.packageName);
929 if (ps != null) {
930 ps.addForegroundTimeLocked(diff);
931 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 }
933 }
934 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700935 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 }
938
939 /**
940 * Once we know that we have asked an application to put an activity in
941 * the resumed state (either by launching it or explicitly telling it),
942 * this function updates the rest of our state to match that fact.
943 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700944 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700945 next.idle = false;
946 next.results = null;
947 next.newIntents = null;
948
949 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700950 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700951
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700952 mStackSupervisor.reportResumedActivityLocked(next);
953
954 next.resumeKeyDispatchingLocked();
955 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700956
957 // Mark the point when the activity is resuming
958 // TODO: To be more accurate, the mark should be before the onCreate,
959 // not after the onResume. But for subsequent starts, onResume is fine.
960 if (next.app != null) {
961 synchronized (mService.mProcessStatsThread) {
962 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
963 }
964 } else {
965 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
966 }
967 }
968
Craig Mautner580ea812013-04-25 12:58:38 -0700969 /**
970 * Version of ensureActivitiesVisible that can easily be called anywhere.
971 */
972 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
973 return ensureActivitiesVisibleLocked(starting, configChanges, false);
974 }
975
976 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
977 boolean forceHomeShown) {
978 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700979 return r != null &&
980 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700981 }
982
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700983 /**
984 * Make sure that all activities that need to be visible (that is, they
985 * currently can be seen by the user) actually are.
986 */
Craig Mautner580ea812013-04-25 12:58:38 -0700987 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
988 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989 if (DEBUG_VISBILITY) Slog.v(
990 TAG, "ensureActivitiesVisible behind " + top
991 + " configChanges=0x" + Integer.toHexString(configChanges));
992
993 // If the top activity is not fullscreen, then we need to
994 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800995 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -0700996 boolean showHomeBehindStack = false;
997 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
998 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700999 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001000 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1001 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1002 final ActivityRecord r = activities.get(activityNdx);
1003 if (r.finishing) {
1004 continue;
1005 }
1006 if (aboveTop && r != top) {
1007 continue;
1008 }
1009 aboveTop = false;
1010 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001011 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001012 TAG, "Make visible? " + r + " finishing=" + r.finishing
1013 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001014
Craig Mautnerd44711d2013-02-23 11:24:36 -08001015 final boolean doThisProcess = onlyThisProcess == null
1016 || onlyThisProcess.equals(r.processName);
1017
1018 // First: if this is not the current activity being started, make
1019 // sure it matches the current configuration.
1020 if (r != starting && doThisProcess) {
1021 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001022 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001023
1024 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001026 // This activity needs to be visible, but isn't even
1027 // running... get it started, but don't resume it
1028 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001030 if (r != starting) {
1031 r.startFreezingScreenLocked(r.app, configChanges);
1032 }
1033 if (!r.visible) {
1034 if (DEBUG_VISBILITY) Slog.v(
1035 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001036 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001037 }
1038 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001039 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001040 }
1041 }
1042
1043 } else if (r.visible) {
1044 // If this activity is already visible, then there is nothing
1045 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001046 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001047 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001048
1049 } else if (onlyThisProcess == null) {
1050 // This activity is not currently visible, but is running.
1051 // Tell it to become visible.
1052 r.visible = true;
1053 if (r.state != ActivityState.RESUMED && r != starting) {
1054 // If this activity is paused, tell it
1055 // to now show its window.
1056 if (DEBUG_VISBILITY) Slog.v(
1057 TAG, "Making visible and scheduling visibility: " + r);
1058 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001059 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001060 r.sleeping = false;
1061 r.app.pendingUiClean = true;
1062 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1063 r.stopFreezingScreenLocked(false);
1064 } catch (Exception e) {
1065 // Just skip on any failure; we'll make it
1066 // visible when it next restarts.
1067 Slog.w(TAG, "Exception thrown making visibile: "
1068 + r.intent.getComponent(), e);
1069 }
1070 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072
Craig Mautnerd44711d2013-02-23 11:24:36 -08001073 // Aggregate current change flags.
1074 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001075
Craig Mautnerd44711d2013-02-23 11:24:36 -08001076 if (r.fullscreen) {
1077 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001078 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1079 behindFullscreen = true;
1080 } else if (r.mLaunchHomeTaskNext) {
1081 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1082 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001083 behindFullscreen = true;
1084 }
1085 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001086 if (DEBUG_VISBILITY) Slog.v(
1087 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1088 + " state=" + r.state
1089 + " behindFullscreen=" + behindFullscreen);
1090 // Now for any activities that aren't visible to the user, make
1091 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001093 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001094 r.visible = false;
1095 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001096 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001097 switch (r.state) {
1098 case STOPPING:
1099 case STOPPED:
1100 if (r.app != null && r.app.thread != null) {
1101 if (DEBUG_VISBILITY) Slog.v(
1102 TAG, "Scheduling invisibility: " + r);
1103 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1104 }
1105 break;
1106
1107 case INITIALIZING:
1108 case RESUMED:
1109 case PAUSING:
1110 case PAUSED:
1111 stopActivityLocked(r);
1112 break;
1113
1114 default:
1115 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116 }
1117 } catch (Exception e) {
1118 // Just skip on any failure; we'll make it
1119 // visible when it next restarts.
1120 Slog.w(TAG, "Exception thrown making hidden: "
1121 + r.intent.getComponent(), e);
1122 }
1123 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001124 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001125 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001126 }
1127 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001128 }
Craig Mautner580ea812013-04-25 12:58:38 -07001129 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001130 }
Craig Mautner58547802013-03-05 08:23:53 -08001131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001132 /**
1133 * Ensure that the top activity in the stack is resumed.
1134 *
1135 * @param prev The previously resumed activity, for when in the process
1136 * of pausing; can be null to call from elsewhere.
1137 *
1138 * @return Returns true if something is being resumed, or false if
1139 * nothing happened.
1140 */
1141 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001142 return resumeTopActivityLocked(prev, null);
1143 }
1144
1145 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 // Find the first activity that is not finishing.
1147 ActivityRecord next = topRunningActivityLocked(null);
1148
1149 // Remember how we'll process this pause/resume situation, and ensure
1150 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001151 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1152 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153
1154 if (next == null) {
1155 // There are no more activities! Let's just start up the
1156 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001157 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001158 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001159 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 }
1161
1162 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001164 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001165 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1166 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001167 // Make sure we have executed any pending transitions, since there
1168 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001169 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001171 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001172 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 return false;
1174 }
1175
Craig Mautner525f3d92013-05-07 14:01:50 -07001176 final TaskRecord nextTask = next.task;
1177 final TaskRecord prevTask = prev != null ? prev.task : null;
1178 if (prevTask != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001179 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001180 if (prevTask == nextTask) {
1181 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1182 final int numActivities = activities.size();
1183 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1184 final ActivityRecord r = activities.get(activityNdx);
1185 // r is usually the same as next, but what if two activities were launched
1186 // before prev finished?
1187 if (!r.finishing) {
1188 r.mLaunchHomeTaskNext = true;
1189 r.frontOfTask = true;
1190 break;
1191 }
1192 }
1193 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001194 // This task is going away but it was supposed to return to the home task.
1195 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001196 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001197 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1198 } else {
1199 return mStackSupervisor.resumeHomeActivity(prev);
1200 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001201 }
1202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001203 // If we are sleeping, and there is no resumed activity, and the top
1204 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001205 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001206 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001207 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208 // Make sure we have executed any pending transitions, since there
1209 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001210 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001212 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001213 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001214 return false;
1215 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001216
1217 // Make sure that the user who owns this activity is started. If not,
1218 // we will just leave it as is because someone should be bringing
1219 // another user's activities to the top of the stack.
1220 if (mService.mStartedUsers.get(next.userId) == null) {
1221 Slog.w(TAG, "Skipping resume of top activity " + next
1222 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001223 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001224 return false;
1225 }
1226
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001227 // The activity may be waiting for stop, but that is no longer
1228 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001229 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001230 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001231 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001232 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001233
Dianne Hackborn84375872012-06-01 19:03:50 -07001234 next.updateOptionsLocked(options);
1235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001236 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1237
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001238 // If we are currently pausing an activity, then don't do anything
1239 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001240 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1241 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Craig Mautnercf910b02013-04-23 11:23:27 -07001242 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001243 return false;
1244 }
1245
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001246 // Okay we are now going to start a switch, to 'next'. We may first
1247 // have to pause the current activity, but this is an important point
1248 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001249 // XXX "App Redirected" dialog is getting too many false positives
1250 // at this point, so turn off for now.
1251 if (false) {
1252 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1253 long now = SystemClock.uptimeMillis();
1254 final boolean inTime = mLastStartedActivity.startTime != 0
1255 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1256 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1257 final int nextUid = next.info.applicationInfo.uid;
1258 if (inTime && lastUid != nextUid
1259 && lastUid != next.launchedFromUid
1260 && mService.checkPermission(
1261 android.Manifest.permission.STOP_APP_SWITCHES,
1262 -1, next.launchedFromUid)
1263 != PackageManager.PERMISSION_GRANTED) {
1264 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1265 } else {
1266 next.startTime = now;
1267 mLastStartedActivity = next;
1268 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001269 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001270 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001271 mLastStartedActivity = next;
1272 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001273 }
Craig Mautner58547802013-03-05 08:23:53 -08001274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001275 // We need to start pausing the current activity so the top one
1276 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001277 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1278 if (mResumedActivity != null) {
1279 pausing = true;
1280 startPausingLocked(userLeaving, false);
1281 }
1282 if (pausing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001283 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001284 // At this point we want to put the upcoming activity's process
1285 // at the top of the LRU list, since we know we will be needing it
1286 // very soon and it would be a waste to let it get killed if it
1287 // happens to be sitting towards the end.
1288 if (next.app != null && next.app.thread != null) {
1289 // No reason to do full oom adj update here; we'll let that
1290 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001291 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001292 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001293 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001294 return true;
1295 }
1296
Christopher Tated3f175c2012-06-14 14:16:54 -07001297 // If the most recent activity was noHistory but was only stopped rather
1298 // than stopped+finished because the device went to sleep, we need to make
1299 // sure to finish it as we're making a new activity topmost.
1300 final ActivityRecord last = mLastPausedActivity;
1301 if (mService.mSleeping && last != null && !last.finishing) {
1302 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1303 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1304 if (DEBUG_STATES) {
1305 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1306 }
1307 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001308 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001309 }
1310 }
1311
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001312 if (prev != null && prev != next) {
1313 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1314 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001315 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001316 if (DEBUG_SWITCH) Slog.v(
1317 TAG, "Resuming top, waiting visible to hide: " + prev);
1318 } else {
1319 // The next activity is already visible, so hide the previous
1320 // activity's windows right now so we can show the new one ASAP.
1321 // We only do this if the previous is finishing, which should mean
1322 // it is on top of the one being resumed so hiding it quickly
1323 // is good. Otherwise, we want to do the normal route of allowing
1324 // the resumed activity to be shown so we can decide if the
1325 // previous should actually be hidden depending on whether the
1326 // new one is found to be full-screen or not.
1327 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001328 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001329 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1330 + prev + ", waitingVisible="
1331 + (prev != null ? prev.waitingVisible : null)
1332 + ", nowVisible=" + next.nowVisible);
1333 } else {
1334 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1335 + prev + ", waitingVisible="
1336 + (prev != null ? prev.waitingVisible : null)
1337 + ", nowVisible=" + next.nowVisible);
1338 }
1339 }
1340 }
1341
Dianne Hackborne7f97212011-02-24 14:40:20 -08001342 // Launching this app's activity, make sure the app is no longer
1343 // considered stopped.
1344 try {
1345 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001346 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001347 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001348 } catch (IllegalArgumentException e) {
1349 Slog.w(TAG, "Failed trying to unstop package "
1350 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001351 }
1352
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001353 // We are starting up the next activity, so tell the window manager
1354 // that the previous one will be hidden soon. This way it can know
1355 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001356 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 if (prev != null) {
1358 if (prev.finishing) {
1359 if (DEBUG_TRANSITION) Slog.v(TAG,
1360 "Prepare close transition: prev=" + prev);
1361 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001362 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001363 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001365 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001366 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1367 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001368 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001369 mWindowManager.setAppWillBeHidden(prev.appToken);
1370 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001371 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001372 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001373 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001374 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001375 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001376 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001377 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001378 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1379 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380 }
1381 }
1382 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001383 mWindowManager.setAppWillBeHidden(prev.appToken);
1384 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001385 }
Craig Mautner967212c2013-04-13 21:10:58 -07001386 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001387 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001388 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001389 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001390 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001391 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001392 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001393 }
1394 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001395 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001396 next.applyOptionsLocked();
1397 } else {
1398 next.clearOptionsLocked();
1399 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001400
Craig Mautnercf910b02013-04-23 11:23:27 -07001401 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 if (next.app != null && next.app.thread != null) {
1403 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1404
1405 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001406 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001408 // schedule launch ticks to collect information about slow apps.
1409 next.startLaunchTickingLocked();
1410
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001411 ActivityRecord lastResumedActivity =
1412 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 ActivityState lastState = next.state;
1414
1415 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001416
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001417 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001418 next.state = ActivityState.RESUMED;
1419 mResumedActivity = next;
1420 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001421 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001422 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001423 updateLRUListLocked(next);
1424
1425 // Have the window manager re-evaluate the orientation of
1426 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001427 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001428 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001429 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001430 mService.mConfiguration,
1431 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1432 if (config != null) {
1433 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001434 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001435 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001436 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001437
Craig Mautner525f3d92013-05-07 14:01:50 -07001438 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 // The configuration update wasn't able to keep the existing
1440 // instance of the activity, and instead started a new one.
1441 // We should be all done, but let's just make sure our activity
1442 // is still at the top and schedule another run if something
1443 // weird happened.
1444 ActivityRecord nextNext = topRunningActivityLocked(null);
1445 if (DEBUG_SWITCH) Slog.i(TAG,
1446 "Activity config changed during resume: " + next
1447 + ", new next: " + nextNext);
1448 if (nextNext != next) {
1449 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001450 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001452 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001453 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001454 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001455 return true;
1456 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001457 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001458 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 }
Craig Mautner58547802013-03-05 08:23:53 -08001460
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 try {
1462 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001463 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 if (a != null) {
1465 final int N = a.size();
1466 if (!next.finishing && N > 0) {
1467 if (DEBUG_RESULTS) Slog.v(
1468 TAG, "Delivering results to " + next
1469 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001470 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 }
1472 }
1473
1474 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001475 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 }
1477
1478 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001479 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001480 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001481
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001482 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001483 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001484 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001485 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001487
Craig Mautner0eea92c2013-05-16 13:35:39 -07001488 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001489
1490 } catch (Exception e) {
1491 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001492 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1493 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001495 if (lastStack != null) {
1496 lastStack.mResumedActivity = lastResumedActivity;
1497 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001498 Slog.i(TAG, "Restarting because process died: " + next);
1499 if (!next.hasBeenLaunched) {
1500 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001501 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1502 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001503 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001504 next.appToken, next.packageName, next.theme,
1505 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001506 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1507 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001508 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001509 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001510 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 return true;
1512 }
1513
1514 // From this point on, if something goes wrong there is no way
1515 // to recover the activity.
1516 try {
1517 next.visible = true;
1518 completeResumeLocked(next);
1519 } catch (Exception e) {
1520 // If any exception gets thrown, toss away this
1521 // activity and try the next one.
1522 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001523 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001524 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001525 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 return true;
1527 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 next.stopped = false;
1529
1530 } else {
1531 // Whoops, need to restart this activity!
1532 if (!next.hasBeenLaunched) {
1533 next.hasBeenLaunched = true;
1534 } else {
1535 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001536 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001537 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001538 mService.compatibilityInfoForPackageLocked(
1539 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001541 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001542 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 }
1544 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1545 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001546 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 }
1548
Craig Mautnercf910b02013-04-23 11:23:27 -07001549 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001550 return true;
1551 }
1552
Craig Mautner11bf9a52013-02-19 14:08:51 -08001553
Craig Mautner8849a5e2013-04-02 16:41:03 -07001554 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001555 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001556 TaskRecord rTask = r.task;
1557 final int taskId = rTask.taskId;
1558 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001559 // Last activity in task had been removed or ActivityManagerService is reusing task.
1560 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001561 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001562 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001563 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001564 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001565 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001566 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001567 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 if (!newTask) {
1569 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001570 boolean startIt = true;
1571 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1572 task = mTaskHistory.get(taskNdx);
1573 if (task == r.task) {
1574 // Here it is! Now, if this is not yet visible to the
1575 // user, then just add it without starting; it will
1576 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001577 if (!startIt) {
1578 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1579 + task, new RuntimeException("here").fillInStackTrace());
1580 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001581 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001582 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1583 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001584 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001586 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001587 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001588 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589 return;
1590 }
1591 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001592 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 startIt = false;
1594 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595 }
1596 }
1597
1598 // Place a new activity at top of stack, so it is next to interact
1599 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001600
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601 // If we are not placing the new activity frontmost, we do not want
1602 // to deliver the onUserLeaving callback to the actual frontmost
1603 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001604 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001605 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001606 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1607 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 }
Craig Mautner70a86932013-02-28 22:37:44 -08001609
1610 task = r.task;
1611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001613 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001614 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001615 task.addActivityToTop(r);
1616
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001617 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001619 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 // We want to show the starting preview window if we are
1621 // switching to a new task, or the next activity's process is
1622 // not currently running.
1623 boolean showStartingIcon = newTask;
1624 ProcessRecord proc = r.app;
1625 if (proc == null) {
1626 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1627 }
1628 if (proc == null || proc.thread == null) {
1629 showStartingIcon = true;
1630 }
1631 if (DEBUG_TRANSITION) Slog.v(TAG,
1632 "Prepare open transition: starting " + r);
1633 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001634 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001637 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001638 ? AppTransition.TRANSIT_TASK_OPEN
1639 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 mNoAnimActivities.remove(r);
1641 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001642 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001643 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001644 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001645 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001646 boolean doShow = true;
1647 if (newTask) {
1648 // Even though this activity is starting fresh, we still need
1649 // to reset it to make sure we apply affinities to move any
1650 // existing activities from other tasks in to it.
1651 // If the caller has requested that the target task be
1652 // reset, then do so.
1653 if ((r.intent.getFlags()
1654 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1655 resetTaskIfNeededLocked(r, r);
1656 doShow = topRunningNonDelayedActivityLocked(null) == r;
1657 }
1658 }
1659 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1660 // Figure out if we are transitioning from another activity that is
1661 // "has the same starting icon" as the next one. This allows the
1662 // window manager to keep the previous window it had previously
1663 // created, if it still had one.
1664 ActivityRecord prev = mResumedActivity;
1665 if (prev != null) {
1666 // We don't want to reuse the previous starting preview if:
1667 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001668 if (prev.task != r.task) {
1669 prev = null;
1670 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001672 else if (prev.nowVisible) {
1673 prev = null;
1674 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001676 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001677 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001678 mService.compatibilityInfoForPackageLocked(
1679 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001680 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001681 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001682 }
1683 } else {
1684 // If this is the first activity, don't do any fancy animations,
1685 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001686 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001687 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001688 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001689 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 }
1691 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001692 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 }
1694
1695 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001696 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001697 }
1698 }
1699
Dianne Hackbornbe707852011-11-11 14:32:10 -08001700 final void validateAppTokensLocked() {
1701 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001702 mValidateAppTokens.ensureCapacity(numActivities());
1703 final int numTasks = mTaskHistory.size();
1704 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1705 TaskRecord task = mTaskHistory.get(taskNdx);
1706 final ArrayList<ActivityRecord> activities = task.mActivities;
1707 if (activities.size() == 0) {
1708 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001709 }
Craig Mautner000f0022013-02-26 15:04:29 -08001710 TaskGroup group = new TaskGroup();
1711 group.taskId = task.taskId;
1712 mValidateAppTokens.add(group);
1713 final int numActivities = activities.size();
1714 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1715 final ActivityRecord r = activities.get(activityNdx);
1716 group.tokens.add(r.appToken);
1717 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001718 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001719 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001720 }
1721
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 /**
1723 * Perform a reset of the given task, if needed as part of launching it.
1724 * Returns the new HistoryRecord at the top of the task.
1725 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001726 /**
1727 * Helper method for #resetTaskIfNeededLocked.
1728 * We are inside of the task being reset... we'll either finish this activity, push it out
1729 * for another task, or leave it as-is.
1730 * @param task The task containing the Activity (taskTop) that might be reset.
1731 * @param forceReset
1732 * @return An ActivityOptions that needs to be processed.
1733 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001734 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001735 ActivityOptions topOptions = null;
1736
1737 int replyChainEnd = -1;
1738 boolean canMoveOptions = true;
1739
1740 // We only do this for activities that are not the root of the task (since if we finish
1741 // the root, we may no longer have the task!).
1742 final ArrayList<ActivityRecord> activities = task.mActivities;
1743 final int numActivities = activities.size();
1744 for (int i = numActivities - 1; i > 0; --i ) {
1745 ActivityRecord target = activities.get(i);
1746
1747 final int flags = target.info.flags;
1748 final boolean finishOnTaskLaunch =
1749 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1750 final boolean allowTaskReparenting =
1751 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1752 final boolean clearWhenTaskReset =
1753 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1754
1755 if (!finishOnTaskLaunch
1756 && !clearWhenTaskReset
1757 && target.resultTo != null) {
1758 // If this activity is sending a reply to a previous
1759 // activity, we can't do anything with it now until
1760 // we reach the start of the reply chain.
1761 // XXX note that we are assuming the result is always
1762 // to the previous activity, which is almost always
1763 // the case but we really shouldn't count on.
1764 if (replyChainEnd < 0) {
1765 replyChainEnd = i;
1766 }
1767 } else if (!finishOnTaskLaunch
1768 && !clearWhenTaskReset
1769 && allowTaskReparenting
1770 && target.taskAffinity != null
1771 && !target.taskAffinity.equals(task.affinity)) {
1772 // If this activity has an affinity for another
1773 // task, then we need to move it out of here. We will
1774 // move it as far out of the way as possible, to the
1775 // bottom of the activity stack. This also keeps it
1776 // correctly ordered with any activities we previously
1777 // moved.
1778 TaskRecord bottomTask = mTaskHistory.get(0);
1779 ActivityRecord p = bottomTask.mActivities.get(0);
1780 if (target.taskAffinity != null
1781 && target.taskAffinity.equals(p.task.affinity)) {
1782 // If the activity currently at the bottom has the
1783 // same task affinity as the one we are moving,
1784 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001785 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001786 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1787 + " out to bottom task " + p.task);
1788 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001789 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001790 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001791 target.task.affinityIntent = target.intent;
1792 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1793 + " out to new task " + target.task);
1794 }
1795
1796 final TaskRecord targetTask = target.task;
1797 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001798 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001799
Craig Mautner525f3d92013-05-07 14:01:50 -07001800 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001801 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1802 for (int srcPos = start; srcPos >= i; --srcPos) {
1803 p = activities.get(srcPos);
1804 if (p.finishing) {
1805 continue;
1806 }
1807
Craig Mautner525f3d92013-05-07 14:01:50 -07001808 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001809 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001810 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001811 topOptions = p.takeOptionsLocked();
1812 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001813 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001814 }
1815 }
1816 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1817 + task + " adding to task=" + targetTask,
1818 new RuntimeException("here").fillInStackTrace());
1819 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1820 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001821 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001822 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001823
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001824 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001825 }
1826
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001827 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001828 if (VALIDATE_TOKENS) {
1829 validateAppTokensLocked();
1830 }
1831
1832 replyChainEnd = -1;
1833 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1834 // If the activity should just be removed -- either
1835 // because it asks for it, or the task should be
1836 // cleared -- then finish it and anything that is
1837 // part of its reply chain.
1838 int end;
1839 if (clearWhenTaskReset) {
1840 // In this case, we want to finish this activity
1841 // and everything above it, so be sneaky and pretend
1842 // like these are all in the reply chain.
1843 end = numActivities - 1;
1844 } else if (replyChainEnd < 0) {
1845 end = i;
1846 } else {
1847 end = replyChainEnd;
1848 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001849 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001850 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001851 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001852 if (p.finishing) {
1853 continue;
1854 }
1855 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001856 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001857 topOptions = p.takeOptionsLocked();
1858 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001859 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001860 }
1861 }
Craig Mautner58547802013-03-05 08:23:53 -08001862 if (DEBUG_TASKS) Slog.w(TAG,
1863 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001864 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001865 end--;
1866 srcPos--;
1867 }
1868 }
1869 replyChainEnd = -1;
1870 } else {
1871 // If we were in the middle of a chain, well the
1872 // activity that started it all doesn't want anything
1873 // special, so leave it all as-is.
1874 replyChainEnd = -1;
1875 }
1876 }
1877
1878 return topOptions;
1879 }
1880
1881 /**
1882 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1883 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1884 * @param affinityTask The task we are looking for an affinity to.
1885 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1886 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1887 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1888 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001889 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001890 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001891 int replyChainEnd = -1;
1892 final int taskId = task.taskId;
1893 final String taskAffinity = task.affinity;
1894
1895 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1896 final int numActivities = activities.size();
1897 // Do not operate on the root Activity.
1898 for (int i = numActivities - 1; i > 0; --i) {
1899 ActivityRecord target = activities.get(i);
1900
1901 final int flags = target.info.flags;
1902 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1903 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1904
1905 if (target.resultTo != null) {
1906 // If this activity is sending a reply to a previous
1907 // activity, we can't do anything with it now until
1908 // we reach the start of the reply chain.
1909 // XXX note that we are assuming the result is always
1910 // to the previous activity, which is almost always
1911 // the case but we really shouldn't count on.
1912 if (replyChainEnd < 0) {
1913 replyChainEnd = i;
1914 }
1915 } else if (topTaskIsHigher
1916 && allowTaskReparenting
1917 && taskAffinity != null
1918 && taskAffinity.equals(target.taskAffinity)) {
1919 // This activity has an affinity for our task. Either remove it if we are
1920 // clearing or move it over to our task. Note that
1921 // we currently punt on the case where we are resetting a
1922 // task that is not at the top but who has activities above
1923 // with an affinity to it... this is really not a normal
1924 // case, and we will need to later pull that task to the front
1925 // and usually at that point we will do the reset and pick
1926 // up those remaining activities. (This only happens if
1927 // someone starts an activity in a new task from an activity
1928 // in a task that is not currently on top.)
1929 if (forceReset || finishOnTaskLaunch) {
1930 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1931 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1932 for (int srcPos = start; srcPos >= i; --srcPos) {
1933 final ActivityRecord p = activities.get(srcPos);
1934 if (p.finishing) {
1935 continue;
1936 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001937 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001938 }
1939 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001940 if (taskInsertionPoint < 0) {
1941 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001942
Craig Mautner77878772013-03-04 19:46:24 -08001943 }
Craig Mautner77878772013-03-04 19:46:24 -08001944
1945 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1946 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1947 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1948 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001949 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001950 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08001951 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001952
Craig Mautnere3a74d52013-02-22 14:14:58 -08001953 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
1954 + " to stack at " + task,
1955 new RuntimeException("here").fillInStackTrace());
1956 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
1957 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001958 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001959 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001960 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08001961 if (VALIDATE_TOKENS) {
1962 validateAppTokensLocked();
1963 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08001964
1965 // Now we've moved it in to place... but what if this is
1966 // a singleTop activity and we have put it on top of another
1967 // instance of the same activity? Then we drop the instance
1968 // below so it remains singleTop.
1969 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
1970 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001971 int targetNdx = taskActivities.indexOf(target);
1972 if (targetNdx > 0) {
1973 ActivityRecord p = taskActivities.get(targetNdx - 1);
1974 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08001975 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
1976 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001977 }
1978 }
1979 }
1980 }
1981
1982 replyChainEnd = -1;
1983 }
1984 }
Craig Mautner77878772013-03-04 19:46:24 -08001985 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001986 }
1987
Craig Mautner8849a5e2013-04-02 16:41:03 -07001988 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08001989 ActivityRecord newActivity) {
1990 boolean forceReset =
1991 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
1992 if (ACTIVITY_INACTIVE_RESET_TIME > 0
1993 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
1994 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
1995 forceReset = true;
1996 }
1997 }
1998
1999 final TaskRecord task = taskTop.task;
2000
2001 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2002 * for remaining tasks. Used for later tasks to reparent to task. */
2003 boolean taskFound = false;
2004
2005 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2006 ActivityOptions topOptions = null;
2007
Craig Mautner77878772013-03-04 19:46:24 -08002008 // Preserve the location for reparenting in the new task.
2009 int reparentInsertionPoint = -1;
2010
Craig Mautnere3a74d52013-02-22 14:14:58 -08002011 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2012 final TaskRecord targetTask = mTaskHistory.get(i);
2013
2014 if (targetTask == task) {
2015 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2016 taskFound = true;
2017 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002018 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2019 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002020 }
2021 }
2022
Craig Mautner70a86932013-02-28 22:37:44 -08002023 int taskNdx = mTaskHistory.indexOf(task);
2024 do {
2025 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2026 } while (taskTop == null && taskNdx >= 0);
2027
Craig Mautnere3a74d52013-02-22 14:14:58 -08002028 if (topOptions != null) {
2029 // If we got some ActivityOptions from an activity on top that
2030 // was removed from the task, propagate them to the new real top.
2031 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002032 taskTop.updateOptionsLocked(topOptions);
2033 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002034 topOptions.abort();
2035 }
2036 }
2037
2038 return taskTop;
2039 }
2040
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2042 String resultWho, int requestCode, int resultCode, Intent data) {
2043
2044 if (callingUid > 0) {
2045 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002046 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002047 }
2048
2049 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2050 + " : who=" + resultWho + " req=" + requestCode
2051 + " res=" + resultCode + " data=" + data);
2052 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2053 try {
2054 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2055 list.add(new ResultInfo(resultWho, requestCode,
2056 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002057 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 return;
2059 } catch (Exception e) {
2060 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2061 }
2062 }
2063
2064 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2065 }
2066
Craig Mautnerf3333272013-04-22 10:55:53 -07002067 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2069 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2070 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2071 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002072 if (!mService.mSleeping) {
2073 if (DEBUG_STATES) {
2074 Slog.d(TAG, "no-history finish of " + r);
2075 }
2076 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002077 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002078 } else {
2079 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2080 + " on stop because we're just sleeping");
2081 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002083 }
2084
2085 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002086 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 if (mService.mFocusedActivity == r) {
2088 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2089 }
2090 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002091 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 try {
2093 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002094 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2095 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 r.state = ActivityState.STOPPING;
2097 if (DEBUG_VISBILITY) Slog.v(
2098 TAG, "Stopping visible=" + r.visible + " for " + r);
2099 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002100 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002101 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002102 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002103 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002104 r.setSleeping(true);
2105 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002106 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002107 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108 } catch (Exception e) {
2109 // Maybe just ignore exceptions here... if the process
2110 // has crashed, our death notification will clean things
2111 // up.
2112 Slog.w(TAG, "Exception thrown during pause", e);
2113 // Just in case, assume it to be stopped.
2114 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002115 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 r.state = ActivityState.STOPPED;
2117 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002118 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 }
2120 }
2121 }
2122 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002123
Craig Mautner525f3d92013-05-07 14:01:50 -07002124 final ActivityRecord activityIdleInternalLocked(final IBinder token) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002125 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2126
Craig Mautnere79d42682013-04-01 19:01:53 -07002127 // Get the activity record.
Craig Mautner525f3d92013-05-07 14:01:50 -07002128 ActivityRecord res = isInStackLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002129 if (res != null) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002130 // No longer need to keep the device awake.
Craig Mautnerf3333272013-04-22 10:55:53 -07002131 if (mResumedActivity == res && mLaunchingActivity.isHeld()) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002132 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Dianne Hackborn053f61d2013-06-26 18:07:43 -07002133 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2134 throw new IllegalStateException("Calling must be system uid");
2135 }
Craig Mautnere79d42682013-04-01 19:01:53 -07002136 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 }
2138
Craig Mautnere79d42682013-04-01 19:01:53 -07002139 // If this activity is fullscreen, set up to hide those under it.
Craig Mautnerf3333272013-04-22 10:55:53 -07002140 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + res);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002141 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002142 }
2143
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002144 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145 }
2146
2147 /**
2148 * @return Returns true if the activity is being finished, false if for
2149 * some reason it is being left as-is.
2150 */
2151 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002152 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002153 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002154 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002155 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002156 + ", result=" + resultCode + ", data=" + resultData
2157 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002158 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 return false;
2160 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161
Craig Mautnerd44711d2013-02-23 11:24:36 -08002162 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 return true;
2164 }
2165
Craig Mautnerd2328952013-03-05 12:46:26 -08002166 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002167 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2168 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2169 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2170 ActivityRecord r = activities.get(activityNdx);
2171 if (r.resultTo == self && r.requestCode == requestCode) {
2172 if ((r.resultWho == null && resultWho == null) ||
2173 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2174 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2175 false);
2176 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002177 }
2178 }
2179 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002180 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002181 }
2182
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002183 final void finishTopRunningActivityLocked(ProcessRecord app) {
2184 ActivityRecord r = topRunningActivityLocked(null);
2185 if (r != null && r.app == app) {
2186 // If the top running activity is from this crashing
2187 // process, then terminate it to avoid getting in a loop.
2188 Slog.w(TAG, " Force finishing activity "
2189 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002190 int taskNdx = mTaskHistory.indexOf(r.task);
2191 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002192 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002193 // Also terminate any activities below it that aren't yet
2194 // stopped, to avoid a situation where one will get
2195 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002196 --activityNdx;
2197 if (activityNdx < 0) {
2198 do {
2199 --taskNdx;
2200 if (taskNdx < 0) {
2201 break;
2202 }
2203 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2204 } while (activityNdx < 0);
2205 }
2206 if (activityNdx >= 0) {
2207 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002208 if (r.state == ActivityState.RESUMED
2209 || r.state == ActivityState.PAUSING
2210 || r.state == ActivityState.PAUSED) {
Craig Mautner86d67a42013-05-14 10:34:38 -07002211 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002212 Slog.w(TAG, " Force finishing activity "
2213 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002214 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002215 }
2216 }
2217 }
2218 }
2219 }
2220
Craig Mautnerd2328952013-03-05 12:46:26 -08002221 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002222 ArrayList<ActivityRecord> activities = r.task.mActivities;
2223 for (int index = activities.indexOf(r); index >= 0; --index) {
2224 ActivityRecord cur = activities.get(index);
2225 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002226 break;
2227 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002228 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002229 }
2230 return true;
2231 }
2232
Dianne Hackborn5c607432012-02-28 14:44:19 -08002233 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2234 // send the result
2235 ActivityRecord resultTo = r.resultTo;
2236 if (resultTo != null) {
2237 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2238 + " who=" + r.resultWho + " req=" + r.requestCode
2239 + " res=" + resultCode + " data=" + resultData);
2240 if (r.info.applicationInfo.uid > 0) {
2241 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2242 resultTo.packageName, resultData,
2243 resultTo.getUriPermissionsLocked());
2244 }
2245 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2246 resultData);
2247 r.resultTo = null;
2248 }
2249 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2250
2251 // Make sure this HistoryRecord is not holding on to other resources,
2252 // because clients have remote IPC references to this object so we
2253 // can't assume that will go away and want to avoid circular IPC refs.
2254 r.results = null;
2255 r.pendingResults = null;
2256 r.newIntents = null;
2257 r.icicle = null;
2258 }
2259
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 /**
2261 * @return Returns true if this activity has been removed from the history
2262 * list, or false if it is still in the list and will be removed later.
2263 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002264 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2265 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002266 if (r.finishing) {
2267 Slog.w(TAG, "Duplicate finish request for " + r);
2268 return false;
2269 }
2270
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002271 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002273 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002275 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2276 final int index = activities.indexOf(r);
2277 if (index < (activities.size() - 1)) {
2278 ActivityRecord next = activities.get(index+1);
2279 if (r.frontOfTask) {
2280 // The next activity is now the front of the task.
2281 next.frontOfTask = true;
2282 }
2283 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2284 // If the caller asked that this activity (and all above it)
2285 // be cleared when the task is reset, don't lose that information,
2286 // but propagate it up to the next activity.
2287 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002288 }
2289 }
2290
2291 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002292 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002294 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 }
2296 }
2297
Dianne Hackborn5c607432012-02-28 14:44:19 -08002298 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002299
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002300 if (mService.mPendingThumbnails.size() > 0) {
2301 // There are clients waiting to receive thumbnails so, in case
2302 // this is an activity that someone is waiting for, add it
2303 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002304 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002305 }
2306
Craig Mautnerde4ef022013-04-07 19:01:33 -07002307 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002308 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 if (DEBUG_TRANSITION) Slog.v(TAG,
2310 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002311 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002312 ? AppTransition.TRANSIT_TASK_CLOSE
2313 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002314
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002315 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002316 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002317
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002318 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2320 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2321 startPausingLocked(false, false);
2322 }
2323
2324 } else if (r.state != ActivityState.PAUSING) {
2325 // If the activity is PAUSING, we will complete the finish once
2326 // it is done pausing; else we can just directly finish it here.
2327 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002328 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 } else {
2330 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2331 }
2332
2333 return false;
2334 }
2335
Craig Mautnerf3333272013-04-22 10:55:53 -07002336 static final int FINISH_IMMEDIATELY = 0;
2337 static final int FINISH_AFTER_PAUSE = 1;
2338 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339
Craig Mautnerf3333272013-04-22 10:55:53 -07002340 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002341 // First things first: if this activity is currently visible,
2342 // and the resumed activity is not yet visible, then hold off on
2343 // finishing until the resumed one becomes visible.
2344 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002345 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2346 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002347 if (mStackSupervisor.mStoppingActivities.size() > 3
2348 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002349 // If we already have a few activities waiting to stop,
2350 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002351 // them out. Or if r is the last of activity of the last task the stack
2352 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002353 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002354 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002355 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002356 }
2357 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002358 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2359 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002361 if (oomAdj) {
2362 mService.updateOomAdjLocked();
2363 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 return r;
2365 }
2366
2367 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002368 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002369 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002370 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 if (mResumedActivity == r) {
2372 mResumedActivity = null;
2373 }
2374 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002375 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376 r.state = ActivityState.FINISHING;
2377
2378 if (mode == FINISH_IMMEDIATELY
2379 || prevState == ActivityState.STOPPED
2380 || prevState == ActivityState.INITIALIZING) {
2381 // If this activity is already stopped, we can just finish
2382 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002383 boolean activityRemoved = destroyActivityLocked(r, true,
2384 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002385 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002386 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002387 }
2388 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002390
2391 // Need to go through the full pause cycle to get this
2392 // activity into the stopped state and then finish it.
2393 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002394 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002395 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002396 return r;
2397 }
2398
Craig Mautnerd2328952013-03-05 12:46:26 -08002399 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002400 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002401 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002402 final TaskRecord task = srec.task;
2403 final ArrayList<ActivityRecord> activities = task.mActivities;
2404 final int start = activities.indexOf(srec);
2405 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002406 return false;
2407 }
2408 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002409 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002410 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002411 final ComponentName dest = destIntent.getComponent();
2412 if (start > 0 && dest != null) {
2413 for (int i = finishTo; i >= 0; i--) {
2414 ActivityRecord r = activities.get(i);
2415 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002416 r.info.name.equals(dest.getClassName())) {
2417 finishTo = i;
2418 parent = r;
2419 foundParentInTask = true;
2420 break;
2421 }
2422 }
2423 }
2424
2425 IActivityController controller = mService.mController;
2426 if (controller != null) {
2427 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2428 if (next != null) {
2429 // ask watcher if this is allowed
2430 boolean resumeOK = true;
2431 try {
2432 resumeOK = controller.activityResuming(next.packageName);
2433 } catch (RemoteException e) {
2434 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002435 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002436 }
2437
2438 if (!resumeOK) {
2439 return false;
2440 }
2441 }
2442 }
2443 final long origId = Binder.clearCallingIdentity();
2444 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002445 ActivityRecord r = activities.get(i);
2446 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002447 // Only return the supplied result for the first activity finished
2448 resultCode = Activity.RESULT_CANCELED;
2449 resultData = null;
2450 }
2451
2452 if (parent != null && foundParentInTask) {
2453 final int parentLaunchMode = parent.info.launchMode;
2454 final int destIntentFlags = destIntent.getFlags();
2455 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2456 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2457 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2458 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2459 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2460 } else {
2461 try {
2462 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2463 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002464 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002465 null, aInfo, parent.appToken, null,
2466 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2467 0, null, true, null);
2468 foundParentInTask = res == ActivityManager.START_SUCCESS;
2469 } catch (RemoteException e) {
2470 foundParentInTask = false;
2471 }
2472 requestFinishActivityLocked(parent.appToken, resultCode,
2473 resultData, "navigate-up", true);
2474 }
2475 }
2476 Binder.restoreCallingIdentity(origId);
2477 return foundParentInTask;
2478 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002479 /**
2480 * Perform the common clean-up of an activity record. This is called both
2481 * as part of destroyActivityLocked() (when destroying the client-side
2482 * representation) and cleaning things up as a result of its hosting
2483 * processing going away, in which case there is no remaining client-side
2484 * state to destroy so only the cleanup here is needed.
2485 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002486 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2487 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002488 if (mResumedActivity == r) {
2489 mResumedActivity = null;
2490 }
2491 if (mService.mFocusedActivity == r) {
2492 mService.mFocusedActivity = null;
2493 }
2494
2495 r.configDestroy = false;
2496 r.frozenBeforeDestroy = false;
2497
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002498 if (setState) {
2499 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2500 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002501 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002502 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002503 }
2504
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 // Make sure this record is no longer in the pending finishes list.
2506 // This could happen, for example, if we are trimming activities
2507 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002508 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002509 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002510
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 // Remove any pending results.
2512 if (r.finishing && r.pendingResults != null) {
2513 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2514 PendingIntentRecord rec = apr.get();
2515 if (rec != null) {
2516 mService.cancelIntentSenderLocked(rec, false);
2517 }
2518 }
2519 r.pendingResults = null;
2520 }
2521
2522 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002523 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002524 }
2525
2526 if (mService.mPendingThumbnails.size() > 0) {
2527 // There are clients waiting to receive thumbnails so, in case
2528 // this is an activity that someone is waiting for, add it
2529 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002530 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 }
2532
2533 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002534 removeTimeoutsForActivityLocked(r);
2535 }
2536
2537 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002538 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002539 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002540 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002541 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002542 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002543 }
2544
Dianne Hackborn5c607432012-02-28 14:44:19 -08002545 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002546 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2547 r.makeFinishing();
2548 if (DEBUG_ADD_REMOVE) {
2549 RuntimeException here = new RuntimeException("here");
2550 here.fillInStackTrace();
2551 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002552 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002553 final TaskRecord task = r.task;
2554 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002555 if (DEBUG_STACK) Slog.i(TAG,
2556 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002557 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002558 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002559 r.takeFromHistory();
2560 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002561 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002562 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002563 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002564 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002565 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002566 if (VALIDATE_TOKENS) {
2567 validateAppTokensLocked();
2568 }
2569 cleanUpActivityServicesLocked(r);
2570 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002571 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002572
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002573 /**
2574 * Perform clean-up of service connections in an activity record.
2575 */
2576 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2577 // Throw away any services that have been bound by this activity.
2578 if (r.connections != null) {
2579 Iterator<ConnectionRecord> it = r.connections.iterator();
2580 while (it.hasNext()) {
2581 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002582 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002583 }
2584 r.connections = null;
2585 }
2586 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002587
2588 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2589 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2590 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2591 mHandler.sendMessage(msg);
2592 }
2593
Dianne Hackborn28695e02011-11-02 21:59:51 -07002594 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002595 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002596 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002597 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2598 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2599 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2600 final ActivityRecord r = activities.get(activityNdx);
2601 if (r.finishing) {
2602 continue;
2603 }
2604 if (r.fullscreen) {
2605 lastIsOpaque = true;
2606 }
2607 if (owner != null && r.app != owner) {
2608 continue;
2609 }
2610 if (!lastIsOpaque) {
2611 continue;
2612 }
2613 // We can destroy this one if we have its icicle saved and
2614 // it is not in the process of pausing/stopping/finishing.
2615 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2616 && r.haveState && !r.visible && r.stopped
2617 && r.state != ActivityState.DESTROYING
2618 && r.state != ActivityState.DESTROYED) {
2619 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2620 + " resumed=" + mResumedActivity
2621 + " pausing=" + mPausingActivity);
2622 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2623 activityRemoved = true;
2624 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002625 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002626 }
2627 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002628 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002629 mStackSupervisor.resumeTopActivitiesLocked();
2630
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002631 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002632 }
2633
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002634 /**
2635 * Destroy the current CLIENT SIDE instance of an activity. This may be
2636 * called both when actually finishing an activity, or when performing
2637 * a configuration switch where we destroy the current client-side object
2638 * but then create a new client-side object for this same HistoryRecord.
2639 */
2640 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002641 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002642 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002643 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002644 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2645 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002646 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002647 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002648
2649 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002650
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002651 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652
2653 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002654
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002655 if (hadApp) {
2656 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002657 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002658 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2659 mService.mHeavyWeightProcess = null;
2660 mService.mHandler.sendEmptyMessage(
2661 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2662 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002664 // No longer have activities, so update oom adj.
2665 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 }
2667 }
2668
2669 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002670
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 try {
2672 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002673 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002674 r.configChangeFlags);
2675 } catch (Exception e) {
2676 // We can just ignore exceptions here... if the process
2677 // has crashed, our death notification will clean things
2678 // up.
2679 //Slog.w(TAG, "Exception thrown during finish", e);
2680 if (r.finishing) {
2681 removeActivityFromHistoryLocked(r);
2682 removedFromHistory = true;
2683 skipDestroy = true;
2684 }
2685 }
2686
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002687 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002688
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002689 // If the activity is finishing, we need to wait on removing it
2690 // from the list to give it a chance to do its cleanup. During
2691 // that time it may make calls back with its token so we need to
2692 // be able to find it on the list and so we don't want to remove
2693 // it from the list yet. Otherwise, we can just immediately put
2694 // it in the destroyed state since we are not removing it from the
2695 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002697 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2698 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002699 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002700 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2702 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002703 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002704 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002705 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002706 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707 }
2708 } else {
2709 // remove this record from the history.
2710 if (r.finishing) {
2711 removeActivityFromHistoryLocked(r);
2712 removedFromHistory = true;
2713 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002714 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002715 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002716 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002717 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002718 }
2719 }
2720
2721 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002722
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002723 if (!mLRUActivities.remove(r) && hadApp) {
2724 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2725 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002726
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002727 return removedFromHistory;
2728 }
2729
Craig Mautnerd2328952013-03-05 12:46:26 -08002730 final void activityDestroyedLocked(IBinder token) {
2731 final long origId = Binder.clearCallingIdentity();
2732 try {
2733 ActivityRecord r = ActivityRecord.forToken(token);
2734 if (r != null) {
2735 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002737
2738 if (isInStackLocked(token) != null) {
2739 if (r.state == ActivityState.DESTROYING) {
2740 cleanUpActivityLocked(r, true, false);
2741 removeActivityFromHistoryLocked(r);
2742 }
2743 }
Craig Mautner05d29032013-05-03 13:40:13 -07002744 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002745 } finally {
2746 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002747 }
2748 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002749
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002750 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2751 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002752 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002753 if (DEBUG_CLEANUP) Slog.v(
2754 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 + " with " + i + " entries");
2756 while (i > 0) {
2757 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002758 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002759 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002761 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002762 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002763 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 }
2765 }
2766 }
2767
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002768 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2769 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002770 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2771 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002772 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2773 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002774 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002775 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002776 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2777 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002778
2779 boolean hasVisibleActivities = false;
2780
2781 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002782 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002783 if (DEBUG_CLEANUP) Slog.v(
2784 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002785 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2786 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2787 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2788 final ActivityRecord r = activities.get(activityNdx);
2789 --i;
2790 if (DEBUG_CLEANUP) Slog.v(
2791 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2792 if (r.app == app) {
2793 boolean remove;
2794 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2795 // Don't currently have state for the activity, or
2796 // it is finishing -- always remove it.
2797 remove = true;
2798 } else if (r.launchCount > 2 &&
2799 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2800 // We have launched this activity too many times since it was
2801 // able to run, so give up and remove it.
2802 remove = true;
2803 } else {
2804 // The process may be gone, but the activity lives on!
2805 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002806 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002807 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002808 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002809 RuntimeException here = new RuntimeException("here");
2810 here.fillInStackTrace();
2811 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2812 + ": haveState=" + r.haveState
2813 + " stateNotNeeded=" + r.stateNotNeeded
2814 + " finishing=" + r.finishing
2815 + " state=" + r.state, here);
2816 }
2817 if (!r.finishing) {
2818 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2819 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2820 r.userId, System.identityHashCode(r),
2821 r.task.taskId, r.shortComponentName,
2822 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002823 if (r.state == ActivityState.RESUMED) {
2824 mService.updateUsageStats(r, false);
2825 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002826 }
2827 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002828
Craig Mautner0247fc82013-02-28 14:32:06 -08002829 } else {
2830 // We have the current state for this activity, so
2831 // it can be restarted later when needed.
2832 if (localLOGV) Slog.v(
2833 TAG, "Keeping entry, setting app to null");
2834 if (r.visible) {
2835 hasVisibleActivities = true;
2836 }
2837 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2838 + r);
2839 r.app = null;
2840 r.nowVisible = false;
2841 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002842 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002843 "App died, clearing saved state of " + r);
2844 r.icicle = null;
2845 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002846 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002847
Craig Mautnerd2328952013-03-05 12:46:26 -08002848 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002849 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002850 }
2851 }
2852
2853 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002854 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002855
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002856 final void updateTransitLocked(int transit, Bundle options) {
2857 if (options != null) {
2858 ActivityRecord r = topRunningActivityLocked(null);
2859 if (r != null && r.state != ActivityState.RESUMED) {
2860 r.updateOptionsLocked(options);
2861 } else {
2862 ActivityOptions.abort(options);
2863 }
2864 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002865 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002866 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002867
Craig Mautnercae015f2013-02-08 14:31:27 -08002868 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002869 final TaskRecord task = taskForIdLocked(taskId);
2870 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002871 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002872 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002873 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002874 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2875 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002876 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002877 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002878 }
2879 moveTaskToFrontLocked(task, null, options);
2880 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002881 }
2882 return false;
2883 }
2884
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002885 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002886 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887
Craig Mautner11bf9a52013-02-19 14:08:51 -08002888 final int numTasks = mTaskHistory.size();
2889 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002890 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002891 // nothing to do!
2892 if (reason != null &&
2893 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2894 ActivityOptions.abort(options);
2895 } else {
2896 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2897 }
2898 return;
2899 }
2900
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002901 mStackSupervisor.moveHomeStack(isHomeStack());
2902
Craig Mautner11bf9a52013-02-19 14:08:51 -08002903 // Shift all activities with this task up to the top
2904 // of the stack, keeping them in the same internal order.
2905 mTaskHistory.remove(tr);
2906 mTaskHistory.add(tr);
2907
2908 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002909 if (reason != null &&
2910 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002911 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002912 ActivityRecord r = topRunningActivityLocked(null);
2913 if (r != null) {
2914 mNoAnimActivities.add(r);
2915 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002916 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002918 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002920
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002921 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002922
Craig Mautner05d29032013-05-03 13:40:13 -07002923 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002924 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002925
2926 if (VALIDATE_TOKENS) {
2927 validateAppTokensLocked();
2928 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 }
2930
2931 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002932 * Worker method for rearranging history stack. Implements the function of moving all
2933 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002934 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002935 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2937 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002938 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002939 * @param task The taskId to collect and move to the bottom.
2940 * @return Returns true if the move completed, false if not.
2941 */
2942 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
2943 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002944
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002945 // If we have a watcher, preflight the move before committing to it. First check
2946 // for *other* available tasks, but if none are available, then try again allowing the
2947 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002948 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949 ActivityRecord next = topRunningActivityLocked(null, task);
2950 if (next == null) {
2951 next = topRunningActivityLocked(null, 0);
2952 }
2953 if (next != null) {
2954 // ask watcher if this is allowed
2955 boolean moveOK = true;
2956 try {
2957 moveOK = mService.mController.activityResuming(next.packageName);
2958 } catch (RemoteException e) {
2959 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002960 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002961 }
2962 if (!moveOK) {
2963 return false;
2964 }
2965 }
2966 }
2967
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002968 if (DEBUG_TRANSITION) Slog.v(TAG,
2969 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970
Craig Mautnerd2328952013-03-05 12:46:26 -08002971 final TaskRecord tr = taskForIdLocked(task);
2972 if (tr == null) {
2973 return false;
2974 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002975 mTaskHistory.remove(tr);
2976 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977
2978 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002979 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2980 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002981 ActivityRecord r = topRunningActivityLocked(null);
2982 if (r != null) {
2983 mNoAnimActivities.add(r);
2984 }
2985 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002986 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08002987 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002989 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002990
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002992 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 }
2994
Craig Mautnerde4ef022013-04-07 19:01:33 -07002995 if (mResumedActivity != null && mResumedActivity.task == tr &&
2996 mResumedActivity.mLaunchHomeTaskNext) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07002997 // TODO: Can we skip the next line and just pass mResumedAct. to resumeHomeAct.()?
Craig Mautnerde4ef022013-04-07 19:01:33 -07002998 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07002999 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003000 }
3001
Craig Mautner05d29032013-05-03 13:40:13 -07003002 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 return true;
3004 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003005
Craig Mautner8849a5e2013-04-02 16:41:03 -07003006 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003008 final Uri data = r.intent.getData();
3009 final String strData = data != null ? data.toSafeString() : null;
3010
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003012 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003014 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015 }
3016
3017 /**
3018 * Make sure the given activity matches the current configuration. Returns
3019 * false if the activity had to be destroyed. Returns true if the
3020 * configuration is the same, or the activity will remain running as-is
3021 * for whatever reason. Ensures the HistoryRecord is updated with the
3022 * correct configuration and all other bookkeeping is handled.
3023 */
3024 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3025 int globalChanges) {
3026 if (mConfigWillChange) {
3027 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3028 "Skipping config check (will change): " + r);
3029 return true;
3030 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003031
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003032 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3033 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003034
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035 // Short circuit: if the two configurations are the exact same
3036 // object (the common case), then there is nothing to do.
3037 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003038 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003039 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3040 "Configuration unchanged in " + r);
3041 return true;
3042 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003043
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003044 // We don't worry about activities that are finishing.
3045 if (r.finishing) {
3046 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3047 "Configuration doesn't matter in finishing " + r);
3048 r.stopFreezingScreenLocked(false);
3049 return true;
3050 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003051
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 // Okay we now are going to make this activity have the new config.
3053 // But then we need to figure out how it needs to deal with that.
3054 Configuration oldConfig = r.configuration;
3055 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003056
3057 // Determine what has changed. May be nothing, if this is a config
3058 // that has come back from the app after going idle. In that case
3059 // we just want to leave the official config object now in the
3060 // activity and do nothing else.
3061 final int changes = oldConfig.diff(newConfig);
3062 if (changes == 0 && !r.forceNewConfig) {
3063 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3064 "Configuration no differences in " + r);
3065 return true;
3066 }
3067
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003068 // If the activity isn't currently running, just leave the new
3069 // configuration and it will pick that up next time it starts.
3070 if (r.app == null || r.app.thread == null) {
3071 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3072 "Configuration doesn't matter not running " + r);
3073 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003074 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 return true;
3076 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003077
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003078 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003079 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3080 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3081 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003082 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003083 + ", newConfig=" + newConfig);
3084 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003085 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003086 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3087 r.configChangeFlags |= changes;
3088 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003089 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003090 if (r.app == null || r.app.thread == null) {
3091 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003092 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003093 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003094 } else if (r.state == ActivityState.PAUSING) {
3095 // A little annoying: we are waiting for this activity to
3096 // finish pausing. Let's not do anything now, but just
3097 // flag that it needs to be restarted when done pausing.
3098 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003099 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003100 r.configDestroy = true;
3101 return true;
3102 } else if (r.state == ActivityState.RESUMED) {
3103 // Try to optimize this case: the configuration is changing
3104 // and we need to restart the top, resumed activity.
3105 // Instead of doing the normal handshaking, just say
3106 // "restart!".
3107 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003108 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003109 relaunchActivityLocked(r, r.configChangeFlags, true);
3110 r.configChangeFlags = 0;
3111 } else {
3112 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003113 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003114 relaunchActivityLocked(r, r.configChangeFlags, false);
3115 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003116 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003117
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003118 // All done... tell the caller we weren't able to keep this
3119 // activity around.
3120 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003121 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003123 // Default case: the activity can handle this new configuration, so
3124 // hand it over. Note that we don't need to give it the new
3125 // configuration, since we always send configuration changes to all
3126 // process when they happen so it can just use whatever configuration
3127 // it last got.
3128 if (r.app != null && r.app.thread != null) {
3129 try {
3130 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003131 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 } catch (RemoteException e) {
3133 // If process died, whatever.
3134 }
3135 }
3136 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003138 return true;
3139 }
3140
3141 private final boolean relaunchActivityLocked(ActivityRecord r,
3142 int changes, boolean andResume) {
3143 List<ResultInfo> results = null;
3144 List<Intent> newIntents = null;
3145 if (andResume) {
3146 results = r.results;
3147 newIntents = r.newIntents;
3148 }
3149 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3150 + " with results=" + results + " newIntents=" + newIntents
3151 + " andResume=" + andResume);
3152 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003153 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003156 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003157
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003159 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3160 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3161 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003162 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003163 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003164 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 // Note: don't need to call pauseIfSleepingLocked() here, because
3166 // the caller will only pass in 'andResume' if this activity is
3167 // currently resumed, which implies we aren't sleeping.
3168 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003169 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003170 }
3171
3172 if (andResume) {
3173 r.results = null;
3174 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003175 r.state = ActivityState.RESUMED;
3176 } else {
3177 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3178 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003179 }
3180
3181 return true;
3182 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003183
3184 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003185 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3186 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3187 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3188 final ActivityRecord r = activities.get(activityNdx);
3189 if (r.appToken == token) {
3190 return true;
3191 }
3192 if (r.fullscreen && !r.finishing) {
3193 return false;
3194 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003195 }
3196 }
3197 return true;
3198 }
3199
3200 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003201 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3202 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3203 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3204 final ActivityRecord r = activities.get(activityNdx);
3205 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003206 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003207 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003208 }
3209 }
3210 }
3211
3212 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3213 boolean didSomething = false;
3214 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003215 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3216 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3217 int numActivities = activities.size();
3218 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3219 ActivityRecord r = activities.get(activityNdx);
3220 final boolean samePackage = r.packageName.equals(name)
3221 || (name == null && r.userId == userId);
3222 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3223 && (samePackage || r.task == lastTask)
3224 && (r.app == null || evenPersistent || !r.app.persistent)) {
3225 if (!doit) {
3226 if (r.finishing) {
3227 // If this activity is just finishing, then it is not
3228 // interesting as far as something to stop.
3229 continue;
3230 }
3231 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003232 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003233 didSomething = true;
3234 Slog.i(TAG, " Force finishing activity " + r);
3235 if (samePackage) {
3236 if (r.app != null) {
3237 r.app.removed = true;
3238 }
3239 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003240 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003241 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003242 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3243 true)) {
3244 // r has been deleted from mActivities, accommodate.
3245 --numActivities;
3246 --activityNdx;
3247 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003248 }
3249 }
3250 }
3251 return didSomething;
3252 }
3253
3254 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003255 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003256 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003257 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3258 --maxNum, --taskNdx) {
3259 final TaskRecord task = mTaskHistory.get(taskNdx);
3260 ActivityRecord r = null;
3261 ActivityRecord top = null;
3262 int numActivities = 0;
3263 int numRunning = 0;
3264 final ArrayList<ActivityRecord> activities = task.mActivities;
3265 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3266 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003267
Craig Mautneraab647e2013-02-28 16:31:36 -08003268 // Initialize state for next task if needed.
3269 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3270 top = r;
3271 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003272 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003273
3274 // Add 'r' into the current task.
3275 numActivities++;
3276 if (r.app != null && r.app.thread != null) {
3277 numRunning++;
3278 }
3279
3280 if (localLOGV) Slog.v(
3281 TAG, r.intent.getComponent().flattenToShortString()
3282 + ": task=" + r.task);
3283 }
3284
3285 RunningTaskInfo ci = new RunningTaskInfo();
3286 ci.id = task.taskId;
3287 ci.baseActivity = r.intent.getComponent();
3288 ci.topActivity = top.intent.getComponent();
3289 if (top.thumbHolder != null) {
3290 ci.description = top.thumbHolder.lastDescription;
3291 }
3292 ci.numActivities = numActivities;
3293 ci.numRunning = numRunning;
3294 //System.out.println(
3295 // "#" + maxNum + ": " + " descr=" + ci.description);
3296 if (receiver != null) {
3297 if (localLOGV) Slog.v(
3298 TAG, "State=" + top.state + "Idle=" + top.idle
3299 + " app=" + top.app
3300 + " thr=" + (top.app != null ? top.app.thread : null));
3301 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3302 if (top.idle && top.app != null && top.app.thread != null) {
3303 topRecord = top;
3304 } else {
3305 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003306 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003307 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003308 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003309 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003310 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003311 }
3312 return topRecord;
3313 }
3314
3315 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003316 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003317 if (DEBUG_SWITCH) Slog.d(
3318 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003319 if (top >= 0) {
3320 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3321 int activityTop = activities.size() - 1;
3322 if (activityTop > 0) {
3323 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3324 "unhandled-back", true);
3325 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003326 }
3327 }
3328
Craig Mautnere79d42682013-04-01 19:01:53 -07003329 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003330 if (!containsApp(app)) {
3331 return;
3332 }
3333 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003334 if (mPausingActivity != null && mPausingActivity.app == app) {
3335 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3336 "App died while pausing: " + mPausingActivity);
3337 mPausingActivity = null;
3338 }
3339 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3340 mLastPausedActivity = null;
3341 }
Craig Mautner37f748d2013-06-24 16:19:55 -07003342 final ActivityRecord top = topRunningActivityLocked(null);
3343 final boolean launchHomeTaskNext =
3344 top != null && top.app == app && top.mLaunchHomeTaskNext;
Craig Mautnere79d42682013-04-01 19:01:53 -07003345
3346 // Remove this application's activities from active lists.
3347 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3348
3349 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003350 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautner37f748d2013-06-24 16:19:55 -07003351 if (stack == null || launchHomeTaskNext) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003352 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003353 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003354 // If there was nothing to resume, and we are not already
3355 // restarting this process, but there is a visible activity that
3356 // is hosted by the process... then make sure all visible
3357 // activities are running, taking care of restarting this
3358 // process.
3359 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003360 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003361 }
3362 }
3363 }
3364 }
3365
Craig Mautnercae015f2013-02-08 14:31:27 -08003366 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003367 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3368 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3369 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3370 final ActivityRecord r = activities.get(activityNdx);
3371 if (r.app == app) {
3372 Slog.w(TAG, " Force finishing activity "
3373 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003374 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003375 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003376 }
3377 }
3378 }
3379
Dianne Hackborn390517b2013-05-30 15:03:32 -07003380 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003381 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003382 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3384 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003385 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3386 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003387 dumpClient, dumpPackage, needSep, header,
3388 " Task " + taskNdx + ": id #" + task.taskId);
3389 if (printed) {
3390 header = null;
3391 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003392 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003393 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003394 }
3395
3396 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3397 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3398
3399 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003400 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3401 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003402 }
3403 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003404 final int top = mTaskHistory.size() - 1;
3405 if (top >= 0) {
3406 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3407 int listTop = list.size() - 1;
3408 if (listTop >= 0) {
3409 activities.add(list.get(listTop));
3410 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003411 }
3412 } else {
3413 ItemMatcher matcher = new ItemMatcher();
3414 matcher.build(name);
3415
Craig Mautneraab647e2013-02-28 16:31:36 -08003416 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3417 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3418 if (matcher.match(r1, r1.intent.getComponent())) {
3419 activities.add(r1);
3420 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003421 }
3422 }
3423 }
3424
3425 return activities;
3426 }
3427
3428 ActivityRecord restartPackage(String packageName) {
3429 ActivityRecord starting = topRunningActivityLocked(null);
3430
3431 // All activities that came from the package must be
3432 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003433 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3434 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3435 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3436 final ActivityRecord a = activities.get(activityNdx);
3437 if (a.info.packageName.equals(packageName)) {
3438 a.forceNewConfig = true;
3439 if (starting != null && a == starting && a.visible) {
3440 a.startFreezingScreenLocked(starting.app,
3441 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3442 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003443 }
3444 }
3445 }
3446
3447 return starting;
3448 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003449
Craig Mautnerde4ef022013-04-07 19:01:33 -07003450 boolean removeTask(TaskRecord task) {
3451 mTaskHistory.remove(task);
3452 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003453 }
3454
Craig Mautnerde4ef022013-04-07 19:01:33 -07003455 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003456 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003457 if (toTop) {
3458 mTaskHistory.add(task);
3459 } else {
3460 mTaskHistory.add(0, task);
3461 }
3462 return task;
3463 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003464
3465 ArrayList<TaskRecord> getAllTasks() {
3466 return new ArrayList<TaskRecord>(mTaskHistory);
3467 }
3468
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003469 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003470 task.stack = this;
3471 if (toTop) {
3472 mTaskHistory.add(task);
3473 } else {
3474 mTaskHistory.add(0, task);
3475 }
3476 }
3477
3478 public int getStackId() {
3479 return mStackId;
3480 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003481
3482 @Override
3483 public String toString() {
3484 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3485 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003486}