blob: 3557e9069492348b95a31a81c7114bda9d78f13f [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautnerde4ef022013-04-07 19:01:33 -070019import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
20
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070021import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080022import com.android.internal.util.Objects;
Craig Mautnercae015f2013-02-08 14:31:27 -080023import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080024import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080025import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070026import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070027
28import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070029import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070030import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070031import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080032import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080033import android.app.IThumbnailReceiver;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070034import android.app.IThumbnailRetriever;
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
Amith Yamasani742a6712011-05-04 14:49:28 -0700237 private int mCurrentUser;
238
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;
248 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
249 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
250 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
251 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700252
253 static class ScheduleDestroyArgs {
254 final ProcessRecord mOwner;
255 final boolean mOomAdj;
256 final String mReason;
257 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
258 mOwner = owner;
259 mOomAdj = oomAdj;
260 mReason = reason;
261 }
262 }
263
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 final Handler mHandler;
265
266 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267 //public Handler() {
268 // if (localLOGV) Slog.v(TAG, "Handler started!");
269 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900270 public ActivityStackHandler(Looper looper) {
271 super(looper);
272 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 public void handleMessage(Message msg) {
276 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800277 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700278 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700279 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700280 Slog.w(TAG, "Sleep timeout! Sleeping now.");
281 mSleepTimeout = true;
282 checkReadyForSleepLocked();
283 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800284 }
285 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800287 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 // We don't at this point know if the activity is fullscreen,
289 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700291 synchronized (mService) {
292 if (r.app != null) {
293 mService.logAppTooSlow(r.app, r.pauseTime,
294 "pausing " + r);
295 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700296
Craig Mautnerd2328952013-03-05 12:46:26 -0800297 activityPausedLocked(r != null ? r.appToken : null, true);
298 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700299 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700300 case LAUNCH_TICK_MSG: {
301 ActivityRecord r = (ActivityRecord)msg.obj;
302 synchronized (mService) {
303 if (r.continueLaunchTickingLocked()) {
304 mService.logAppTooSlow(r.app, r.launchTickTime,
305 "launching " + r);
306 }
307 }
308 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700311 // We don't at this point know if the activity is fullscreen,
312 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800313 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800314 synchronized (mService) {
315 activityDestroyedLocked(r != null ? r.appToken : null);
316 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700317 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700318 case LAUNCH_TIMEOUT_MSG: {
319 if (mService.mDidDexOpt) {
320 mService.mDidDexOpt = false;
321 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
322 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
323 return;
324 }
325 synchronized (mService) {
326 if (mLaunchingActivity.isHeld()) {
327 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
328 mLaunchingActivity.release();
329 }
330 }
331 } break;
332 case RESUME_TOP_ACTIVITY_MSG: {
333 synchronized (mService) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700334 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 }
336 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700337 case STOP_TIMEOUT_MSG: {
338 ActivityRecord r = (ActivityRecord)msg.obj;
339 // We don't at this point know if the activity is fullscreen,
340 // so we need to be conservative and assume it isn't.
341 Slog.w(TAG, "Activity stop timeout for " + r);
342 synchronized (mService) {
343 if (r.isInHistory()) {
344 activityStoppedLocked(r, null, null, null);
345 }
346 }
347 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700348 case DESTROY_ACTIVITIES_MSG: {
349 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
350 synchronized (mService) {
351 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
352 }
353 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700354 }
355 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800356 }
357
Craig Mautner000f0022013-02-26 15:04:29 -0800358 private int numActivities() {
359 int count = 0;
360 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
361 count += mTaskHistory.get(taskNdx).mActivities.size();
362 }
363 return count;
364 }
365
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700366 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900367 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700368 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700369 mWindowManager = service.mWindowManager;
370 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700371 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 PowerManager pm =
373 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
374 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700375 mLaunchingActivity =
376 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700377 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800378 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700379 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 }
Craig Mautner5962b122012-10-05 14:45:52 -0700381
382 private boolean okToShow(ActivityRecord r) {
383 return r.userId == mCurrentUser
384 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
385 }
386
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800388 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
389 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800390 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800391 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
392 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800393 if (!r.finishing && r != notTop && okToShow(r)) {
394 return r;
395 }
396 }
397 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700398 return null;
399 }
400
401 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800402 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
403 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800404 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800405 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
406 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800407 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
408 return r;
409 }
410 }
411 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700412 return null;
413 }
414
415 /**
416 * This is a simplified version of topRunningActivityLocked that provides a number of
417 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800418 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700419 * @param token If non-null, any history records matching this token will be skipped.
420 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800421 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700422 * @return Returns the HistoryRecord of the next activity on the stack.
423 */
424 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800425 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
426 TaskRecord task = mTaskHistory.get(taskNdx);
427 if (task.taskId == taskId) {
428 continue;
429 }
430 ArrayList<ActivityRecord> activities = task.mActivities;
431 for (int i = activities.size() - 1; i >= 0; --i) {
432 final ActivityRecord r = activities.get(i);
433 // Note: the taskId check depends on real taskId fields being non-zero
434 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
435 return r;
436 }
437 }
438 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700439 return null;
440 }
441
Craig Mautner8849a5e2013-04-02 16:41:03 -0700442 final ActivityRecord topActivity() {
443 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700444 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
447 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700448 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700449 }
450 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700451 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700452 }
453
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 TaskRecord taskForIdLocked(int id) {
455 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
456 final TaskRecord task = mTaskHistory.get(taskNdx);
457 if (task.taskId == id) {
458 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800459 }
460 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700461 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 }
463
Craig Mautnerd2328952013-03-05 12:46:26 -0800464 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800465 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800466 if (r != null) {
467 final TaskRecord task = r.task;
468 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
469 if (task.stack != this) Slog.w(TAG,
470 "Illegal state! task does not point to stack it is in.");
471 return r;
472 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800473 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800474 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800475 }
476
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700477 boolean containsApp(ProcessRecord app) {
478 if (app == null) {
479 return false;
480 }
481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
482 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
483 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
484 final ActivityRecord r = activities.get(activityNdx);
485 if (r.finishing) {
486 continue;
487 }
488 if (r.app == app) {
489 return true;
490 }
491 }
492 }
493 return false;
494 }
495
Craig Mautner2420ead2013-04-01 17:13:20 -0700496 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700497 final boolean hadit = mLRUActivities.remove(r);
498 mLRUActivities.add(r);
499 return hadit;
500 }
501
Craig Mautnerde4ef022013-04-07 19:01:33 -0700502 final boolean isHomeStack() {
503 return mStackId == HOME_STACK_ID;
504 }
505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700506 /**
507 * Returns the top activity in any existing task matching the given
508 * Intent. Returns null if no such task is found.
509 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700510 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700511 ComponentName cls = intent.getComponent();
512 if (info.targetActivity != null) {
513 cls = new ComponentName(info.packageName, info.targetActivity);
514 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700515 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800516
517 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
518 final TaskRecord task = mTaskHistory.get(taskNdx);
519 final ActivityRecord r = task.getTopActivity();
520 if (r == null || r.finishing || r.userId != userId ||
521 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
522 continue;
523 }
524
525 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
526 // + "/aff=" + r.task.affinity + " to new cls="
527 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
528 if (task.affinity != null) {
529 if (task.affinity.equals(info.taskAffinity)) {
530 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700531 return r;
532 }
Craig Mautner000f0022013-02-26 15:04:29 -0800533 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
534 //Slog.i(TAG, "Found matching class!");
535 //dump();
536 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
537 return r;
538 } else if (task.affinityIntent != null
539 && task.affinityIntent.getComponent().equals(cls)) {
540 //Slog.i(TAG, "Found matching class!");
541 //dump();
542 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
543 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700544 }
545 }
546
547 return null;
548 }
549
550 /**
551 * Returns the first activity (starting from the top of the stack) that
552 * is the same as the given activity. Returns null if no such activity
553 * is found.
554 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700555 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700556 ComponentName cls = intent.getComponent();
557 if (info.targetActivity != null) {
558 cls = new ComponentName(info.packageName, info.targetActivity);
559 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700560 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700561
Craig Mautner000f0022013-02-26 15:04:29 -0800562 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
563 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
564 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
565 ActivityRecord r = activities.get(activityNdx);
566 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700567 //Slog.i(TAG, "Found matching class!");
568 //dump();
569 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
570 return r;
571 }
572 }
573 }
574
575 return null;
576 }
577
Amith Yamasani742a6712011-05-04 14:49:28 -0700578 /*
579 * Move the activities around in the stack to bring a user to the foreground.
580 * @return whether there are any activities for the specified user.
581 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700582 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800583 if (VALIDATE_TOKENS) {
584 validateAppTokensLocked();
585 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800586 if (mCurrentUser == userId) {
587 return true;
588 }
589 mCurrentUser = userId;
590
591 // Move userId's tasks to the top.
592 boolean haveActivities = false;
593 TaskRecord task = null;
594 int index = mTaskHistory.size();
595 for (int i = 0; i < index; ++i) {
596 task = mTaskHistory.get(i);
597 if (task.userId == userId) {
598 haveActivities = true;
599 mTaskHistory.remove(i);
600 mTaskHistory.add(task);
601 --index;
602 }
603 }
604
605 // task is now the original topmost TaskRecord. Transition from the old top to the new top.
606 ActivityRecord top = task != null ? task.getTopActivity() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800607 resumeTopActivityLocked(top);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700608 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700609 }
610
Craig Mautner2420ead2013-04-01 17:13:20 -0700611 void minimalResumeActivityLocked(ActivityRecord r) {
612 r.state = ActivityState.RESUMED;
613 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
614 + " (starting new instance)");
615 r.stopped = false;
616 mResumedActivity = r;
617 r.task.touchActiveTime();
618 mService.addRecentTaskLocked(r.task);
619 completeResumeLocked(r);
620 checkReadyForSleepLocked();
621 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700622 }
623
Craig Mautnere79d42682013-04-01 19:01:53 -0700624 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700625 if (r.launchTime == 0) {
626 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700627 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700628 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 }
630 } else if (mInitialStartTime == 0) {
631 mInitialStartTime = SystemClock.uptimeMillis();
632 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700633 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800634
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800635 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700636 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700637 if (!mGoingToSleep.isHeld()) {
638 mGoingToSleep.acquire();
639 if (mLaunchingActivity.isHeld()) {
640 mLaunchingActivity.release();
641 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
642 }
643 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800644 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
645 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
646 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
647 checkReadyForSleepLocked();
648 }
649 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700650
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800651 void awakeFromSleepingLocked() {
652 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
653 mSleepTimeout = false;
654 if (mGoingToSleep.isHeld()) {
655 mGoingToSleep.release();
656 }
657 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800658 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
659 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
660 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700661 // TODO: Skip if finishing?
Craig Mautnerd44711d2013-02-23 11:24:36 -0800662 activities.get(activityNdx).setSleeping(false);
663 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800664 }
665 mGoingToSleepActivities.clear();
666 }
667
668 void activitySleptLocked(ActivityRecord r) {
669 mGoingToSleepActivities.remove(r);
670 checkReadyForSleepLocked();
671 }
672
Craig Mautnere79d42682013-04-01 19:01:53 -0700673 // Checked.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800674 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700675 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800676 // Do not care.
677 return;
678 }
679
680 if (!mSleepTimeout) {
681 if (mResumedActivity != null) {
682 // Still have something resumed; can't sleep until it is paused.
683 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700684 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
685 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800686 return;
687 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800688 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800689 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800690 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800691 return;
692 }
693
Craig Mautnerde4ef022013-04-07 19:01:33 -0700694 if (mStackSupervisor.mStoppingActivities.size() > 0) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800695 // Still need to tell some activities to stop; can't sleep yet.
696 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
Craig Mautnerde4ef022013-04-07 19:01:33 -0700697 + mStackSupervisor.mStoppingActivities.size() + " activities");
Craig Mautnerf3333272013-04-22 10:55:53 -0700698 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800699 return;
700 }
701
702 ensureActivitiesVisibleLocked(null, 0);
703
704 // Make sure any stopped but visible activities are now sleeping.
705 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800706 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
707 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
708 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
709 final ActivityRecord r = activities.get(activityNdx);
710 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
711 r.setSleeping(true);
712 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800713 }
714 }
715
716 if (mGoingToSleepActivities.size() > 0) {
717 // Still need to tell some activities to sleep; can't sleep yet.
718 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
719 + mGoingToSleepActivities.size() + " activities");
720 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700721 }
722 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800723
724 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
725
726 if (mGoingToSleep.isHeld()) {
727 mGoingToSleep.release();
728 }
729 if (mService.mShuttingDown) {
730 mService.notifyAll();
731 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700732 }
Craig Mautner59c00972012-07-30 12:10:24 -0700733
Dianne Hackbornd2835932010-12-13 16:28:46 -0800734 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800735 if (who.noDisplay) {
736 return null;
737 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800738
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800739 Resources res = mService.mContext.getResources();
740 int w = mThumbnailWidth;
741 int h = mThumbnailHeight;
742 if (w < 0) {
743 mThumbnailWidth = w =
744 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
745 mThumbnailHeight = h =
746 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
747 }
748
749 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800750 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
751 || mLastScreenshotBitmap.getWidth() != w
752 || mLastScreenshotBitmap.getHeight() != h) {
753 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700754 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800755 who.appToken, Display.DEFAULT_DISPLAY, w, h);
756 }
757 if (mLastScreenshotBitmap != null) {
758 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
759 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800760 }
761 return null;
762 }
763
Craig Mautnercf910b02013-04-23 11:23:27 -0700764 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800765 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800766 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700767 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800768 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700769 ActivityRecord prev = mResumedActivity;
770 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700771 Slog.e(TAG, "Trying to pause when nothing is resumed",
772 new RuntimeException("here").fillInStackTrace());
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700773 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700774 return;
775 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700776 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
777 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700778 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800779 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700780 mLastPausedActivity = prev;
781 prev.state = ActivityState.PAUSING;
782 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700783 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700784
785 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800786
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700787 if (prev.app != null && prev.app.thread != null) {
788 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
789 try {
790 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700791 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700792 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800793 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
794 userLeaving, prev.configChangeFlags);
Craig Mautner7d9eaa42013-04-19 13:57:33 -0700795 mService.updateUsageStats(prev, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700796 } catch (Exception e) {
797 // Ignore exception, if process died other code will cleanup.
798 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800799 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800 mLastPausedActivity = null;
801 }
802 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800803 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700804 mLastPausedActivity = null;
805 }
806
807 // If we are not going to sleep, we want to ensure the device is
808 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700809 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 mLaunchingActivity.acquire();
811 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
812 // To be safe, don't allow the wake lock to be held for too long.
813 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
814 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
815 }
816 }
817
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800818 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 // Have the window manager pause its key dispatching until the new
820 // activity has started. If we're pausing the activity just because
821 // the screen is being turned off and the UI is sleeping, don't interrupt
822 // key dispatch; the same activity will pick it up again on wakeup.
823 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800824 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700825 } else {
826 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
827 }
828
829 // Schedule a pause timeout in case the app doesn't respond.
830 // We don't give it much time because this directly impacts the
831 // responsiveness seen by the user.
832 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
833 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700834 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
836 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
837 } else {
838 // This activity failed to schedule the
839 // pause, so just treat it as being paused now.
840 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700841 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842 }
843 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700844
Craig Mautnerd2328952013-03-05 12:46:26 -0800845 final void activityResumedLocked(IBinder token) {
846 final ActivityRecord r = ActivityRecord.forToken(token);
847 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
848 r.icicle = null;
849 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700850 }
851
Craig Mautnerd2328952013-03-05 12:46:26 -0800852 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800854 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700855
Craig Mautnerd2328952013-03-05 12:46:26 -0800856 final ActivityRecord r = isInStackLocked(token);
857 if (r != null) {
858 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
859 if (mPausingActivity == r) {
860 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
861 + (timeout ? " (due to timeout)" : " (pause complete)"));
862 r.state = ActivityState.PAUSED;
863 completePauseLocked();
864 } else {
865 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
866 r.userId, System.identityHashCode(r), r.shortComponentName,
867 mPausingActivity != null
868 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 }
870 }
871 }
872
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700873 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
874 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700875 if (r.state != ActivityState.STOPPING) {
876 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
877 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
878 return;
879 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700880 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700881 if (icicle != null) {
882 // If icicle is null, this is happening due to a timeout, so we
883 // haven't really saved the state.
884 r.icicle = icicle;
885 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800886 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700887 r.updateThumbnail(thumbnail, description);
888 }
889 if (!r.stopped) {
890 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
891 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
892 r.stopped = true;
893 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700894 if (r.finishing) {
895 r.clearOptionsLocked();
896 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700897 if (r.configDestroy) {
898 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700899 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700900 } else {
901 // Now that this process has stopped, we may want to consider
902 // it to be the previous app to try to keep around in case
903 // the user wants to return to it.
904 ProcessRecord fgApp = null;
905 if (mResumedActivity != null) {
906 fgApp = mResumedActivity.app;
907 } else if (mPausingActivity != null) {
908 fgApp = mPausingActivity.app;
909 }
910 if (r.app != null && fgApp != null && r.app != fgApp
911 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
912 && r.app != mService.mHomeProcess) {
913 mService.mPreviousProcess = r.app;
914 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
915 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800916 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700917 }
918 }
919 }
920
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800921 private final void completePauseLocked() {
922 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800924
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800925 if (prev != null) {
926 if (prev.finishing) {
927 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700928 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800929 } else if (prev.app != null) {
930 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
931 if (prev.waitingVisible) {
932 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700933 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800934 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
935 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800936 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800937 if (prev.configDestroy) {
938 // The previous is being paused because the configuration
939 // is changing, which means it is actually stopping...
940 // To juggle the fact that we are also starting a new
941 // instance right now, we need to first completely stop
942 // the current instance before starting the new one.
943 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
944 destroyActivityLocked(prev, true, false, "pause-config");
945 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700946 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700947 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
948 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800949 // If we already have a few activities waiting to stop,
950 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700951 // them out. Or if r is the last of activity of the last task the stack
952 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800953 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700954 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800955 } else {
956 checkReadyForSleepLocked();
957 }
958 }
959 } else {
960 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
961 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800962 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800963 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700964 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800965
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700966 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700967 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700968 topStack.resumeTopActivityLocked(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800969 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800970 checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700971 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700972 if (top == null || (prev != null && top != prev)) {
973 // If there are no more activities available to run,
974 // do resume anyway to start something. Also if the top
975 // activity on the stack is not the just paused activity,
976 // we need to go ahead and resume it to ensure we complete
977 // an in-flight app switch.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700978 topStack.resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700979 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700980 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800981
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800982 if (prev != null) {
983 prev.resumeKeyDispatchingLocked();
984 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700985
986 if (prev.app != null && prev.cpuTimeAtResume > 0
987 && mService.mBatteryStatsService.isOnBattery()) {
988 long diff = 0;
989 synchronized (mService.mProcessStatsThread) {
990 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
991 - prev.cpuTimeAtResume;
992 }
993 if (diff > 0) {
994 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
995 synchronized (bsi) {
996 BatteryStatsImpl.Uid.Proc ps =
997 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
998 prev.info.packageName);
999 if (ps != null) {
1000 ps.addForegroundTimeLocked(diff);
1001 }
1002 }
1003 }
1004 }
1005 prev.cpuTimeAtResume = 0; // reset it
1006 }
1007
Craig Mautnere79d42682013-04-01 19:01:53 -07001008 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001009 /**
1010 * Once we know that we have asked an application to put an activity in
1011 * the resumed state (either by launching it or explicitly telling it),
1012 * this function updates the rest of our state to match that fact.
1013 */
1014 private final void completeResumeLocked(ActivityRecord next) {
1015 next.idle = false;
1016 next.results = null;
1017 next.newIntents = null;
1018
1019 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001020 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001021
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001022 mStackSupervisor.reportResumedActivityLocked(next);
1023
1024 next.resumeKeyDispatchingLocked();
1025 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026
1027 // Mark the point when the activity is resuming
1028 // TODO: To be more accurate, the mark should be before the onCreate,
1029 // not after the onResume. But for subsequent starts, onResume is fine.
1030 if (next.app != null) {
1031 synchronized (mService.mProcessStatsThread) {
1032 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1033 }
1034 } else {
1035 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1036 }
1037 }
1038
Craig Mautnere79d42682013-04-01 19:01:53 -07001039 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040 /**
1041 * Make sure that all activities that need to be visible (that is, they
1042 * currently can be seen by the user) actually are.
1043 */
1044 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1045 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1046 if (DEBUG_VISBILITY) Slog.v(
1047 TAG, "ensureActivitiesVisible behind " + top
1048 + " configChanges=0x" + Integer.toHexString(configChanges));
1049
1050 // If the top activity is not fullscreen, then we need to
1051 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001052 boolean aboveTop = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001053 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this);
1054 int taskNdx;
1055 for (taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001056 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1057 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1058 final ActivityRecord r = activities.get(activityNdx);
1059 if (r.finishing) {
1060 continue;
1061 }
1062 if (aboveTop && r != top) {
1063 continue;
1064 }
1065 aboveTop = false;
1066 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001067 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001068 TAG, "Make visible? " + r + " finishing=" + r.finishing
1069 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001070
Craig Mautnerd44711d2013-02-23 11:24:36 -08001071 final boolean doThisProcess = onlyThisProcess == null
1072 || onlyThisProcess.equals(r.processName);
1073
1074 // First: if this is not the current activity being started, make
1075 // sure it matches the current configuration.
1076 if (r != starting && doThisProcess) {
1077 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001079
1080 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001081 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001082 // This activity needs to be visible, but isn't even
1083 // running... get it started, but don't resume it
1084 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001085 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001086 if (r != starting) {
1087 r.startFreezingScreenLocked(r.app, configChanges);
1088 }
1089 if (!r.visible) {
1090 if (DEBUG_VISBILITY) Slog.v(
1091 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001092 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001093 }
1094 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001095 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001096 }
1097 }
1098
1099 } else if (r.visible) {
1100 // If this activity is already visible, then there is nothing
1101 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001102 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001103 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001104
1105 } else if (onlyThisProcess == null) {
1106 // This activity is not currently visible, but is running.
1107 // Tell it to become visible.
1108 r.visible = true;
1109 if (r.state != ActivityState.RESUMED && r != starting) {
1110 // If this activity is paused, tell it
1111 // to now show its window.
1112 if (DEBUG_VISBILITY) Slog.v(
1113 TAG, "Making visible and scheduling visibility: " + r);
1114 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001115 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001116 r.sleeping = false;
1117 r.app.pendingUiClean = true;
1118 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1119 r.stopFreezingScreenLocked(false);
1120 } catch (Exception e) {
1121 // Just skip on any failure; we'll make it
1122 // visible when it next restarts.
1123 Slog.w(TAG, "Exception thrown making visibile: "
1124 + r.intent.getComponent(), e);
1125 }
1126 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001128
Craig Mautnerd44711d2013-02-23 11:24:36 -08001129 // Aggregate current change flags.
1130 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131
Craig Mautnerd44711d2013-02-23 11:24:36 -08001132 if (r.fullscreen) {
1133 // At this point, nothing else needs to be shown
1134 if (DEBUG_VISBILITY) Slog.v(
1135 TAG, "Stopping: fullscreen at " + r);
1136 behindFullscreen = true;
1137 }
1138 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001139 if (DEBUG_VISBILITY) Slog.v(
1140 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1141 + " state=" + r.state
1142 + " behindFullscreen=" + behindFullscreen);
1143 // Now for any activities that aren't visible to the user, make
1144 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001145 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001146 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001147 r.visible = false;
1148 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001149 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001150 if ((r.state == ActivityState.STOPPING
1151 || r.state == ActivityState.STOPPED)
1152 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001153 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001154 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001155 }
1156 } catch (Exception e) {
1157 // Just skip on any failure; we'll make it
1158 // visible when it next restarts.
1159 Slog.w(TAG, "Exception thrown making hidden: "
1160 + r.intent.getComponent(), e);
1161 }
1162 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001163 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001164 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001165 }
1166 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001167 }
1168 }
1169
Craig Mautnere79d42682013-04-01 19:01:53 -07001170 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001171 /**
1172 * Version of ensureActivitiesVisible that can easily be called anywhere.
1173 */
1174 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1175 int configChanges) {
1176 ActivityRecord r = topRunningActivityLocked(null);
1177 if (r != null) {
1178 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1179 }
1180 }
Craig Mautner58547802013-03-05 08:23:53 -08001181
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001182 /**
1183 * Ensure that the top activity in the stack is resumed.
1184 *
1185 * @param prev The previously resumed activity, for when in the process
1186 * of pausing; can be null to call from elsewhere.
1187 *
1188 * @return Returns true if something is being resumed, or false if
1189 * nothing happened.
1190 */
1191 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001192 return resumeTopActivityLocked(prev, null);
1193 }
1194
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07001195 // Checked.
Dianne Hackborn84375872012-06-01 19:03:50 -07001196 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197 // Find the first activity that is not finishing.
1198 ActivityRecord next = topRunningActivityLocked(null);
1199
1200 // Remember how we'll process this pause/resume situation, and ensure
1201 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001202 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1203 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001204
1205 if (next == null) {
1206 // There are no more activities! Let's just start up the
1207 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001208 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001209 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001210 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211 }
1212
1213 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001214
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001216 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1217 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 // Make sure we have executed any pending transitions, since there
1219 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001220 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001221 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001222 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001223 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001224 return false;
1225 }
1226
Craig Mautnerde4ef022013-04-07 19:01:33 -07001227 if (prev != null && prev.mLaunchHomeTaskNext && prev.finishing &&
1228 prev.task.getTopActivity() == null) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001229 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001230 return mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001231 }
1232
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001233 // If we are sleeping, and there is no resumed activity, and the top
1234 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001235 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001236 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001237 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001238 // Make sure we have executed any pending transitions, since there
1239 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001240 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001241 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001242 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001243 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001244 return false;
1245 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001246
1247 // Make sure that the user who owns this activity is started. If not,
1248 // we will just leave it as is because someone should be bringing
1249 // another user's activities to the top of the stack.
1250 if (mService.mStartedUsers.get(next.userId) == null) {
1251 Slog.w(TAG, "Skipping resume of top activity " + next
1252 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001253 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001254 return false;
1255 }
1256
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001257 // The activity may be waiting for stop, but that is no longer
1258 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001259 mStackSupervisor.mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001260 mGoingToSleepActivities.remove(next);
1261 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001262 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001263
Dianne Hackborn84375872012-06-01 19:03:50 -07001264 next.updateOptionsLocked(options);
1265
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001266 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1267
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001268 // If we are currently pausing an activity, then don't do anything
1269 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001270 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1271 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Craig Mautnercf910b02013-04-23 11:23:27 -07001272 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001273 return false;
1274 }
1275
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001276 // Okay we are now going to start a switch, to 'next'. We may first
1277 // have to pause the current activity, but this is an important point
1278 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001279 // XXX "App Redirected" dialog is getting too many false positives
1280 // at this point, so turn off for now.
1281 if (false) {
1282 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1283 long now = SystemClock.uptimeMillis();
1284 final boolean inTime = mLastStartedActivity.startTime != 0
1285 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1286 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1287 final int nextUid = next.info.applicationInfo.uid;
1288 if (inTime && lastUid != nextUid
1289 && lastUid != next.launchedFromUid
1290 && mService.checkPermission(
1291 android.Manifest.permission.STOP_APP_SWITCHES,
1292 -1, next.launchedFromUid)
1293 != PackageManager.PERMISSION_GRANTED) {
1294 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1295 } else {
1296 next.startTime = now;
1297 mLastStartedActivity = next;
1298 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001299 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001300 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001301 mLastStartedActivity = next;
1302 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001303 }
Craig Mautner58547802013-03-05 08:23:53 -08001304
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001305 // We need to start pausing the current activity so the top one
1306 // can be resumed...
Craig Mautnercf910b02013-04-23 11:23:27 -07001307 if (mStackSupervisor.pauseBackStacks(userLeaving)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001308 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001309 // At this point we want to put the upcoming activity's process
1310 // at the top of the LRU list, since we know we will be needing it
1311 // very soon and it would be a waste to let it get killed if it
1312 // happens to be sitting towards the end.
1313 if (next.app != null && next.app.thread != null) {
1314 // No reason to do full oom adj update here; we'll let that
1315 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001316 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001317 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001318 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 return true;
1320 }
1321
Christopher Tated3f175c2012-06-14 14:16:54 -07001322 // If the most recent activity was noHistory but was only stopped rather
1323 // than stopped+finished because the device went to sleep, we need to make
1324 // sure to finish it as we're making a new activity topmost.
1325 final ActivityRecord last = mLastPausedActivity;
1326 if (mService.mSleeping && last != null && !last.finishing) {
1327 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1328 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1329 if (DEBUG_STATES) {
1330 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1331 }
1332 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001333 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001334 }
1335 }
1336
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001337 if (prev != null && prev != next) {
1338 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1339 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001340 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001341 if (DEBUG_SWITCH) Slog.v(
1342 TAG, "Resuming top, waiting visible to hide: " + prev);
1343 } else {
1344 // The next activity is already visible, so hide the previous
1345 // activity's windows right now so we can show the new one ASAP.
1346 // We only do this if the previous is finishing, which should mean
1347 // it is on top of the one being resumed so hiding it quickly
1348 // is good. Otherwise, we want to do the normal route of allowing
1349 // the resumed activity to be shown so we can decide if the
1350 // previous should actually be hidden depending on whether the
1351 // new one is found to be full-screen or not.
1352 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001353 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001354 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1355 + prev + ", waitingVisible="
1356 + (prev != null ? prev.waitingVisible : null)
1357 + ", nowVisible=" + next.nowVisible);
1358 } else {
1359 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1360 + prev + ", waitingVisible="
1361 + (prev != null ? prev.waitingVisible : null)
1362 + ", nowVisible=" + next.nowVisible);
1363 }
1364 }
1365 }
1366
Dianne Hackborne7f97212011-02-24 14:40:20 -08001367 // Launching this app's activity, make sure the app is no longer
1368 // considered stopped.
1369 try {
1370 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001371 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001372 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001373 } catch (IllegalArgumentException e) {
1374 Slog.w(TAG, "Failed trying to unstop package "
1375 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001376 }
1377
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001378 // We are starting up the next activity, so tell the window manager
1379 // that the previous one will be hidden soon. This way it can know
1380 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001381 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001382 if (prev != null) {
1383 if (prev.finishing) {
1384 if (DEBUG_TRANSITION) Slog.v(TAG,
1385 "Prepare close transition: prev=" + prev);
1386 if (mNoAnimActivities.contains(prev)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001387 noAnim = true;
1388 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001389 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001390 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001391 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1392 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001393 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001394 mWindowManager.setAppWillBeHidden(prev.appToken);
1395 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001396 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001397 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001399 noAnim = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001400 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001402 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001403 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1404 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001405 }
1406 }
1407 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001408 mWindowManager.setAppWillBeHidden(prev.appToken);
1409 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001410 }
Craig Mautner967212c2013-04-13 21:10:58 -07001411 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001412 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001414 noAnim = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001415 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001416 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001417 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001418 }
1419 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001420 if (!noAnim) {
1421 next.applyOptionsLocked();
1422 } else {
1423 next.clearOptionsLocked();
1424 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001425
Craig Mautnercf910b02013-04-23 11:23:27 -07001426 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001427 if (next.app != null && next.app.thread != null) {
1428 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1429
1430 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001431 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001432
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001433 // schedule launch ticks to collect information about slow apps.
1434 next.startLaunchTickingLocked();
1435
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001436 ActivityRecord lastResumedActivity =
1437 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001438 ActivityState lastState = next.state;
1439
1440 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001441
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001442 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001443 next.state = ActivityState.RESUMED;
1444 mResumedActivity = next;
1445 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001446 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001447 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001448 updateLRUListLocked(next);
1449
1450 // Have the window manager re-evaluate the orientation of
1451 // the screen based on the new activity order.
1452 boolean updated = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001453 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001454 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001455 mService.mConfiguration,
1456 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1457 if (config != null) {
1458 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001460 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001462
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 if (!updated) {
1464 // The configuration update wasn't able to keep the existing
1465 // instance of the activity, and instead started a new one.
1466 // We should be all done, but let's just make sure our activity
1467 // is still at the top and schedule another run if something
1468 // weird happened.
1469 ActivityRecord nextNext = topRunningActivityLocked(null);
1470 if (DEBUG_SWITCH) Slog.i(TAG,
1471 "Activity config changed during resume: " + next
1472 + ", new next: " + nextNext);
1473 if (nextNext != next) {
1474 // Do over!
1475 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1476 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001477 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001478 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001479 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001480 return true;
1481 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001482 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001483 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484 }
Craig Mautner58547802013-03-05 08:23:53 -08001485
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 try {
1487 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001488 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001489 if (a != null) {
1490 final int N = a.size();
1491 if (!next.finishing && N > 0) {
1492 if (DEBUG_RESULTS) Slog.v(
1493 TAG, "Delivering results to " + next
1494 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001495 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 }
1497 }
1498
1499 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001500 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 }
1502
1503 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001504 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001505 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001506
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001507 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001508 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001509 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001510 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001512
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001513 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001514
1515 } catch (Exception e) {
1516 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001517 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1518 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001520 if (lastStack != null) {
1521 lastStack.mResumedActivity = lastResumedActivity;
1522 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523 Slog.i(TAG, "Restarting because process died: " + next);
1524 if (!next.hasBeenLaunched) {
1525 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001526 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1527 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001528 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001529 next.appToken, next.packageName, next.theme,
1530 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
1531 next.nonLocalizedLabel, next.labelRes, next.icon, next.windowFlags,
1532 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001534 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001535 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 return true;
1537 }
1538
1539 // From this point on, if something goes wrong there is no way
1540 // to recover the activity.
1541 try {
1542 next.visible = true;
1543 completeResumeLocked(next);
1544 } catch (Exception e) {
1545 // If any exception gets thrown, toss away this
1546 // activity and try the next one.
1547 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001548 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001549 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001550 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551 return true;
1552 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553 next.stopped = false;
1554
1555 } else {
1556 // Whoops, need to restart this activity!
1557 if (!next.hasBeenLaunched) {
1558 next.hasBeenLaunched = true;
1559 } else {
1560 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001561 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001562 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001563 mService.compatibilityInfoForPackageLocked(
1564 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001566 next.labelRes, next.icon, next.windowFlags,
1567 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 }
1569 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1570 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001571 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572 }
1573
Craig Mautnercf910b02013-04-23 11:23:27 -07001574 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 return true;
1576 }
1577
Craig Mautner11bf9a52013-02-19 14:08:51 -08001578
Craig Mautner8849a5e2013-04-02 16:41:03 -07001579 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001580 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001581 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001582 TaskRecord rTask = r.task;
1583 final int taskId = rTask.taskId;
1584 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001585 // Last activity in task had been removed or ActivityManagerService is reusing task.
1586 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001587 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001588 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001589 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001590 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001591 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001592 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 if (!newTask) {
1594 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001595 boolean startIt = true;
1596 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1597 task = mTaskHistory.get(taskNdx);
1598 if (task == r.task) {
1599 // Here it is! Now, if this is not yet visible to the
1600 // user, then just add it without starting; it will
1601 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001602 if (!startIt) {
1603 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1604 + task, new RuntimeException("here").fillInStackTrace());
1605 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001606 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001607 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1608 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001609 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001610 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001611 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001613 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001614 return;
1615 }
1616 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001617 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 startIt = false;
1619 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 }
1621 }
1622
1623 // Place a new activity at top of stack, so it is next to interact
1624 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001625
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 // If we are not placing the new activity frontmost, we do not want
1627 // to deliver the onUserLeaving callback to the actual frontmost
1628 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001629 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001630 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001631 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1632 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 }
Craig Mautner70a86932013-02-28 22:37:44 -08001634
1635 task = r.task;
1636
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001638 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001639 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001640 task.addActivityToTop(r);
1641
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001642 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001643 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001644 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001645 // We want to show the starting preview window if we are
1646 // switching to a new task, or the next activity's process is
1647 // not currently running.
1648 boolean showStartingIcon = newTask;
1649 ProcessRecord proc = r.app;
1650 if (proc == null) {
1651 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1652 }
1653 if (proc == null || proc.thread == null) {
1654 showStartingIcon = true;
1655 }
1656 if (DEBUG_TRANSITION) Slog.v(TAG,
1657 "Prepare open transition: starting " + r);
1658 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001659 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001662 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001663 ? AppTransition.TRANSIT_TASK_OPEN
1664 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 mNoAnimActivities.remove(r);
1666 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001667 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001668 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001669 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001670 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 boolean doShow = true;
1672 if (newTask) {
1673 // Even though this activity is starting fresh, we still need
1674 // to reset it to make sure we apply affinities to move any
1675 // existing activities from other tasks in to it.
1676 // If the caller has requested that the target task be
1677 // reset, then do so.
1678 if ((r.intent.getFlags()
1679 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1680 resetTaskIfNeededLocked(r, r);
1681 doShow = topRunningNonDelayedActivityLocked(null) == r;
1682 }
1683 }
1684 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1685 // Figure out if we are transitioning from another activity that is
1686 // "has the same starting icon" as the next one. This allows the
1687 // window manager to keep the previous window it had previously
1688 // created, if it still had one.
1689 ActivityRecord prev = mResumedActivity;
1690 if (prev != null) {
1691 // We don't want to reuse the previous starting preview if:
1692 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001693 if (prev.task != r.task) {
1694 prev = null;
1695 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001697 else if (prev.nowVisible) {
1698 prev = null;
1699 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001701 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001702 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001703 mService.compatibilityInfoForPackageLocked(
1704 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001705 r.labelRes, r.icon, r.windowFlags,
1706 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 }
1708 } else {
1709 // If this is the first activity, don't do any fancy animations,
1710 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001711 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001712 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001713 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001714 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 }
1716 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001717 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001718 }
1719
1720 if (doResume) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001721 mStackSupervisor.resumeTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 }
1723 }
1724
Dianne Hackbornbe707852011-11-11 14:32:10 -08001725 final void validateAppTokensLocked() {
1726 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001727 mValidateAppTokens.ensureCapacity(numActivities());
1728 final int numTasks = mTaskHistory.size();
1729 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1730 TaskRecord task = mTaskHistory.get(taskNdx);
1731 final ArrayList<ActivityRecord> activities = task.mActivities;
1732 if (activities.size() == 0) {
1733 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001734 }
Craig Mautner000f0022013-02-26 15:04:29 -08001735 TaskGroup group = new TaskGroup();
1736 group.taskId = task.taskId;
1737 mValidateAppTokens.add(group);
1738 final int numActivities = activities.size();
1739 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1740 final ActivityRecord r = activities.get(activityNdx);
1741 group.tokens.add(r.appToken);
1742 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001743 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001744 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001745 }
1746
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 /**
1748 * Perform a reset of the given task, if needed as part of launching it.
1749 * Returns the new HistoryRecord at the top of the task.
1750 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001751 /**
1752 * Helper method for #resetTaskIfNeededLocked.
1753 * We are inside of the task being reset... we'll either finish this activity, push it out
1754 * for another task, or leave it as-is.
1755 * @param task The task containing the Activity (taskTop) that might be reset.
1756 * @param forceReset
1757 * @return An ActivityOptions that needs to be processed.
1758 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001759 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001760 ActivityOptions topOptions = null;
1761
1762 int replyChainEnd = -1;
1763 boolean canMoveOptions = true;
1764
1765 // We only do this for activities that are not the root of the task (since if we finish
1766 // the root, we may no longer have the task!).
1767 final ArrayList<ActivityRecord> activities = task.mActivities;
1768 final int numActivities = activities.size();
1769 for (int i = numActivities - 1; i > 0; --i ) {
1770 ActivityRecord target = activities.get(i);
1771
1772 final int flags = target.info.flags;
1773 final boolean finishOnTaskLaunch =
1774 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1775 final boolean allowTaskReparenting =
1776 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1777 final boolean clearWhenTaskReset =
1778 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1779
1780 if (!finishOnTaskLaunch
1781 && !clearWhenTaskReset
1782 && target.resultTo != null) {
1783 // If this activity is sending a reply to a previous
1784 // activity, we can't do anything with it now until
1785 // we reach the start of the reply chain.
1786 // XXX note that we are assuming the result is always
1787 // to the previous activity, which is almost always
1788 // the case but we really shouldn't count on.
1789 if (replyChainEnd < 0) {
1790 replyChainEnd = i;
1791 }
1792 } else if (!finishOnTaskLaunch
1793 && !clearWhenTaskReset
1794 && allowTaskReparenting
1795 && target.taskAffinity != null
1796 && !target.taskAffinity.equals(task.affinity)) {
1797 // If this activity has an affinity for another
1798 // task, then we need to move it out of here. We will
1799 // move it as far out of the way as possible, to the
1800 // bottom of the activity stack. This also keeps it
1801 // correctly ordered with any activities we previously
1802 // moved.
1803 TaskRecord bottomTask = mTaskHistory.get(0);
1804 ActivityRecord p = bottomTask.mActivities.get(0);
1805 if (target.taskAffinity != null
1806 && target.taskAffinity.equals(p.task.affinity)) {
1807 // If the activity currently at the bottom has the
1808 // same task affinity as the one we are moving,
1809 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001810 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001811 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1812 + " out to bottom task " + p.task);
1813 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001814 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001815 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001816 target.task.affinityIntent = target.intent;
1817 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1818 + " out to new task " + target.task);
1819 }
1820
1821 final TaskRecord targetTask = target.task;
1822 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001823 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001824
1825 ThumbnailHolder curThumbHolder = target.thumbHolder;
1826 boolean gotOptions = !canMoveOptions;
1827
1828 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1829 for (int srcPos = start; srcPos >= i; --srcPos) {
1830 p = activities.get(srcPos);
1831 if (p.finishing) {
1832 continue;
1833 }
1834
1835 curThumbHolder = p.thumbHolder;
1836 canMoveOptions = false;
1837 if (!gotOptions && topOptions == null) {
1838 topOptions = p.takeOptionsLocked();
1839 if (topOptions != null) {
1840 gotOptions = true;
1841 }
1842 }
1843 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1844 + task + " adding to task=" + targetTask,
1845 new RuntimeException("here").fillInStackTrace());
1846 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1847 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001848 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001849 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001850
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001851 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001852 }
1853
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001854 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001855 if (VALIDATE_TOKENS) {
1856 validateAppTokensLocked();
1857 }
1858
1859 replyChainEnd = -1;
1860 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1861 // If the activity should just be removed -- either
1862 // because it asks for it, or the task should be
1863 // cleared -- then finish it and anything that is
1864 // part of its reply chain.
1865 int end;
1866 if (clearWhenTaskReset) {
1867 // In this case, we want to finish this activity
1868 // and everything above it, so be sneaky and pretend
1869 // like these are all in the reply chain.
1870 end = numActivities - 1;
1871 } else if (replyChainEnd < 0) {
1872 end = i;
1873 } else {
1874 end = replyChainEnd;
1875 }
1876 ActivityRecord p = null;
1877 boolean gotOptions = !canMoveOptions;
1878 for (int srcPos = i; srcPos <= end; srcPos++) {
1879 p = activities.get(srcPos);
1880 if (p.finishing) {
1881 continue;
1882 }
1883 canMoveOptions = false;
1884 if (!gotOptions && topOptions == null) {
1885 topOptions = p.takeOptionsLocked();
1886 if (topOptions != null) {
1887 gotOptions = true;
1888 }
1889 }
Craig Mautner58547802013-03-05 08:23:53 -08001890 if (DEBUG_TASKS) Slog.w(TAG,
1891 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001892 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001893 end--;
1894 srcPos--;
1895 }
1896 }
1897 replyChainEnd = -1;
1898 } else {
1899 // If we were in the middle of a chain, well the
1900 // activity that started it all doesn't want anything
1901 // special, so leave it all as-is.
1902 replyChainEnd = -1;
1903 }
1904 }
1905
1906 return topOptions;
1907 }
1908
1909 /**
1910 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1911 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1912 * @param affinityTask The task we are looking for an affinity to.
1913 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1914 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1915 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1916 */
Craig Mautner77878772013-03-04 19:46:24 -08001917 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
1918 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001919 int replyChainEnd = -1;
1920 final int taskId = task.taskId;
1921 final String taskAffinity = task.affinity;
1922
1923 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1924 final int numActivities = activities.size();
1925 // Do not operate on the root Activity.
1926 for (int i = numActivities - 1; i > 0; --i) {
1927 ActivityRecord target = activities.get(i);
1928
1929 final int flags = target.info.flags;
1930 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1931 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1932
1933 if (target.resultTo != null) {
1934 // If this activity is sending a reply to a previous
1935 // activity, we can't do anything with it now until
1936 // we reach the start of the reply chain.
1937 // XXX note that we are assuming the result is always
1938 // to the previous activity, which is almost always
1939 // the case but we really shouldn't count on.
1940 if (replyChainEnd < 0) {
1941 replyChainEnd = i;
1942 }
1943 } else if (topTaskIsHigher
1944 && allowTaskReparenting
1945 && taskAffinity != null
1946 && taskAffinity.equals(target.taskAffinity)) {
1947 // This activity has an affinity for our task. Either remove it if we are
1948 // clearing or move it over to our task. Note that
1949 // we currently punt on the case where we are resetting a
1950 // task that is not at the top but who has activities above
1951 // with an affinity to it... this is really not a normal
1952 // case, and we will need to later pull that task to the front
1953 // and usually at that point we will do the reset and pick
1954 // up those remaining activities. (This only happens if
1955 // someone starts an activity in a new task from an activity
1956 // in a task that is not currently on top.)
1957 if (forceReset || finishOnTaskLaunch) {
1958 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1959 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1960 for (int srcPos = start; srcPos >= i; --srcPos) {
1961 final ActivityRecord p = activities.get(srcPos);
1962 if (p.finishing) {
1963 continue;
1964 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001965 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001966 }
1967 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001968 if (taskInsertionPoint < 0) {
1969 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001970
Craig Mautner77878772013-03-04 19:46:24 -08001971 }
Craig Mautner77878772013-03-04 19:46:24 -08001972
1973 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1974 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1975 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1976 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001977 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001978 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08001979 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001980
Craig Mautnere3a74d52013-02-22 14:14:58 -08001981 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
1982 + " to stack at " + task,
1983 new RuntimeException("here").fillInStackTrace());
1984 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
1985 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001986 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001987 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001988 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08001989 if (VALIDATE_TOKENS) {
1990 validateAppTokensLocked();
1991 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08001992
1993 // Now we've moved it in to place... but what if this is
1994 // a singleTop activity and we have put it on top of another
1995 // instance of the same activity? Then we drop the instance
1996 // below so it remains singleTop.
1997 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
1998 ArrayList<ActivityRecord> taskActivities = task.mActivities;
1999 boolean found = false;
2000 int targetNdx = taskActivities.indexOf(target);
2001 if (targetNdx > 0) {
2002 ActivityRecord p = taskActivities.get(targetNdx - 1);
2003 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002004 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2005 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002006 }
2007 }
2008 }
2009 }
2010
2011 replyChainEnd = -1;
2012 }
2013 }
Craig Mautner77878772013-03-04 19:46:24 -08002014 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002015 }
2016
Craig Mautner8849a5e2013-04-02 16:41:03 -07002017 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002018 ActivityRecord newActivity) {
2019 boolean forceReset =
2020 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2021 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2022 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2023 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2024 forceReset = true;
2025 }
2026 }
2027
2028 final TaskRecord task = taskTop.task;
2029
2030 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2031 * for remaining tasks. Used for later tasks to reparent to task. */
2032 boolean taskFound = false;
2033
2034 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2035 ActivityOptions topOptions = null;
2036
Craig Mautner77878772013-03-04 19:46:24 -08002037 // Preserve the location for reparenting in the new task.
2038 int reparentInsertionPoint = -1;
2039
Craig Mautnere3a74d52013-02-22 14:14:58 -08002040 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2041 final TaskRecord targetTask = mTaskHistory.get(i);
2042
2043 if (targetTask == task) {
2044 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2045 taskFound = true;
2046 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002047 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2048 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002049 }
2050 }
2051
Craig Mautner70a86932013-02-28 22:37:44 -08002052 int taskNdx = mTaskHistory.indexOf(task);
2053 do {
2054 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2055 } while (taskTop == null && taskNdx >= 0);
2056
Craig Mautnere3a74d52013-02-22 14:14:58 -08002057 if (topOptions != null) {
2058 // If we got some ActivityOptions from an activity on top that
2059 // was removed from the task, propagate them to the new real top.
2060 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002061 taskTop.updateOptionsLocked(topOptions);
2062 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002063 topOptions.abort();
2064 }
2065 }
2066
2067 return taskTop;
2068 }
2069
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071 * Find the activity in the history stack within the given task. Returns
2072 * the index within the history at which it's found, or < 0 if not found.
2073 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07002074 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002075 final ComponentName realActivity = r.realActivity;
2076 ArrayList<ActivityRecord> activities = task.mActivities;
2077 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2078 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002079 if (candidate.finishing) {
2080 continue;
2081 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002082 if (candidate.realActivity.equals(realActivity)) {
2083 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084 }
2085 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002086 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 }
2088
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2090 String resultWho, int requestCode, int resultCode, Intent data) {
2091
2092 if (callingUid > 0) {
2093 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002094 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002095 }
2096
2097 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2098 + " : who=" + resultWho + " req=" + requestCode
2099 + " res=" + resultCode + " data=" + data);
2100 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2101 try {
2102 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2103 list.add(new ResultInfo(resultWho, requestCode,
2104 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002105 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002106 return;
2107 } catch (Exception e) {
2108 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2109 }
2110 }
2111
2112 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2113 }
2114
Craig Mautnerf3333272013-04-22 10:55:53 -07002115 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2117 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2118 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2119 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002120 if (!mService.mSleeping) {
2121 if (DEBUG_STATES) {
2122 Slog.d(TAG, "no-history finish of " + r);
2123 }
2124 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002125 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002126 } else {
2127 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2128 + " on stop because we're just sleeping");
2129 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002130 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002131 }
2132
2133 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002134 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 if (mService.mFocusedActivity == r) {
2136 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2137 }
2138 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002139 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 try {
2141 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002142 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2143 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002144 r.state = ActivityState.STOPPING;
2145 if (DEBUG_VISBILITY) Slog.v(
2146 TAG, "Stopping visible=" + r.visible + " for " + r);
2147 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002148 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002149 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002150 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002151 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002152 r.setSleeping(true);
2153 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002154 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2155 msg.obj = r;
2156 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 } catch (Exception e) {
2158 // Maybe just ignore exceptions here... if the process
2159 // has crashed, our death notification will clean things
2160 // up.
2161 Slog.w(TAG, "Exception thrown during pause", e);
2162 // Just in case, assume it to be stopped.
2163 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002164 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 r.state = ActivityState.STOPPED;
2166 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002167 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002168 }
2169 }
2170 }
2171 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002172
Craig Mautnere79d42682013-04-01 19:01:53 -07002173 // Checked.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002174 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002175 Configuration config) {
2176 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2177
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002178 ActivityRecord res = null;
2179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 ArrayList<ActivityRecord> stops = null;
2181 ArrayList<ActivityRecord> finishes = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002182 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002183 int NS = 0;
2184 int NF = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 IApplicationThread sendThumbnail = null;
2186 boolean booting = false;
2187 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002188 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002189
Craig Mautnere79d42682013-04-01 19:01:53 -07002190 // Get the activity record.
Craig Mautnerf3333272013-04-22 10:55:53 -07002191 res = isInStackLocked(token);
2192 if (res != null) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002193 // No longer need to keep the device awake.
Craig Mautnerf3333272013-04-22 10:55:53 -07002194 if (mResumedActivity == res && mLaunchingActivity.isHeld()) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002195 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2196 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 }
2198
Craig Mautnere79d42682013-04-01 19:01:53 -07002199 // If this activity is fullscreen, set up to hide those under it.
Craig Mautnerf3333272013-04-22 10:55:53 -07002200 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + res);
Craig Mautnere79d42682013-04-01 19:01:53 -07002201 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002202 }
2203
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002204 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002205 }
2206
2207 /**
2208 * @return Returns true if the activity is being finished, false if for
2209 * some reason it is being left as-is.
2210 */
2211 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002212 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002213 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002214 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002215 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002216 + ", result=" + resultCode + ", data=" + resultData
2217 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002218 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002219 return false;
2220 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221
Craig Mautnerd44711d2013-02-23 11:24:36 -08002222 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002223 return true;
2224 }
2225
Craig Mautnerd2328952013-03-05 12:46:26 -08002226 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002227 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2228 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2229 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2230 ActivityRecord r = activities.get(activityNdx);
2231 if (r.resultTo == self && r.requestCode == requestCode) {
2232 if ((r.resultWho == null && resultWho == null) ||
2233 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2234 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2235 false);
2236 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002237 }
2238 }
2239 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002240 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002241 }
2242
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002243 final void finishTopRunningActivityLocked(ProcessRecord app) {
2244 ActivityRecord r = topRunningActivityLocked(null);
2245 if (r != null && r.app == app) {
2246 // If the top running activity is from this crashing
2247 // process, then terminate it to avoid getting in a loop.
2248 Slog.w(TAG, " Force finishing activity "
2249 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002250 int taskNdx = mTaskHistory.indexOf(r.task);
2251 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002252 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002253 // Also terminate any activities below it that aren't yet
2254 // stopped, to avoid a situation where one will get
2255 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002256 --activityNdx;
2257 if (activityNdx < 0) {
2258 do {
2259 --taskNdx;
2260 if (taskNdx < 0) {
2261 break;
2262 }
2263 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2264 } while (activityNdx < 0);
2265 }
2266 if (activityNdx >= 0) {
2267 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002268 if (r.state == ActivityState.RESUMED
2269 || r.state == ActivityState.PAUSING
2270 || r.state == ActivityState.PAUSED) {
2271 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
2272 Slog.w(TAG, " Force finishing activity "
2273 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002274 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002275 }
2276 }
2277 }
2278 }
2279 }
2280
Craig Mautnerd2328952013-03-05 12:46:26 -08002281 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002282 ArrayList<ActivityRecord> activities = r.task.mActivities;
2283 for (int index = activities.indexOf(r); index >= 0; --index) {
2284 ActivityRecord cur = activities.get(index);
2285 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002286 break;
2287 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002288 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002289 }
2290 return true;
2291 }
2292
Dianne Hackborn5c607432012-02-28 14:44:19 -08002293 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2294 // send the result
2295 ActivityRecord resultTo = r.resultTo;
2296 if (resultTo != null) {
2297 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2298 + " who=" + r.resultWho + " req=" + r.requestCode
2299 + " res=" + resultCode + " data=" + resultData);
2300 if (r.info.applicationInfo.uid > 0) {
2301 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2302 resultTo.packageName, resultData,
2303 resultTo.getUriPermissionsLocked());
2304 }
2305 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2306 resultData);
2307 r.resultTo = null;
2308 }
2309 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2310
2311 // Make sure this HistoryRecord is not holding on to other resources,
2312 // because clients have remote IPC references to this object so we
2313 // can't assume that will go away and want to avoid circular IPC refs.
2314 r.results = null;
2315 r.pendingResults = null;
2316 r.newIntents = null;
2317 r.icicle = null;
2318 }
2319
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002320 /**
2321 * @return Returns true if this activity has been removed from the history
2322 * list, or false if it is still in the list and will be removed later.
2323 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002324 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2325 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 if (r.finishing) {
2327 Slog.w(TAG, "Duplicate finish request for " + r);
2328 return false;
2329 }
2330
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002331 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002332 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002333 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002335 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2336 final int index = activities.indexOf(r);
2337 if (index < (activities.size() - 1)) {
2338 ActivityRecord next = activities.get(index+1);
2339 if (r.frontOfTask) {
2340 // The next activity is now the front of the task.
2341 next.frontOfTask = true;
2342 }
2343 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2344 // If the caller asked that this activity (and all above it)
2345 // be cleared when the task is reset, don't lose that information,
2346 // but propagate it up to the next activity.
2347 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 }
2349 }
2350
2351 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002352 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002353 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002354 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 }
2356 }
2357
Dianne Hackborn5c607432012-02-28 14:44:19 -08002358 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 if (mService.mPendingThumbnails.size() > 0) {
2361 // There are clients waiting to receive thumbnails so, in case
2362 // this is an activity that someone is waiting for, add it
2363 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002364 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 }
2366
Craig Mautnerde4ef022013-04-07 19:01:33 -07002367 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002368 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002369 if (DEBUG_TRANSITION) Slog.v(TAG,
2370 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002371 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002372 ? AppTransition.TRANSIT_TASK_CLOSE
2373 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002374
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002376 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002377
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002378 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2380 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2381 startPausingLocked(false, false);
2382 }
2383
2384 } else if (r.state != ActivityState.PAUSING) {
2385 // If the activity is PAUSING, we will complete the finish once
2386 // it is done pausing; else we can just directly finish it here.
2387 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002388 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389 } else {
2390 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2391 }
2392
2393 return false;
2394 }
2395
Craig Mautnerf3333272013-04-22 10:55:53 -07002396 static final int FINISH_IMMEDIATELY = 0;
2397 static final int FINISH_AFTER_PAUSE = 1;
2398 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399
Craig Mautnerf3333272013-04-22 10:55:53 -07002400 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 // First things first: if this activity is currently visible,
2402 // and the resumed activity is not yet visible, then hold off on
2403 // finishing until the resumed one becomes visible.
2404 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002405 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2406 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002407 if (mStackSupervisor.mStoppingActivities.size() > 3
2408 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409 // If we already have a few activities waiting to stop,
2410 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002411 // them out. Or if r is the last of activity of the last task the stack
2412 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002413 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002414 } else {
2415 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416 }
2417 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002418 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2419 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002420 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002421 if (oomAdj) {
2422 mService.updateOomAdjLocked();
2423 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 return r;
2425 }
2426
2427 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002428 mStackSupervisor.mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002429 mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002430 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431 if (mResumedActivity == r) {
2432 mResumedActivity = null;
2433 }
2434 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002435 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002436 r.state = ActivityState.FINISHING;
2437
2438 if (mode == FINISH_IMMEDIATELY
2439 || prevState == ActivityState.STOPPED
2440 || prevState == ActivityState.INITIALIZING) {
2441 // If this activity is already stopped, we can just finish
2442 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002443 boolean activityRemoved = destroyActivityLocked(r, true,
2444 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002445 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002446 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002447 }
2448 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002450
2451 // Need to go through the full pause cycle to get this
2452 // activity into the stopped state and then finish it.
2453 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002454 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002455 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002456 return r;
2457 }
2458
Craig Mautnerd2328952013-03-05 12:46:26 -08002459 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002460 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002461 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002462 final TaskRecord task = srec.task;
2463 final ArrayList<ActivityRecord> activities = task.mActivities;
2464 final int start = activities.indexOf(srec);
2465 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002466 return false;
2467 }
2468 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002469 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002470 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002471 final ComponentName dest = destIntent.getComponent();
2472 if (start > 0 && dest != null) {
2473 for (int i = finishTo; i >= 0; i--) {
2474 ActivityRecord r = activities.get(i);
2475 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002476 r.info.name.equals(dest.getClassName())) {
2477 finishTo = i;
2478 parent = r;
2479 foundParentInTask = true;
2480 break;
2481 }
2482 }
2483 }
2484
2485 IActivityController controller = mService.mController;
2486 if (controller != null) {
2487 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2488 if (next != null) {
2489 // ask watcher if this is allowed
2490 boolean resumeOK = true;
2491 try {
2492 resumeOK = controller.activityResuming(next.packageName);
2493 } catch (RemoteException e) {
2494 mService.mController = null;
2495 }
2496
2497 if (!resumeOK) {
2498 return false;
2499 }
2500 }
2501 }
2502 final long origId = Binder.clearCallingIdentity();
2503 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002504 ActivityRecord r = activities.get(i);
2505 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002506 // Only return the supplied result for the first activity finished
2507 resultCode = Activity.RESULT_CANCELED;
2508 resultData = null;
2509 }
2510
2511 if (parent != null && foundParentInTask) {
2512 final int parentLaunchMode = parent.info.launchMode;
2513 final int destIntentFlags = destIntent.getFlags();
2514 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2515 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2516 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2517 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2518 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2519 } else {
2520 try {
2521 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2522 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002523 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002524 null, aInfo, parent.appToken, null,
2525 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2526 0, null, true, null);
2527 foundParentInTask = res == ActivityManager.START_SUCCESS;
2528 } catch (RemoteException e) {
2529 foundParentInTask = false;
2530 }
2531 requestFinishActivityLocked(parent.appToken, resultCode,
2532 resultData, "navigate-up", true);
2533 }
2534 }
2535 Binder.restoreCallingIdentity(origId);
2536 return foundParentInTask;
2537 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 /**
2539 * Perform the common clean-up of an activity record. This is called both
2540 * as part of destroyActivityLocked() (when destroying the client-side
2541 * representation) and cleaning things up as a result of its hosting
2542 * processing going away, in which case there is no remaining client-side
2543 * state to destroy so only the cleanup here is needed.
2544 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002545 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2546 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 if (mResumedActivity == r) {
2548 mResumedActivity = null;
2549 }
2550 if (mService.mFocusedActivity == r) {
2551 mService.mFocusedActivity = null;
2552 }
2553
2554 r.configDestroy = false;
2555 r.frozenBeforeDestroy = false;
2556
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002557 if (setState) {
2558 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2559 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002560 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002561 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002562 }
2563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002564 // Make sure this record is no longer in the pending finishes list.
2565 // This could happen, for example, if we are trimming activities
2566 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002567 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002568 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002569
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570 // Remove any pending results.
2571 if (r.finishing && r.pendingResults != null) {
2572 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2573 PendingIntentRecord rec = apr.get();
2574 if (rec != null) {
2575 mService.cancelIntentSenderLocked(rec, false);
2576 }
2577 }
2578 r.pendingResults = null;
2579 }
2580
2581 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002582 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002583 }
2584
2585 if (mService.mPendingThumbnails.size() > 0) {
2586 // There are clients waiting to receive thumbnails so, in case
2587 // this is an activity that someone is waiting for, add it
2588 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002589 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002590 }
2591
2592 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002593 removeTimeoutsForActivityLocked(r);
2594 }
2595
2596 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002597 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002598 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002599 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002600 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002601 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002602 }
2603
Dianne Hackborn5c607432012-02-28 14:44:19 -08002604 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002605 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2606 r.makeFinishing();
2607 if (DEBUG_ADD_REMOVE) {
2608 RuntimeException here = new RuntimeException("here");
2609 here.fillInStackTrace();
2610 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002611 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002612 final TaskRecord task = r.task;
2613 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002614 if (DEBUG_STACK) Slog.i(TAG,
2615 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002616 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002617 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002618 r.takeFromHistory();
2619 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002620 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002621 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002622 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002623 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002624 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002625 if (VALIDATE_TOKENS) {
2626 validateAppTokensLocked();
2627 }
2628 cleanUpActivityServicesLocked(r);
2629 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002630 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002632 /**
2633 * Perform clean-up of service connections in an activity record.
2634 */
2635 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2636 // Throw away any services that have been bound by this activity.
2637 if (r.connections != null) {
2638 Iterator<ConnectionRecord> it = r.connections.iterator();
2639 while (it.hasNext()) {
2640 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002641 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002642 }
2643 r.connections = null;
2644 }
2645 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002646
2647 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2648 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2649 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2650 mHandler.sendMessage(msg);
2651 }
2652
Dianne Hackborn28695e02011-11-02 21:59:51 -07002653 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002654 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002655 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2657 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2658 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2659 final ActivityRecord r = activities.get(activityNdx);
2660 if (r.finishing) {
2661 continue;
2662 }
2663 if (r.fullscreen) {
2664 lastIsOpaque = true;
2665 }
2666 if (owner != null && r.app != owner) {
2667 continue;
2668 }
2669 if (!lastIsOpaque) {
2670 continue;
2671 }
2672 // We can destroy this one if we have its icicle saved and
2673 // it is not in the process of pausing/stopping/finishing.
2674 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2675 && r.haveState && !r.visible && r.stopped
2676 && r.state != ActivityState.DESTROYING
2677 && r.state != ActivityState.DESTROYED) {
2678 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2679 + " resumed=" + mResumedActivity
2680 + " pausing=" + mPausingActivity);
2681 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2682 activityRemoved = true;
2683 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002684 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002685 }
2686 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002687 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002688 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002689 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002690 }
2691
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002692 /**
2693 * Destroy the current CLIENT SIDE instance of an activity. This may be
2694 * called both when actually finishing an activity, or when performing
2695 * a configuration switch where we destroy the current client-side object
2696 * but then create a new client-side object for this same HistoryRecord.
2697 */
2698 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002699 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002700 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002701 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002702 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2703 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002704 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002705 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002706
2707 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002708
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002709 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002710
2711 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002712
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713 if (hadApp) {
2714 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002715 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2717 mService.mHeavyWeightProcess = null;
2718 mService.mHandler.sendEmptyMessage(
2719 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2720 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002722 // No longer have activities, so update oom adj.
2723 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002724 }
2725 }
2726
2727 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002728
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002729 try {
2730 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002731 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002732 r.configChangeFlags);
2733 } catch (Exception e) {
2734 // We can just ignore exceptions here... if the process
2735 // has crashed, our death notification will clean things
2736 // up.
2737 //Slog.w(TAG, "Exception thrown during finish", e);
2738 if (r.finishing) {
2739 removeActivityFromHistoryLocked(r);
2740 removedFromHistory = true;
2741 skipDestroy = true;
2742 }
2743 }
2744
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002746
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002747 // If the activity is finishing, we need to wait on removing it
2748 // from the list to give it a chance to do its cleanup. During
2749 // that time it may make calls back with its token so we need to
2750 // be able to find it on the list and so we don't want to remove
2751 // it from the list yet. Otherwise, we can just immediately put
2752 // it in the destroyed state since we are not removing it from the
2753 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002754 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002755 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2756 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002757 r.state = ActivityState.DESTROYING;
2758 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
2759 msg.obj = r;
2760 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2761 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002762 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2763 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002765 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002766 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 }
2768 } else {
2769 // remove this record from the history.
2770 if (r.finishing) {
2771 removeActivityFromHistoryLocked(r);
2772 removedFromHistory = true;
2773 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002774 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2775 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002776 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002777 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002778 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779 }
2780 }
2781
2782 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002783
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 if (!mLRUActivities.remove(r) && hadApp) {
2785 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2786 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002787
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002788 return removedFromHistory;
2789 }
2790
Craig Mautnerd2328952013-03-05 12:46:26 -08002791 final void activityDestroyedLocked(IBinder token) {
2792 final long origId = Binder.clearCallingIdentity();
2793 try {
2794 ActivityRecord r = ActivityRecord.forToken(token);
2795 if (r != null) {
2796 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002797 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002798
2799 if (isInStackLocked(token) != null) {
2800 if (r.state == ActivityState.DESTROYING) {
2801 cleanUpActivityLocked(r, true, false);
2802 removeActivityFromHistoryLocked(r);
2803 }
2804 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002805 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Craig Mautnerd2328952013-03-05 12:46:26 -08002806 } finally {
2807 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002808 }
2809 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002810
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002811 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2812 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002814 if (DEBUG_CLEANUP) Slog.v(
2815 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002816 + " with " + i + " entries");
2817 while (i > 0) {
2818 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002819 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002820 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002821 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002822 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002824 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002825 }
2826 }
2827 }
2828
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002829 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2830 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002831 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2832 "mStoppingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002833 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002834 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002835 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002836 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2837 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002838
2839 boolean hasVisibleActivities = false;
2840
2841 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002842 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002843 if (DEBUG_CLEANUP) Slog.v(
2844 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002845 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2846 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2847 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2848 final ActivityRecord r = activities.get(activityNdx);
2849 --i;
2850 if (DEBUG_CLEANUP) Slog.v(
2851 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2852 if (r.app == app) {
2853 boolean remove;
2854 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2855 // Don't currently have state for the activity, or
2856 // it is finishing -- always remove it.
2857 remove = true;
2858 } else if (r.launchCount > 2 &&
2859 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2860 // We have launched this activity too many times since it was
2861 // able to run, so give up and remove it.
2862 remove = true;
2863 } else {
2864 // The process may be gone, but the activity lives on!
2865 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002866 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002867 if (remove) {
2868 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
2869 RuntimeException here = new RuntimeException("here");
2870 here.fillInStackTrace();
2871 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2872 + ": haveState=" + r.haveState
2873 + " stateNotNeeded=" + r.stateNotNeeded
2874 + " finishing=" + r.finishing
2875 + " state=" + r.state, here);
2876 }
2877 if (!r.finishing) {
2878 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2879 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2880 r.userId, System.identityHashCode(r),
2881 r.task.taskId, r.shortComponentName,
2882 "proc died without state saved");
2883 }
2884 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002885
Craig Mautner0247fc82013-02-28 14:32:06 -08002886 } else {
2887 // We have the current state for this activity, so
2888 // it can be restarted later when needed.
2889 if (localLOGV) Slog.v(
2890 TAG, "Keeping entry, setting app to null");
2891 if (r.visible) {
2892 hasVisibleActivities = true;
2893 }
2894 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2895 + r);
2896 r.app = null;
2897 r.nowVisible = false;
2898 if (!r.haveState) {
2899 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
2900 "App died, clearing saved state of " + r);
2901 r.icicle = null;
2902 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002903 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002904
Craig Mautnerd2328952013-03-05 12:46:26 -08002905 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002906 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002907 }
2908 }
2909
2910 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002912
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002913 final void updateTransitLocked(int transit, Bundle options) {
2914 if (options != null) {
2915 ActivityRecord r = topRunningActivityLocked(null);
2916 if (r != null && r.state != ActivityState.RESUMED) {
2917 r.updateOptionsLocked(options);
2918 } else {
2919 ActivityOptions.abort(options);
2920 }
2921 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002922 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002923 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002924
Craig Mautnercae015f2013-02-08 14:31:27 -08002925 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002926 final TaskRecord task = taskForIdLocked(taskId);
2927 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002928 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002929 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002930 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002931 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2932 // Caller wants the home activity moved with it. To accomplish this,
2933 // we'll just move the home task to the top first.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002934 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002935 }
2936 moveTaskToFrontLocked(task, null, options);
2937 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002938 }
2939 return false;
2940 }
2941
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002942 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002943 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002944
Craig Mautner11bf9a52013-02-19 14:08:51 -08002945 final int numTasks = mTaskHistory.size();
2946 final int index = mTaskHistory.indexOf(tr);
2947 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
2948 // nothing to do!
2949 if (reason != null &&
2950 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2951 ActivityOptions.abort(options);
2952 } else {
2953 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2954 }
2955 return;
2956 }
2957
2958 // Shift all activities with this task up to the top
2959 // of the stack, keeping them in the same internal order.
2960 mTaskHistory.remove(tr);
2961 mTaskHistory.add(tr);
2962
2963 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964 if (reason != null &&
2965 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002966 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002967 ActivityRecord r = topRunningActivityLocked(null);
2968 if (r != null) {
2969 mNoAnimActivities.add(r);
2970 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002971 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002973 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002975
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002976 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002978 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Craig Mautner58547802013-03-05 08:23:53 -08002979 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002980
2981 if (VALIDATE_TOKENS) {
2982 validateAppTokensLocked();
2983 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002984 }
2985
2986 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002987 * Worker method for rearranging history stack. Implements the function of moving all
2988 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002990 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2992 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002993 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 * @param task The taskId to collect and move to the bottom.
2995 * @return Returns true if the move completed, false if not.
2996 */
2997 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
2998 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002999
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003000 // If we have a watcher, preflight the move before committing to it. First check
3001 // for *other* available tasks, but if none are available, then try again allowing the
3002 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003003 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003004 ActivityRecord next = topRunningActivityLocked(null, task);
3005 if (next == null) {
3006 next = topRunningActivityLocked(null, 0);
3007 }
3008 if (next != null) {
3009 // ask watcher if this is allowed
3010 boolean moveOK = true;
3011 try {
3012 moveOK = mService.mController.activityResuming(next.packageName);
3013 } catch (RemoteException e) {
3014 mService.mController = null;
3015 }
3016 if (!moveOK) {
3017 return false;
3018 }
3019 }
3020 }
3021
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 if (DEBUG_TRANSITION) Slog.v(TAG,
3023 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003024
Craig Mautnerd2328952013-03-05 12:46:26 -08003025 final TaskRecord tr = taskForIdLocked(task);
3026 if (tr == null) {
3027 return false;
3028 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003029 mTaskHistory.remove(tr);
3030 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031
3032 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003033 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3034 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035 ActivityRecord r = topRunningActivityLocked(null);
3036 if (r != null) {
3037 mNoAnimActivities.add(r);
3038 }
3039 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003040 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003041 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003043 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003044
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003046 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 }
3048
Craig Mautnerde4ef022013-04-07 19:01:33 -07003049 if (mResumedActivity != null && mResumedActivity.task == tr &&
3050 mResumedActivity.mLaunchHomeTaskNext) {
3051 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003052 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003053 }
3054
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003055 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003056 return true;
3057 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003058
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003059 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08003060 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003061 if (mResumedActivity != null && mResumedActivity.thumbHolder == tr) {
3062 info.mainThumbnail = screenshotActivities(mResumedActivity);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003063 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07003064 if (info.mainThumbnail == null) {
3065 info.mainThumbnail = tr.lastThumbnail;
3066 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003067 return info;
3068 }
3069
Dianne Hackborn15491c62012-09-19 10:59:14 -07003070 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003071 if (mResumedActivity != null && mResumedActivity.task == tr) {
Dianne Hackborn15491c62012-09-19 10:59:14 -07003072 // This task is the current resumed task, we just need to take
3073 // a screenshot of it and return that.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003074 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003075 }
3076 // Return the information about the task, to figure out the top
3077 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08003078 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003079 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07003080 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003081 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003082 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003083 }
3084
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003085 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
3086 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003087 final TaskRecord task = taskForIdLocked(taskId);
3088 if (task == null) {
3089 return null;
3090 }
Craig Mautner16030772013-02-26 16:05:47 -08003091 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003092 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003093 if (taskRequired) {
3094 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
3095 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003096 return null;
3097 }
3098
3099 if (subTaskIndex < 0) {
3100 // Just remove the entire task.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003101 task.performClearTaskAtIndexLocked(info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003102 return info.root;
3103 }
3104
3105 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003106 if (taskRequired) {
3107 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
3108 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003109 return null;
3110 }
3111
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003112 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003113 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003114 task.performClearTaskAtIndexLocked(subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003115 return subtask.activity;
3116 }
3117
Craig Mautner16030772013-02-26 16:05:47 -08003118 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003119 final TaskAccessInfo thumbs = new TaskAccessInfo();
3120 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08003121 final ArrayList<ActivityRecord> activities = task.mActivities;
3122 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003123 int j = 0;
3124 ThumbnailHolder holder = null;
3125 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003126 ActivityRecord ar = activities.get(j);
3127 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07003128 thumbs.root = ar;
3129 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003130 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003131 if (holder != null) {
3132 thumbs.mainThumbnail = holder.lastThumbnail;
3133 }
3134 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003135 break;
3136 }
3137 j++;
3138 }
3139
3140 if (j >= NA) {
3141 return thumbs;
3142 }
3143
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003144 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
3145 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003146 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003147 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003148 j++;
3149 if (ar.finishing) {
3150 continue;
3151 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003152 if (ar.thumbHolder != holder && holder != null) {
3153 thumbs.numSubThumbbails++;
3154 holder = ar.thumbHolder;
3155 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07003156 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003157 sub.activity = ar;
3158 sub.index = j-1;
3159 subtasks.add(sub);
3160 }
3161 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003162 if (thumbs.numSubThumbbails > 0) {
3163 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003164 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003165 public Bitmap getThumbnail(int index) {
3166 if (index < 0 || index >= thumbs.subtasks.size()) {
3167 return null;
3168 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003169 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003170 if (mResumedActivity != null && mResumedActivity.thumbHolder == sub.holder) {
3171 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003172 }
3173 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003174 }
3175 };
3176 }
3177 return thumbs;
3178 }
3179
Craig Mautner8849a5e2013-04-02 16:41:03 -07003180 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003182 final Uri data = r.intent.getData();
3183 final String strData = data != null ? data.toSafeString() : null;
3184
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003185 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003186 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003187 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003188 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003189 }
3190
3191 /**
3192 * Make sure the given activity matches the current configuration. Returns
3193 * false if the activity had to be destroyed. Returns true if the
3194 * configuration is the same, or the activity will remain running as-is
3195 * for whatever reason. Ensures the HistoryRecord is updated with the
3196 * correct configuration and all other bookkeeping is handled.
3197 */
3198 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3199 int globalChanges) {
3200 if (mConfigWillChange) {
3201 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3202 "Skipping config check (will change): " + r);
3203 return true;
3204 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003205
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003206 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3207 "Ensuring correct configuration: " + r);
3208
3209 // Short circuit: if the two configurations are the exact same
3210 // object (the common case), then there is nothing to do.
3211 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003212 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3214 "Configuration unchanged in " + r);
3215 return true;
3216 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003217
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 // We don't worry about activities that are finishing.
3219 if (r.finishing) {
3220 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3221 "Configuration doesn't matter in finishing " + r);
3222 r.stopFreezingScreenLocked(false);
3223 return true;
3224 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003225
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226 // Okay we now are going to make this activity have the new config.
3227 // But then we need to figure out how it needs to deal with that.
3228 Configuration oldConfig = r.configuration;
3229 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003230
3231 // Determine what has changed. May be nothing, if this is a config
3232 // that has come back from the app after going idle. In that case
3233 // we just want to leave the official config object now in the
3234 // activity and do nothing else.
3235 final int changes = oldConfig.diff(newConfig);
3236 if (changes == 0 && !r.forceNewConfig) {
3237 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3238 "Configuration no differences in " + r);
3239 return true;
3240 }
3241
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242 // If the activity isn't currently running, just leave the new
3243 // configuration and it will pick that up next time it starts.
3244 if (r.app == null || r.app.thread == null) {
3245 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3246 "Configuration doesn't matter not running " + r);
3247 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003248 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 return true;
3250 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003251
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003252 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003253 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3254 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3255 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003256 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003257 + ", newConfig=" + newConfig);
3258 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003259 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003260 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3261 r.configChangeFlags |= changes;
3262 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003263 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003264 if (r.app == null || r.app.thread == null) {
3265 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003266 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003267 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003268 } else if (r.state == ActivityState.PAUSING) {
3269 // A little annoying: we are waiting for this activity to
3270 // finish pausing. Let's not do anything now, but just
3271 // flag that it needs to be restarted when done pausing.
3272 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003273 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003274 r.configDestroy = true;
3275 return true;
3276 } else if (r.state == ActivityState.RESUMED) {
3277 // Try to optimize this case: the configuration is changing
3278 // and we need to restart the top, resumed activity.
3279 // Instead of doing the normal handshaking, just say
3280 // "restart!".
3281 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003282 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003283 relaunchActivityLocked(r, r.configChangeFlags, true);
3284 r.configChangeFlags = 0;
3285 } else {
3286 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003287 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003288 relaunchActivityLocked(r, r.configChangeFlags, false);
3289 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003290 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003291
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003292 // All done... tell the caller we weren't able to keep this
3293 // activity around.
3294 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003296
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003297 // Default case: the activity can handle this new configuration, so
3298 // hand it over. Note that we don't need to give it the new
3299 // configuration, since we always send configuration changes to all
3300 // process when they happen so it can just use whatever configuration
3301 // it last got.
3302 if (r.app != null && r.app.thread != null) {
3303 try {
3304 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003305 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003306 } catch (RemoteException e) {
3307 // If process died, whatever.
3308 }
3309 }
3310 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003311
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 return true;
3313 }
3314
3315 private final boolean relaunchActivityLocked(ActivityRecord r,
3316 int changes, boolean andResume) {
3317 List<ResultInfo> results = null;
3318 List<Intent> newIntents = null;
3319 if (andResume) {
3320 results = r.results;
3321 newIntents = r.newIntents;
3322 }
3323 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3324 + " with results=" + results + " newIntents=" + newIntents
3325 + " andResume=" + andResume);
3326 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003327 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003328 r.task.taskId, r.shortComponentName);
3329
3330 r.startFreezingScreenLocked(r.app, 0);
3331
3332 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003333 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3334 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3335 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003336 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003337 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003338 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003339 // Note: don't need to call pauseIfSleepingLocked() here, because
3340 // the caller will only pass in 'andResume' if this activity is
3341 // currently resumed, which implies we aren't sleeping.
3342 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003343 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003344 }
3345
3346 if (andResume) {
3347 r.results = null;
3348 r.newIntents = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003349 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003350 mService.reportResumedActivityLocked(r);
3351 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003352 r.state = ActivityState.RESUMED;
3353 } else {
3354 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3355 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003356 }
3357
3358 return true;
3359 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003360
3361 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003362 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3363 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3364 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3365 final ActivityRecord r = activities.get(activityNdx);
3366 if (r.appToken == token) {
3367 return true;
3368 }
3369 if (r.fullscreen && !r.finishing) {
3370 return false;
3371 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003372 }
3373 }
3374 return true;
3375 }
3376
3377 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003378 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3379 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3380 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3381 final ActivityRecord r = activities.get(activityNdx);
3382 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003383 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003384 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003385 }
3386 }
3387 }
3388
3389 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3390 boolean didSomething = false;
3391 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003392 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3393 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3394 int numActivities = activities.size();
3395 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3396 ActivityRecord r = activities.get(activityNdx);
3397 final boolean samePackage = r.packageName.equals(name)
3398 || (name == null && r.userId == userId);
3399 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3400 && (samePackage || r.task == lastTask)
3401 && (r.app == null || evenPersistent || !r.app.persistent)) {
3402 if (!doit) {
3403 if (r.finishing) {
3404 // If this activity is just finishing, then it is not
3405 // interesting as far as something to stop.
3406 continue;
3407 }
3408 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003409 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003410 didSomething = true;
3411 Slog.i(TAG, " Force finishing activity " + r);
3412 if (samePackage) {
3413 if (r.app != null) {
3414 r.app.removed = true;
3415 }
3416 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003417 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003418 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08003419 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08003420 }
3421 }
3422 }
3423 return didSomething;
3424 }
3425
3426 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003427 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003428 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003429 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3430 --maxNum, --taskNdx) {
3431 final TaskRecord task = mTaskHistory.get(taskNdx);
3432 ActivityRecord r = null;
3433 ActivityRecord top = null;
3434 int numActivities = 0;
3435 int numRunning = 0;
3436 final ArrayList<ActivityRecord> activities = task.mActivities;
3437 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3438 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003439
Craig Mautneraab647e2013-02-28 16:31:36 -08003440 // Initialize state for next task if needed.
3441 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3442 top = r;
3443 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003444 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003445
3446 // Add 'r' into the current task.
3447 numActivities++;
3448 if (r.app != null && r.app.thread != null) {
3449 numRunning++;
3450 }
3451
3452 if (localLOGV) Slog.v(
3453 TAG, r.intent.getComponent().flattenToShortString()
3454 + ": task=" + r.task);
3455 }
3456
3457 RunningTaskInfo ci = new RunningTaskInfo();
3458 ci.id = task.taskId;
3459 ci.baseActivity = r.intent.getComponent();
3460 ci.topActivity = top.intent.getComponent();
3461 if (top.thumbHolder != null) {
3462 ci.description = top.thumbHolder.lastDescription;
3463 }
3464 ci.numActivities = numActivities;
3465 ci.numRunning = numRunning;
3466 //System.out.println(
3467 // "#" + maxNum + ": " + " descr=" + ci.description);
3468 if (receiver != null) {
3469 if (localLOGV) Slog.v(
3470 TAG, "State=" + top.state + "Idle=" + top.idle
3471 + " app=" + top.app
3472 + " thr=" + (top.app != null ? top.app.thread : null));
3473 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3474 if (top.idle && top.app != null && top.app.thread != null) {
3475 topRecord = top;
3476 } else {
3477 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003478 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003479 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003480 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003481 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003482 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003483 }
3484 return topRecord;
3485 }
3486
3487 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003488 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003489 if (DEBUG_SWITCH) Slog.d(
3490 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003491 if (top >= 0) {
3492 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3493 int activityTop = activities.size() - 1;
3494 if (activityTop > 0) {
3495 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3496 "unhandled-back", true);
3497 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003498 }
3499 }
3500
Craig Mautnere79d42682013-04-01 19:01:53 -07003501 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003502 if (!containsApp(app)) {
3503 return;
3504 }
3505 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003506 if (mPausingActivity != null && mPausingActivity.app == app) {
3507 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3508 "App died while pausing: " + mPausingActivity);
3509 mPausingActivity = null;
3510 }
3511 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3512 mLastPausedActivity = null;
3513 }
3514
3515 // Remove this application's activities from active lists.
3516 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3517
3518 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003519 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003520 if (stack == null) {
3521 mStackSupervisor.resumeHomeActivity(null);
3522 } else if (!stack.resumeTopActivityLocked(null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003523 // If there was nothing to resume, and we are not already
3524 // restarting this process, but there is a visible activity that
3525 // is hosted by the process... then make sure all visible
3526 // activities are running, taking care of restarting this
3527 // process.
3528 if (hasVisibleActivities) {
3529 ensureActivitiesVisibleLocked(null, 0);
3530 }
3531 }
3532 }
3533 }
3534
Craig Mautnercae015f2013-02-08 14:31:27 -08003535 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003536 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3537 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3538 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3539 final ActivityRecord r = activities.get(activityNdx);
3540 if (r.app == app) {
3541 Slog.w(TAG, " Force finishing activity "
3542 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003543 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003544 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003545 }
3546 }
3547 }
3548
3549 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3550 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003551 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3552 final TaskRecord task = mTaskHistory.get(taskNdx);
3553 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003554 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003555 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3556 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003557 }
3558
3559 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3560 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3561
3562 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003563 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3564 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003565 }
3566 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003567 final int top = mTaskHistory.size() - 1;
3568 if (top >= 0) {
3569 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3570 int listTop = list.size() - 1;
3571 if (listTop >= 0) {
3572 activities.add(list.get(listTop));
3573 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003574 }
3575 } else {
3576 ItemMatcher matcher = new ItemMatcher();
3577 matcher.build(name);
3578
Craig Mautneraab647e2013-02-28 16:31:36 -08003579 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3580 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3581 if (matcher.match(r1, r1.intent.getComponent())) {
3582 activities.add(r1);
3583 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003584 }
3585 }
3586 }
3587
3588 return activities;
3589 }
3590
3591 ActivityRecord restartPackage(String packageName) {
3592 ActivityRecord starting = topRunningActivityLocked(null);
3593
3594 // All activities that came from the package must be
3595 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003596 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3597 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3598 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3599 final ActivityRecord a = activities.get(activityNdx);
3600 if (a.info.packageName.equals(packageName)) {
3601 a.forceNewConfig = true;
3602 if (starting != null && a == starting && a.visible) {
3603 a.startFreezingScreenLocked(starting.app,
3604 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3605 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003606 }
3607 }
3608 }
3609
3610 return starting;
3611 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003612
Craig Mautnerde4ef022013-04-07 19:01:33 -07003613 boolean removeTask(TaskRecord task) {
3614 mTaskHistory.remove(task);
3615 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003616 }
3617
Craig Mautnerde4ef022013-04-07 19:01:33 -07003618 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003619 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003620 if (toTop) {
3621 mTaskHistory.add(task);
3622 } else {
3623 mTaskHistory.add(0, task);
3624 }
3625 return task;
3626 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003627
3628 ArrayList<TaskRecord> getAllTasks() {
3629 return new ArrayList<TaskRecord>(mTaskHistory);
3630 }
3631
3632 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003633 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003634 if (task == null) {
3635 return;
3636 }
3637 task.stack.mTaskHistory.remove(task);
3638 task.stack = this;
3639 if (toTop) {
3640 mTaskHistory.add(task);
3641 } else {
3642 mTaskHistory.add(0, task);
3643 }
3644 }
3645
3646 public int getStackId() {
3647 return mStackId;
3648 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003649
3650 @Override
3651 public String toString() {
3652 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3653 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654}