blob: e984be229621ba0642bc8c80a5e8121b4543dd7d [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 Mautnerde4ef022013-04-07 19:01:33 -070019import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
20
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070021import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080022import com.android.internal.util.Objects;
Craig Mautnercae015f2013-02-08 14:31:27 -080023import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080024import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080025import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070026import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070027
28import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070029import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070030import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070031import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080032import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080033import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070034import android.app.IApplicationThread;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070035import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080036import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070037import android.content.ComponentName;
38import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070039import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080043import android.content.res.Resources;
44import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080045import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080046import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070048import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import android.os.Handler;
50import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090051import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.os.Message;
53import android.os.PowerManager;
54import android.os.RemoteException;
55import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070056import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070059import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060
Craig Mautnercae015f2013-02-08 14:31:27 -080061import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080062import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import java.lang.ref.WeakReference;
64import java.util.ArrayList;
65import java.util.Iterator;
66import java.util.List;
67
68/**
69 * State and management of a single stack of activities.
70 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070071final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070073 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
75 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
Craig Mautner29219d92013-04-16 20:19:12 -070076 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
78 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
79 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
80 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
81 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070082 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig Mautnera9a3fb12013-04-18 10:01:00 -070083 static final boolean DEBUG_STACK = ActivityManagerService.DEBUG_STACK;
Craig Mautner9658b312013-02-28 10:55:59 -080084
Craig Mautner2420ead2013-04-01 17:13:20 -070085 static final boolean DEBUG_STATES = ActivityStackSupervisor.DEBUG_STATES;
86 static final boolean DEBUG_ADD_REMOVE = ActivityStackSupervisor.DEBUG_ADD_REMOVE;
87 static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE;
88 static final boolean DEBUG_APP = ActivityStackSupervisor.DEBUG_APP;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070089
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070090 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
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 Hackborn4eba96b2011-01-21 13:34:36 -0800105 // How long we can hold the sleep wake lock before giving up.
106 static final int SLEEP_TIMEOUT = 5*1000;
107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108 // How long we can hold the launch wake lock before giving up.
109 static final int LAUNCH_TIMEOUT = 10*1000;
110
111 // How long we wait until giving up on an activity telling us it has
112 // finished destroying itself.
113 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700115 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800116 // disabled.
117 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800118
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700119 // How long between activity launches that we consider safe to not warn
120 // the user about an unexpected activity being launched on top.
121 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700123 // Set to false to disable the preview that is shown while a new activity
124 // is being started.
125 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 enum ActivityState {
128 INITIALIZING,
129 RESUMED,
130 PAUSING,
131 PAUSED,
132 STOPPING,
133 STOPPED,
134 FINISHING,
135 DESTROYING,
136 DESTROYED
137 }
138
139 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700140 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800143
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700144 /**
145 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800146 * running) activities. It contains #TaskRecord objects.
147 */
148 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
149
150 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800151 * Used for validating app tokens with window manager.
152 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800153 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800154
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700155 /**
156 * List of running activities, sorted by recent usage.
157 * The first entry in the list is the least recently used.
158 * It contains HistoryRecord objects.
159 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700160 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161
162 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800163 * List of activities that are in the process of going to sleep.
164 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700165 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800166
167 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168 * Animations that for the current transition have requested not to
169 * be considered for the transition animation.
170 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700171 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700172
173 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174 * Set when the system is going to sleep, until we have
175 * successfully paused the current activity and released our wake lock.
176 * At that point the system is allowed to actually sleep.
177 */
178 final PowerManager.WakeLock mGoingToSleep;
179
180 /**
181 * We don't want to allow the device to go to sleep while in the process
182 * of launching an activity. This is primarily to allow alarm intent
183 * receivers to launch an activity and get that to run before the device
184 * goes back to sleep.
185 */
186 final PowerManager.WakeLock mLaunchingActivity;
187
188 /**
189 * When we are in the process of pausing an activity, before starting the
190 * next one, this variable holds the activity that is currently being paused.
191 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800192 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193
194 /**
195 * This is the last activity that we put into the paused state. This is
196 * used to determine if we need to do an activity transition while sleeping,
197 * when we normally hold the top activity paused.
198 */
199 ActivityRecord mLastPausedActivity = null;
200
201 /**
202 * Current activity that is resumed, or null if there is none.
203 */
204 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800205
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700206 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700207 * This is the last activity that has been started. It is only used to
208 * identify when multiple activities are started at once so that the user
209 * can be warned they may not be in the activity they think they are.
210 */
211 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800212
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700213 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700214 * Set when we know we are going to be calling updateConfiguration()
215 * soon, so want to skip intermediate config checks.
216 */
217 boolean mConfigWillChange;
218
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700219 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800220
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800221 /**
222 * Set when we have taken too long waiting to go to sleep.
223 */
224 boolean mSleepTimeout = false;
225
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700226 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800227 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
228 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
229 */
230 private ActivityRecord mLastScreenshotActivity = null;
231 private Bitmap mLastScreenshotBitmap = null;
232
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800233 int mThumbnailWidth = -1;
234 int mThumbnailHeight = -1;
235
Craig Mautner858d8a62013-04-23 17:08:34 -0700236 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700237
Craig Mautnerc00204b2013-03-05 15:02:14 -0800238 final int mStackId;
239
Craig Mautner27084302013-03-25 08:05:25 -0700240 /** Run all ActivityStacks through this */
241 final ActivityStackSupervisor mStackSupervisor;
242
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800243 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
244 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautnerf3333272013-04-22 10:55:53 -0700245 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
246 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
247 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
248 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
249 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
250 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700251
252 static class ScheduleDestroyArgs {
253 final ProcessRecord mOwner;
254 final boolean mOomAdj;
255 final String mReason;
256 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
257 mOwner = owner;
258 mOomAdj = oomAdj;
259 mReason = reason;
260 }
261 }
262
Zoran Marcetaf958b322012-08-09 20:27:12 +0900263 final Handler mHandler;
264
265 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700266 //public Handler() {
267 // if (localLOGV) Slog.v(TAG, "Handler started!");
268 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900269 public ActivityStackHandler(Looper looper) {
270 super(looper);
271 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700272
Zoran Marcetaf958b322012-08-09 20:27:12 +0900273 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274 public void handleMessage(Message msg) {
275 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800276 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700277 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700278 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700279 Slog.w(TAG, "Sleep timeout! Sleeping now.");
280 mSleepTimeout = true;
281 checkReadyForSleepLocked();
282 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800283 }
284 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700285 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800286 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 // We don't at this point know if the activity is fullscreen,
288 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800289 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700290 synchronized (mService) {
291 if (r.app != null) {
292 mService.logAppTooSlow(r.app, r.pauseTime,
293 "pausing " + r);
294 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700295
Craig Mautnerd2328952013-03-05 12:46:26 -0800296 activityPausedLocked(r != null ? r.appToken : null, true);
297 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700299 case LAUNCH_TICK_MSG: {
300 ActivityRecord r = (ActivityRecord)msg.obj;
301 synchronized (mService) {
302 if (r.continueLaunchTickingLocked()) {
303 mService.logAppTooSlow(r.app, r.launchTickTime,
304 "launching " + r);
305 }
306 }
307 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800309 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700310 // We don't at this point know if the activity is fullscreen,
311 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800312 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800313 synchronized (mService) {
314 activityDestroyedLocked(r != null ? r.appToken : null);
315 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700316 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700317 case LAUNCH_TIMEOUT_MSG: {
318 if (mService.mDidDexOpt) {
319 mService.mDidDexOpt = false;
320 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
321 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
322 return;
323 }
324 synchronized (mService) {
325 if (mLaunchingActivity.isHeld()) {
326 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
327 mLaunchingActivity.release();
328 }
329 }
330 } break;
331 case RESUME_TOP_ACTIVITY_MSG: {
332 synchronized (mService) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700334 }
335 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700336 case STOP_TIMEOUT_MSG: {
337 ActivityRecord r = (ActivityRecord)msg.obj;
338 // We don't at this point know if the activity is fullscreen,
339 // so we need to be conservative and assume it isn't.
340 Slog.w(TAG, "Activity stop timeout for " + r);
341 synchronized (mService) {
342 if (r.isInHistory()) {
343 activityStoppedLocked(r, null, null, null);
344 }
345 }
346 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700347 case DESTROY_ACTIVITIES_MSG: {
348 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
349 synchronized (mService) {
350 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
351 }
352 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700353 }
354 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800355 }
356
Craig Mautner000f0022013-02-26 15:04:29 -0800357 private int numActivities() {
358 int count = 0;
359 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
360 count += mTaskHistory.get(taskNdx).mActivities.size();
361 }
362 return count;
363 }
364
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700365 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900366 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700367 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700368 mWindowManager = service.mWindowManager;
369 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700370 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700371 PowerManager pm =
372 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
373 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700374 mLaunchingActivity =
375 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800377 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700378 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700379 }
Craig Mautner5962b122012-10-05 14:45:52 -0700380
381 private boolean okToShow(ActivityRecord r) {
382 return r.userId == mCurrentUser
383 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
384 }
385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700386 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800387 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
388 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800389 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800390 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
391 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800392 if (!r.finishing && r != notTop && okToShow(r)) {
393 return r;
394 }
395 }
396 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700397 return null;
398 }
399
400 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800401 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
402 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800403 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800404 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
405 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800406 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
407 return r;
408 }
409 }
410 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700411 return null;
412 }
413
414 /**
415 * This is a simplified version of topRunningActivityLocked that provides a number of
416 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800417 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700418 * @param token If non-null, any history records matching this token will be skipped.
419 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800420 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700421 * @return Returns the HistoryRecord of the next activity on the stack.
422 */
423 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
425 TaskRecord task = mTaskHistory.get(taskNdx);
426 if (task.taskId == taskId) {
427 continue;
428 }
429 ArrayList<ActivityRecord> activities = task.mActivities;
430 for (int i = activities.size() - 1; i >= 0; --i) {
431 final ActivityRecord r = activities.get(i);
432 // Note: the taskId check depends on real taskId fields being non-zero
433 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
434 return r;
435 }
436 }
437 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 return null;
439 }
440
Craig Mautner8849a5e2013-04-02 16:41:03 -0700441 final ActivityRecord topActivity() {
442 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700443 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700444 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
445 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
446 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700447 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700448 }
449 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700450 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700451 }
452
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700453 final TaskRecord topTask() {
454 final int size = mTaskHistory.size();
455 if (size > 0) {
456 return mTaskHistory.get(size - 1);
457 }
458 return null;
459 }
460
Craig Mautnerd2328952013-03-05 12:46:26 -0800461 TaskRecord taskForIdLocked(int id) {
462 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
463 final TaskRecord task = mTaskHistory.get(taskNdx);
464 if (task.taskId == id) {
465 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800466 }
467 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700468 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700469 }
470
Craig Mautnerd2328952013-03-05 12:46:26 -0800471 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800472 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800473 if (r != null) {
474 final TaskRecord task = r.task;
475 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
476 if (task.stack != this) Slog.w(TAG,
477 "Illegal state! task does not point to stack it is in.");
478 return r;
479 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800480 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800481 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800482 }
483
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700484 boolean containsApp(ProcessRecord app) {
485 if (app == null) {
486 return false;
487 }
488 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
489 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
490 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
491 final ActivityRecord r = activities.get(activityNdx);
492 if (r.finishing) {
493 continue;
494 }
495 if (r.app == app) {
496 return true;
497 }
498 }
499 }
500 return false;
501 }
502
Craig Mautner2420ead2013-04-01 17:13:20 -0700503 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700504 final boolean hadit = mLRUActivities.remove(r);
505 mLRUActivities.add(r);
506 return hadit;
507 }
508
Craig Mautnerde4ef022013-04-07 19:01:33 -0700509 final boolean isHomeStack() {
510 return mStackId == HOME_STACK_ID;
511 }
512
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700513 /**
514 * Returns the top activity in any existing task matching the given
515 * Intent. Returns null if no such task is found.
516 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700517 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700518 ComponentName cls = intent.getComponent();
519 if (info.targetActivity != null) {
520 cls = new ComponentName(info.packageName, info.targetActivity);
521 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700522 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800523
524 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
525 final TaskRecord task = mTaskHistory.get(taskNdx);
526 final ActivityRecord r = task.getTopActivity();
527 if (r == null || r.finishing || r.userId != userId ||
528 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
529 continue;
530 }
531
532 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
533 // + "/aff=" + r.task.affinity + " to new cls="
534 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
535 if (task.affinity != null) {
536 if (task.affinity.equals(info.taskAffinity)) {
537 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700538 return r;
539 }
Craig Mautner000f0022013-02-26 15:04:29 -0800540 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
541 //Slog.i(TAG, "Found matching class!");
542 //dump();
543 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
544 return r;
545 } else if (task.affinityIntent != null
546 && task.affinityIntent.getComponent().equals(cls)) {
547 //Slog.i(TAG, "Found matching class!");
548 //dump();
549 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
550 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700551 }
552 }
553
554 return null;
555 }
556
557 /**
558 * Returns the first activity (starting from the top of the stack) that
559 * is the same as the given activity. Returns null if no such activity
560 * is found.
561 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700562 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700563 ComponentName cls = intent.getComponent();
564 if (info.targetActivity != null) {
565 cls = new ComponentName(info.packageName, info.targetActivity);
566 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700567 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700568
Craig Mautner000f0022013-02-26 15:04:29 -0800569 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
570 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
571 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
572 ActivityRecord r = activities.get(activityNdx);
573 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700574 //Slog.i(TAG, "Found matching class!");
575 //dump();
576 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
577 return r;
578 }
579 }
580 }
581
582 return null;
583 }
584
Amith Yamasani742a6712011-05-04 14:49:28 -0700585 /*
Craig Mautner858d8a62013-04-23 17:08:34 -0700586 * Move the activities around in the stack to bring a user to the foreground. This only
587 * matters on the home stack. All other stacks are single user.
Amith Yamasani742a6712011-05-04 14:49:28 -0700588 * @return whether there are any activities for the specified user.
589 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700590 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800591 if (VALIDATE_TOKENS) {
592 validateAppTokensLocked();
593 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800594 if (mCurrentUser == userId) {
595 return true;
596 }
597 mCurrentUser = userId;
598
599 // Move userId's tasks to the top.
600 boolean haveActivities = false;
601 TaskRecord task = null;
602 int index = mTaskHistory.size();
603 for (int i = 0; i < index; ++i) {
604 task = mTaskHistory.get(i);
605 if (task.userId == userId) {
606 haveActivities = true;
607 mTaskHistory.remove(i);
608 mTaskHistory.add(task);
609 --index;
610 }
611 }
612
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700613 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700614 }
615
Craig Mautner2420ead2013-04-01 17:13:20 -0700616 void minimalResumeActivityLocked(ActivityRecord r) {
617 r.state = ActivityState.RESUMED;
618 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
619 + " (starting new instance)");
620 r.stopped = false;
621 mResumedActivity = r;
622 r.task.touchActiveTime();
623 mService.addRecentTaskLocked(r.task);
624 completeResumeLocked(r);
625 checkReadyForSleepLocked();
626 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700627 }
628
Craig Mautnere79d42682013-04-01 19:01:53 -0700629 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700630 if (r.launchTime == 0) {
631 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700632 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700633 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700634 }
635 } else if (mInitialStartTime == 0) {
636 mInitialStartTime = SystemClock.uptimeMillis();
637 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700638 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800639
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800640 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700641 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700642 if (!mGoingToSleep.isHeld()) {
643 mGoingToSleep.acquire();
644 if (mLaunchingActivity.isHeld()) {
645 mLaunchingActivity.release();
646 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
647 }
648 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800649 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
650 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
651 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
652 checkReadyForSleepLocked();
653 }
654 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700655
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800656 void awakeFromSleepingLocked() {
657 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
658 mSleepTimeout = false;
659 if (mGoingToSleep.isHeld()) {
660 mGoingToSleep.release();
661 }
662 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800663 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
664 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
665 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700666 // TODO: Skip if finishing?
Craig Mautnerd44711d2013-02-23 11:24:36 -0800667 activities.get(activityNdx).setSleeping(false);
668 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800669 }
670 mGoingToSleepActivities.clear();
671 }
672
673 void activitySleptLocked(ActivityRecord r) {
674 mGoingToSleepActivities.remove(r);
675 checkReadyForSleepLocked();
676 }
677
678 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700679 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800680 // Do not care.
681 return;
682 }
683
684 if (!mSleepTimeout) {
685 if (mResumedActivity != null) {
686 // Still have something resumed; can't sleep until it is paused.
687 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700688 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
689 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800690 return;
691 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800692 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800693 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800694 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800695 return;
696 }
697
Craig Mautnerde4ef022013-04-07 19:01:33 -0700698 if (mStackSupervisor.mStoppingActivities.size() > 0) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800699 // Still need to tell some activities to stop; can't sleep yet.
700 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 + mStackSupervisor.mStoppingActivities.size() + " activities");
Craig Mautnerf3333272013-04-22 10:55:53 -0700702 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800703 return;
704 }
705
706 ensureActivitiesVisibleLocked(null, 0);
707
708 // Make sure any stopped but visible activities are now sleeping.
709 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800710 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
711 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
712 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
713 final ActivityRecord r = activities.get(activityNdx);
714 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
715 r.setSleeping(true);
716 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800717 }
718 }
719
720 if (mGoingToSleepActivities.size() > 0) {
721 // Still need to tell some activities to sleep; can't sleep yet.
722 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
723 + mGoingToSleepActivities.size() + " activities");
724 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 }
726 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800727
728 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
729
730 if (mGoingToSleep.isHeld()) {
731 mGoingToSleep.release();
732 }
733 if (mService.mShuttingDown) {
734 mService.notifyAll();
735 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700736 }
Craig Mautner59c00972012-07-30 12:10:24 -0700737
Dianne Hackbornd2835932010-12-13 16:28:46 -0800738 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800739 if (who.noDisplay) {
740 return null;
741 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800742
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800743 Resources res = mService.mContext.getResources();
744 int w = mThumbnailWidth;
745 int h = mThumbnailHeight;
746 if (w < 0) {
747 mThumbnailWidth = w =
748 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
749 mThumbnailHeight = h =
750 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
751 }
752
753 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800754 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
755 || mLastScreenshotBitmap.getWidth() != w
756 || mLastScreenshotBitmap.getHeight() != h) {
757 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700758 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800759 who.appToken, Display.DEFAULT_DISPLAY, w, h);
760 }
761 if (mLastScreenshotBitmap != null) {
762 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
763 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800764 }
765 return null;
766 }
767
Craig Mautnercf910b02013-04-23 11:23:27 -0700768 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800769 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800770 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700771 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800772 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700773 ActivityRecord prev = mResumedActivity;
774 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700775 Slog.e(TAG, "Trying to pause when nothing is resumed",
776 new RuntimeException("here").fillInStackTrace());
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700777 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700778 return;
779 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700780 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
781 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800783 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700784 mLastPausedActivity = prev;
785 prev.state = ActivityState.PAUSING;
786 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700787 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700788
789 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800790
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700791 if (prev.app != null && prev.app.thread != null) {
792 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
793 try {
794 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700795 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700796 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700797 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800798 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
799 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800 } catch (Exception e) {
801 // Ignore exception, if process died other code will cleanup.
802 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800803 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700804 mLastPausedActivity = null;
805 }
806 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800807 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 mLastPausedActivity = null;
809 }
810
811 // If we are not going to sleep, we want to ensure the device is
812 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700813 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 mLaunchingActivity.acquire();
815 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
816 // To be safe, don't allow the wake lock to be held for too long.
817 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
818 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
819 }
820 }
821
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800822 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 // Have the window manager pause its key dispatching until the new
824 // activity has started. If we're pausing the activity just because
825 // the screen is being turned off and the UI is sleeping, don't interrupt
826 // key dispatch; the same activity will pick it up again on wakeup.
827 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800828 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829 } else {
830 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
831 }
832
833 // Schedule a pause timeout in case the app doesn't respond.
834 // We don't give it much time because this directly impacts the
835 // responsiveness seen by the user.
836 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
837 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700838 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
840 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
841 } else {
842 // This activity failed to schedule the
843 // pause, so just treat it as being paused now.
844 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700845 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846 }
847 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700848
Craig Mautnerd2328952013-03-05 12:46:26 -0800849 final void activityResumedLocked(IBinder token) {
850 final ActivityRecord r = ActivityRecord.forToken(token);
851 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
852 r.icicle = null;
853 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700854 }
855
Craig Mautnerd2328952013-03-05 12:46:26 -0800856 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800858 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700859
Craig Mautnerd2328952013-03-05 12:46:26 -0800860 final ActivityRecord r = isInStackLocked(token);
861 if (r != null) {
862 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
863 if (mPausingActivity == r) {
864 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
865 + (timeout ? " (due to timeout)" : " (pause complete)"));
866 r.state = ActivityState.PAUSED;
867 completePauseLocked();
868 } else {
869 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
870 r.userId, System.identityHashCode(r), r.shortComponentName,
871 mPausingActivity != null
872 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700873 }
874 }
875 }
876
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700877 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
878 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700879 if (r.state != ActivityState.STOPPING) {
880 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
881 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
882 return;
883 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700884 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700885 if (icicle != null) {
886 // If icicle is null, this is happening due to a timeout, so we
887 // haven't really saved the state.
888 r.icicle = icicle;
889 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800890 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700891 r.updateThumbnail(thumbnail, description);
892 }
893 if (!r.stopped) {
894 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
895 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
896 r.stopped = true;
897 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700898 if (r.finishing) {
899 r.clearOptionsLocked();
900 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700901 if (r.configDestroy) {
902 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700903 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700904 } else {
905 // Now that this process has stopped, we may want to consider
906 // it to be the previous app to try to keep around in case
907 // the user wants to return to it.
908 ProcessRecord fgApp = null;
909 if (mResumedActivity != null) {
910 fgApp = mResumedActivity.app;
911 } else if (mPausingActivity != null) {
912 fgApp = mPausingActivity.app;
913 }
914 if (r.app != null && fgApp != null && r.app != fgApp
915 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
916 && r.app != mService.mHomeProcess) {
917 mService.mPreviousProcess = r.app;
918 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
919 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800920 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700921 }
922 }
923 }
924
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800925 private final void completePauseLocked() {
926 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800928
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800929 if (prev != null) {
930 if (prev.finishing) {
931 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700932 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800933 } else if (prev.app != null) {
934 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
935 if (prev.waitingVisible) {
936 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700937 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800938 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
939 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800940 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800941 if (prev.configDestroy) {
942 // The previous is being paused because the configuration
943 // is changing, which means it is actually stopping...
944 // To juggle the fact that we are also starting a new
945 // instance right now, we need to first completely stop
946 // the current instance before starting the new one.
947 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
948 destroyActivityLocked(prev, true, false, "pause-config");
949 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700950 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700951 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
952 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800953 // If we already have a few activities waiting to stop,
954 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700955 // them out. Or if r is the last of activity of the last task the stack
956 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800957 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700958 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 } else {
960 checkReadyForSleepLocked();
961 }
962 }
963 } else {
964 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
965 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800966 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800967 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800969
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700970 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700971 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700972 topStack.resumeTopActivityLocked(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800973 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800974 checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700975 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700976 if (top == null || (prev != null && top != prev)) {
977 // If there are no more activities available to run,
978 // do resume anyway to start something. Also if the top
979 // activity on the stack is not the just paused activity,
980 // we need to go ahead and resume it to ensure we complete
981 // an in-flight app switch.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700982 topStack.resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700983 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700984 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800985
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800986 if (prev != null) {
987 prev.resumeKeyDispatchingLocked();
988 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989
990 if (prev.app != null && prev.cpuTimeAtResume > 0
991 && mService.mBatteryStatsService.isOnBattery()) {
992 long diff = 0;
993 synchronized (mService.mProcessStatsThread) {
994 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
995 - prev.cpuTimeAtResume;
996 }
997 if (diff > 0) {
998 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
999 synchronized (bsi) {
1000 BatteryStatsImpl.Uid.Proc ps =
1001 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1002 prev.info.packageName);
1003 if (ps != null) {
1004 ps.addForegroundTimeLocked(diff);
1005 }
1006 }
1007 }
1008 }
1009 prev.cpuTimeAtResume = 0; // reset it
1010 }
1011
1012 /**
1013 * Once we know that we have asked an application to put an activity in
1014 * the resumed state (either by launching it or explicitly telling it),
1015 * this function updates the rest of our state to match that fact.
1016 */
1017 private final void completeResumeLocked(ActivityRecord next) {
1018 next.idle = false;
1019 next.results = null;
1020 next.newIntents = null;
1021
1022 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001023 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001025 mStackSupervisor.reportResumedActivityLocked(next);
1026
1027 next.resumeKeyDispatchingLocked();
1028 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029
1030 // Mark the point when the activity is resuming
1031 // TODO: To be more accurate, the mark should be before the onCreate,
1032 // not after the onResume. But for subsequent starts, onResume is fine.
1033 if (next.app != null) {
1034 synchronized (mService.mProcessStatsThread) {
1035 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1036 }
1037 } else {
1038 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1039 }
1040 }
1041
Craig Mautner580ea812013-04-25 12:58:38 -07001042 /**
1043 * Version of ensureActivitiesVisible that can easily be called anywhere.
1044 */
1045 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1046 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1047 }
1048
1049 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1050 boolean forceHomeShown) {
1051 ActivityRecord r = topRunningActivityLocked(null);
1052 if (r != null) {
1053 return ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
1054 }
1055 return false;
1056 }
1057
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001058 /**
1059 * Make sure that all activities that need to be visible (that is, they
1060 * currently can be seen by the user) actually are.
1061 */
Craig Mautner580ea812013-04-25 12:58:38 -07001062 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1063 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064 if (DEBUG_VISBILITY) Slog.v(
1065 TAG, "ensureActivitiesVisible behind " + top
1066 + " configChanges=0x" + Integer.toHexString(configChanges));
1067
1068 // If the top activity is not fullscreen, then we need to
1069 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001070 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001071 boolean showHomeBehindStack = false;
1072 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1073 !(forceHomeShown && isHomeStack());
Craig Mautnerde4ef022013-04-07 19:01:33 -07001074 int taskNdx;
1075 for (taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001076 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1077 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1078 final ActivityRecord r = activities.get(activityNdx);
1079 if (r.finishing) {
1080 continue;
1081 }
1082 if (aboveTop && r != top) {
1083 continue;
1084 }
1085 aboveTop = false;
1086 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001088 TAG, "Make visible? " + r + " finishing=" + r.finishing
1089 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001090
Craig Mautnerd44711d2013-02-23 11:24:36 -08001091 final boolean doThisProcess = onlyThisProcess == null
1092 || onlyThisProcess.equals(r.processName);
1093
1094 // First: if this is not the current activity being started, make
1095 // sure it matches the current configuration.
1096 if (r != starting && doThisProcess) {
1097 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001098 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001099
1100 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001101 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001102 // This activity needs to be visible, but isn't even
1103 // running... get it started, but don't resume it
1104 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001105 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001106 if (r != starting) {
1107 r.startFreezingScreenLocked(r.app, configChanges);
1108 }
1109 if (!r.visible) {
1110 if (DEBUG_VISBILITY) Slog.v(
1111 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001112 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001113 }
1114 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001115 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001116 }
1117 }
1118
1119 } else if (r.visible) {
1120 // If this activity is already visible, then there is nothing
1121 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001122 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001124
1125 } else if (onlyThisProcess == null) {
1126 // This activity is not currently visible, but is running.
1127 // Tell it to become visible.
1128 r.visible = true;
1129 if (r.state != ActivityState.RESUMED && r != starting) {
1130 // If this activity is paused, tell it
1131 // to now show its window.
1132 if (DEBUG_VISBILITY) Slog.v(
1133 TAG, "Making visible and scheduling visibility: " + r);
1134 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001135 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001136 r.sleeping = false;
1137 r.app.pendingUiClean = true;
1138 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1139 r.stopFreezingScreenLocked(false);
1140 } catch (Exception e) {
1141 // Just skip on any failure; we'll make it
1142 // visible when it next restarts.
1143 Slog.w(TAG, "Exception thrown making visibile: "
1144 + r.intent.getComponent(), e);
1145 }
1146 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001147 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001148
Craig Mautnerd44711d2013-02-23 11:24:36 -08001149 // Aggregate current change flags.
1150 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001151
Craig Mautnerd44711d2013-02-23 11:24:36 -08001152 if (r.fullscreen) {
1153 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001154 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1155 behindFullscreen = true;
1156 } else if (r.mLaunchHomeTaskNext) {
1157 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1158 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001159 behindFullscreen = true;
1160 }
1161 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001162 if (DEBUG_VISBILITY) Slog.v(
1163 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1164 + " state=" + r.state
1165 + " behindFullscreen=" + behindFullscreen);
1166 // Now for any activities that aren't visible to the user, make
1167 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001168 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001169 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170 r.visible = false;
1171 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001172 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 if ((r.state == ActivityState.STOPPING
1174 || r.state == ActivityState.STOPPED)
1175 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001176 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001177 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001178 }
1179 } catch (Exception e) {
1180 // Just skip on any failure; we'll make it
1181 // visible when it next restarts.
1182 Slog.w(TAG, "Exception thrown making hidden: "
1183 + r.intent.getComponent(), e);
1184 }
1185 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001186 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001188 }
1189 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001190 }
Craig Mautner580ea812013-04-25 12:58:38 -07001191 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001192 }
Craig Mautner58547802013-03-05 08:23:53 -08001193
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 /**
1195 * Ensure that the top activity in the stack is resumed.
1196 *
1197 * @param prev The previously resumed activity, for when in the process
1198 * of pausing; can be null to call from elsewhere.
1199 *
1200 * @return Returns true if something is being resumed, or false if
1201 * nothing happened.
1202 */
1203 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001204 return resumeTopActivityLocked(prev, null);
1205 }
1206
1207 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208 // Find the first activity that is not finishing.
1209 ActivityRecord next = topRunningActivityLocked(null);
1210
1211 // Remember how we'll process this pause/resume situation, and ensure
1212 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001213 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1214 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215
1216 if (next == null) {
1217 // There are no more activities! Let's just start up the
1218 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001219 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001220 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001221 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222 }
1223
1224 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001225
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001226 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001227 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1228 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001229 // Make sure we have executed any pending transitions, since there
1230 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001231 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001232 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001233 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001234 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001235 return false;
1236 }
1237
Craig Mautnere418ecd2013-05-01 17:02:29 -07001238 if (prev != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001239 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere418ecd2013-05-01 17:02:29 -07001240 final TaskRecord task = prev.task;
1241 if (topTask() != task) {
1242 // This task is going away but it was supposed to return to the home task.
1243 // Now the task above it has to return to the home task instead.
1244 final int taskNdx = mTaskHistory.indexOf(task) + 1;
1245 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1246 } else {
1247 return mStackSupervisor.resumeHomeActivity(prev);
1248 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001249 }
1250
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001251 // If we are sleeping, and there is no resumed activity, and the top
1252 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001253 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001254 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001255 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256 // Make sure we have executed any pending transitions, since there
1257 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001258 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001260 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001261 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001262 return false;
1263 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001264
1265 // Make sure that the user who owns this activity is started. If not,
1266 // we will just leave it as is because someone should be bringing
1267 // another user's activities to the top of the stack.
1268 if (mService.mStartedUsers.get(next.userId) == null) {
1269 Slog.w(TAG, "Skipping resume of top activity " + next
1270 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001271 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001272 return false;
1273 }
1274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001275 // The activity may be waiting for stop, but that is no longer
1276 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001277 mStackSupervisor.mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001278 mGoingToSleepActivities.remove(next);
1279 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001280 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001281
Dianne Hackborn84375872012-06-01 19:03:50 -07001282 next.updateOptionsLocked(options);
1283
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001284 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1285
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001286 // If we are currently pausing an activity, then don't do anything
1287 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001288 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1289 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Craig Mautnercf910b02013-04-23 11:23:27 -07001290 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001291 return false;
1292 }
1293
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001294 // Okay we are now going to start a switch, to 'next'. We may first
1295 // have to pause the current activity, but this is an important point
1296 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001297 // XXX "App Redirected" dialog is getting too many false positives
1298 // at this point, so turn off for now.
1299 if (false) {
1300 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1301 long now = SystemClock.uptimeMillis();
1302 final boolean inTime = mLastStartedActivity.startTime != 0
1303 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1304 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1305 final int nextUid = next.info.applicationInfo.uid;
1306 if (inTime && lastUid != nextUid
1307 && lastUid != next.launchedFromUid
1308 && mService.checkPermission(
1309 android.Manifest.permission.STOP_APP_SWITCHES,
1310 -1, next.launchedFromUid)
1311 != PackageManager.PERMISSION_GRANTED) {
1312 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1313 } else {
1314 next.startTime = now;
1315 mLastStartedActivity = next;
1316 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001317 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001318 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001319 mLastStartedActivity = next;
1320 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001321 }
Craig Mautner58547802013-03-05 08:23:53 -08001322
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001323 // We need to start pausing the current activity so the top one
1324 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001325 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1326 if (mResumedActivity != null) {
1327 pausing = true;
1328 startPausingLocked(userLeaving, false);
1329 }
1330 if (pausing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001331 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001332 // At this point we want to put the upcoming activity's process
1333 // at the top of the LRU list, since we know we will be needing it
1334 // very soon and it would be a waste to let it get killed if it
1335 // happens to be sitting towards the end.
1336 if (next.app != null && next.app.thread != null) {
1337 // No reason to do full oom adj update here; we'll let that
1338 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001339 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001340 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001341 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001342 return true;
1343 }
1344
Christopher Tated3f175c2012-06-14 14:16:54 -07001345 // If the most recent activity was noHistory but was only stopped rather
1346 // than stopped+finished because the device went to sleep, we need to make
1347 // sure to finish it as we're making a new activity topmost.
1348 final ActivityRecord last = mLastPausedActivity;
1349 if (mService.mSleeping && last != null && !last.finishing) {
1350 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1351 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1352 if (DEBUG_STATES) {
1353 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1354 }
1355 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001356 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001357 }
1358 }
1359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360 if (prev != null && prev != next) {
1361 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1362 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001363 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 if (DEBUG_SWITCH) Slog.v(
1365 TAG, "Resuming top, waiting visible to hide: " + prev);
1366 } else {
1367 // The next activity is already visible, so hide the previous
1368 // activity's windows right now so we can show the new one ASAP.
1369 // We only do this if the previous is finishing, which should mean
1370 // it is on top of the one being resumed so hiding it quickly
1371 // is good. Otherwise, we want to do the normal route of allowing
1372 // the resumed activity to be shown so we can decide if the
1373 // previous should actually be hidden depending on whether the
1374 // new one is found to be full-screen or not.
1375 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001376 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001377 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1378 + prev + ", waitingVisible="
1379 + (prev != null ? prev.waitingVisible : null)
1380 + ", nowVisible=" + next.nowVisible);
1381 } else {
1382 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1383 + prev + ", waitingVisible="
1384 + (prev != null ? prev.waitingVisible : null)
1385 + ", nowVisible=" + next.nowVisible);
1386 }
1387 }
1388 }
1389
Dianne Hackborne7f97212011-02-24 14:40:20 -08001390 // Launching this app's activity, make sure the app is no longer
1391 // considered stopped.
1392 try {
1393 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001394 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001395 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001396 } catch (IllegalArgumentException e) {
1397 Slog.w(TAG, "Failed trying to unstop package "
1398 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001399 }
1400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 // We are starting up the next activity, so tell the window manager
1402 // that the previous one will be hidden soon. This way it can know
1403 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001404 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001405 if (prev != null) {
1406 if (prev.finishing) {
1407 if (DEBUG_TRANSITION) Slog.v(TAG,
1408 "Prepare close transition: prev=" + prev);
1409 if (mNoAnimActivities.contains(prev)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001410 noAnim = true;
1411 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001412 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001413 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001414 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1415 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001416 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001417 mWindowManager.setAppWillBeHidden(prev.appToken);
1418 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001420 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001422 noAnim = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001423 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001425 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001426 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1427 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001428 }
1429 }
1430 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001431 mWindowManager.setAppWillBeHidden(prev.appToken);
1432 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 }
Craig Mautner967212c2013-04-13 21:10:58 -07001434 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001435 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001436 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001437 noAnim = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001438 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001440 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001441 }
1442 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001443 if (!noAnim) {
1444 next.applyOptionsLocked();
1445 } else {
1446 next.clearOptionsLocked();
1447 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001448
Craig Mautnercf910b02013-04-23 11:23:27 -07001449 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001450 if (next.app != null && next.app.thread != null) {
1451 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1452
1453 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001454 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001455
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001456 // schedule launch ticks to collect information about slow apps.
1457 next.startLaunchTickingLocked();
1458
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001459 ActivityRecord lastResumedActivity =
1460 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 ActivityState lastState = next.state;
1462
1463 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001464
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001465 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 next.state = ActivityState.RESUMED;
1467 mResumedActivity = next;
1468 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001469 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001470 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 updateLRUListLocked(next);
1472
1473 // Have the window manager re-evaluate the orientation of
1474 // the screen based on the new activity order.
1475 boolean updated = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001476 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001477 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001478 mService.mConfiguration,
1479 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1480 if (config != null) {
1481 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001483 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001485
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 if (!updated) {
1487 // The configuration update wasn't able to keep the existing
1488 // instance of the activity, and instead started a new one.
1489 // We should be all done, but let's just make sure our activity
1490 // is still at the top and schedule another run if something
1491 // weird happened.
1492 ActivityRecord nextNext = topRunningActivityLocked(null);
1493 if (DEBUG_SWITCH) Slog.i(TAG,
1494 "Activity config changed during resume: " + next
1495 + ", new next: " + nextNext);
1496 if (nextNext != next) {
1497 // Do over!
1498 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1499 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001500 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001501 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001502 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001503 return true;
1504 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001505 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001506 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 }
Craig Mautner58547802013-03-05 08:23:53 -08001508
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509 try {
1510 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001511 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 if (a != null) {
1513 final int N = a.size();
1514 if (!next.finishing && N > 0) {
1515 if (DEBUG_RESULTS) Slog.v(
1516 TAG, "Delivering results to " + next
1517 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001518 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 }
1520 }
1521
1522 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001523 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 }
1525
1526 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001527 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001529
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001530 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001531 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001532 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001533 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001535
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001536 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001537
1538 } catch (Exception e) {
1539 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001540 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1541 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001543 if (lastStack != null) {
1544 lastStack.mResumedActivity = lastResumedActivity;
1545 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 Slog.i(TAG, "Restarting because process died: " + next);
1547 if (!next.hasBeenLaunched) {
1548 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001549 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1550 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001551 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001552 next.appToken, next.packageName, next.theme,
1553 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
1554 next.nonLocalizedLabel, next.labelRes, next.icon, next.windowFlags,
1555 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001557 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001558 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 return true;
1560 }
1561
1562 // From this point on, if something goes wrong there is no way
1563 // to recover the activity.
1564 try {
1565 next.visible = true;
1566 completeResumeLocked(next);
1567 } catch (Exception e) {
1568 // If any exception gets thrown, toss away this
1569 // activity and try the next one.
1570 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001571 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001572 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001573 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 return true;
1575 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576 next.stopped = false;
1577
1578 } else {
1579 // Whoops, need to restart this activity!
1580 if (!next.hasBeenLaunched) {
1581 next.hasBeenLaunched = true;
1582 } else {
1583 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001584 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001585 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001586 mService.compatibilityInfoForPackageLocked(
1587 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001589 next.labelRes, next.icon, next.windowFlags,
1590 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 }
1592 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1593 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001594 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595 }
1596
Craig Mautnercf910b02013-04-23 11:23:27 -07001597 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001598 return true;
1599 }
1600
Craig Mautner11bf9a52013-02-19 14:08:51 -08001601
Craig Mautner8849a5e2013-04-02 16:41:03 -07001602 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001603 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001604 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001605 TaskRecord rTask = r.task;
1606 final int taskId = rTask.taskId;
1607 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001608 // Last activity in task had been removed or ActivityManagerService is reusing task.
1609 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001610 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001611 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001612 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001613 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001614 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001615 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 if (!newTask) {
1617 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001618 boolean startIt = true;
1619 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1620 task = mTaskHistory.get(taskNdx);
1621 if (task == r.task) {
1622 // Here it is! Now, if this is not yet visible to the
1623 // user, then just add it without starting; it will
1624 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001625 if (!startIt) {
1626 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1627 + task, new RuntimeException("here").fillInStackTrace());
1628 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001629 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001630 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1631 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001632 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001634 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001636 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 return;
1638 }
1639 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001640 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001641 startIt = false;
1642 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001643 }
1644 }
1645
1646 // Place a new activity at top of stack, so it is next to interact
1647 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001648
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649 // If we are not placing the new activity frontmost, we do not want
1650 // to deliver the onUserLeaving callback to the actual frontmost
1651 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001652 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001653 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001654 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1655 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001656 }
Craig Mautner70a86932013-02-28 22:37:44 -08001657
1658 task = r.task;
1659
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001661 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001662 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001663 task.addActivityToTop(r);
1664
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001665 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001666 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001667 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001668 // We want to show the starting preview window if we are
1669 // switching to a new task, or the next activity's process is
1670 // not currently running.
1671 boolean showStartingIcon = newTask;
1672 ProcessRecord proc = r.app;
1673 if (proc == null) {
1674 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1675 }
1676 if (proc == null || proc.thread == null) {
1677 showStartingIcon = true;
1678 }
1679 if (DEBUG_TRANSITION) Slog.v(TAG,
1680 "Prepare open transition: starting " + r);
1681 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001682 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001683 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001685 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001686 ? AppTransition.TRANSIT_TASK_OPEN
1687 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001688 mNoAnimActivities.remove(r);
1689 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001690 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001691 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001692 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001693 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 boolean doShow = true;
1695 if (newTask) {
1696 // Even though this activity is starting fresh, we still need
1697 // to reset it to make sure we apply affinities to move any
1698 // existing activities from other tasks in to it.
1699 // If the caller has requested that the target task be
1700 // reset, then do so.
1701 if ((r.intent.getFlags()
1702 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1703 resetTaskIfNeededLocked(r, r);
1704 doShow = topRunningNonDelayedActivityLocked(null) == r;
1705 }
1706 }
1707 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1708 // Figure out if we are transitioning from another activity that is
1709 // "has the same starting icon" as the next one. This allows the
1710 // window manager to keep the previous window it had previously
1711 // created, if it still had one.
1712 ActivityRecord prev = mResumedActivity;
1713 if (prev != null) {
1714 // We don't want to reuse the previous starting preview if:
1715 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001716 if (prev.task != r.task) {
1717 prev = null;
1718 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001720 else if (prev.nowVisible) {
1721 prev = null;
1722 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001724 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001725 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001726 mService.compatibilityInfoForPackageLocked(
1727 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001728 r.labelRes, r.icon, r.windowFlags,
1729 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 }
1731 } else {
1732 // If this is the first activity, don't do any fancy animations,
1733 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001734 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001735 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001736 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001737 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 }
1739 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001740 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001741 }
1742
1743 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001744 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001745 }
1746 }
1747
Dianne Hackbornbe707852011-11-11 14:32:10 -08001748 final void validateAppTokensLocked() {
1749 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001750 mValidateAppTokens.ensureCapacity(numActivities());
1751 final int numTasks = mTaskHistory.size();
1752 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1753 TaskRecord task = mTaskHistory.get(taskNdx);
1754 final ArrayList<ActivityRecord> activities = task.mActivities;
1755 if (activities.size() == 0) {
1756 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001757 }
Craig Mautner000f0022013-02-26 15:04:29 -08001758 TaskGroup group = new TaskGroup();
1759 group.taskId = task.taskId;
1760 mValidateAppTokens.add(group);
1761 final int numActivities = activities.size();
1762 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1763 final ActivityRecord r = activities.get(activityNdx);
1764 group.tokens.add(r.appToken);
1765 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001766 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001767 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001768 }
1769
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 /**
1771 * Perform a reset of the given task, if needed as part of launching it.
1772 * Returns the new HistoryRecord at the top of the task.
1773 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001774 /**
1775 * Helper method for #resetTaskIfNeededLocked.
1776 * We are inside of the task being reset... we'll either finish this activity, push it out
1777 * for another task, or leave it as-is.
1778 * @param task The task containing the Activity (taskTop) that might be reset.
1779 * @param forceReset
1780 * @return An ActivityOptions that needs to be processed.
1781 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001782 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001783 ActivityOptions topOptions = null;
1784
1785 int replyChainEnd = -1;
1786 boolean canMoveOptions = true;
1787
1788 // We only do this for activities that are not the root of the task (since if we finish
1789 // the root, we may no longer have the task!).
1790 final ArrayList<ActivityRecord> activities = task.mActivities;
1791 final int numActivities = activities.size();
1792 for (int i = numActivities - 1; i > 0; --i ) {
1793 ActivityRecord target = activities.get(i);
1794
1795 final int flags = target.info.flags;
1796 final boolean finishOnTaskLaunch =
1797 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1798 final boolean allowTaskReparenting =
1799 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1800 final boolean clearWhenTaskReset =
1801 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1802
1803 if (!finishOnTaskLaunch
1804 && !clearWhenTaskReset
1805 && target.resultTo != null) {
1806 // If this activity is sending a reply to a previous
1807 // activity, we can't do anything with it now until
1808 // we reach the start of the reply chain.
1809 // XXX note that we are assuming the result is always
1810 // to the previous activity, which is almost always
1811 // the case but we really shouldn't count on.
1812 if (replyChainEnd < 0) {
1813 replyChainEnd = i;
1814 }
1815 } else if (!finishOnTaskLaunch
1816 && !clearWhenTaskReset
1817 && allowTaskReparenting
1818 && target.taskAffinity != null
1819 && !target.taskAffinity.equals(task.affinity)) {
1820 // If this activity has an affinity for another
1821 // task, then we need to move it out of here. We will
1822 // move it as far out of the way as possible, to the
1823 // bottom of the activity stack. This also keeps it
1824 // correctly ordered with any activities we previously
1825 // moved.
1826 TaskRecord bottomTask = mTaskHistory.get(0);
1827 ActivityRecord p = bottomTask.mActivities.get(0);
1828 if (target.taskAffinity != null
1829 && target.taskAffinity.equals(p.task.affinity)) {
1830 // If the activity currently at the bottom has the
1831 // same task affinity as the one we are moving,
1832 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001833 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001834 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1835 + " out to bottom task " + p.task);
1836 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001837 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001838 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001839 target.task.affinityIntent = target.intent;
1840 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1841 + " out to new task " + target.task);
1842 }
1843
1844 final TaskRecord targetTask = target.task;
1845 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001846 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001847
1848 ThumbnailHolder curThumbHolder = target.thumbHolder;
1849 boolean gotOptions = !canMoveOptions;
1850
1851 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1852 for (int srcPos = start; srcPos >= i; --srcPos) {
1853 p = activities.get(srcPos);
1854 if (p.finishing) {
1855 continue;
1856 }
1857
1858 curThumbHolder = p.thumbHolder;
1859 canMoveOptions = false;
1860 if (!gotOptions && topOptions == null) {
1861 topOptions = p.takeOptionsLocked();
1862 if (topOptions != null) {
1863 gotOptions = true;
1864 }
1865 }
1866 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1867 + task + " adding to task=" + targetTask,
1868 new RuntimeException("here").fillInStackTrace());
1869 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1870 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001871 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001872 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001873
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001874 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001875 }
1876
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001877 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001878 if (VALIDATE_TOKENS) {
1879 validateAppTokensLocked();
1880 }
1881
1882 replyChainEnd = -1;
1883 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1884 // If the activity should just be removed -- either
1885 // because it asks for it, or the task should be
1886 // cleared -- then finish it and anything that is
1887 // part of its reply chain.
1888 int end;
1889 if (clearWhenTaskReset) {
1890 // In this case, we want to finish this activity
1891 // and everything above it, so be sneaky and pretend
1892 // like these are all in the reply chain.
1893 end = numActivities - 1;
1894 } else if (replyChainEnd < 0) {
1895 end = i;
1896 } else {
1897 end = replyChainEnd;
1898 }
1899 ActivityRecord p = null;
1900 boolean gotOptions = !canMoveOptions;
1901 for (int srcPos = i; srcPos <= end; srcPos++) {
1902 p = activities.get(srcPos);
1903 if (p.finishing) {
1904 continue;
1905 }
1906 canMoveOptions = false;
1907 if (!gotOptions && topOptions == null) {
1908 topOptions = p.takeOptionsLocked();
1909 if (topOptions != null) {
1910 gotOptions = true;
1911 }
1912 }
Craig Mautner58547802013-03-05 08:23:53 -08001913 if (DEBUG_TASKS) Slog.w(TAG,
1914 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001915 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001916 end--;
1917 srcPos--;
1918 }
1919 }
1920 replyChainEnd = -1;
1921 } else {
1922 // If we were in the middle of a chain, well the
1923 // activity that started it all doesn't want anything
1924 // special, so leave it all as-is.
1925 replyChainEnd = -1;
1926 }
1927 }
1928
1929 return topOptions;
1930 }
1931
1932 /**
1933 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1934 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1935 * @param affinityTask The task we are looking for an affinity to.
1936 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1937 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1938 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1939 */
Craig Mautner77878772013-03-04 19:46:24 -08001940 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
1941 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001942 int replyChainEnd = -1;
1943 final int taskId = task.taskId;
1944 final String taskAffinity = task.affinity;
1945
1946 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1947 final int numActivities = activities.size();
1948 // Do not operate on the root Activity.
1949 for (int i = numActivities - 1; i > 0; --i) {
1950 ActivityRecord target = activities.get(i);
1951
1952 final int flags = target.info.flags;
1953 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1954 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1955
1956 if (target.resultTo != null) {
1957 // If this activity is sending a reply to a previous
1958 // activity, we can't do anything with it now until
1959 // we reach the start of the reply chain.
1960 // XXX note that we are assuming the result is always
1961 // to the previous activity, which is almost always
1962 // the case but we really shouldn't count on.
1963 if (replyChainEnd < 0) {
1964 replyChainEnd = i;
1965 }
1966 } else if (topTaskIsHigher
1967 && allowTaskReparenting
1968 && taskAffinity != null
1969 && taskAffinity.equals(target.taskAffinity)) {
1970 // This activity has an affinity for our task. Either remove it if we are
1971 // clearing or move it over to our task. Note that
1972 // we currently punt on the case where we are resetting a
1973 // task that is not at the top but who has activities above
1974 // with an affinity to it... this is really not a normal
1975 // case, and we will need to later pull that task to the front
1976 // and usually at that point we will do the reset and pick
1977 // up those remaining activities. (This only happens if
1978 // someone starts an activity in a new task from an activity
1979 // in a task that is not currently on top.)
1980 if (forceReset || finishOnTaskLaunch) {
1981 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1982 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1983 for (int srcPos = start; srcPos >= i; --srcPos) {
1984 final ActivityRecord p = activities.get(srcPos);
1985 if (p.finishing) {
1986 continue;
1987 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001988 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001989 }
1990 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001991 if (taskInsertionPoint < 0) {
1992 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001993
Craig Mautner77878772013-03-04 19:46:24 -08001994 }
Craig Mautner77878772013-03-04 19:46:24 -08001995
1996 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1997 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1998 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1999 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002000 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002001 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002002 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002003
Craig Mautnere3a74d52013-02-22 14:14:58 -08002004 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2005 + " to stack at " + task,
2006 new RuntimeException("here").fillInStackTrace());
2007 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2008 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002009 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002010 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002011 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002012 if (VALIDATE_TOKENS) {
2013 validateAppTokensLocked();
2014 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002015
2016 // Now we've moved it in to place... but what if this is
2017 // a singleTop activity and we have put it on top of another
2018 // instance of the same activity? Then we drop the instance
2019 // below so it remains singleTop.
2020 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2021 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2022 boolean found = false;
2023 int targetNdx = taskActivities.indexOf(target);
2024 if (targetNdx > 0) {
2025 ActivityRecord p = taskActivities.get(targetNdx - 1);
2026 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002027 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2028 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002029 }
2030 }
2031 }
2032 }
2033
2034 replyChainEnd = -1;
2035 }
2036 }
Craig Mautner77878772013-03-04 19:46:24 -08002037 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002038 }
2039
Craig Mautner8849a5e2013-04-02 16:41:03 -07002040 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002041 ActivityRecord newActivity) {
2042 boolean forceReset =
2043 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2044 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2045 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2046 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2047 forceReset = true;
2048 }
2049 }
2050
2051 final TaskRecord task = taskTop.task;
2052
2053 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2054 * for remaining tasks. Used for later tasks to reparent to task. */
2055 boolean taskFound = false;
2056
2057 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2058 ActivityOptions topOptions = null;
2059
Craig Mautner77878772013-03-04 19:46:24 -08002060 // Preserve the location for reparenting in the new task.
2061 int reparentInsertionPoint = -1;
2062
Craig Mautnere3a74d52013-02-22 14:14:58 -08002063 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2064 final TaskRecord targetTask = mTaskHistory.get(i);
2065
2066 if (targetTask == task) {
2067 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2068 taskFound = true;
2069 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002070 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2071 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002072 }
2073 }
2074
Craig Mautner70a86932013-02-28 22:37:44 -08002075 int taskNdx = mTaskHistory.indexOf(task);
2076 do {
2077 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2078 } while (taskTop == null && taskNdx >= 0);
2079
Craig Mautnere3a74d52013-02-22 14:14:58 -08002080 if (topOptions != null) {
2081 // If we got some ActivityOptions from an activity on top that
2082 // was removed from the task, propagate them to the new real top.
2083 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002084 taskTop.updateOptionsLocked(topOptions);
2085 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002086 topOptions.abort();
2087 }
2088 }
2089
2090 return taskTop;
2091 }
2092
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002094 * Find the activity in the history stack within the given task. Returns
2095 * the index within the history at which it's found, or < 0 if not found.
2096 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07002097 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002098 final ComponentName realActivity = r.realActivity;
2099 ArrayList<ActivityRecord> activities = task.mActivities;
2100 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2101 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002102 if (candidate.finishing) {
2103 continue;
2104 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002105 if (candidate.realActivity.equals(realActivity)) {
2106 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002107 }
2108 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002109 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 }
2111
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2113 String resultWho, int requestCode, int resultCode, Intent data) {
2114
2115 if (callingUid > 0) {
2116 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002117 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 }
2119
2120 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2121 + " : who=" + resultWho + " req=" + requestCode
2122 + " res=" + resultCode + " data=" + data);
2123 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2124 try {
2125 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2126 list.add(new ResultInfo(resultWho, requestCode,
2127 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002128 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002129 return;
2130 } catch (Exception e) {
2131 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2132 }
2133 }
2134
2135 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2136 }
2137
Craig Mautnerf3333272013-04-22 10:55:53 -07002138 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2140 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2141 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2142 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002143 if (!mService.mSleeping) {
2144 if (DEBUG_STATES) {
2145 Slog.d(TAG, "no-history finish of " + r);
2146 }
2147 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002148 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002149 } else {
2150 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2151 + " on stop because we're just sleeping");
2152 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002154 }
2155
2156 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002157 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002158 if (mService.mFocusedActivity == r) {
2159 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2160 }
2161 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002162 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 try {
2164 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002165 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2166 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 r.state = ActivityState.STOPPING;
2168 if (DEBUG_VISBILITY) Slog.v(
2169 TAG, "Stopping visible=" + r.visible + " for " + r);
2170 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002171 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002173 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002174 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002175 r.setSleeping(true);
2176 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002177 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2178 msg.obj = r;
2179 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 } catch (Exception e) {
2181 // Maybe just ignore exceptions here... if the process
2182 // has crashed, our death notification will clean things
2183 // up.
2184 Slog.w(TAG, "Exception thrown during pause", e);
2185 // Just in case, assume it to be stopped.
2186 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002187 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188 r.state = ActivityState.STOPPED;
2189 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002190 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002191 }
2192 }
2193 }
2194 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002195
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002196 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 Configuration config) {
2198 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2199
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002200 ActivityRecord res = null;
2201
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002202 ArrayList<ActivityRecord> stops = null;
2203 ArrayList<ActivityRecord> finishes = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002204 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002205 int NS = 0;
2206 int NF = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207 IApplicationThread sendThumbnail = null;
2208 boolean booting = false;
2209 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002210 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002211
Craig Mautnere79d42682013-04-01 19:01:53 -07002212 // Get the activity record.
Craig Mautnerf3333272013-04-22 10:55:53 -07002213 res = isInStackLocked(token);
2214 if (res != null) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002215 // No longer need to keep the device awake.
Craig Mautnerf3333272013-04-22 10:55:53 -07002216 if (mResumedActivity == res && mLaunchingActivity.isHeld()) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002217 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2218 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002219 }
2220
Craig Mautnere79d42682013-04-01 19:01:53 -07002221 // If this activity is fullscreen, set up to hide those under it.
Craig Mautnerf3333272013-04-22 10:55:53 -07002222 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + res);
Craig Mautnere79d42682013-04-01 19:01:53 -07002223 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002224 }
2225
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002226 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002227 }
2228
2229 /**
2230 * @return Returns true if the activity is being finished, false if for
2231 * some reason it is being left as-is.
2232 */
2233 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002234 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002235 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002236 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002237 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002238 + ", result=" + resultCode + ", data=" + resultData
2239 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002240 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002241 return false;
2242 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243
Craig Mautnerd44711d2013-02-23 11:24:36 -08002244 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002245 return true;
2246 }
2247
Craig Mautnerd2328952013-03-05 12:46:26 -08002248 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002249 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2250 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2251 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2252 ActivityRecord r = activities.get(activityNdx);
2253 if (r.resultTo == self && r.requestCode == requestCode) {
2254 if ((r.resultWho == null && resultWho == null) ||
2255 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2256 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2257 false);
2258 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002259 }
2260 }
2261 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002262 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002263 }
2264
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002265 final void finishTopRunningActivityLocked(ProcessRecord app) {
2266 ActivityRecord r = topRunningActivityLocked(null);
2267 if (r != null && r.app == app) {
2268 // If the top running activity is from this crashing
2269 // process, then terminate it to avoid getting in a loop.
2270 Slog.w(TAG, " Force finishing activity "
2271 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002272 int taskNdx = mTaskHistory.indexOf(r.task);
2273 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002274 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002275 // Also terminate any activities below it that aren't yet
2276 // stopped, to avoid a situation where one will get
2277 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002278 --activityNdx;
2279 if (activityNdx < 0) {
2280 do {
2281 --taskNdx;
2282 if (taskNdx < 0) {
2283 break;
2284 }
2285 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2286 } while (activityNdx < 0);
2287 }
2288 if (activityNdx >= 0) {
2289 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002290 if (r.state == ActivityState.RESUMED
2291 || r.state == ActivityState.PAUSING
2292 || r.state == ActivityState.PAUSED) {
2293 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
2294 Slog.w(TAG, " Force finishing activity "
2295 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002296 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002297 }
2298 }
2299 }
2300 }
2301 }
2302
Craig Mautnerd2328952013-03-05 12:46:26 -08002303 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002304 ArrayList<ActivityRecord> activities = r.task.mActivities;
2305 for (int index = activities.indexOf(r); index >= 0; --index) {
2306 ActivityRecord cur = activities.get(index);
2307 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002308 break;
2309 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002310 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002311 }
2312 return true;
2313 }
2314
Dianne Hackborn5c607432012-02-28 14:44:19 -08002315 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2316 // send the result
2317 ActivityRecord resultTo = r.resultTo;
2318 if (resultTo != null) {
2319 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2320 + " who=" + r.resultWho + " req=" + r.requestCode
2321 + " res=" + resultCode + " data=" + resultData);
2322 if (r.info.applicationInfo.uid > 0) {
2323 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2324 resultTo.packageName, resultData,
2325 resultTo.getUriPermissionsLocked());
2326 }
2327 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2328 resultData);
2329 r.resultTo = null;
2330 }
2331 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2332
2333 // Make sure this HistoryRecord is not holding on to other resources,
2334 // because clients have remote IPC references to this object so we
2335 // can't assume that will go away and want to avoid circular IPC refs.
2336 r.results = null;
2337 r.pendingResults = null;
2338 r.newIntents = null;
2339 r.icicle = null;
2340 }
2341
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002342 /**
2343 * @return Returns true if this activity has been removed from the history
2344 * list, or false if it is still in the list and will be removed later.
2345 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002346 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2347 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 if (r.finishing) {
2349 Slog.w(TAG, "Duplicate finish request for " + r);
2350 return false;
2351 }
2352
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002353 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002354 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002355 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002356 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002357 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2358 final int index = activities.indexOf(r);
2359 if (index < (activities.size() - 1)) {
2360 ActivityRecord next = activities.get(index+1);
2361 if (r.frontOfTask) {
2362 // The next activity is now the front of the task.
2363 next.frontOfTask = true;
2364 }
2365 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2366 // If the caller asked that this activity (and all above it)
2367 // be cleared when the task is reset, don't lose that information,
2368 // but propagate it up to the next activity.
2369 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002370 }
2371 }
2372
2373 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002374 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002376 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002377 }
2378 }
2379
Dianne Hackborn5c607432012-02-28 14:44:19 -08002380 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002381
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 if (mService.mPendingThumbnails.size() > 0) {
2383 // There are clients waiting to receive thumbnails so, in case
2384 // this is an activity that someone is waiting for, add it
2385 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002386 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 }
2388
Craig Mautnerde4ef022013-04-07 19:01:33 -07002389 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002390 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 if (DEBUG_TRANSITION) Slog.v(TAG,
2392 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002393 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002394 ? AppTransition.TRANSIT_TASK_CLOSE
2395 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002396
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002398 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002399
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002400 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2402 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2403 startPausingLocked(false, false);
2404 }
2405
2406 } else if (r.state != ActivityState.PAUSING) {
2407 // If the activity is PAUSING, we will complete the finish once
2408 // it is done pausing; else we can just directly finish it here.
2409 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002410 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 } else {
2412 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2413 }
2414
2415 return false;
2416 }
2417
Craig Mautnerf3333272013-04-22 10:55:53 -07002418 static final int FINISH_IMMEDIATELY = 0;
2419 static final int FINISH_AFTER_PAUSE = 1;
2420 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421
Craig Mautnerf3333272013-04-22 10:55:53 -07002422 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002423 // First things first: if this activity is currently visible,
2424 // and the resumed activity is not yet visible, then hold off on
2425 // finishing until the resumed one becomes visible.
2426 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002427 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2428 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002429 if (mStackSupervisor.mStoppingActivities.size() > 3
2430 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431 // If we already have a few activities waiting to stop,
2432 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002433 // them out. Or if r is the last of activity of the last task the stack
2434 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002435 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002436 } else {
2437 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 }
2439 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002440 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2441 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002442 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002443 if (oomAdj) {
2444 mService.updateOomAdjLocked();
2445 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002446 return r;
2447 }
2448
2449 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002450 mStackSupervisor.mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002451 mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002452 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002453 if (mResumedActivity == r) {
2454 mResumedActivity = null;
2455 }
2456 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002457 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458 r.state = ActivityState.FINISHING;
2459
2460 if (mode == FINISH_IMMEDIATELY
2461 || prevState == ActivityState.STOPPED
2462 || prevState == ActivityState.INITIALIZING) {
2463 // If this activity is already stopped, we can just finish
2464 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002465 boolean activityRemoved = destroyActivityLocked(r, true,
2466 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002467 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002468 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002469 }
2470 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002471 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002472
2473 // Need to go through the full pause cycle to get this
2474 // activity into the stopped state and then finish it.
2475 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002476 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002477 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002478 return r;
2479 }
2480
Craig Mautnerd2328952013-03-05 12:46:26 -08002481 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002482 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002483 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002484 final TaskRecord task = srec.task;
2485 final ArrayList<ActivityRecord> activities = task.mActivities;
2486 final int start = activities.indexOf(srec);
2487 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002488 return false;
2489 }
2490 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002491 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002492 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002493 final ComponentName dest = destIntent.getComponent();
2494 if (start > 0 && dest != null) {
2495 for (int i = finishTo; i >= 0; i--) {
2496 ActivityRecord r = activities.get(i);
2497 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002498 r.info.name.equals(dest.getClassName())) {
2499 finishTo = i;
2500 parent = r;
2501 foundParentInTask = true;
2502 break;
2503 }
2504 }
2505 }
2506
2507 IActivityController controller = mService.mController;
2508 if (controller != null) {
2509 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2510 if (next != null) {
2511 // ask watcher if this is allowed
2512 boolean resumeOK = true;
2513 try {
2514 resumeOK = controller.activityResuming(next.packageName);
2515 } catch (RemoteException e) {
2516 mService.mController = null;
2517 }
2518
2519 if (!resumeOK) {
2520 return false;
2521 }
2522 }
2523 }
2524 final long origId = Binder.clearCallingIdentity();
2525 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002526 ActivityRecord r = activities.get(i);
2527 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002528 // Only return the supplied result for the first activity finished
2529 resultCode = Activity.RESULT_CANCELED;
2530 resultData = null;
2531 }
2532
2533 if (parent != null && foundParentInTask) {
2534 final int parentLaunchMode = parent.info.launchMode;
2535 final int destIntentFlags = destIntent.getFlags();
2536 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2537 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2538 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2539 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2540 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2541 } else {
2542 try {
2543 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2544 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002545 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002546 null, aInfo, parent.appToken, null,
2547 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2548 0, null, true, null);
2549 foundParentInTask = res == ActivityManager.START_SUCCESS;
2550 } catch (RemoteException e) {
2551 foundParentInTask = false;
2552 }
2553 requestFinishActivityLocked(parent.appToken, resultCode,
2554 resultData, "navigate-up", true);
2555 }
2556 }
2557 Binder.restoreCallingIdentity(origId);
2558 return foundParentInTask;
2559 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 /**
2561 * Perform the common clean-up of an activity record. This is called both
2562 * as part of destroyActivityLocked() (when destroying the client-side
2563 * representation) and cleaning things up as a result of its hosting
2564 * processing going away, in which case there is no remaining client-side
2565 * state to destroy so only the cleanup here is needed.
2566 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002567 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2568 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 if (mResumedActivity == r) {
2570 mResumedActivity = null;
2571 }
2572 if (mService.mFocusedActivity == r) {
2573 mService.mFocusedActivity = null;
2574 }
2575
2576 r.configDestroy = false;
2577 r.frozenBeforeDestroy = false;
2578
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002579 if (setState) {
2580 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2581 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002582 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002583 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002584 }
2585
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 // Make sure this record is no longer in the pending finishes list.
2587 // This could happen, for example, if we are trimming activities
2588 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002589 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002590 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002591
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002592 // Remove any pending results.
2593 if (r.finishing && r.pendingResults != null) {
2594 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2595 PendingIntentRecord rec = apr.get();
2596 if (rec != null) {
2597 mService.cancelIntentSenderLocked(rec, false);
2598 }
2599 }
2600 r.pendingResults = null;
2601 }
2602
2603 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002604 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 }
2606
2607 if (mService.mPendingThumbnails.size() > 0) {
2608 // There are clients waiting to receive thumbnails so, in case
2609 // this is an activity that someone is waiting for, add it
2610 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002611 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002612 }
2613
2614 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002615 removeTimeoutsForActivityLocked(r);
2616 }
2617
2618 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002619 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002620 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002621 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002622 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002623 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002624 }
2625
Dianne Hackborn5c607432012-02-28 14:44:19 -08002626 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002627 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2628 r.makeFinishing();
2629 if (DEBUG_ADD_REMOVE) {
2630 RuntimeException here = new RuntimeException("here");
2631 here.fillInStackTrace();
2632 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002634 final TaskRecord task = r.task;
2635 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002636 if (DEBUG_STACK) Slog.i(TAG,
2637 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002638 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002639 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002640 r.takeFromHistory();
2641 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002642 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002643 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002644 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002645 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002646 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002647 if (VALIDATE_TOKENS) {
2648 validateAppTokensLocked();
2649 }
2650 cleanUpActivityServicesLocked(r);
2651 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002653
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 /**
2655 * Perform clean-up of service connections in an activity record.
2656 */
2657 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2658 // Throw away any services that have been bound by this activity.
2659 if (r.connections != null) {
2660 Iterator<ConnectionRecord> it = r.connections.iterator();
2661 while (it.hasNext()) {
2662 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002663 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002664 }
2665 r.connections = null;
2666 }
2667 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002668
2669 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2670 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2671 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2672 mHandler.sendMessage(msg);
2673 }
2674
Dianne Hackborn28695e02011-11-02 21:59:51 -07002675 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002676 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002677 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002678 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2679 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2680 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2681 final ActivityRecord r = activities.get(activityNdx);
2682 if (r.finishing) {
2683 continue;
2684 }
2685 if (r.fullscreen) {
2686 lastIsOpaque = true;
2687 }
2688 if (owner != null && r.app != owner) {
2689 continue;
2690 }
2691 if (!lastIsOpaque) {
2692 continue;
2693 }
2694 // We can destroy this one if we have its icicle saved and
2695 // it is not in the process of pausing/stopping/finishing.
2696 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2697 && r.haveState && !r.visible && r.stopped
2698 && r.state != ActivityState.DESTROYING
2699 && r.state != ActivityState.DESTROYED) {
2700 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2701 + " resumed=" + mResumedActivity
2702 + " pausing=" + mPausingActivity);
2703 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2704 activityRemoved = true;
2705 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002706 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002707 }
2708 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002709 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002710 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002711 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002712 }
2713
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002714 /**
2715 * Destroy the current CLIENT SIDE instance of an activity. This may be
2716 * called both when actually finishing an activity, or when performing
2717 * a configuration switch where we destroy the current client-side object
2718 * but then create a new client-side object for this same HistoryRecord.
2719 */
2720 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002721 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002722 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002723 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002724 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2725 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002726 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002727 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728
2729 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002730
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002731 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002732
2733 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002734
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002735 if (hadApp) {
2736 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002737 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002738 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2739 mService.mHeavyWeightProcess = null;
2740 mService.mHandler.sendEmptyMessage(
2741 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2742 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002744 // No longer have activities, so update oom adj.
2745 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002746 }
2747 }
2748
2749 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002750
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002751 try {
2752 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002753 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002754 r.configChangeFlags);
2755 } catch (Exception e) {
2756 // We can just ignore exceptions here... if the process
2757 // has crashed, our death notification will clean things
2758 // up.
2759 //Slog.w(TAG, "Exception thrown during finish", e);
2760 if (r.finishing) {
2761 removeActivityFromHistoryLocked(r);
2762 removedFromHistory = true;
2763 skipDestroy = true;
2764 }
2765 }
2766
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002768
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002769 // If the activity is finishing, we need to wait on removing it
2770 // from the list to give it a chance to do its cleanup. During
2771 // that time it may make calls back with its token so we need to
2772 // be able to find it on the list and so we don't want to remove
2773 // it from the list yet. Otherwise, we can just immediately put
2774 // it in the destroyed state since we are not removing it from the
2775 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002776 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002777 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2778 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779 r.state = ActivityState.DESTROYING;
2780 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
2781 msg.obj = r;
2782 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2783 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002784 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2785 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002786 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002787 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002788 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 }
2790 } else {
2791 // remove this record from the history.
2792 if (r.finishing) {
2793 removeActivityFromHistoryLocked(r);
2794 removedFromHistory = true;
2795 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002796 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2797 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002798 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002799 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002800 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 }
2802 }
2803
2804 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002805
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 if (!mLRUActivities.remove(r) && hadApp) {
2807 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2808 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002809
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 return removedFromHistory;
2811 }
2812
Craig Mautnerd2328952013-03-05 12:46:26 -08002813 final void activityDestroyedLocked(IBinder token) {
2814 final long origId = Binder.clearCallingIdentity();
2815 try {
2816 ActivityRecord r = ActivityRecord.forToken(token);
2817 if (r != null) {
2818 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002820
2821 if (isInStackLocked(token) != null) {
2822 if (r.state == ActivityState.DESTROYING) {
2823 cleanUpActivityLocked(r, true, false);
2824 removeActivityFromHistoryLocked(r);
2825 }
2826 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002827 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Craig Mautnerd2328952013-03-05 12:46:26 -08002828 } finally {
2829 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002830 }
2831 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002832
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002833 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2834 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002836 if (DEBUG_CLEANUP) Slog.v(
2837 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 + " with " + i + " entries");
2839 while (i > 0) {
2840 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002841 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002842 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002844 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002846 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 }
2848 }
2849 }
2850
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002851 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2852 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002853 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2854 "mStoppingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002855 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002856 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002857 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002858 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2859 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002860
2861 boolean hasVisibleActivities = false;
2862
2863 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002864 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002865 if (DEBUG_CLEANUP) Slog.v(
2866 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002867 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2868 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2869 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2870 final ActivityRecord r = activities.get(activityNdx);
2871 --i;
2872 if (DEBUG_CLEANUP) Slog.v(
2873 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2874 if (r.app == app) {
2875 boolean remove;
2876 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2877 // Don't currently have state for the activity, or
2878 // it is finishing -- always remove it.
2879 remove = true;
2880 } else if (r.launchCount > 2 &&
2881 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2882 // We have launched this activity too many times since it was
2883 // able to run, so give up and remove it.
2884 remove = true;
2885 } else {
2886 // The process may be gone, but the activity lives on!
2887 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002888 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002889 if (remove) {
2890 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
2891 RuntimeException here = new RuntimeException("here");
2892 here.fillInStackTrace();
2893 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2894 + ": haveState=" + r.haveState
2895 + " stateNotNeeded=" + r.stateNotNeeded
2896 + " finishing=" + r.finishing
2897 + " state=" + r.state, here);
2898 }
2899 if (!r.finishing) {
2900 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2901 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2902 r.userId, System.identityHashCode(r),
2903 r.task.taskId, r.shortComponentName,
2904 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002905 if (r.state == ActivityState.RESUMED) {
2906 mService.updateUsageStats(r, false);
2907 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002908 }
2909 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002910
Craig Mautner0247fc82013-02-28 14:32:06 -08002911 } else {
2912 // We have the current state for this activity, so
2913 // it can be restarted later when needed.
2914 if (localLOGV) Slog.v(
2915 TAG, "Keeping entry, setting app to null");
2916 if (r.visible) {
2917 hasVisibleActivities = true;
2918 }
2919 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2920 + r);
2921 r.app = null;
2922 r.nowVisible = false;
2923 if (!r.haveState) {
2924 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
2925 "App died, clearing saved state of " + r);
2926 r.icicle = null;
2927 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002928 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002929
Craig Mautnerd2328952013-03-05 12:46:26 -08002930 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002931 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002932 }
2933 }
2934
2935 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002937
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002938 final void updateTransitLocked(int transit, Bundle options) {
2939 if (options != null) {
2940 ActivityRecord r = topRunningActivityLocked(null);
2941 if (r != null && r.state != ActivityState.RESUMED) {
2942 r.updateOptionsLocked(options);
2943 } else {
2944 ActivityOptions.abort(options);
2945 }
2946 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002947 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002948 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002949
Craig Mautnercae015f2013-02-08 14:31:27 -08002950 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002951 final TaskRecord task = taskForIdLocked(taskId);
2952 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002953 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002954 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002955 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002956 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2957 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002958 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002959 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002960 }
2961 moveTaskToFrontLocked(task, null, options);
2962 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002963 }
2964 return false;
2965 }
2966
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002967 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002968 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969
Craig Mautner11bf9a52013-02-19 14:08:51 -08002970 final int numTasks = mTaskHistory.size();
2971 final int index = mTaskHistory.indexOf(tr);
2972 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
2973 // nothing to do!
2974 if (reason != null &&
2975 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2976 ActivityOptions.abort(options);
2977 } else {
2978 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2979 }
2980 return;
2981 }
2982
2983 // Shift all activities with this task up to the top
2984 // of the stack, keeping them in the same internal order.
2985 mTaskHistory.remove(tr);
2986 mTaskHistory.add(tr);
2987
2988 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 if (reason != null &&
2990 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002991 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 ActivityRecord r = topRunningActivityLocked(null);
2993 if (r != null) {
2994 mNoAnimActivities.add(r);
2995 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002996 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002997 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002998 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002999 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003000
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003001 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003003 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Craig Mautner58547802013-03-05 08:23:53 -08003004 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003005
3006 if (VALIDATE_TOKENS) {
3007 validateAppTokensLocked();
3008 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 }
3010
3011 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003012 * Worker method for rearranging history stack. Implements the function of moving all
3013 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003014 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003015 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003016 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3017 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003018 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003019 * @param task The taskId to collect and move to the bottom.
3020 * @return Returns true if the move completed, false if not.
3021 */
3022 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3023 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003024
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003025 // If we have a watcher, preflight the move before committing to it. First check
3026 // for *other* available tasks, but if none are available, then try again allowing the
3027 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003028 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 ActivityRecord next = topRunningActivityLocked(null, task);
3030 if (next == null) {
3031 next = topRunningActivityLocked(null, 0);
3032 }
3033 if (next != null) {
3034 // ask watcher if this is allowed
3035 boolean moveOK = true;
3036 try {
3037 moveOK = mService.mController.activityResuming(next.packageName);
3038 } catch (RemoteException e) {
3039 mService.mController = null;
3040 }
3041 if (!moveOK) {
3042 return false;
3043 }
3044 }
3045 }
3046
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 if (DEBUG_TRANSITION) Slog.v(TAG,
3048 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003049
Craig Mautnerd2328952013-03-05 12:46:26 -08003050 final TaskRecord tr = taskForIdLocked(task);
3051 if (tr == null) {
3052 return false;
3053 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003054 mTaskHistory.remove(tr);
3055 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003056
3057 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003058 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3059 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 ActivityRecord r = topRunningActivityLocked(null);
3061 if (r != null) {
3062 mNoAnimActivities.add(r);
3063 }
3064 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003065 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003066 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003068 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003069
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003071 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003072 }
3073
Craig Mautnerde4ef022013-04-07 19:01:33 -07003074 if (mResumedActivity != null && mResumedActivity.task == tr &&
3075 mResumedActivity.mLaunchHomeTaskNext) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07003076 // TODO: Can we skip the next line and just pass mResumedAct. to resumeHomeAct.()?
Craig Mautnerde4ef022013-04-07 19:01:33 -07003077 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003078 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003079 }
3080
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003081 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 return true;
3083 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003084
Craig Mautner8849a5e2013-04-02 16:41:03 -07003085 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003086 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003087 final Uri data = r.intent.getData();
3088 final String strData = data != null ? data.toSafeString() : null;
3089
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003090 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003091 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003092 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003093 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 }
3095
3096 /**
3097 * Make sure the given activity matches the current configuration. Returns
3098 * false if the activity had to be destroyed. Returns true if the
3099 * configuration is the same, or the activity will remain running as-is
3100 * for whatever reason. Ensures the HistoryRecord is updated with the
3101 * correct configuration and all other bookkeeping is handled.
3102 */
3103 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3104 int globalChanges) {
3105 if (mConfigWillChange) {
3106 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3107 "Skipping config check (will change): " + r);
3108 return true;
3109 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3112 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114 // Short circuit: if the two configurations are the exact same
3115 // object (the common case), then there is nothing to do.
3116 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003117 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3119 "Configuration unchanged in " + r);
3120 return true;
3121 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003123 // We don't worry about activities that are finishing.
3124 if (r.finishing) {
3125 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3126 "Configuration doesn't matter in finishing " + r);
3127 r.stopFreezingScreenLocked(false);
3128 return true;
3129 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003130
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003131 // Okay we now are going to make this activity have the new config.
3132 // But then we need to figure out how it needs to deal with that.
3133 Configuration oldConfig = r.configuration;
3134 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003135
3136 // Determine what has changed. May be nothing, if this is a config
3137 // that has come back from the app after going idle. In that case
3138 // we just want to leave the official config object now in the
3139 // activity and do nothing else.
3140 final int changes = oldConfig.diff(newConfig);
3141 if (changes == 0 && !r.forceNewConfig) {
3142 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3143 "Configuration no differences in " + r);
3144 return true;
3145 }
3146
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003147 // If the activity isn't currently running, just leave the new
3148 // configuration and it will pick that up next time it starts.
3149 if (r.app == null || r.app.thread == null) {
3150 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3151 "Configuration doesn't matter not running " + r);
3152 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003153 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 return true;
3155 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003156
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003157 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003158 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3159 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3160 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003161 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003162 + ", newConfig=" + newConfig);
3163 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003164 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003165 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3166 r.configChangeFlags |= changes;
3167 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003168 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003169 if (r.app == null || r.app.thread == null) {
3170 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003171 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003172 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003173 } else if (r.state == ActivityState.PAUSING) {
3174 // A little annoying: we are waiting for this activity to
3175 // finish pausing. Let's not do anything now, but just
3176 // flag that it needs to be restarted when done pausing.
3177 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003178 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003179 r.configDestroy = true;
3180 return true;
3181 } else if (r.state == ActivityState.RESUMED) {
3182 // Try to optimize this case: the configuration is changing
3183 // and we need to restart the top, resumed activity.
3184 // Instead of doing the normal handshaking, just say
3185 // "restart!".
3186 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003187 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003188 relaunchActivityLocked(r, r.configChangeFlags, true);
3189 r.configChangeFlags = 0;
3190 } else {
3191 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003192 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003193 relaunchActivityLocked(r, r.configChangeFlags, false);
3194 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003196
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003197 // All done... tell the caller we weren't able to keep this
3198 // activity around.
3199 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003201
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003202 // Default case: the activity can handle this new configuration, so
3203 // hand it over. Note that we don't need to give it the new
3204 // configuration, since we always send configuration changes to all
3205 // process when they happen so it can just use whatever configuration
3206 // it last got.
3207 if (r.app != null && r.app.thread != null) {
3208 try {
3209 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003210 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 } catch (RemoteException e) {
3212 // If process died, whatever.
3213 }
3214 }
3215 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003216
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 return true;
3218 }
3219
3220 private final boolean relaunchActivityLocked(ActivityRecord r,
3221 int changes, boolean andResume) {
3222 List<ResultInfo> results = null;
3223 List<Intent> newIntents = null;
3224 if (andResume) {
3225 results = r.results;
3226 newIntents = r.newIntents;
3227 }
3228 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3229 + " with results=" + results + " newIntents=" + newIntents
3230 + " andResume=" + andResume);
3231 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003232 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003235 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003237 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003238 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3239 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3240 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003241 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003242 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003243 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003244 // Note: don't need to call pauseIfSleepingLocked() here, because
3245 // the caller will only pass in 'andResume' if this activity is
3246 // currently resumed, which implies we aren't sleeping.
3247 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003248 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 }
3250
3251 if (andResume) {
3252 r.results = null;
3253 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003254 r.state = ActivityState.RESUMED;
3255 } else {
3256 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3257 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003258 }
3259
3260 return true;
3261 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003262
3263 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003264 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3265 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3266 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3267 final ActivityRecord r = activities.get(activityNdx);
3268 if (r.appToken == token) {
3269 return true;
3270 }
3271 if (r.fullscreen && !r.finishing) {
3272 return false;
3273 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003274 }
3275 }
3276 return true;
3277 }
3278
3279 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003280 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3281 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3282 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3283 final ActivityRecord r = activities.get(activityNdx);
3284 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003285 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003286 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003287 }
3288 }
3289 }
3290
3291 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3292 boolean didSomething = false;
3293 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003294 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3295 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3296 int numActivities = activities.size();
3297 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3298 ActivityRecord r = activities.get(activityNdx);
3299 final boolean samePackage = r.packageName.equals(name)
3300 || (name == null && r.userId == userId);
3301 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3302 && (samePackage || r.task == lastTask)
3303 && (r.app == null || evenPersistent || !r.app.persistent)) {
3304 if (!doit) {
3305 if (r.finishing) {
3306 // If this activity is just finishing, then it is not
3307 // interesting as far as something to stop.
3308 continue;
3309 }
3310 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003311 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003312 didSomething = true;
3313 Slog.i(TAG, " Force finishing activity " + r);
3314 if (samePackage) {
3315 if (r.app != null) {
3316 r.app.removed = true;
3317 }
3318 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003319 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003320 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003321 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3322 true)) {
3323 // r has been deleted from mActivities, accommodate.
3324 --numActivities;
3325 --activityNdx;
3326 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003327 }
3328 }
3329 }
3330 return didSomething;
3331 }
3332
3333 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003334 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003335 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003336 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3337 --maxNum, --taskNdx) {
3338 final TaskRecord task = mTaskHistory.get(taskNdx);
3339 ActivityRecord r = null;
3340 ActivityRecord top = null;
3341 int numActivities = 0;
3342 int numRunning = 0;
3343 final ArrayList<ActivityRecord> activities = task.mActivities;
3344 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3345 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003346
Craig Mautneraab647e2013-02-28 16:31:36 -08003347 // Initialize state for next task if needed.
3348 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3349 top = r;
3350 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003351 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003352
3353 // Add 'r' into the current task.
3354 numActivities++;
3355 if (r.app != null && r.app.thread != null) {
3356 numRunning++;
3357 }
3358
3359 if (localLOGV) Slog.v(
3360 TAG, r.intent.getComponent().flattenToShortString()
3361 + ": task=" + r.task);
3362 }
3363
3364 RunningTaskInfo ci = new RunningTaskInfo();
3365 ci.id = task.taskId;
3366 ci.baseActivity = r.intent.getComponent();
3367 ci.topActivity = top.intent.getComponent();
3368 if (top.thumbHolder != null) {
3369 ci.description = top.thumbHolder.lastDescription;
3370 }
3371 ci.numActivities = numActivities;
3372 ci.numRunning = numRunning;
3373 //System.out.println(
3374 // "#" + maxNum + ": " + " descr=" + ci.description);
3375 if (receiver != null) {
3376 if (localLOGV) Slog.v(
3377 TAG, "State=" + top.state + "Idle=" + top.idle
3378 + " app=" + top.app
3379 + " thr=" + (top.app != null ? top.app.thread : null));
3380 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3381 if (top.idle && top.app != null && top.app.thread != null) {
3382 topRecord = top;
3383 } else {
3384 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003385 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003386 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003387 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003388 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003389 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003390 }
3391 return topRecord;
3392 }
3393
3394 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003395 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003396 if (DEBUG_SWITCH) Slog.d(
3397 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003398 if (top >= 0) {
3399 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3400 int activityTop = activities.size() - 1;
3401 if (activityTop > 0) {
3402 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3403 "unhandled-back", true);
3404 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003405 }
3406 }
3407
Craig Mautnere79d42682013-04-01 19:01:53 -07003408 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003409 if (!containsApp(app)) {
3410 return;
3411 }
3412 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003413 if (mPausingActivity != null && mPausingActivity.app == app) {
3414 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3415 "App died while pausing: " + mPausingActivity);
3416 mPausingActivity = null;
3417 }
3418 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3419 mLastPausedActivity = null;
3420 }
3421
3422 // Remove this application's activities from active lists.
3423 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3424
3425 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003426 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003427 if (stack == null) {
3428 mStackSupervisor.resumeHomeActivity(null);
3429 } else if (!stack.resumeTopActivityLocked(null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003430 // If there was nothing to resume, and we are not already
3431 // restarting this process, but there is a visible activity that
3432 // is hosted by the process... then make sure all visible
3433 // activities are running, taking care of restarting this
3434 // process.
3435 if (hasVisibleActivities) {
3436 ensureActivitiesVisibleLocked(null, 0);
3437 }
3438 }
3439 }
3440 }
3441
Craig Mautnercae015f2013-02-08 14:31:27 -08003442 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003443 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3444 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3445 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3446 final ActivityRecord r = activities.get(activityNdx);
3447 if (r.app == app) {
3448 Slog.w(TAG, " Force finishing activity "
3449 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003450 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003451 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003452 }
3453 }
3454 }
3455
3456 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3457 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003458 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3459 final TaskRecord task = mTaskHistory.get(taskNdx);
3460 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003461 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003462 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3463 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003464 }
3465
3466 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3467 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3468
3469 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003470 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3471 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003472 }
3473 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003474 final int top = mTaskHistory.size() - 1;
3475 if (top >= 0) {
3476 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3477 int listTop = list.size() - 1;
3478 if (listTop >= 0) {
3479 activities.add(list.get(listTop));
3480 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003481 }
3482 } else {
3483 ItemMatcher matcher = new ItemMatcher();
3484 matcher.build(name);
3485
Craig Mautneraab647e2013-02-28 16:31:36 -08003486 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3487 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3488 if (matcher.match(r1, r1.intent.getComponent())) {
3489 activities.add(r1);
3490 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003491 }
3492 }
3493 }
3494
3495 return activities;
3496 }
3497
3498 ActivityRecord restartPackage(String packageName) {
3499 ActivityRecord starting = topRunningActivityLocked(null);
3500
3501 // All activities that came from the package must be
3502 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003503 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3504 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3505 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3506 final ActivityRecord a = activities.get(activityNdx);
3507 if (a.info.packageName.equals(packageName)) {
3508 a.forceNewConfig = true;
3509 if (starting != null && a == starting && a.visible) {
3510 a.startFreezingScreenLocked(starting.app,
3511 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3512 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003513 }
3514 }
3515 }
3516
3517 return starting;
3518 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003519
Craig Mautnerde4ef022013-04-07 19:01:33 -07003520 boolean removeTask(TaskRecord task) {
3521 mTaskHistory.remove(task);
3522 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003523 }
3524
Craig Mautnerde4ef022013-04-07 19:01:33 -07003525 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003526 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003527 if (toTop) {
3528 mTaskHistory.add(task);
3529 } else {
3530 mTaskHistory.add(0, task);
3531 }
3532 return task;
3533 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003534
3535 ArrayList<TaskRecord> getAllTasks() {
3536 return new ArrayList<TaskRecord>(mTaskHistory);
3537 }
3538
3539 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003540 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003541 if (task == null) {
3542 return;
3543 }
3544 task.stack.mTaskHistory.remove(task);
3545 task.stack = this;
3546 if (toTop) {
3547 mTaskHistory.add(task);
3548 } else {
3549 mTaskHistory.add(0, task);
3550 }
3551 }
3552
3553 public int getStackId() {
3554 return mStackId;
3555 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003556
3557 @Override
3558 public String toString() {
3559 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3560 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003561}