blob: e39ea4ac3041a673c632c876d42a4ae046bdb8b6 [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;
Kenny Rootadd58212013-05-07 09:47:34 -070023import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080024import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080025import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080026import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070027import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070028
29import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070030import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070031import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070032import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080033import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080034import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070035import android.app.IApplicationThread;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070036import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080037import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038import android.content.ComponentName;
39import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070043import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080044import android.content.res.Resources;
45import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080046import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080047import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070049import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.os.Handler;
51import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090052import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.os.Message;
54import android.os.PowerManager;
55import android.os.RemoteException;
56import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070057import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070060import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061
Craig Mautnercae015f2013-02-08 14:31:27 -080062import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080063import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import java.lang.ref.WeakReference;
65import java.util.ArrayList;
66import java.util.Iterator;
67import java.util.List;
68
69/**
70 * State and management of a single stack of activities.
71 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070072final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070074 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
76 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
Craig Mautner29219d92013-04-16 20:19:12 -070077 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
79 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
80 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
81 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
82 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070083 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig Mautnera9a3fb12013-04-18 10:01:00 -070084 static final boolean DEBUG_STACK = ActivityManagerService.DEBUG_STACK;
Craig Mautner9658b312013-02-28 10:55:59 -080085
Craig Mautner2420ead2013-04-01 17:13:20 -070086 static final boolean DEBUG_STATES = ActivityStackSupervisor.DEBUG_STATES;
87 static final boolean DEBUG_ADD_REMOVE = ActivityStackSupervisor.DEBUG_ADD_REMOVE;
88 static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE;
89 static final boolean DEBUG_APP = ActivityStackSupervisor.DEBUG_APP;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070090
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070091 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080092
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070093 // Ticks during which we check progress while waiting for an app to launch.
94 static final int LAUNCH_TICK = 500;
95
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096 // How long we wait until giving up on the last activity to pause. This
97 // is short because it directly impacts the responsiveness of starting the
98 // next activity.
99 static final int PAUSE_TIMEOUT = 500;
100
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700101 // How long we wait for the activity to tell us it has stopped before
102 // giving up. This is a good amount of time because we really need this
103 // from the application in order to get its saved state.
104 static final int STOP_TIMEOUT = 10*1000;
105
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800106 // How long we can hold the sleep wake lock before giving up.
107 static final int SLEEP_TIMEOUT = 5*1000;
108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109 // How long we can hold the launch wake lock before giving up.
110 static final int LAUNCH_TIMEOUT = 10*1000;
111
112 // How long we wait until giving up on an activity telling us it has
113 // finished destroying itself.
114 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800117 // disabled.
118 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800119
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700120 // How long between activity launches that we consider safe to not warn
121 // the user about an unexpected activity being launched on top.
122 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124 // Set to false to disable the preview that is shown while a new activity
125 // is being started.
126 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 enum ActivityState {
129 INITIALIZING,
130 RESUMED,
131 PAUSING,
132 PAUSED,
133 STOPPING,
134 STOPPED,
135 FINISHING,
136 DESTROYING,
137 DESTROYED
138 }
139
140 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700141 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700143 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700145 /**
146 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800147 * running) activities. It contains #TaskRecord objects.
148 */
149 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
150
151 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152 * Used for validating app tokens with window manager.
153 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800154 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700156 /**
157 * List of running activities, sorted by recent usage.
158 * The first entry in the list is the least recently used.
159 * It contains HistoryRecord objects.
160 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700161 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162
163 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800164 * List of activities that are in the process of going to sleep.
165 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700166 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * Animations that for the current transition have requested not to
170 * be considered for the transition animation.
171 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 * Set when the system is going to sleep, until we have
176 * successfully paused the current activity and released our wake lock.
177 * At that point the system is allowed to actually sleep.
178 */
179 final PowerManager.WakeLock mGoingToSleep;
180
181 /**
182 * We don't want to allow the device to go to sleep while in the process
183 * of launching an activity. This is primarily to allow alarm intent
184 * receivers to launch an activity and get that to run before the device
185 * goes back to sleep.
186 */
187 final PowerManager.WakeLock mLaunchingActivity;
188
189 /**
190 * When we are in the process of pausing an activity, before starting the
191 * next one, this variable holds the activity that is currently being paused.
192 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800193 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194
195 /**
196 * This is the last activity that we put into the paused state. This is
197 * used to determine if we need to do an activity transition while sleeping,
198 * when we normally hold the top activity paused.
199 */
200 ActivityRecord mLastPausedActivity = null;
201
202 /**
203 * Current activity that is resumed, or null if there is none.
204 */
205 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800206
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700207 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700208 * This is the last activity that has been started. It is only used to
209 * identify when multiple activities are started at once so that the user
210 * can be warned they may not be in the activity they think they are.
211 */
212 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800213
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700214 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700215 * Set when we know we are going to be calling updateConfiguration()
216 * soon, so want to skip intermediate config checks.
217 */
218 boolean mConfigWillChange;
219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700220 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800221
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800222 /**
223 * Set when we have taken too long waiting to go to sleep.
224 */
225 boolean mSleepTimeout = false;
226
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700227 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800228 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
229 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
230 */
231 private ActivityRecord mLastScreenshotActivity = null;
232 private Bitmap mLastScreenshotBitmap = null;
233
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800234 int mThumbnailWidth = -1;
235 int mThumbnailHeight = -1;
236
Craig Mautner858d8a62013-04-23 17:08:34 -0700237 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700238
Craig Mautnerc00204b2013-03-05 15:02:14 -0800239 final int mStackId;
240
Craig Mautner27084302013-03-25 08:05:25 -0700241 /** Run all ActivityStacks through this */
242 final ActivityStackSupervisor mStackSupervisor;
243
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800244 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
245 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautnerf3333272013-04-22 10:55:53 -0700246 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
247 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
Craig Mautner05d29032013-05-03 13:40:13 -0700248 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
249 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
250 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
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 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700295 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700298 case LAUNCH_TICK_MSG: {
299 ActivityRecord r = (ActivityRecord)msg.obj;
300 synchronized (mService) {
301 if (r.continueLaunchTickingLocked()) {
302 mService.logAppTooSlow(r.app, r.launchTickTime,
303 "launching " + r);
304 }
305 }
306 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700307 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800308 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 // We don't at this point know if the activity is fullscreen,
310 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800311 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800312 synchronized (mService) {
313 activityDestroyedLocked(r != null ? r.appToken : null);
314 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700315 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700316 case LAUNCH_TIMEOUT_MSG: {
317 if (mService.mDidDexOpt) {
318 mService.mDidDexOpt = false;
319 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
320 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
321 return;
322 }
323 synchronized (mService) {
324 if (mLaunchingActivity.isHeld()) {
325 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
326 mLaunchingActivity.release();
327 }
328 }
329 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700330 case STOP_TIMEOUT_MSG: {
331 ActivityRecord r = (ActivityRecord)msg.obj;
332 // We don't at this point know if the activity is fullscreen,
333 // so we need to be conservative and assume it isn't.
334 Slog.w(TAG, "Activity stop timeout for " + r);
335 synchronized (mService) {
336 if (r.isInHistory()) {
337 activityStoppedLocked(r, null, null, null);
338 }
339 }
340 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700341 case DESTROY_ACTIVITIES_MSG: {
342 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
343 synchronized (mService) {
344 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
345 }
346 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700347 }
348 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800349 }
350
Craig Mautner000f0022013-02-26 15:04:29 -0800351 private int numActivities() {
352 int count = 0;
353 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
354 count += mTaskHistory.get(taskNdx).mActivities.size();
355 }
356 return count;
357 }
358
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700359 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900360 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700361 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700362 mWindowManager = service.mWindowManager;
363 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700364 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700365 PowerManager pm =
366 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
367 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700368 mLaunchingActivity =
369 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700370 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800371 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700372 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 }
Craig Mautner5962b122012-10-05 14:45:52 -0700374
375 private boolean okToShow(ActivityRecord r) {
376 return r.userId == mCurrentUser
377 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
378 }
379
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
382 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800383 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800384 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
385 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800386 if (!r.finishing && r != notTop && okToShow(r)) {
387 return r;
388 }
389 }
390 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700391 return null;
392 }
393
394 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800395 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
396 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800397 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800398 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
399 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800400 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
401 return r;
402 }
403 }
404 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700405 return null;
406 }
407
408 /**
409 * This is a simplified version of topRunningActivityLocked that provides a number of
410 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800411 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700412 * @param token If non-null, any history records matching this token will be skipped.
413 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800414 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700415 * @return Returns the HistoryRecord of the next activity on the stack.
416 */
417 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 TaskRecord task = mTaskHistory.get(taskNdx);
420 if (task.taskId == taskId) {
421 continue;
422 }
423 ArrayList<ActivityRecord> activities = task.mActivities;
424 for (int i = activities.size() - 1; i >= 0; --i) {
425 final ActivityRecord r = activities.get(i);
426 // Note: the taskId check depends on real taskId fields being non-zero
427 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
428 return r;
429 }
430 }
431 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700432 return null;
433 }
434
Craig Mautner8849a5e2013-04-02 16:41:03 -0700435 final ActivityRecord topActivity() {
436 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700437 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700438 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
439 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
440 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700441 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700442 }
443 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700444 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700445 }
446
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700447 final TaskRecord topTask() {
448 final int size = mTaskHistory.size();
449 if (size > 0) {
450 return mTaskHistory.get(size - 1);
451 }
452 return null;
453 }
454
Craig Mautnerd2328952013-03-05 12:46:26 -0800455 TaskRecord taskForIdLocked(int id) {
456 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
457 final TaskRecord task = mTaskHistory.get(taskNdx);
458 if (task.taskId == id) {
459 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800460 }
461 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700462 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700463 }
464
Craig Mautnerd2328952013-03-05 12:46:26 -0800465 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800466 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800467 if (r != null) {
468 final TaskRecord task = r.task;
469 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
470 if (task.stack != this) Slog.w(TAG,
471 "Illegal state! task does not point to stack it is in.");
472 return r;
473 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800474 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800475 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800476 }
477
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700478 boolean containsApp(ProcessRecord app) {
479 if (app == null) {
480 return false;
481 }
482 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
483 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
484 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
485 final ActivityRecord r = activities.get(activityNdx);
486 if (r.finishing) {
487 continue;
488 }
489 if (r.app == app) {
490 return true;
491 }
492 }
493 }
494 return false;
495 }
496
Craig Mautner2420ead2013-04-01 17:13:20 -0700497 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700498 final boolean hadit = mLRUActivities.remove(r);
499 mLRUActivities.add(r);
500 return hadit;
501 }
502
Craig Mautnerde4ef022013-04-07 19:01:33 -0700503 final boolean isHomeStack() {
504 return mStackId == HOME_STACK_ID;
505 }
506
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700507 /**
508 * Returns the top activity in any existing task matching the given
509 * Intent. Returns null if no such task is found.
510 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700511 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700512 ComponentName cls = intent.getComponent();
513 if (info.targetActivity != null) {
514 cls = new ComponentName(info.packageName, info.targetActivity);
515 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700516 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800517
518 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
519 final TaskRecord task = mTaskHistory.get(taskNdx);
520 final ActivityRecord r = task.getTopActivity();
521 if (r == null || r.finishing || r.userId != userId ||
522 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
523 continue;
524 }
525
526 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
527 // + "/aff=" + r.task.affinity + " to new cls="
528 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
529 if (task.affinity != null) {
530 if (task.affinity.equals(info.taskAffinity)) {
531 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700532 return r;
533 }
Craig Mautner000f0022013-02-26 15:04:29 -0800534 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
535 //Slog.i(TAG, "Found matching class!");
536 //dump();
537 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
538 return r;
539 } else if (task.affinityIntent != null
540 && task.affinityIntent.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;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700545 }
546 }
547
548 return null;
549 }
550
551 /**
552 * Returns the first activity (starting from the top of the stack) that
553 * is the same as the given activity. Returns null if no such activity
554 * is found.
555 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700556 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700557 ComponentName cls = intent.getComponent();
558 if (info.targetActivity != null) {
559 cls = new ComponentName(info.packageName, info.targetActivity);
560 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700561 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700562
Craig Mautner000f0022013-02-26 15:04:29 -0800563 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
564 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
565 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
566 ActivityRecord r = activities.get(activityNdx);
567 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700568 //Slog.i(TAG, "Found matching class!");
569 //dump();
570 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
571 return r;
572 }
573 }
574 }
575
576 return null;
577 }
578
Amith Yamasani742a6712011-05-04 14:49:28 -0700579 /*
Craig Mautner858d8a62013-04-23 17:08:34 -0700580 * Move the activities around in the stack to bring a user to the foreground. This only
581 * matters on the home stack. All other stacks are single user.
Amith Yamasani742a6712011-05-04 14:49:28 -0700582 * @return whether there are any activities for the specified user.
583 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700584 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800585 if (VALIDATE_TOKENS) {
586 validateAppTokensLocked();
587 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800588 if (mCurrentUser == userId) {
589 return true;
590 }
591 mCurrentUser = userId;
592
593 // Move userId's tasks to the top.
594 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800595 int index = mTaskHistory.size();
596 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700597 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800598 if (task.userId == userId) {
599 haveActivities = true;
600 mTaskHistory.remove(i);
601 mTaskHistory.add(task);
602 --index;
603 }
604 }
605
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700606 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700607 }
608
Craig Mautner2420ead2013-04-01 17:13:20 -0700609 void minimalResumeActivityLocked(ActivityRecord r) {
610 r.state = ActivityState.RESUMED;
611 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
612 + " (starting new instance)");
613 r.stopped = false;
614 mResumedActivity = r;
615 r.task.touchActiveTime();
616 mService.addRecentTaskLocked(r.task);
617 completeResumeLocked(r);
618 checkReadyForSleepLocked();
619 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620 }
621
Craig Mautnere79d42682013-04-01 19:01:53 -0700622 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700623 if (r.launchTime == 0) {
624 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700625 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700626 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700627 }
628 } else if (mInitialStartTime == 0) {
629 mInitialStartTime = SystemClock.uptimeMillis();
630 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700631 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800632
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800633 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700634 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700635 if (!mGoingToSleep.isHeld()) {
636 mGoingToSleep.acquire();
637 if (mLaunchingActivity.isHeld()) {
638 mLaunchingActivity.release();
639 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
640 }
641 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800642 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
643 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
644 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
645 checkReadyForSleepLocked();
646 }
647 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700648
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800649 void awakeFromSleepingLocked() {
650 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
651 mSleepTimeout = false;
652 if (mGoingToSleep.isHeld()) {
653 mGoingToSleep.release();
654 }
655 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
657 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
658 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700659 // TODO: Skip if finishing?
Craig Mautnerd44711d2013-02-23 11:24:36 -0800660 activities.get(activityNdx).setSleeping(false);
661 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800662 }
663 mGoingToSleepActivities.clear();
664 }
665
666 void activitySleptLocked(ActivityRecord r) {
667 mGoingToSleepActivities.remove(r);
668 checkReadyForSleepLocked();
669 }
670
671 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700672 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800673 // Do not care.
674 return;
675 }
676
677 if (!mSleepTimeout) {
678 if (mResumedActivity != null) {
679 // Still have something resumed; can't sleep until it is paused.
680 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
682 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800683 return;
684 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800685 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800686 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800687 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800688 return;
689 }
690
Craig Mautnerde4ef022013-04-07 19:01:33 -0700691 if (mStackSupervisor.mStoppingActivities.size() > 0) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800692 // Still need to tell some activities to stop; can't sleep yet.
693 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
Craig Mautnerde4ef022013-04-07 19:01:33 -0700694 + mStackSupervisor.mStoppingActivities.size() + " activities");
Craig Mautnerf3333272013-04-22 10:55:53 -0700695 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800696 return;
697 }
698
699 ensureActivitiesVisibleLocked(null, 0);
700
701 // Make sure any stopped but visible activities are now sleeping.
702 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800703 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
704 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
705 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
706 final ActivityRecord r = activities.get(activityNdx);
707 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
708 r.setSleeping(true);
709 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800710 }
711 }
712
713 if (mGoingToSleepActivities.size() > 0) {
714 // Still need to tell some activities to sleep; can't sleep yet.
715 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
716 + mGoingToSleepActivities.size() + " activities");
717 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700718 }
719 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800720
721 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
722
723 if (mGoingToSleep.isHeld()) {
724 mGoingToSleep.release();
725 }
726 if (mService.mShuttingDown) {
727 mService.notifyAll();
728 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 }
Craig Mautner59c00972012-07-30 12:10:24 -0700730
Dianne Hackbornd2835932010-12-13 16:28:46 -0800731 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800732 if (who.noDisplay) {
733 return null;
734 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800735
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800736 Resources res = mService.mContext.getResources();
737 int w = mThumbnailWidth;
738 int h = mThumbnailHeight;
739 if (w < 0) {
740 mThumbnailWidth = w =
741 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
742 mThumbnailHeight = h =
743 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
744 }
745
746 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800747 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
748 || mLastScreenshotBitmap.getWidth() != w
749 || mLastScreenshotBitmap.getHeight() != h) {
750 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700751 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800752 who.appToken, Display.DEFAULT_DISPLAY, w, h);
753 }
754 if (mLastScreenshotBitmap != null) {
755 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
756 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800757 }
758 return null;
759 }
760
Craig Mautnercf910b02013-04-23 11:23:27 -0700761 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800762 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800763 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700764 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800765 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 ActivityRecord prev = mResumedActivity;
767 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700768 Slog.e(TAG, "Trying to pause when nothing is resumed",
769 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700770 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700771 return;
772 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700773 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
774 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700775 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800776 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700777 mLastPausedActivity = prev;
778 prev.state = ActivityState.PAUSING;
779 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700780 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700781
782 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800783
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700784 if (prev.app != null && prev.app.thread != null) {
785 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
786 try {
787 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700788 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700790 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800791 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
792 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700793 } catch (Exception e) {
794 // Ignore exception, if process died other code will cleanup.
795 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800796 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700797 mLastPausedActivity = null;
798 }
799 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800800 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700801 mLastPausedActivity = null;
802 }
803
804 // If we are not going to sleep, we want to ensure the device is
805 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700806 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 mLaunchingActivity.acquire();
808 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
809 // To be safe, don't allow the wake lock to be held for too long.
810 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
811 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
812 }
813 }
814
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800815 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 // Have the window manager pause its key dispatching until the new
817 // activity has started. If we're pausing the activity just because
818 // the screen is being turned off and the UI is sleeping, don't interrupt
819 // key dispatch; the same activity will pick it up again on wakeup.
820 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800821 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 } else {
823 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
824 }
825
826 // Schedule a pause timeout in case the app doesn't respond.
827 // We don't give it much time because this directly impacts the
828 // responsiveness seen by the user.
829 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
830 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700831 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
833 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
834 } else {
835 // This activity failed to schedule the
836 // pause, so just treat it as being paused now.
837 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700838 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 }
840 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700841
Craig Mautnerd2328952013-03-05 12:46:26 -0800842 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700843 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800844 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845
Craig Mautnerd2328952013-03-05 12:46:26 -0800846 final ActivityRecord r = isInStackLocked(token);
847 if (r != null) {
848 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
849 if (mPausingActivity == r) {
850 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
851 + (timeout ? " (due to timeout)" : " (pause complete)"));
852 r.state = ActivityState.PAUSED;
853 completePauseLocked();
854 } else {
855 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
856 r.userId, System.identityHashCode(r), r.shortComponentName,
857 mPausingActivity != null
858 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700859 }
860 }
861 }
862
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700863 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
864 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700865 if (r.state != ActivityState.STOPPING) {
866 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
867 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
868 return;
869 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700870 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700871 if (icicle != null) {
872 // If icicle is null, this is happening due to a timeout, so we
873 // haven't really saved the state.
874 r.icicle = icicle;
875 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800876 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700877 r.updateThumbnail(thumbnail, description);
878 }
879 if (!r.stopped) {
880 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
881 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
882 r.stopped = true;
883 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700884 if (r.finishing) {
885 r.clearOptionsLocked();
886 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700887 if (r.configDestroy) {
888 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700889 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700890 } else {
891 // Now that this process has stopped, we may want to consider
892 // it to be the previous app to try to keep around in case
893 // the user wants to return to it.
894 ProcessRecord fgApp = null;
895 if (mResumedActivity != null) {
896 fgApp = mResumedActivity.app;
897 } else if (mPausingActivity != null) {
898 fgApp = mPausingActivity.app;
899 }
900 if (r.app != null && fgApp != null && r.app != fgApp
901 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
902 && r.app != mService.mHomeProcess) {
903 mService.mPreviousProcess = r.app;
904 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
905 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800906 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700907 }
908 }
909 }
910
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800911 private final void completePauseLocked() {
912 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800914
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800915 if (prev != null) {
916 if (prev.finishing) {
917 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700918 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800919 } else if (prev.app != null) {
920 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
921 if (prev.waitingVisible) {
922 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700923 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800924 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
925 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800926 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800927 if (prev.configDestroy) {
928 // The previous is being paused because the configuration
929 // is changing, which means it is actually stopping...
930 // To juggle the fact that we are also starting a new
931 // instance right now, we need to first completely stop
932 // the current instance before starting the new one.
933 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
934 destroyActivityLocked(prev, true, false, "pause-config");
935 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700936 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700937 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
938 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800939 // If we already have a few activities waiting to stop,
940 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700941 // them out. Or if r is the last of activity of the last task the stack
942 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800943 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700944 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800945 } else {
946 checkReadyForSleepLocked();
947 }
948 }
949 } else {
950 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
951 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800952 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800953 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700954 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800955
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700956 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700957 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700958 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800960 checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700961 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700962 if (top == null || (prev != null && top != prev)) {
963 // If there are no more activities available to run,
964 // do resume anyway to start something. Also if the top
965 // activity on the stack is not the just paused activity,
966 // we need to go ahead and resume it to ensure we complete
967 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700968 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700969 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700970 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800971
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800972 if (prev != null) {
973 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700974
Craig Mautner525f3d92013-05-07 14:01:50 -0700975 if (prev.app != null && prev.cpuTimeAtResume > 0
976 && mService.mBatteryStatsService.isOnBattery()) {
977 long diff;
978 synchronized (mService.mProcessStatsThread) {
979 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
980 - prev.cpuTimeAtResume;
981 }
982 if (diff > 0) {
983 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
984 synchronized (bsi) {
985 BatteryStatsImpl.Uid.Proc ps =
986 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
987 prev.info.packageName);
988 if (ps != null) {
989 ps.addForegroundTimeLocked(diff);
990 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700991 }
992 }
993 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700994 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700995 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996 }
997
998 /**
999 * Once we know that we have asked an application to put an activity in
1000 * the resumed state (either by launching it or explicitly telling it),
1001 * this function updates the rest of our state to match that fact.
1002 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001003 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001004 next.idle = false;
1005 next.results = null;
1006 next.newIntents = null;
1007
1008 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001009 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001010
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001011 mStackSupervisor.reportResumedActivityLocked(next);
1012
1013 next.resumeKeyDispatchingLocked();
1014 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015
1016 // Mark the point when the activity is resuming
1017 // TODO: To be more accurate, the mark should be before the onCreate,
1018 // not after the onResume. But for subsequent starts, onResume is fine.
1019 if (next.app != null) {
1020 synchronized (mService.mProcessStatsThread) {
1021 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1022 }
1023 } else {
1024 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1025 }
1026 }
1027
Craig Mautner580ea812013-04-25 12:58:38 -07001028 /**
1029 * Version of ensureActivitiesVisible that can easily be called anywhere.
1030 */
1031 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1032 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1033 }
1034
1035 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1036 boolean forceHomeShown) {
1037 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001038 return r != null &&
1039 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001040 }
1041
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001042 /**
1043 * Make sure that all activities that need to be visible (that is, they
1044 * currently can be seen by the user) actually are.
1045 */
Craig Mautner580ea812013-04-25 12:58:38 -07001046 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1047 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001048 if (DEBUG_VISBILITY) Slog.v(
1049 TAG, "ensureActivitiesVisible behind " + top
1050 + " configChanges=0x" + Integer.toHexString(configChanges));
1051
1052 // If the top activity is not fullscreen, then we need to
1053 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001054 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001055 boolean showHomeBehindStack = false;
1056 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1057 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001058 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001059 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1060 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1061 final ActivityRecord r = activities.get(activityNdx);
1062 if (r.finishing) {
1063 continue;
1064 }
1065 if (aboveTop && r != top) {
1066 continue;
1067 }
1068 aboveTop = false;
1069 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001070 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001071 TAG, "Make visible? " + r + " finishing=" + r.finishing
1072 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001073
Craig Mautnerd44711d2013-02-23 11:24:36 -08001074 final boolean doThisProcess = onlyThisProcess == null
1075 || onlyThisProcess.equals(r.processName);
1076
1077 // First: if this is not the current activity being started, make
1078 // sure it matches the current configuration.
1079 if (r != starting && doThisProcess) {
1080 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001081 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001082
1083 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001084 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001085 // This activity needs to be visible, but isn't even
1086 // running... get it started, but don't resume it
1087 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001088 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001089 if (r != starting) {
1090 r.startFreezingScreenLocked(r.app, configChanges);
1091 }
1092 if (!r.visible) {
1093 if (DEBUG_VISBILITY) Slog.v(
1094 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001095 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001096 }
1097 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001098 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001099 }
1100 }
1101
1102 } else if (r.visible) {
1103 // If this activity is already visible, then there is nothing
1104 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001105 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001106 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001107
1108 } else if (onlyThisProcess == null) {
1109 // This activity is not currently visible, but is running.
1110 // Tell it to become visible.
1111 r.visible = true;
1112 if (r.state != ActivityState.RESUMED && r != starting) {
1113 // If this activity is paused, tell it
1114 // to now show its window.
1115 if (DEBUG_VISBILITY) Slog.v(
1116 TAG, "Making visible and scheduling visibility: " + r);
1117 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001118 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001119 r.sleeping = false;
1120 r.app.pendingUiClean = true;
1121 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1122 r.stopFreezingScreenLocked(false);
1123 } catch (Exception e) {
1124 // Just skip on any failure; we'll make it
1125 // visible when it next restarts.
1126 Slog.w(TAG, "Exception thrown making visibile: "
1127 + r.intent.getComponent(), e);
1128 }
1129 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001130 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131
Craig Mautnerd44711d2013-02-23 11:24:36 -08001132 // Aggregate current change flags.
1133 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134
Craig Mautnerd44711d2013-02-23 11:24:36 -08001135 if (r.fullscreen) {
1136 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001137 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1138 behindFullscreen = true;
1139 } else if (r.mLaunchHomeTaskNext) {
1140 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1141 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001142 behindFullscreen = true;
1143 }
1144 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001145 if (DEBUG_VISBILITY) Slog.v(
1146 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1147 + " state=" + r.state
1148 + " behindFullscreen=" + behindFullscreen);
1149 // Now for any activities that aren't visible to the user, make
1150 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001151 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001152 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153 r.visible = false;
1154 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001155 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 if ((r.state == ActivityState.STOPPING
1157 || r.state == ActivityState.STOPPED)
1158 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001159 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001160 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001161 }
1162 } catch (Exception e) {
1163 // Just skip on any failure; we'll make it
1164 // visible when it next restarts.
1165 Slog.w(TAG, "Exception thrown making hidden: "
1166 + r.intent.getComponent(), e);
1167 }
1168 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001169 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001171 }
1172 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 }
Craig Mautner580ea812013-04-25 12:58:38 -07001174 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175 }
Craig Mautner58547802013-03-05 08:23:53 -08001176
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001177 /**
1178 * Ensure that the top activity in the stack is resumed.
1179 *
1180 * @param prev The previously resumed activity, for when in the process
1181 * of pausing; can be null to call from elsewhere.
1182 *
1183 * @return Returns true if something is being resumed, or false if
1184 * nothing happened.
1185 */
1186 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001187 return resumeTopActivityLocked(prev, null);
1188 }
1189
1190 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 // Find the first activity that is not finishing.
1192 ActivityRecord next = topRunningActivityLocked(null);
1193
1194 // Remember how we'll process this pause/resume situation, and ensure
1195 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001196 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1197 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198
1199 if (next == null) {
1200 // There are no more activities! Let's just start up the
1201 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001202 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001203 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001204 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205 }
1206
1207 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001208
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001209 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001210 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1211 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 // Make sure we have executed any pending transitions, since there
1213 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001214 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001216 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001217 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 return false;
1219 }
1220
Craig Mautner525f3d92013-05-07 14:01:50 -07001221 final TaskRecord nextTask = next.task;
1222 final TaskRecord prevTask = prev != null ? prev.task : null;
1223 if (prevTask != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001224 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001225 if (prevTask == nextTask) {
1226 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1227 final int numActivities = activities.size();
1228 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1229 final ActivityRecord r = activities.get(activityNdx);
1230 // r is usually the same as next, but what if two activities were launched
1231 // before prev finished?
1232 if (!r.finishing) {
1233 r.mLaunchHomeTaskNext = true;
1234 r.frontOfTask = true;
1235 break;
1236 }
1237 }
1238 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001239 // This task is going away but it was supposed to return to the home task.
1240 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001241 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001242 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1243 } else {
1244 return mStackSupervisor.resumeHomeActivity(prev);
1245 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001246 }
1247
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248 // If we are sleeping, and there is no resumed activity, and the top
1249 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001250 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001251 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001252 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 // Make sure we have executed any pending transitions, since there
1254 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001255 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001257 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001258 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 return false;
1260 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001261
1262 // Make sure that the user who owns this activity is started. If not,
1263 // we will just leave it as is because someone should be bringing
1264 // another user's activities to the top of the stack.
1265 if (mService.mStartedUsers.get(next.userId) == null) {
1266 Slog.w(TAG, "Skipping resume of top activity " + next
1267 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001268 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001269 return false;
1270 }
1271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001272 // The activity may be waiting for stop, but that is no longer
1273 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001274 mStackSupervisor.mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001275 mGoingToSleepActivities.remove(next);
1276 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001277 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001278
Dianne Hackborn84375872012-06-01 19:03:50 -07001279 next.updateOptionsLocked(options);
1280
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001281 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1282
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001283 // If we are currently pausing an activity, then don't do anything
1284 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001285 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1286 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Craig Mautnercf910b02013-04-23 11:23:27 -07001287 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001288 return false;
1289 }
1290
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001291 // Okay we are now going to start a switch, to 'next'. We may first
1292 // have to pause the current activity, but this is an important point
1293 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001294 // XXX "App Redirected" dialog is getting too many false positives
1295 // at this point, so turn off for now.
1296 if (false) {
1297 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1298 long now = SystemClock.uptimeMillis();
1299 final boolean inTime = mLastStartedActivity.startTime != 0
1300 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1301 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1302 final int nextUid = next.info.applicationInfo.uid;
1303 if (inTime && lastUid != nextUid
1304 && lastUid != next.launchedFromUid
1305 && mService.checkPermission(
1306 android.Manifest.permission.STOP_APP_SWITCHES,
1307 -1, next.launchedFromUid)
1308 != PackageManager.PERMISSION_GRANTED) {
1309 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1310 } else {
1311 next.startTime = now;
1312 mLastStartedActivity = next;
1313 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001314 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001315 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001316 mLastStartedActivity = next;
1317 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001318 }
Craig Mautner58547802013-03-05 08:23:53 -08001319
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001320 // We need to start pausing the current activity so the top one
1321 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001322 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1323 if (mResumedActivity != null) {
1324 pausing = true;
1325 startPausingLocked(userLeaving, false);
1326 }
1327 if (pausing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001328 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001329 // At this point we want to put the upcoming activity's process
1330 // at the top of the LRU list, since we know we will be needing it
1331 // very soon and it would be a waste to let it get killed if it
1332 // happens to be sitting towards the end.
1333 if (next.app != null && next.app.thread != null) {
1334 // No reason to do full oom adj update here; we'll let that
1335 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001336 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001337 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001338 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001339 return true;
1340 }
1341
Christopher Tated3f175c2012-06-14 14:16:54 -07001342 // If the most recent activity was noHistory but was only stopped rather
1343 // than stopped+finished because the device went to sleep, we need to make
1344 // sure to finish it as we're making a new activity topmost.
1345 final ActivityRecord last = mLastPausedActivity;
1346 if (mService.mSleeping && last != null && !last.finishing) {
1347 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1348 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1349 if (DEBUG_STATES) {
1350 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1351 }
1352 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001353 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001354 }
1355 }
1356
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 if (prev != null && prev != next) {
1358 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1359 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001360 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001361 if (DEBUG_SWITCH) Slog.v(
1362 TAG, "Resuming top, waiting visible to hide: " + prev);
1363 } else {
1364 // The next activity is already visible, so hide the previous
1365 // activity's windows right now so we can show the new one ASAP.
1366 // We only do this if the previous is finishing, which should mean
1367 // it is on top of the one being resumed so hiding it quickly
1368 // is good. Otherwise, we want to do the normal route of allowing
1369 // the resumed activity to be shown so we can decide if the
1370 // previous should actually be hidden depending on whether the
1371 // new one is found to be full-screen or not.
1372 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001373 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001374 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1375 + prev + ", waitingVisible="
1376 + (prev != null ? prev.waitingVisible : null)
1377 + ", nowVisible=" + next.nowVisible);
1378 } else {
1379 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1380 + prev + ", waitingVisible="
1381 + (prev != null ? prev.waitingVisible : null)
1382 + ", nowVisible=" + next.nowVisible);
1383 }
1384 }
1385 }
1386
Dianne Hackborne7f97212011-02-24 14:40:20 -08001387 // Launching this app's activity, make sure the app is no longer
1388 // considered stopped.
1389 try {
1390 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001391 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001392 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001393 } catch (IllegalArgumentException e) {
1394 Slog.w(TAG, "Failed trying to unstop package "
1395 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001396 }
1397
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 // We are starting up the next activity, so tell the window manager
1399 // that the previous one will be hidden soon. This way it can know
1400 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001401 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 if (prev != null) {
1403 if (prev.finishing) {
1404 if (DEBUG_TRANSITION) Slog.v(TAG,
1405 "Prepare close transition: prev=" + prev);
1406 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001407 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001408 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001410 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001411 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1412 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001414 mWindowManager.setAppWillBeHidden(prev.appToken);
1415 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001416 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001417 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001418 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001419 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001420 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001422 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001423 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1424 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001425 }
1426 }
1427 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001428 mWindowManager.setAppWillBeHidden(prev.appToken);
1429 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 }
Craig Mautner967212c2013-04-13 21:10:58 -07001431 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001432 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001434 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001435 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001436 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001437 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001438 }
1439 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001440 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001441 next.applyOptionsLocked();
1442 } else {
1443 next.clearOptionsLocked();
1444 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001445
Craig Mautnercf910b02013-04-23 11:23:27 -07001446 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001447 if (next.app != null && next.app.thread != null) {
1448 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1449
1450 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001451 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001452
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001453 // schedule launch ticks to collect information about slow apps.
1454 next.startLaunchTickingLocked();
1455
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001456 ActivityRecord lastResumedActivity =
1457 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458 ActivityState lastState = next.state;
1459
1460 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001461
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001462 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 next.state = ActivityState.RESUMED;
1464 mResumedActivity = next;
1465 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001466 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001467 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468 updateLRUListLocked(next);
1469
1470 // Have the window manager re-evaluate the orientation of
1471 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001472 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001473 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001474 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001475 mService.mConfiguration,
1476 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1477 if (config != null) {
1478 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001480 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001482
Craig Mautner525f3d92013-05-07 14:01:50 -07001483 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484 // The configuration update wasn't able to keep the existing
1485 // instance of the activity, and instead started a new one.
1486 // We should be all done, but let's just make sure our activity
1487 // is still at the top and schedule another run if something
1488 // weird happened.
1489 ActivityRecord nextNext = topRunningActivityLocked(null);
1490 if (DEBUG_SWITCH) Slog.i(TAG,
1491 "Activity config changed during resume: " + next
1492 + ", new next: " + nextNext);
1493 if (nextNext != next) {
1494 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001495 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001497 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001498 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001499 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001500 return true;
1501 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001502 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001503 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504 }
Craig Mautner58547802013-03-05 08:23:53 -08001505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 try {
1507 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001508 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509 if (a != null) {
1510 final int N = a.size();
1511 if (!next.finishing && N > 0) {
1512 if (DEBUG_RESULTS) Slog.v(
1513 TAG, "Delivering results to " + next
1514 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001515 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001516 }
1517 }
1518
1519 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001520 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001521 }
1522
1523 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001524 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001525 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001526
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001527 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001528 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001529 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001530 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001531 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001532
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001533 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534
1535 } catch (Exception e) {
1536 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001537 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1538 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001539 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001540 if (lastStack != null) {
1541 lastStack.mResumedActivity = lastResumedActivity;
1542 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 Slog.i(TAG, "Restarting because process died: " + next);
1544 if (!next.hasBeenLaunched) {
1545 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001546 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1547 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001548 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001549 next.appToken, next.packageName, next.theme,
1550 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
1551 next.nonLocalizedLabel, next.labelRes, next.icon, next.windowFlags,
1552 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001554 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001555 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 return true;
1557 }
1558
1559 // From this point on, if something goes wrong there is no way
1560 // to recover the activity.
1561 try {
1562 next.visible = true;
1563 completeResumeLocked(next);
1564 } catch (Exception e) {
1565 // If any exception gets thrown, toss away this
1566 // activity and try the next one.
1567 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001568 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001569 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001570 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 return true;
1572 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001573 next.stopped = false;
1574
1575 } else {
1576 // Whoops, need to restart this activity!
1577 if (!next.hasBeenLaunched) {
1578 next.hasBeenLaunched = true;
1579 } else {
1580 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001581 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001582 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001583 mService.compatibilityInfoForPackageLocked(
1584 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001586 next.labelRes, next.icon, next.windowFlags,
1587 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 }
1589 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1590 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001591 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001592 }
1593
Craig Mautnercf910b02013-04-23 11:23:27 -07001594 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595 return true;
1596 }
1597
Craig Mautner11bf9a52013-02-19 14:08:51 -08001598
Craig Mautner8849a5e2013-04-02 16:41:03 -07001599 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001600 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001601 TaskRecord rTask = r.task;
1602 final int taskId = rTask.taskId;
1603 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001604 // Last activity in task had been removed or ActivityManagerService is reusing task.
1605 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001606 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001607 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001608 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001609 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001610 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001611 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001612 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 if (!newTask) {
1614 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001615 boolean startIt = true;
1616 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1617 task = mTaskHistory.get(taskNdx);
1618 if (task == r.task) {
1619 // Here it is! Now, if this is not yet visible to the
1620 // user, then just add it without starting; it will
1621 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001622 if (!startIt) {
1623 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1624 + task, new RuntimeException("here").fillInStackTrace());
1625 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001626 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001627 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1628 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001629 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001631 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001633 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 return;
1635 }
1636 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001637 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638 startIt = false;
1639 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 }
1641 }
1642
1643 // Place a new activity at top of stack, so it is next to interact
1644 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001645
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001646 // If we are not placing the new activity frontmost, we do not want
1647 // to deliver the onUserLeaving callback to the actual frontmost
1648 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001649 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001650 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001651 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1652 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 }
Craig Mautner70a86932013-02-28 22:37:44 -08001654
1655 task = r.task;
1656
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001657 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001658 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001659 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001660 task.addActivityToTop(r);
1661
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001662 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001664 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 // We want to show the starting preview window if we are
1666 // switching to a new task, or the next activity's process is
1667 // not currently running.
1668 boolean showStartingIcon = newTask;
1669 ProcessRecord proc = r.app;
1670 if (proc == null) {
1671 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1672 }
1673 if (proc == null || proc.thread == null) {
1674 showStartingIcon = true;
1675 }
1676 if (DEBUG_TRANSITION) Slog.v(TAG,
1677 "Prepare open transition: starting " + r);
1678 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001679 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001681 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001682 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001683 ? AppTransition.TRANSIT_TASK_OPEN
1684 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 mNoAnimActivities.remove(r);
1686 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001687 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001688 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001689 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001690 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691 boolean doShow = true;
1692 if (newTask) {
1693 // Even though this activity is starting fresh, we still need
1694 // to reset it to make sure we apply affinities to move any
1695 // existing activities from other tasks in to it.
1696 // If the caller has requested that the target task be
1697 // reset, then do so.
1698 if ((r.intent.getFlags()
1699 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1700 resetTaskIfNeededLocked(r, r);
1701 doShow = topRunningNonDelayedActivityLocked(null) == r;
1702 }
1703 }
1704 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1705 // Figure out if we are transitioning from another activity that is
1706 // "has the same starting icon" as the next one. This allows the
1707 // window manager to keep the previous window it had previously
1708 // created, if it still had one.
1709 ActivityRecord prev = mResumedActivity;
1710 if (prev != null) {
1711 // We don't want to reuse the previous starting preview if:
1712 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001713 if (prev.task != r.task) {
1714 prev = null;
1715 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001716 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001717 else if (prev.nowVisible) {
1718 prev = null;
1719 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001720 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001721 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001722 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001723 mService.compatibilityInfoForPackageLocked(
1724 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001725 r.labelRes, r.icon, r.windowFlags,
1726 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 }
1728 } else {
1729 // If this is the first activity, don't do any fancy animations,
1730 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001731 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001732 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001733 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001734 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 }
1736 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001737 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 }
1739
1740 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001741 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742 }
1743 }
1744
Dianne Hackbornbe707852011-11-11 14:32:10 -08001745 final void validateAppTokensLocked() {
1746 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001747 mValidateAppTokens.ensureCapacity(numActivities());
1748 final int numTasks = mTaskHistory.size();
1749 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1750 TaskRecord task = mTaskHistory.get(taskNdx);
1751 final ArrayList<ActivityRecord> activities = task.mActivities;
1752 if (activities.size() == 0) {
1753 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001754 }
Craig Mautner000f0022013-02-26 15:04:29 -08001755 TaskGroup group = new TaskGroup();
1756 group.taskId = task.taskId;
1757 mValidateAppTokens.add(group);
1758 final int numActivities = activities.size();
1759 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1760 final ActivityRecord r = activities.get(activityNdx);
1761 group.tokens.add(r.appToken);
1762 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001763 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001764 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001765 }
1766
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767 /**
1768 * Perform a reset of the given task, if needed as part of launching it.
1769 * Returns the new HistoryRecord at the top of the task.
1770 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001771 /**
1772 * Helper method for #resetTaskIfNeededLocked.
1773 * We are inside of the task being reset... we'll either finish this activity, push it out
1774 * for another task, or leave it as-is.
1775 * @param task The task containing the Activity (taskTop) that might be reset.
1776 * @param forceReset
1777 * @return An ActivityOptions that needs to be processed.
1778 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001779 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001780 ActivityOptions topOptions = null;
1781
1782 int replyChainEnd = -1;
1783 boolean canMoveOptions = true;
1784
1785 // We only do this for activities that are not the root of the task (since if we finish
1786 // the root, we may no longer have the task!).
1787 final ArrayList<ActivityRecord> activities = task.mActivities;
1788 final int numActivities = activities.size();
1789 for (int i = numActivities - 1; i > 0; --i ) {
1790 ActivityRecord target = activities.get(i);
1791
1792 final int flags = target.info.flags;
1793 final boolean finishOnTaskLaunch =
1794 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1795 final boolean allowTaskReparenting =
1796 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1797 final boolean clearWhenTaskReset =
1798 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1799
1800 if (!finishOnTaskLaunch
1801 && !clearWhenTaskReset
1802 && target.resultTo != null) {
1803 // If this activity is sending a reply to a previous
1804 // activity, we can't do anything with it now until
1805 // we reach the start of the reply chain.
1806 // XXX note that we are assuming the result is always
1807 // to the previous activity, which is almost always
1808 // the case but we really shouldn't count on.
1809 if (replyChainEnd < 0) {
1810 replyChainEnd = i;
1811 }
1812 } else if (!finishOnTaskLaunch
1813 && !clearWhenTaskReset
1814 && allowTaskReparenting
1815 && target.taskAffinity != null
1816 && !target.taskAffinity.equals(task.affinity)) {
1817 // If this activity has an affinity for another
1818 // task, then we need to move it out of here. We will
1819 // move it as far out of the way as possible, to the
1820 // bottom of the activity stack. This also keeps it
1821 // correctly ordered with any activities we previously
1822 // moved.
1823 TaskRecord bottomTask = mTaskHistory.get(0);
1824 ActivityRecord p = bottomTask.mActivities.get(0);
1825 if (target.taskAffinity != null
1826 && target.taskAffinity.equals(p.task.affinity)) {
1827 // If the activity currently at the bottom has the
1828 // same task affinity as the one we are moving,
1829 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001830 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001831 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1832 + " out to bottom task " + p.task);
1833 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001834 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001835 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001836 target.task.affinityIntent = target.intent;
1837 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1838 + " out to new task " + target.task);
1839 }
1840
1841 final TaskRecord targetTask = target.task;
1842 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001843 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001844
Craig Mautner525f3d92013-05-07 14:01:50 -07001845 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001846 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1847 for (int srcPos = start; srcPos >= i; --srcPos) {
1848 p = activities.get(srcPos);
1849 if (p.finishing) {
1850 continue;
1851 }
1852
Craig Mautner525f3d92013-05-07 14:01:50 -07001853 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001854 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001855 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001856 topOptions = p.takeOptionsLocked();
1857 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001858 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001859 }
1860 }
1861 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1862 + task + " adding to task=" + targetTask,
1863 new RuntimeException("here").fillInStackTrace());
1864 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1865 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001866 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001867 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001868
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001869 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001870 }
1871
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001872 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001873 if (VALIDATE_TOKENS) {
1874 validateAppTokensLocked();
1875 }
1876
1877 replyChainEnd = -1;
1878 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1879 // If the activity should just be removed -- either
1880 // because it asks for it, or the task should be
1881 // cleared -- then finish it and anything that is
1882 // part of its reply chain.
1883 int end;
1884 if (clearWhenTaskReset) {
1885 // In this case, we want to finish this activity
1886 // and everything above it, so be sneaky and pretend
1887 // like these are all in the reply chain.
1888 end = numActivities - 1;
1889 } else if (replyChainEnd < 0) {
1890 end = i;
1891 } else {
1892 end = replyChainEnd;
1893 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001894 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001895 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001896 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001897 if (p.finishing) {
1898 continue;
1899 }
1900 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001901 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001902 topOptions = p.takeOptionsLocked();
1903 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001904 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001905 }
1906 }
Craig Mautner58547802013-03-05 08:23:53 -08001907 if (DEBUG_TASKS) Slog.w(TAG,
1908 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001909 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001910 end--;
1911 srcPos--;
1912 }
1913 }
1914 replyChainEnd = -1;
1915 } else {
1916 // If we were in the middle of a chain, well the
1917 // activity that started it all doesn't want anything
1918 // special, so leave it all as-is.
1919 replyChainEnd = -1;
1920 }
1921 }
1922
1923 return topOptions;
1924 }
1925
1926 /**
1927 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1928 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1929 * @param affinityTask The task we are looking for an affinity to.
1930 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1931 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1932 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1933 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001934 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001935 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001936 int replyChainEnd = -1;
1937 final int taskId = task.taskId;
1938 final String taskAffinity = task.affinity;
1939
1940 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1941 final int numActivities = activities.size();
1942 // Do not operate on the root Activity.
1943 for (int i = numActivities - 1; i > 0; --i) {
1944 ActivityRecord target = activities.get(i);
1945
1946 final int flags = target.info.flags;
1947 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1948 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1949
1950 if (target.resultTo != null) {
1951 // If this activity is sending a reply to a previous
1952 // activity, we can't do anything with it now until
1953 // we reach the start of the reply chain.
1954 // XXX note that we are assuming the result is always
1955 // to the previous activity, which is almost always
1956 // the case but we really shouldn't count on.
1957 if (replyChainEnd < 0) {
1958 replyChainEnd = i;
1959 }
1960 } else if (topTaskIsHigher
1961 && allowTaskReparenting
1962 && taskAffinity != null
1963 && taskAffinity.equals(target.taskAffinity)) {
1964 // This activity has an affinity for our task. Either remove it if we are
1965 // clearing or move it over to our task. Note that
1966 // we currently punt on the case where we are resetting a
1967 // task that is not at the top but who has activities above
1968 // with an affinity to it... this is really not a normal
1969 // case, and we will need to later pull that task to the front
1970 // and usually at that point we will do the reset and pick
1971 // up those remaining activities. (This only happens if
1972 // someone starts an activity in a new task from an activity
1973 // in a task that is not currently on top.)
1974 if (forceReset || finishOnTaskLaunch) {
1975 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1976 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1977 for (int srcPos = start; srcPos >= i; --srcPos) {
1978 final ActivityRecord p = activities.get(srcPos);
1979 if (p.finishing) {
1980 continue;
1981 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001982 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001983 }
1984 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001985 if (taskInsertionPoint < 0) {
1986 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001987
Craig Mautner77878772013-03-04 19:46:24 -08001988 }
Craig Mautner77878772013-03-04 19:46:24 -08001989
1990 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1991 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1992 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1993 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001994 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001995 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08001996 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001997
Craig Mautnere3a74d52013-02-22 14:14:58 -08001998 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
1999 + " to stack at " + task,
2000 new RuntimeException("here").fillInStackTrace());
2001 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2002 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002003 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002004 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002005 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002006 if (VALIDATE_TOKENS) {
2007 validateAppTokensLocked();
2008 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002009
2010 // Now we've moved it in to place... but what if this is
2011 // a singleTop activity and we have put it on top of another
2012 // instance of the same activity? Then we drop the instance
2013 // below so it remains singleTop.
2014 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2015 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002016 int targetNdx = taskActivities.indexOf(target);
2017 if (targetNdx > 0) {
2018 ActivityRecord p = taskActivities.get(targetNdx - 1);
2019 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002020 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2021 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002022 }
2023 }
2024 }
2025 }
2026
2027 replyChainEnd = -1;
2028 }
2029 }
Craig Mautner77878772013-03-04 19:46:24 -08002030 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002031 }
2032
Craig Mautner8849a5e2013-04-02 16:41:03 -07002033 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002034 ActivityRecord newActivity) {
2035 boolean forceReset =
2036 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2037 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2038 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2039 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2040 forceReset = true;
2041 }
2042 }
2043
2044 final TaskRecord task = taskTop.task;
2045
2046 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2047 * for remaining tasks. Used for later tasks to reparent to task. */
2048 boolean taskFound = false;
2049
2050 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2051 ActivityOptions topOptions = null;
2052
Craig Mautner77878772013-03-04 19:46:24 -08002053 // Preserve the location for reparenting in the new task.
2054 int reparentInsertionPoint = -1;
2055
Craig Mautnere3a74d52013-02-22 14:14:58 -08002056 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2057 final TaskRecord targetTask = mTaskHistory.get(i);
2058
2059 if (targetTask == task) {
2060 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2061 taskFound = true;
2062 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002063 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2064 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002065 }
2066 }
2067
Craig Mautner70a86932013-02-28 22:37:44 -08002068 int taskNdx = mTaskHistory.indexOf(task);
2069 do {
2070 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2071 } while (taskTop == null && taskNdx >= 0);
2072
Craig Mautnere3a74d52013-02-22 14:14:58 -08002073 if (topOptions != null) {
2074 // If we got some ActivityOptions from an activity on top that
2075 // was removed from the task, propagate them to the new real top.
2076 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002077 taskTop.updateOptionsLocked(topOptions);
2078 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002079 topOptions.abort();
2080 }
2081 }
2082
2083 return taskTop;
2084 }
2085
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002086 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2087 String resultWho, int requestCode, int resultCode, Intent data) {
2088
2089 if (callingUid > 0) {
2090 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002091 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 }
2093
2094 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2095 + " : who=" + resultWho + " req=" + requestCode
2096 + " res=" + resultCode + " data=" + data);
2097 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2098 try {
2099 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2100 list.add(new ResultInfo(resultWho, requestCode,
2101 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002102 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002103 return;
2104 } catch (Exception e) {
2105 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2106 }
2107 }
2108
2109 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2110 }
2111
Craig Mautnerf3333272013-04-22 10:55:53 -07002112 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002113 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2114 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2115 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2116 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002117 if (!mService.mSleeping) {
2118 if (DEBUG_STATES) {
2119 Slog.d(TAG, "no-history finish of " + r);
2120 }
2121 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002122 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002123 } else {
2124 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2125 + " on stop because we're just sleeping");
2126 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002128 }
2129
2130 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002131 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002132 if (mService.mFocusedActivity == r) {
2133 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2134 }
2135 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002136 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 try {
2138 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002139 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2140 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141 r.state = ActivityState.STOPPING;
2142 if (DEBUG_VISBILITY) Slog.v(
2143 TAG, "Stopping visible=" + r.visible + " for " + r);
2144 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002145 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002147 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002148 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002149 r.setSleeping(true);
2150 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002151 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2152 msg.obj = r;
2153 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002154 } catch (Exception e) {
2155 // Maybe just ignore exceptions here... if the process
2156 // has crashed, our death notification will clean things
2157 // up.
2158 Slog.w(TAG, "Exception thrown during pause", e);
2159 // Just in case, assume it to be stopped.
2160 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002161 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002162 r.state = ActivityState.STOPPED;
2163 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002164 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 }
2166 }
2167 }
2168 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002169
Craig Mautner525f3d92013-05-07 14:01:50 -07002170 final ActivityRecord activityIdleInternalLocked(final IBinder token) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2172
Craig Mautnere79d42682013-04-01 19:01:53 -07002173 // Get the activity record.
Craig Mautner525f3d92013-05-07 14:01:50 -07002174 ActivityRecord res = isInStackLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002175 if (res != null) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002176 // No longer need to keep the device awake.
Craig Mautnerf3333272013-04-22 10:55:53 -07002177 if (mResumedActivity == res && mLaunchingActivity.isHeld()) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002178 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2179 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 }
2181
Craig Mautnere79d42682013-04-01 19:01:53 -07002182 // If this activity is fullscreen, set up to hide those under it.
Craig Mautnerf3333272013-04-22 10:55:53 -07002183 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + res);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002184 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002185 }
2186
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002187 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188 }
2189
2190 /**
2191 * @return Returns true if the activity is being finished, false if for
2192 * some reason it is being left as-is.
2193 */
2194 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002195 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002196 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002197 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002198 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002199 + ", result=" + resultCode + ", data=" + resultData
2200 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002201 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002202 return false;
2203 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204
Craig Mautnerd44711d2013-02-23 11:24:36 -08002205 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002206 return true;
2207 }
2208
Craig Mautnerd2328952013-03-05 12:46:26 -08002209 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002210 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2211 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2212 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2213 ActivityRecord r = activities.get(activityNdx);
2214 if (r.resultTo == self && r.requestCode == requestCode) {
2215 if ((r.resultWho == null && resultWho == null) ||
2216 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2217 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2218 false);
2219 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002220 }
2221 }
2222 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002223 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002224 }
2225
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002226 final void finishTopRunningActivityLocked(ProcessRecord app) {
2227 ActivityRecord r = topRunningActivityLocked(null);
2228 if (r != null && r.app == app) {
2229 // If the top running activity is from this crashing
2230 // process, then terminate it to avoid getting in a loop.
2231 Slog.w(TAG, " Force finishing activity "
2232 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002233 int taskNdx = mTaskHistory.indexOf(r.task);
2234 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002235 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002236 // Also terminate any activities below it that aren't yet
2237 // stopped, to avoid a situation where one will get
2238 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002239 --activityNdx;
2240 if (activityNdx < 0) {
2241 do {
2242 --taskNdx;
2243 if (taskNdx < 0) {
2244 break;
2245 }
2246 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2247 } while (activityNdx < 0);
2248 }
2249 if (activityNdx >= 0) {
2250 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002251 if (r.state == ActivityState.RESUMED
2252 || r.state == ActivityState.PAUSING
2253 || r.state == ActivityState.PAUSED) {
2254 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
2255 Slog.w(TAG, " Force finishing activity "
2256 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002257 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002258 }
2259 }
2260 }
2261 }
2262 }
2263
Craig Mautnerd2328952013-03-05 12:46:26 -08002264 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002265 ArrayList<ActivityRecord> activities = r.task.mActivities;
2266 for (int index = activities.indexOf(r); index >= 0; --index) {
2267 ActivityRecord cur = activities.get(index);
2268 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002269 break;
2270 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002271 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002272 }
2273 return true;
2274 }
2275
Dianne Hackborn5c607432012-02-28 14:44:19 -08002276 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2277 // send the result
2278 ActivityRecord resultTo = r.resultTo;
2279 if (resultTo != null) {
2280 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2281 + " who=" + r.resultWho + " req=" + r.requestCode
2282 + " res=" + resultCode + " data=" + resultData);
2283 if (r.info.applicationInfo.uid > 0) {
2284 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2285 resultTo.packageName, resultData,
2286 resultTo.getUriPermissionsLocked());
2287 }
2288 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2289 resultData);
2290 r.resultTo = null;
2291 }
2292 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2293
2294 // Make sure this HistoryRecord is not holding on to other resources,
2295 // because clients have remote IPC references to this object so we
2296 // can't assume that will go away and want to avoid circular IPC refs.
2297 r.results = null;
2298 r.pendingResults = null;
2299 r.newIntents = null;
2300 r.icicle = null;
2301 }
2302
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303 /**
2304 * @return Returns true if this activity has been removed from the history
2305 * list, or false if it is still in the list and will be removed later.
2306 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002307 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2308 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 if (r.finishing) {
2310 Slog.w(TAG, "Duplicate finish request for " + r);
2311 return false;
2312 }
2313
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002314 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002315 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002316 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002318 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2319 final int index = activities.indexOf(r);
2320 if (index < (activities.size() - 1)) {
2321 ActivityRecord next = activities.get(index+1);
2322 if (r.frontOfTask) {
2323 // The next activity is now the front of the task.
2324 next.frontOfTask = true;
2325 }
2326 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2327 // If the caller asked that this activity (and all above it)
2328 // be cleared when the task is reset, don't lose that information,
2329 // but propagate it up to the next activity.
2330 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002331 }
2332 }
2333
2334 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002335 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002337 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 }
2339 }
2340
Dianne Hackborn5c607432012-02-28 14:44:19 -08002341 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002342
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 if (mService.mPendingThumbnails.size() > 0) {
2344 // There are clients waiting to receive thumbnails so, in case
2345 // this is an activity that someone is waiting for, add it
2346 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002347 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 }
2349
Craig Mautnerde4ef022013-04-07 19:01:33 -07002350 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002351 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 if (DEBUG_TRANSITION) Slog.v(TAG,
2353 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002354 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002355 ? AppTransition.TRANSIT_TASK_CLOSE
2356 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002357
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002359 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002360
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002361 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002362 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2363 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2364 startPausingLocked(false, false);
2365 }
2366
2367 } else if (r.state != ActivityState.PAUSING) {
2368 // If the activity is PAUSING, we will complete the finish once
2369 // it is done pausing; else we can just directly finish it here.
2370 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002371 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 } else {
2373 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2374 }
2375
2376 return false;
2377 }
2378
Craig Mautnerf3333272013-04-22 10:55:53 -07002379 static final int FINISH_IMMEDIATELY = 0;
2380 static final int FINISH_AFTER_PAUSE = 1;
2381 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382
Craig Mautnerf3333272013-04-22 10:55:53 -07002383 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 // First things first: if this activity is currently visible,
2385 // and the resumed activity is not yet visible, then hold off on
2386 // finishing until the resumed one becomes visible.
2387 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002388 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2389 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002390 if (mStackSupervisor.mStoppingActivities.size() > 3
2391 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002392 // If we already have a few activities waiting to stop,
2393 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002394 // them out. Or if r is the last of activity of the last task the stack
2395 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002396 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002397 } else {
2398 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 }
2400 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002401 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2402 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002403 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002404 if (oomAdj) {
2405 mService.updateOomAdjLocked();
2406 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002407 return r;
2408 }
2409
2410 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002411 mStackSupervisor.mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002412 mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002413 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002414 if (mResumedActivity == r) {
2415 mResumedActivity = null;
2416 }
2417 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002418 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002419 r.state = ActivityState.FINISHING;
2420
2421 if (mode == FINISH_IMMEDIATELY
2422 || prevState == ActivityState.STOPPED
2423 || prevState == ActivityState.INITIALIZING) {
2424 // If this activity is already stopped, we can just finish
2425 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002426 boolean activityRemoved = destroyActivityLocked(r, true,
2427 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002428 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002429 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002430 }
2431 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002433
2434 // Need to go through the full pause cycle to get this
2435 // activity into the stopped state and then finish it.
2436 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002437 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002438 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 return r;
2440 }
2441
Craig Mautnerd2328952013-03-05 12:46:26 -08002442 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002443 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002444 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002445 final TaskRecord task = srec.task;
2446 final ArrayList<ActivityRecord> activities = task.mActivities;
2447 final int start = activities.indexOf(srec);
2448 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002449 return false;
2450 }
2451 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002452 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002453 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002454 final ComponentName dest = destIntent.getComponent();
2455 if (start > 0 && dest != null) {
2456 for (int i = finishTo; i >= 0; i--) {
2457 ActivityRecord r = activities.get(i);
2458 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002459 r.info.name.equals(dest.getClassName())) {
2460 finishTo = i;
2461 parent = r;
2462 foundParentInTask = true;
2463 break;
2464 }
2465 }
2466 }
2467
2468 IActivityController controller = mService.mController;
2469 if (controller != null) {
2470 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2471 if (next != null) {
2472 // ask watcher if this is allowed
2473 boolean resumeOK = true;
2474 try {
2475 resumeOK = controller.activityResuming(next.packageName);
2476 } catch (RemoteException e) {
2477 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002478 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002479 }
2480
2481 if (!resumeOK) {
2482 return false;
2483 }
2484 }
2485 }
2486 final long origId = Binder.clearCallingIdentity();
2487 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002488 ActivityRecord r = activities.get(i);
2489 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002490 // Only return the supplied result for the first activity finished
2491 resultCode = Activity.RESULT_CANCELED;
2492 resultData = null;
2493 }
2494
2495 if (parent != null && foundParentInTask) {
2496 final int parentLaunchMode = parent.info.launchMode;
2497 final int destIntentFlags = destIntent.getFlags();
2498 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2499 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2500 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2501 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2502 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2503 } else {
2504 try {
2505 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2506 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002507 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002508 null, aInfo, parent.appToken, null,
2509 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2510 0, null, true, null);
2511 foundParentInTask = res == ActivityManager.START_SUCCESS;
2512 } catch (RemoteException e) {
2513 foundParentInTask = false;
2514 }
2515 requestFinishActivityLocked(parent.appToken, resultCode,
2516 resultData, "navigate-up", true);
2517 }
2518 }
2519 Binder.restoreCallingIdentity(origId);
2520 return foundParentInTask;
2521 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002522 /**
2523 * Perform the common clean-up of an activity record. This is called both
2524 * as part of destroyActivityLocked() (when destroying the client-side
2525 * representation) and cleaning things up as a result of its hosting
2526 * processing going away, in which case there is no remaining client-side
2527 * state to destroy so only the cleanup here is needed.
2528 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002529 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2530 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 if (mResumedActivity == r) {
2532 mResumedActivity = null;
2533 }
2534 if (mService.mFocusedActivity == r) {
2535 mService.mFocusedActivity = null;
2536 }
2537
2538 r.configDestroy = false;
2539 r.frozenBeforeDestroy = false;
2540
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002541 if (setState) {
2542 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2543 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002544 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002545 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002546 }
2547
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002548 // Make sure this record is no longer in the pending finishes list.
2549 // This could happen, for example, if we are trimming activities
2550 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002551 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002552 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002553
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002554 // Remove any pending results.
2555 if (r.finishing && r.pendingResults != null) {
2556 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2557 PendingIntentRecord rec = apr.get();
2558 if (rec != null) {
2559 mService.cancelIntentSenderLocked(rec, false);
2560 }
2561 }
2562 r.pendingResults = null;
2563 }
2564
2565 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002566 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567 }
2568
2569 if (mService.mPendingThumbnails.size() > 0) {
2570 // There are clients waiting to receive thumbnails so, in case
2571 // this is an activity that someone is waiting for, add it
2572 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002573 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002574 }
2575
2576 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002577 removeTimeoutsForActivityLocked(r);
2578 }
2579
2580 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002581 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002582 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002583 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002584 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002585 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 }
2587
Dianne Hackborn5c607432012-02-28 14:44:19 -08002588 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002589 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2590 r.makeFinishing();
2591 if (DEBUG_ADD_REMOVE) {
2592 RuntimeException here = new RuntimeException("here");
2593 here.fillInStackTrace();
2594 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002595 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002596 final TaskRecord task = r.task;
2597 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002598 if (DEBUG_STACK) Slog.i(TAG,
2599 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002600 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002601 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002602 r.takeFromHistory();
2603 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002604 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002605 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002606 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002607 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002608 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002609 if (VALIDATE_TOKENS) {
2610 validateAppTokensLocked();
2611 }
2612 cleanUpActivityServicesLocked(r);
2613 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002615
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002616 /**
2617 * Perform clean-up of service connections in an activity record.
2618 */
2619 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2620 // Throw away any services that have been bound by this activity.
2621 if (r.connections != null) {
2622 Iterator<ConnectionRecord> it = r.connections.iterator();
2623 while (it.hasNext()) {
2624 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002625 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002626 }
2627 r.connections = null;
2628 }
2629 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002630
2631 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2632 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2633 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2634 mHandler.sendMessage(msg);
2635 }
2636
Dianne Hackborn28695e02011-11-02 21:59:51 -07002637 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002638 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002639 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002640 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2641 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2642 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2643 final ActivityRecord r = activities.get(activityNdx);
2644 if (r.finishing) {
2645 continue;
2646 }
2647 if (r.fullscreen) {
2648 lastIsOpaque = true;
2649 }
2650 if (owner != null && r.app != owner) {
2651 continue;
2652 }
2653 if (!lastIsOpaque) {
2654 continue;
2655 }
2656 // We can destroy this one if we have its icicle saved and
2657 // it is not in the process of pausing/stopping/finishing.
2658 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2659 && r.haveState && !r.visible && r.stopped
2660 && r.state != ActivityState.DESTROYING
2661 && r.state != ActivityState.DESTROYED) {
2662 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2663 + " resumed=" + mResumedActivity
2664 + " pausing=" + mPausingActivity);
2665 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2666 activityRemoved = true;
2667 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002668 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002669 }
2670 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002671 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002672 mStackSupervisor.resumeTopActivitiesLocked();
2673
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002674 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002675 }
2676
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002677 /**
2678 * Destroy the current CLIENT SIDE instance of an activity. This may be
2679 * called both when actually finishing an activity, or when performing
2680 * a configuration switch where we destroy the current client-side object
2681 * but then create a new client-side object for this same HistoryRecord.
2682 */
2683 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002684 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002685 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002686 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002687 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2688 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002689 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002690 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002691
2692 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002693
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002694 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695
2696 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002697
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002698 if (hadApp) {
2699 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002700 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2702 mService.mHeavyWeightProcess = null;
2703 mService.mHandler.sendEmptyMessage(
2704 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2705 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002706 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002707 // No longer have activities, so update oom adj.
2708 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 }
2710 }
2711
2712 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002713
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002714 try {
2715 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002716 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002717 r.configChangeFlags);
2718 } catch (Exception e) {
2719 // We can just ignore exceptions here... if the process
2720 // has crashed, our death notification will clean things
2721 // up.
2722 //Slog.w(TAG, "Exception thrown during finish", e);
2723 if (r.finishing) {
2724 removeActivityFromHistoryLocked(r);
2725 removedFromHistory = true;
2726 skipDestroy = true;
2727 }
2728 }
2729
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002731
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002732 // If the activity is finishing, we need to wait on removing it
2733 // from the list to give it a chance to do its cleanup. During
2734 // that time it may make calls back with its token so we need to
2735 // be able to find it on the list and so we don't want to remove
2736 // it from the list yet. Otherwise, we can just immediately put
2737 // it in the destroyed state since we are not removing it from the
2738 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002739 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002740 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2741 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 r.state = ActivityState.DESTROYING;
2743 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
2744 msg.obj = r;
2745 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2746 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002747 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2748 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002750 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002751 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002752 }
2753 } else {
2754 // remove this record from the history.
2755 if (r.finishing) {
2756 removeActivityFromHistoryLocked(r);
2757 removedFromHistory = true;
2758 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002759 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2760 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002762 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002763 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 }
2765 }
2766
2767 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002768
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002769 if (!mLRUActivities.remove(r) && hadApp) {
2770 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2771 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002772
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002773 return removedFromHistory;
2774 }
2775
Craig Mautnerd2328952013-03-05 12:46:26 -08002776 final void activityDestroyedLocked(IBinder token) {
2777 final long origId = Binder.clearCallingIdentity();
2778 try {
2779 ActivityRecord r = ActivityRecord.forToken(token);
2780 if (r != null) {
2781 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002782 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002783
2784 if (isInStackLocked(token) != null) {
2785 if (r.state == ActivityState.DESTROYING) {
2786 cleanUpActivityLocked(r, true, false);
2787 removeActivityFromHistoryLocked(r);
2788 }
2789 }
Craig Mautner05d29032013-05-03 13:40:13 -07002790 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002791 } finally {
2792 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002793 }
2794 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002795
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002796 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2797 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002798 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002799 if (DEBUG_CLEANUP) Slog.v(
2800 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 + " with " + i + " entries");
2802 while (i > 0) {
2803 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002804 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002805 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002807 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002808 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002809 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 }
2811 }
2812 }
2813
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002814 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2815 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002816 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2817 "mStoppingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002818 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002819 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002820 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002821 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2822 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002823
2824 boolean hasVisibleActivities = false;
2825
2826 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002827 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002828 if (DEBUG_CLEANUP) Slog.v(
2829 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002830 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2831 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2832 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2833 final ActivityRecord r = activities.get(activityNdx);
2834 --i;
2835 if (DEBUG_CLEANUP) Slog.v(
2836 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2837 if (r.app == app) {
2838 boolean remove;
2839 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2840 // Don't currently have state for the activity, or
2841 // it is finishing -- always remove it.
2842 remove = true;
2843 } else if (r.launchCount > 2 &&
2844 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2845 // We have launched this activity too many times since it was
2846 // able to run, so give up and remove it.
2847 remove = true;
2848 } else {
2849 // The process may be gone, but the activity lives on!
2850 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002851 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002852 if (remove) {
2853 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
2854 RuntimeException here = new RuntimeException("here");
2855 here.fillInStackTrace();
2856 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2857 + ": haveState=" + r.haveState
2858 + " stateNotNeeded=" + r.stateNotNeeded
2859 + " finishing=" + r.finishing
2860 + " state=" + r.state, here);
2861 }
2862 if (!r.finishing) {
2863 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2864 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2865 r.userId, System.identityHashCode(r),
2866 r.task.taskId, r.shortComponentName,
2867 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002868 if (r.state == ActivityState.RESUMED) {
2869 mService.updateUsageStats(r, false);
2870 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002871 }
2872 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002873
Craig Mautner0247fc82013-02-28 14:32:06 -08002874 } else {
2875 // We have the current state for this activity, so
2876 // it can be restarted later when needed.
2877 if (localLOGV) Slog.v(
2878 TAG, "Keeping entry, setting app to null");
2879 if (r.visible) {
2880 hasVisibleActivities = true;
2881 }
2882 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2883 + r);
2884 r.app = null;
2885 r.nowVisible = false;
2886 if (!r.haveState) {
2887 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
2888 "App died, clearing saved state of " + r);
2889 r.icicle = null;
2890 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002891 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002892
Craig Mautnerd2328952013-03-05 12:46:26 -08002893 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002894 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002895 }
2896 }
2897
2898 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002899 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002900
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002901 final void updateTransitLocked(int transit, Bundle options) {
2902 if (options != null) {
2903 ActivityRecord r = topRunningActivityLocked(null);
2904 if (r != null && r.state != ActivityState.RESUMED) {
2905 r.updateOptionsLocked(options);
2906 } else {
2907 ActivityOptions.abort(options);
2908 }
2909 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002910 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002911 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002912
Craig Mautnercae015f2013-02-08 14:31:27 -08002913 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002914 final TaskRecord task = taskForIdLocked(taskId);
2915 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002916 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002917 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002918 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002919 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2920 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002921 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002922 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002923 }
2924 moveTaskToFrontLocked(task, null, options);
2925 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002926 }
2927 return false;
2928 }
2929
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002930 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002931 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002932
Craig Mautner11bf9a52013-02-19 14:08:51 -08002933 final int numTasks = mTaskHistory.size();
2934 final int index = mTaskHistory.indexOf(tr);
2935 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
2936 // nothing to do!
2937 if (reason != null &&
2938 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2939 ActivityOptions.abort(options);
2940 } else {
2941 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2942 }
2943 return;
2944 }
2945
2946 // Shift all activities with this task up to the top
2947 // of the stack, keeping them in the same internal order.
2948 mTaskHistory.remove(tr);
2949 mTaskHistory.add(tr);
2950
2951 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 if (reason != null &&
2953 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002954 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 ActivityRecord r = topRunningActivityLocked(null);
2956 if (r != null) {
2957 mNoAnimActivities.add(r);
2958 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002959 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002961 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002962 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002963
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002964 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965
Craig Mautner05d29032013-05-03 13:40:13 -07002966 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002967 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002968
2969 if (VALIDATE_TOKENS) {
2970 validateAppTokensLocked();
2971 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 }
2973
2974 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002975 * Worker method for rearranging history stack. Implements the function of moving all
2976 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002978 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2980 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002981 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002982 * @param task The taskId to collect and move to the bottom.
2983 * @return Returns true if the move completed, false if not.
2984 */
2985 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
2986 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 // If we have a watcher, preflight the move before committing to it. First check
2989 // for *other* available tasks, but if none are available, then try again allowing the
2990 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002991 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 ActivityRecord next = topRunningActivityLocked(null, task);
2993 if (next == null) {
2994 next = topRunningActivityLocked(null, 0);
2995 }
2996 if (next != null) {
2997 // ask watcher if this is allowed
2998 boolean moveOK = true;
2999 try {
3000 moveOK = mService.mController.activityResuming(next.packageName);
3001 } catch (RemoteException e) {
3002 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003003 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003004 }
3005 if (!moveOK) {
3006 return false;
3007 }
3008 }
3009 }
3010
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011 if (DEBUG_TRANSITION) Slog.v(TAG,
3012 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013
Craig Mautnerd2328952013-03-05 12:46:26 -08003014 final TaskRecord tr = taskForIdLocked(task);
3015 if (tr == null) {
3016 return false;
3017 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003018 mTaskHistory.remove(tr);
3019 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020
3021 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003022 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3023 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003024 ActivityRecord r = topRunningActivityLocked(null);
3025 if (r != null) {
3026 mNoAnimActivities.add(r);
3027 }
3028 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003029 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003030 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003032 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003033
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003034 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003035 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036 }
3037
Craig Mautnerde4ef022013-04-07 19:01:33 -07003038 if (mResumedActivity != null && mResumedActivity.task == tr &&
3039 mResumedActivity.mLaunchHomeTaskNext) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07003040 // TODO: Can we skip the next line and just pass mResumedAct. to resumeHomeAct.()?
Craig Mautnerde4ef022013-04-07 19:01:33 -07003041 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003042 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003043 }
3044
Craig Mautner05d29032013-05-03 13:40:13 -07003045 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003046 return true;
3047 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003048
Craig Mautner8849a5e2013-04-02 16:41:03 -07003049 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003051 final Uri data = r.intent.getData();
3052 final String strData = data != null ? data.toSafeString() : null;
3053
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003054 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003055 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003056 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003057 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003058 }
3059
3060 /**
3061 * Make sure the given activity matches the current configuration. Returns
3062 * false if the activity had to be destroyed. Returns true if the
3063 * configuration is the same, or the activity will remain running as-is
3064 * for whatever reason. Ensures the HistoryRecord is updated with the
3065 * correct configuration and all other bookkeeping is handled.
3066 */
3067 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3068 int globalChanges) {
3069 if (mConfigWillChange) {
3070 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3071 "Skipping config check (will change): " + r);
3072 return true;
3073 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003074
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3076 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003077
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 // Short circuit: if the two configurations are the exact same
3079 // object (the common case), then there is nothing to do.
3080 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003081 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3083 "Configuration unchanged in " + r);
3084 return true;
3085 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 // We don't worry about activities that are finishing.
3088 if (r.finishing) {
3089 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3090 "Configuration doesn't matter in finishing " + r);
3091 r.stopFreezingScreenLocked(false);
3092 return true;
3093 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003094
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 // Okay we now are going to make this activity have the new config.
3096 // But then we need to figure out how it needs to deal with that.
3097 Configuration oldConfig = r.configuration;
3098 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003099
3100 // Determine what has changed. May be nothing, if this is a config
3101 // that has come back from the app after going idle. In that case
3102 // we just want to leave the official config object now in the
3103 // activity and do nothing else.
3104 final int changes = oldConfig.diff(newConfig);
3105 if (changes == 0 && !r.forceNewConfig) {
3106 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3107 "Configuration no differences in " + r);
3108 return true;
3109 }
3110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111 // If the activity isn't currently running, just leave the new
3112 // configuration and it will pick that up next time it starts.
3113 if (r.app == null || r.app.thread == null) {
3114 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3115 "Configuration doesn't matter not running " + r);
3116 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003117 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 return true;
3119 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003120
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003121 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003122 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3123 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3124 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003125 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003126 + ", newConfig=" + newConfig);
3127 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003128 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003129 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3130 r.configChangeFlags |= changes;
3131 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003132 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003133 if (r.app == null || r.app.thread == null) {
3134 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003135 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003136 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003137 } else if (r.state == ActivityState.PAUSING) {
3138 // A little annoying: we are waiting for this activity to
3139 // finish pausing. Let's not do anything now, but just
3140 // flag that it needs to be restarted when done pausing.
3141 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003142 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003143 r.configDestroy = true;
3144 return true;
3145 } else if (r.state == ActivityState.RESUMED) {
3146 // Try to optimize this case: the configuration is changing
3147 // and we need to restart the top, resumed activity.
3148 // Instead of doing the normal handshaking, just say
3149 // "restart!".
3150 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003151 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003152 relaunchActivityLocked(r, r.configChangeFlags, true);
3153 r.configChangeFlags = 0;
3154 } else {
3155 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003156 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003157 relaunchActivityLocked(r, r.configChangeFlags, false);
3158 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003159 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003160
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003161 // All done... tell the caller we weren't able to keep this
3162 // activity around.
3163 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003164 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003165
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 // Default case: the activity can handle this new configuration, so
3167 // hand it over. Note that we don't need to give it the new
3168 // configuration, since we always send configuration changes to all
3169 // process when they happen so it can just use whatever configuration
3170 // it last got.
3171 if (r.app != null && r.app.thread != null) {
3172 try {
3173 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003174 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003175 } catch (RemoteException e) {
3176 // If process died, whatever.
3177 }
3178 }
3179 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 return true;
3182 }
3183
3184 private final boolean relaunchActivityLocked(ActivityRecord r,
3185 int changes, boolean andResume) {
3186 List<ResultInfo> results = null;
3187 List<Intent> newIntents = null;
3188 if (andResume) {
3189 results = r.results;
3190 newIntents = r.newIntents;
3191 }
3192 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3193 + " with results=" + results + " newIntents=" + newIntents
3194 + " andResume=" + andResume);
3195 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003196 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003197 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003198
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003199 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003202 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3203 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3204 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003205 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003206 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003207 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003208 // Note: don't need to call pauseIfSleepingLocked() here, because
3209 // the caller will only pass in 'andResume' if this activity is
3210 // currently resumed, which implies we aren't sleeping.
3211 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003212 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 }
3214
3215 if (andResume) {
3216 r.results = null;
3217 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003218 r.state = ActivityState.RESUMED;
3219 } else {
3220 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3221 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 }
3223
3224 return true;
3225 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003226
3227 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003228 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3229 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3230 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3231 final ActivityRecord r = activities.get(activityNdx);
3232 if (r.appToken == token) {
3233 return true;
3234 }
3235 if (r.fullscreen && !r.finishing) {
3236 return false;
3237 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003238 }
3239 }
3240 return true;
3241 }
3242
3243 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003244 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3245 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3246 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3247 final ActivityRecord r = activities.get(activityNdx);
3248 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003249 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003250 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003251 }
3252 }
3253 }
3254
3255 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3256 boolean didSomething = false;
3257 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003258 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3259 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3260 int numActivities = activities.size();
3261 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3262 ActivityRecord r = activities.get(activityNdx);
3263 final boolean samePackage = r.packageName.equals(name)
3264 || (name == null && r.userId == userId);
3265 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3266 && (samePackage || r.task == lastTask)
3267 && (r.app == null || evenPersistent || !r.app.persistent)) {
3268 if (!doit) {
3269 if (r.finishing) {
3270 // If this activity is just finishing, then it is not
3271 // interesting as far as something to stop.
3272 continue;
3273 }
3274 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003275 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003276 didSomething = true;
3277 Slog.i(TAG, " Force finishing activity " + r);
3278 if (samePackage) {
3279 if (r.app != null) {
3280 r.app.removed = true;
3281 }
3282 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003283 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003284 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003285 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3286 true)) {
3287 // r has been deleted from mActivities, accommodate.
3288 --numActivities;
3289 --activityNdx;
3290 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003291 }
3292 }
3293 }
3294 return didSomething;
3295 }
3296
3297 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003298 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003299 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003300 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3301 --maxNum, --taskNdx) {
3302 final TaskRecord task = mTaskHistory.get(taskNdx);
3303 ActivityRecord r = null;
3304 ActivityRecord top = null;
3305 int numActivities = 0;
3306 int numRunning = 0;
3307 final ArrayList<ActivityRecord> activities = task.mActivities;
3308 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3309 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003310
Craig Mautneraab647e2013-02-28 16:31:36 -08003311 // Initialize state for next task if needed.
3312 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3313 top = r;
3314 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003315 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003316
3317 // Add 'r' into the current task.
3318 numActivities++;
3319 if (r.app != null && r.app.thread != null) {
3320 numRunning++;
3321 }
3322
3323 if (localLOGV) Slog.v(
3324 TAG, r.intent.getComponent().flattenToShortString()
3325 + ": task=" + r.task);
3326 }
3327
3328 RunningTaskInfo ci = new RunningTaskInfo();
3329 ci.id = task.taskId;
3330 ci.baseActivity = r.intent.getComponent();
3331 ci.topActivity = top.intent.getComponent();
3332 if (top.thumbHolder != null) {
3333 ci.description = top.thumbHolder.lastDescription;
3334 }
3335 ci.numActivities = numActivities;
3336 ci.numRunning = numRunning;
3337 //System.out.println(
3338 // "#" + maxNum + ": " + " descr=" + ci.description);
3339 if (receiver != null) {
3340 if (localLOGV) Slog.v(
3341 TAG, "State=" + top.state + "Idle=" + top.idle
3342 + " app=" + top.app
3343 + " thr=" + (top.app != null ? top.app.thread : null));
3344 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3345 if (top.idle && top.app != null && top.app.thread != null) {
3346 topRecord = top;
3347 } else {
3348 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003349 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003350 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003351 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003352 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003353 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003354 }
3355 return topRecord;
3356 }
3357
3358 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003359 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003360 if (DEBUG_SWITCH) Slog.d(
3361 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003362 if (top >= 0) {
3363 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3364 int activityTop = activities.size() - 1;
3365 if (activityTop > 0) {
3366 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3367 "unhandled-back", true);
3368 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003369 }
3370 }
3371
Craig Mautnere79d42682013-04-01 19:01:53 -07003372 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003373 if (!containsApp(app)) {
3374 return;
3375 }
3376 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003377 if (mPausingActivity != null && mPausingActivity.app == app) {
3378 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3379 "App died while pausing: " + mPausingActivity);
3380 mPausingActivity = null;
3381 }
3382 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3383 mLastPausedActivity = null;
3384 }
3385
3386 // Remove this application's activities from active lists.
3387 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3388
3389 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003390 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003391 if (stack == null) {
3392 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003393 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003394 // If there was nothing to resume, and we are not already
3395 // restarting this process, but there is a visible activity that
3396 // is hosted by the process... then make sure all visible
3397 // activities are running, taking care of restarting this
3398 // process.
3399 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003400 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003401 }
3402 }
3403 }
3404 }
3405
Craig Mautnercae015f2013-02-08 14:31:27 -08003406 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003407 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3408 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3409 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3410 final ActivityRecord r = activities.get(activityNdx);
3411 if (r.app == app) {
3412 Slog.w(TAG, " Force finishing activity "
3413 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003414 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003415 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003416 }
3417 }
3418 }
3419
3420 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3421 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003422 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3423 final TaskRecord task = mTaskHistory.get(taskNdx);
3424 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003425 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003426 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3427 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003428 }
3429
3430 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3431 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3432
3433 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003434 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3435 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003436 }
3437 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003438 final int top = mTaskHistory.size() - 1;
3439 if (top >= 0) {
3440 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3441 int listTop = list.size() - 1;
3442 if (listTop >= 0) {
3443 activities.add(list.get(listTop));
3444 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003445 }
3446 } else {
3447 ItemMatcher matcher = new ItemMatcher();
3448 matcher.build(name);
3449
Craig Mautneraab647e2013-02-28 16:31:36 -08003450 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3451 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3452 if (matcher.match(r1, r1.intent.getComponent())) {
3453 activities.add(r1);
3454 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003455 }
3456 }
3457 }
3458
3459 return activities;
3460 }
3461
3462 ActivityRecord restartPackage(String packageName) {
3463 ActivityRecord starting = topRunningActivityLocked(null);
3464
3465 // All activities that came from the package must be
3466 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003467 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3468 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3469 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3470 final ActivityRecord a = activities.get(activityNdx);
3471 if (a.info.packageName.equals(packageName)) {
3472 a.forceNewConfig = true;
3473 if (starting != null && a == starting && a.visible) {
3474 a.startFreezingScreenLocked(starting.app,
3475 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3476 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003477 }
3478 }
3479 }
3480
3481 return starting;
3482 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003483
Craig Mautnerde4ef022013-04-07 19:01:33 -07003484 boolean removeTask(TaskRecord task) {
3485 mTaskHistory.remove(task);
3486 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003487 }
3488
Craig Mautnerde4ef022013-04-07 19:01:33 -07003489 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003490 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003491 if (toTop) {
3492 mTaskHistory.add(task);
3493 } else {
3494 mTaskHistory.add(0, task);
3495 }
3496 return task;
3497 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003498
3499 ArrayList<TaskRecord> getAllTasks() {
3500 return new ArrayList<TaskRecord>(mTaskHistory);
3501 }
3502
3503 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003504 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003505 if (task == null) {
3506 return;
3507 }
3508 task.stack.mTaskHistory.remove(task);
3509 task.stack = this;
3510 if (toTop) {
3511 mTaskHistory.add(task);
3512 } else {
3513 mTaskHistory.add(0, task);
3514 }
3515 }
3516
3517 public int getStackId() {
3518 return mStackId;
3519 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003520
3521 @Override
3522 public String toString() {
3523 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3524 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003525}