blob: e7b50f9fbb3c5a2cae75d71c7e43a215c9b6848a [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;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070026
27import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070028import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070029import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070030import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080031import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070032import android.app.IActivityManager;
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.app.IActivityManager.WaitResult;
39import android.content.ComponentName;
40import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070043import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070044import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080045import android.content.res.Resources;
46import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080047import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080048import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070050import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.os.Handler;
52import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090053import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.os.Message;
55import android.os.PowerManager;
56import android.os.RemoteException;
57import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070058import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070061import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062
Craig Mautnercae015f2013-02-08 14:31:27 -080063import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080064import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import java.lang.ref.WeakReference;
66import java.util.ArrayList;
67import java.util.Iterator;
68import java.util.List;
69
70/**
71 * State and management of a single stack of activities.
72 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070073final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070075 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
77 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
Craig Mautner29219d92013-04-16 20:19:12 -070078 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
80 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
81 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
82 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
83 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070084 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig Mautnera9a3fb12013-04-18 10:01:00 -070085 static final boolean DEBUG_STACK = ActivityManagerService.DEBUG_STACK;
Craig Mautner9658b312013-02-28 10:55:59 -080086
Craig Mautner2420ead2013-04-01 17:13:20 -070087 static final boolean DEBUG_STATES = ActivityStackSupervisor.DEBUG_STATES;
88 static final boolean DEBUG_ADD_REMOVE = ActivityStackSupervisor.DEBUG_ADD_REMOVE;
89 static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE;
90 static final boolean DEBUG_APP = ActivityStackSupervisor.DEBUG_APP;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070091
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094 // How long we wait until giving up on the last activity telling us it
95 // is idle.
96 static final int IDLE_TIMEOUT = 10*1000;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070097
98 // Ticks during which we check progress while waiting for an app to launch.
99 static final int LAUNCH_TICK = 500;
100
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700101 // How long we wait until giving up on the last activity to pause. This
102 // is short because it directly impacts the responsiveness of starting the
103 // next activity.
104 static final int PAUSE_TIMEOUT = 500;
105
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700106 // How long we wait for the activity to tell us it has stopped before
107 // giving up. This is a good amount of time because we really need this
108 // from the application in order to get its saved state.
109 static final int STOP_TIMEOUT = 10*1000;
110
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800111 // How long we can hold the sleep wake lock before giving up.
112 static final int SLEEP_TIMEOUT = 5*1000;
113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700114 // How long we can hold the launch wake lock before giving up.
115 static final int LAUNCH_TIMEOUT = 10*1000;
116
117 // How long we wait until giving up on an activity telling us it has
118 // finished destroying itself.
119 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800122 // disabled.
123 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700125 // How long between activity launches that we consider safe to not warn
126 // the user about an unexpected activity being launched on top.
127 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129 // Set to false to disable the preview that is shown while a new activity
130 // is being started.
131 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133 enum ActivityState {
134 INITIALIZING,
135 RESUMED,
136 PAUSING,
137 PAUSED,
138 STOPPING,
139 STOPPED,
140 FINISHING,
141 DESTROYING,
142 DESTROYED
143 }
144
145 final ActivityManagerService mService;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800146
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700147 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700149 /**
150 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800151 * running) activities. It contains #TaskRecord objects.
152 */
153 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
154
155 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800156 * Used for validating app tokens with window manager.
157 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800158 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160 /**
161 * List of running activities, sorted by recent usage.
162 * The first entry in the list is the least recently used.
163 * It contains HistoryRecord objects.
164 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700165 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800168 * List of activities that are in the process of going to sleep.
169 */
170 final ArrayList<ActivityRecord> mGoingToSleepActivities
171 = new ArrayList<ActivityRecord>();
172
173 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174 * Animations that for the current transition have requested not to
175 * be considered for the transition animation.
176 */
177 final ArrayList<ActivityRecord> mNoAnimActivities
178 = new ArrayList<ActivityRecord>();
179
180 /**
181 * List of activities that are ready to be finished, but waiting
182 * for the previous activity to settle down before doing so. It contains
183 * HistoryRecord objects.
184 */
185 final ArrayList<ActivityRecord> mFinishingActivities
186 = new ArrayList<ActivityRecord>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800187
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700188 /**
189 * List of people waiting to find out about the next launched activity.
190 */
191 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched
192 = new ArrayList<IActivityManager.WaitResult>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800193
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194 /**
195 * List of people waiting to find out about the next visible activity.
196 */
197 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible
198 = new ArrayList<IActivityManager.WaitResult>();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700199
200 final ArrayList<UserStartedState> mStartingUsers
201 = new ArrayList<UserStartedState>();
202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 /**
204 * Set when the system is going to sleep, until we have
205 * successfully paused the current activity and released our wake lock.
206 * At that point the system is allowed to actually sleep.
207 */
208 final PowerManager.WakeLock mGoingToSleep;
209
210 /**
211 * We don't want to allow the device to go to sleep while in the process
212 * of launching an activity. This is primarily to allow alarm intent
213 * receivers to launch an activity and get that to run before the device
214 * goes back to sleep.
215 */
216 final PowerManager.WakeLock mLaunchingActivity;
217
218 /**
219 * When we are in the process of pausing an activity, before starting the
220 * next one, this variable holds the activity that is currently being paused.
221 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800222 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700223
224 /**
225 * This is the last activity that we put into the paused state. This is
226 * used to determine if we need to do an activity transition while sleeping,
227 * when we normally hold the top activity paused.
228 */
229 ActivityRecord mLastPausedActivity = null;
230
231 /**
232 * Current activity that is resumed, or null if there is none.
233 */
234 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700236 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700237 * This is the last activity that has been started. It is only used to
238 * identify when multiple activities are started at once so that the user
239 * can be warned they may not be in the activity they think they are.
240 */
241 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800242
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700243 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700244 * Set when we know we are going to be calling updateConfiguration()
245 * soon, so want to skip intermediate config checks.
246 */
247 boolean mConfigWillChange;
248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800250
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800251 /**
252 * Set when we have taken too long waiting to go to sleep.
253 */
254 boolean mSleepTimeout = false;
255
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700256 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800257 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
258 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
259 */
260 private ActivityRecord mLastScreenshotActivity = null;
261 private Bitmap mLastScreenshotBitmap = null;
262
Craig Mautnercae015f2013-02-08 14:31:27 -0800263 /**
264 * List of ActivityRecord objects that have been finished and must
265 * still report back to a pending thumbnail receiver.
266 */
267 private final ArrayList<ActivityRecord> mCancelledThumbnails = new ArrayList<ActivityRecord>();
268
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800269 int mThumbnailWidth = -1;
270 int mThumbnailHeight = -1;
271
Amith Yamasani742a6712011-05-04 14:49:28 -0700272 private int mCurrentUser;
273
Craig Mautnerc00204b2013-03-05 15:02:14 -0800274 final int mStackId;
275
Craig Mautner27084302013-03-25 08:05:25 -0700276 /** Run all ActivityStacks through this */
277 final ActivityStackSupervisor mStackSupervisor;
278
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800279 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
280 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
281 static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
282 static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
283 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
284 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
285 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn29ba7e62012-03-16 15:03:36 -0700286 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700287 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700288 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9;
289
290 static class ScheduleDestroyArgs {
291 final ProcessRecord mOwner;
292 final boolean mOomAdj;
293 final String mReason;
294 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
295 mOwner = owner;
296 mOomAdj = oomAdj;
297 mReason = reason;
298 }
299 }
300
Zoran Marcetaf958b322012-08-09 20:27:12 +0900301 final Handler mHandler;
302
303 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 //public Handler() {
305 // if (localLOGV) Slog.v(TAG, "Handler started!");
306 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900307 public ActivityStackHandler(Looper looper) {
308 super(looper);
309 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700310
Zoran Marcetaf958b322012-08-09 20:27:12 +0900311 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700312 public void handleMessage(Message msg) {
313 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800314 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700315 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700316 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700317 Slog.w(TAG, "Sleep timeout! Sleeping now.");
318 mSleepTimeout = true;
319 checkReadyForSleepLocked();
320 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800321 }
322 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700323 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800324 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700325 // We don't at this point know if the activity is fullscreen,
326 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800327 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700328 synchronized (mService) {
329 if (r.app != null) {
330 mService.logAppTooSlow(r.app, r.pauseTime,
331 "pausing " + r);
332 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700333
Craig Mautnerd2328952013-03-05 12:46:26 -0800334 activityPausedLocked(r != null ? r.appToken : null, true);
335 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 } break;
337 case IDLE_TIMEOUT_MSG: {
338 if (mService.mDidDexOpt) {
339 mService.mDidDexOpt = false;
340 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
341 nmsg.obj = msg.obj;
342 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
343 return;
344 }
345 // We don't at this point know if the activity is fullscreen,
346 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800347 ActivityRecord r = (ActivityRecord)msg.obj;
348 Slog.w(TAG, "Activity idle timeout for " + r);
Craig Mautner6170f732013-04-02 13:05:23 -0700349 synchronized (mService) {
Craig Mautnere79d42682013-04-01 19:01:53 -0700350 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700352 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700353 case LAUNCH_TICK_MSG: {
354 ActivityRecord r = (ActivityRecord)msg.obj;
355 synchronized (mService) {
356 if (r.continueLaunchTickingLocked()) {
357 mService.logAppTooSlow(r.app, r.launchTickTime,
358 "launching " + r);
359 }
360 }
361 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800363 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700364 // We don't at this point know if the activity is fullscreen,
365 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800366 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800367 synchronized (mService) {
368 activityDestroyedLocked(r != null ? r.appToken : null);
369 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700370 } break;
371 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800372 ActivityRecord r = (ActivityRecord)msg.obj;
Craig Mautnere79d42682013-04-01 19:01:53 -0700373 synchronized (mService) {
374 activityIdleInternalLocked(r != null ? r.appToken : null, false, null);
375 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 } break;
377 case LAUNCH_TIMEOUT_MSG: {
378 if (mService.mDidDexOpt) {
379 mService.mDidDexOpt = false;
380 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
381 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
382 return;
383 }
384 synchronized (mService) {
385 if (mLaunchingActivity.isHeld()) {
386 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
387 mLaunchingActivity.release();
388 }
389 }
390 } break;
391 case RESUME_TOP_ACTIVITY_MSG: {
392 synchronized (mService) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700393 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 }
395 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700396 case STOP_TIMEOUT_MSG: {
397 ActivityRecord r = (ActivityRecord)msg.obj;
398 // We don't at this point know if the activity is fullscreen,
399 // so we need to be conservative and assume it isn't.
400 Slog.w(TAG, "Activity stop timeout for " + r);
401 synchronized (mService) {
402 if (r.isInHistory()) {
403 activityStoppedLocked(r, null, null, null);
404 }
405 }
406 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700407 case DESTROY_ACTIVITIES_MSG: {
408 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
409 synchronized (mService) {
410 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
411 }
412 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700413 }
414 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800415 }
416
Craig Mautner000f0022013-02-26 15:04:29 -0800417 private int numActivities() {
418 int count = 0;
419 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
420 count += mTaskHistory.get(taskNdx).mActivities.size();
421 }
422 return count;
423 }
424
Craig Mautner2219a1b2013-03-25 09:44:30 -0700425 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId,
Craig Mautner2420ead2013-04-01 17:13:20 -0700426 ActivityStackSupervisor supervisor, int userId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900427 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 mService = service;
429 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700430 PowerManager pm =
431 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
432 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
433 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
434 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800435 mStackId = stackId;
Craig Mautner27084302013-03-25 08:05:25 -0700436 mStackSupervisor = supervisor;
Craig Mautner2420ead2013-04-01 17:13:20 -0700437 mCurrentUser = userId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 }
Craig Mautner5962b122012-10-05 14:45:52 -0700439
440 private boolean okToShow(ActivityRecord r) {
441 return r.userId == mCurrentUser
442 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
443 }
444
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700445 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800446 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
447 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800448 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800449 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
450 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800451 if (!r.finishing && r != notTop && okToShow(r)) {
452 return r;
453 }
454 }
455 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700456 return null;
457 }
458
459 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800460 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
461 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800462 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800463 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
464 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800465 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
466 return r;
467 }
468 }
469 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700470 return null;
471 }
472
473 /**
474 * This is a simplified version of topRunningActivityLocked that provides a number of
475 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800476 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700477 * @param token If non-null, any history records matching this token will be skipped.
478 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800479 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700480 * @return Returns the HistoryRecord of the next activity on the stack.
481 */
482 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800483 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
484 TaskRecord task = mTaskHistory.get(taskNdx);
485 if (task.taskId == taskId) {
486 continue;
487 }
488 ArrayList<ActivityRecord> activities = task.mActivities;
489 for (int i = activities.size() - 1; i >= 0; --i) {
490 final ActivityRecord r = activities.get(i);
491 // Note: the taskId check depends on real taskId fields being non-zero
492 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
493 return r;
494 }
495 }
496 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700497 return null;
498 }
499
Craig Mautner8849a5e2013-04-02 16:41:03 -0700500 final ActivityRecord topActivity() {
501 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700502 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700503 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
504 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
505 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700506 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700507 }
508 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700509 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700510 }
511
Craig Mautnerd2328952013-03-05 12:46:26 -0800512 TaskRecord taskForIdLocked(int id) {
513 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
514 final TaskRecord task = mTaskHistory.get(taskNdx);
515 if (task.taskId == id) {
516 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800517 }
518 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700519 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700520 }
521
Craig Mautnerd2328952013-03-05 12:46:26 -0800522 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800523 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800524 if (r != null) {
525 final TaskRecord task = r.task;
526 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
527 if (task.stack != this) Slog.w(TAG,
528 "Illegal state! task does not point to stack it is in.");
529 return r;
530 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800531 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800532 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800533 }
534
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700535 boolean containsApp(ProcessRecord app) {
536 if (app == null) {
537 return false;
538 }
539 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
540 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
541 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
542 final ActivityRecord r = activities.get(activityNdx);
543 if (r.finishing) {
544 continue;
545 }
546 if (r.app == app) {
547 return true;
548 }
549 }
550 }
551 return false;
552 }
553
Craig Mautner2420ead2013-04-01 17:13:20 -0700554 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700555 final boolean hadit = mLRUActivities.remove(r);
556 mLRUActivities.add(r);
557 return hadit;
558 }
559
Craig Mautnerde4ef022013-04-07 19:01:33 -0700560 final boolean isHomeStack() {
561 return mStackId == HOME_STACK_ID;
562 }
563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700564 /**
565 * Returns the top activity in any existing task matching the given
566 * Intent. Returns null if no such task is found.
567 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700568 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700569 ComponentName cls = intent.getComponent();
570 if (info.targetActivity != null) {
571 cls = new ComponentName(info.packageName, info.targetActivity);
572 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700573 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800574
575 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
576 final TaskRecord task = mTaskHistory.get(taskNdx);
577 final ActivityRecord r = task.getTopActivity();
578 if (r == null || r.finishing || r.userId != userId ||
579 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
580 continue;
581 }
582
583 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
584 // + "/aff=" + r.task.affinity + " to new cls="
585 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
586 if (task.affinity != null) {
587 if (task.affinity.equals(info.taskAffinity)) {
588 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700589 return r;
590 }
Craig Mautner000f0022013-02-26 15:04:29 -0800591 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
592 //Slog.i(TAG, "Found matching class!");
593 //dump();
594 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
595 return r;
596 } else if (task.affinityIntent != null
597 && task.affinityIntent.getComponent().equals(cls)) {
598 //Slog.i(TAG, "Found matching class!");
599 //dump();
600 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
601 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602 }
603 }
604
605 return null;
606 }
607
608 /**
609 * Returns the first activity (starting from the top of the stack) that
610 * is the same as the given activity. Returns null if no such activity
611 * is found.
612 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700613 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700614 ComponentName cls = intent.getComponent();
615 if (info.targetActivity != null) {
616 cls = new ComponentName(info.packageName, info.targetActivity);
617 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700618 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700619
Craig Mautner000f0022013-02-26 15:04:29 -0800620 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
621 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
622 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
623 ActivityRecord r = activities.get(activityNdx);
624 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700625 //Slog.i(TAG, "Found matching class!");
626 //dump();
627 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
628 return r;
629 }
630 }
631 }
632
633 return null;
634 }
635
Amith Yamasani742a6712011-05-04 14:49:28 -0700636 /*
637 * Move the activities around in the stack to bring a user to the foreground.
638 * @return whether there are any activities for the specified user.
639 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700640 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800641 if (VALIDATE_TOKENS) {
642 validateAppTokensLocked();
643 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700644 mStartingUsers.add(uss);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800645 if (mCurrentUser == userId) {
646 return true;
647 }
648 mCurrentUser = userId;
649
650 // Move userId's tasks to the top.
651 boolean haveActivities = false;
652 TaskRecord task = null;
653 int index = mTaskHistory.size();
654 for (int i = 0; i < index; ++i) {
655 task = mTaskHistory.get(i);
656 if (task.userId == userId) {
657 haveActivities = true;
658 mTaskHistory.remove(i);
659 mTaskHistory.add(task);
660 --index;
661 }
662 }
663
664 // task is now the original topmost TaskRecord. Transition from the old top to the new top.
665 ActivityRecord top = task != null ? task.getTopActivity() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800666 resumeTopActivityLocked(top);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700667 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700668 }
669
Craig Mautner2420ead2013-04-01 17:13:20 -0700670 void minimalResumeActivityLocked(ActivityRecord r) {
671 r.state = ActivityState.RESUMED;
672 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
673 + " (starting new instance)");
674 r.stopped = false;
675 mResumedActivity = r;
676 r.task.touchActiveTime();
677 mService.addRecentTaskLocked(r.task);
678 completeResumeLocked(r);
679 checkReadyForSleepLocked();
680 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 }
682
Craig Mautnere79d42682013-04-01 19:01:53 -0700683 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700684 if (r.launchTime == 0) {
685 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700686 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700687 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700688 }
689 } else if (mInitialStartTime == 0) {
690 mInitialStartTime = SystemClock.uptimeMillis();
691 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700692 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800693
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800694 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700695 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700696 if (!mGoingToSleep.isHeld()) {
697 mGoingToSleep.acquire();
698 if (mLaunchingActivity.isHeld()) {
699 mLaunchingActivity.release();
700 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
701 }
702 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800703 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
704 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
705 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
706 checkReadyForSleepLocked();
707 }
708 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700709
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800710 void awakeFromSleepingLocked() {
711 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
712 mSleepTimeout = false;
713 if (mGoingToSleep.isHeld()) {
714 mGoingToSleep.release();
715 }
716 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800717 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
718 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
719 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
720 activities.get(activityNdx).setSleeping(false);
721 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800722 }
723 mGoingToSleepActivities.clear();
724 }
725
726 void activitySleptLocked(ActivityRecord r) {
727 mGoingToSleepActivities.remove(r);
728 checkReadyForSleepLocked();
729 }
730
Craig Mautnere79d42682013-04-01 19:01:53 -0700731 // Checked.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800732 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700733 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800734 // Do not care.
735 return;
736 }
737
738 if (!mSleepTimeout) {
739 if (mResumedActivity != null) {
740 // Still have something resumed; can't sleep until it is paused.
741 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700742 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
743 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800744 return;
745 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800746 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800747 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800748 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800749 return;
750 }
751
Craig Mautnerde4ef022013-04-07 19:01:33 -0700752 if (mStackSupervisor.mStoppingActivities.size() > 0) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800753 // Still need to tell some activities to stop; can't sleep yet.
754 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
Craig Mautnerde4ef022013-04-07 19:01:33 -0700755 + mStackSupervisor.mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700756 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800757 return;
758 }
759
760 ensureActivitiesVisibleLocked(null, 0);
761
762 // Make sure any stopped but visible activities are now sleeping.
763 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800764 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
765 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
766 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
767 final ActivityRecord r = activities.get(activityNdx);
768 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
769 r.setSleeping(true);
770 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800771 }
772 }
773
774 if (mGoingToSleepActivities.size() > 0) {
775 // Still need to tell some activities to sleep; can't sleep yet.
776 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
777 + mGoingToSleepActivities.size() + " activities");
778 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700779 }
780 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800781
782 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
783
784 if (mGoingToSleep.isHeld()) {
785 mGoingToSleep.release();
786 }
787 if (mService.mShuttingDown) {
788 mService.notifyAll();
789 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700790 }
Craig Mautner59c00972012-07-30 12:10:24 -0700791
Dianne Hackbornd2835932010-12-13 16:28:46 -0800792 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800793 if (who.noDisplay) {
794 return null;
795 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800796
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800797 Resources res = mService.mContext.getResources();
798 int w = mThumbnailWidth;
799 int h = mThumbnailHeight;
800 if (w < 0) {
801 mThumbnailWidth = w =
802 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
803 mThumbnailHeight = h =
804 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
805 }
806
807 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800808 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
809 || mLastScreenshotBitmap.getWidth() != w
810 || mLastScreenshotBitmap.getHeight() != h) {
811 mLastScreenshotActivity = who;
812 mLastScreenshotBitmap = mService.mWindowManager.screenshotApplications(
813 who.appToken, Display.DEFAULT_DISPLAY, w, h);
814 }
815 if (mLastScreenshotBitmap != null) {
816 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
817 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800818 }
819 return null;
820 }
821
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800823 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800824 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700825 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800826 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 ActivityRecord prev = mResumedActivity;
828 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700829 Slog.e(TAG, "Trying to pause when nothing is resumed",
830 new RuntimeException("here").fillInStackTrace());
Craig Mautnerde4ef022013-04-07 19:01:33 -0700831 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 return;
833 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700834 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
835 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800837 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700838 mLastPausedActivity = prev;
839 prev.state = ActivityState.PAUSING;
840 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700841 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842
843 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800844
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845 if (prev.app != null && prev.app.thread != null) {
846 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
847 try {
848 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700849 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800851 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
852 userLeaving, prev.configChangeFlags);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700853 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 mService.updateUsageStats(prev, false);
855 }
856 } catch (Exception e) {
857 // Ignore exception, if process died other code will cleanup.
858 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800859 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 mLastPausedActivity = null;
861 }
862 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800863 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700864 mLastPausedActivity = null;
865 }
866
867 // If we are not going to sleep, we want to ensure the device is
868 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700869 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700870 mLaunchingActivity.acquire();
871 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
872 // To be safe, don't allow the wake lock to be held for too long.
873 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
874 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
875 }
876 }
877
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800878 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 // Have the window manager pause its key dispatching until the new
880 // activity has started. If we're pausing the activity just because
881 // the screen is being turned off and the UI is sleeping, don't interrupt
882 // key dispatch; the same activity will pick it up again on wakeup.
883 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800884 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700885 } else {
886 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
887 }
888
889 // Schedule a pause timeout in case the app doesn't respond.
890 // We don't give it much time because this directly impacts the
891 // responsiveness seen by the user.
892 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
893 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700894 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700895 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
896 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
897 } else {
898 // This activity failed to schedule the
899 // pause, so just treat it as being paused now.
900 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700901 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700902 }
903 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700904
Craig Mautnerd2328952013-03-05 12:46:26 -0800905 final void activityResumedLocked(IBinder token) {
906 final ActivityRecord r = ActivityRecord.forToken(token);
907 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
908 r.icicle = null;
909 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700910 }
911
Craig Mautnerd2328952013-03-05 12:46:26 -0800912 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800914 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700915
Craig Mautnerd2328952013-03-05 12:46:26 -0800916 final ActivityRecord r = isInStackLocked(token);
917 if (r != null) {
918 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
919 if (mPausingActivity == r) {
920 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
921 + (timeout ? " (due to timeout)" : " (pause complete)"));
922 r.state = ActivityState.PAUSED;
923 completePauseLocked();
924 } else {
925 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
926 r.userId, System.identityHashCode(r), r.shortComponentName,
927 mPausingActivity != null
928 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700929 }
930 }
931 }
932
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700933 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
934 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700935 if (r.state != ActivityState.STOPPING) {
936 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
937 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
938 return;
939 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700940 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700941 if (icicle != null) {
942 // If icicle is null, this is happening due to a timeout, so we
943 // haven't really saved the state.
944 r.icicle = icicle;
945 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800946 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700947 r.updateThumbnail(thumbnail, description);
948 }
949 if (!r.stopped) {
950 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
951 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
952 r.stopped = true;
953 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700954 if (r.finishing) {
955 r.clearOptionsLocked();
956 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700957 if (r.configDestroy) {
958 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700959 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700960 } else {
961 // Now that this process has stopped, we may want to consider
962 // it to be the previous app to try to keep around in case
963 // the user wants to return to it.
964 ProcessRecord fgApp = null;
965 if (mResumedActivity != null) {
966 fgApp = mResumedActivity.app;
967 } else if (mPausingActivity != null) {
968 fgApp = mPausingActivity.app;
969 }
970 if (r.app != null && fgApp != null && r.app != fgApp
971 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
972 && r.app != mService.mHomeProcess) {
973 mService.mPreviousProcess = r.app;
974 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
975 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800976 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700977 }
978 }
979 }
980
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800981 private final void completePauseLocked() {
982 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700983 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800984
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800985 if (prev != null) {
986 if (prev.finishing) {
987 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700988 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 } else if (prev.app != null) {
990 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
991 if (prev.waitingVisible) {
992 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700993 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800994 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
995 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800996 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800997 if (prev.configDestroy) {
998 // The previous is being paused because the configuration
999 // is changing, which means it is actually stopping...
1000 // To juggle the fact that we are also starting a new
1001 // instance right now, we need to first completely stop
1002 // the current instance before starting the new one.
1003 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
1004 destroyActivityLocked(prev, true, false, "pause-config");
1005 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001006 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001007 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1008 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001009 // If we already have a few activities waiting to stop,
1010 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001011 // them out. Or if r is the last of activity of the last task the stack
1012 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001013 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
1014 scheduleIdleLocked();
1015 } else {
1016 checkReadyForSleepLocked();
1017 }
1018 }
1019 } else {
1020 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1021 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001022 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001023 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001025
Craig Mautnerde4ef022013-04-07 19:01:33 -07001026 final ActivityStack topStack = mStackSupervisor.getTopStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -07001027 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001028 topStack.resumeTopActivityLocked(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001029 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001030 checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001031 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001032 if (top == null || (prev != null && top != prev)) {
1033 // If there are no more activities available to run,
1034 // do resume anyway to start something. Also if the top
1035 // activity on the stack is not the just paused activity,
1036 // we need to go ahead and resume it to ensure we complete
1037 // an in-flight app switch.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001038 topStack.resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001039 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001041
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001042 if (prev != null) {
1043 prev.resumeKeyDispatchingLocked();
1044 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001045
1046 if (prev.app != null && prev.cpuTimeAtResume > 0
1047 && mService.mBatteryStatsService.isOnBattery()) {
1048 long diff = 0;
1049 synchronized (mService.mProcessStatsThread) {
1050 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1051 - prev.cpuTimeAtResume;
1052 }
1053 if (diff > 0) {
1054 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1055 synchronized (bsi) {
1056 BatteryStatsImpl.Uid.Proc ps =
1057 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1058 prev.info.packageName);
1059 if (ps != null) {
1060 ps.addForegroundTimeLocked(diff);
1061 }
1062 }
1063 }
1064 }
1065 prev.cpuTimeAtResume = 0; // reset it
1066 }
1067
Craig Mautnere79d42682013-04-01 19:01:53 -07001068 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001069 /**
1070 * Once we know that we have asked an application to put an activity in
1071 * the resumed state (either by launching it or explicitly telling it),
1072 * this function updates the rest of our state to match that fact.
1073 */
1074 private final void completeResumeLocked(ActivityRecord next) {
1075 next.idle = false;
1076 next.results = null;
1077 next.newIntents = null;
1078
1079 // schedule an idle timeout in case the app doesn't do it for us.
1080 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1081 msg.obj = next;
1082 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1083
1084 if (false) {
1085 // The activity was never told to pause, so just keep
1086 // things going as-is. To maintain our own state,
1087 // we need to emulate it coming back and saying it is
1088 // idle.
1089 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1090 msg.obj = next;
1091 mHandler.sendMessage(msg);
1092 }
1093
Craig Mautnerde4ef022013-04-07 19:01:33 -07001094 if (mStackSupervisor.isFrontStack(this)) {
1095 // TODO: Should this be done for all stacks, not just mMainStack?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096 mService.reportResumedActivityLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001097 mService.setFocusedActivityLocked(next);
1098 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001099 if (mStackSupervisor.allResumedActivitiesComplete()) {
1100 next.resumeKeyDispatchingLocked();
1101 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
1102 mService.mWindowManager.executeAppTransition();
1103 mNoAnimActivities.clear();
1104 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001105
1106 // Mark the point when the activity is resuming
1107 // TODO: To be more accurate, the mark should be before the onCreate,
1108 // not after the onResume. But for subsequent starts, onResume is fine.
1109 if (next.app != null) {
1110 synchronized (mService.mProcessStatsThread) {
1111 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1112 }
1113 } else {
1114 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1115 }
1116 }
1117
Craig Mautnere79d42682013-04-01 19:01:53 -07001118 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001119 /**
1120 * Make sure that all activities that need to be visible (that is, they
1121 * currently can be seen by the user) actually are.
1122 */
1123 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1124 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1125 if (DEBUG_VISBILITY) Slog.v(
1126 TAG, "ensureActivitiesVisible behind " + top
1127 + " configChanges=0x" + Integer.toHexString(configChanges));
1128
1129 // If the top activity is not fullscreen, then we need to
1130 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001131 boolean aboveTop = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001132 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this);
1133 int taskNdx;
1134 for (taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001135 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1136 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1137 final ActivityRecord r = activities.get(activityNdx);
1138 if (r.finishing) {
1139 continue;
1140 }
1141 if (aboveTop && r != top) {
1142 continue;
1143 }
1144 aboveTop = false;
1145 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001147 TAG, "Make visible? " + r + " finishing=" + r.finishing
1148 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001149
Craig Mautnerd44711d2013-02-23 11:24:36 -08001150 final boolean doThisProcess = onlyThisProcess == null
1151 || onlyThisProcess.equals(r.processName);
1152
1153 // First: if this is not the current activity being started, make
1154 // sure it matches the current configuration.
1155 if (r != starting && doThisProcess) {
1156 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001157 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001158
1159 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001160 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001161 // This activity needs to be visible, but isn't even
1162 // running... get it started, but don't resume it
1163 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001164 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001165 if (r != starting) {
1166 r.startFreezingScreenLocked(r.app, configChanges);
1167 }
1168 if (!r.visible) {
1169 if (DEBUG_VISBILITY) Slog.v(
1170 TAG, "Starting and making visible: " + r);
1171 mService.mWindowManager.setAppVisibility(r.appToken, true);
1172 }
1173 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001174 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001175 }
1176 }
1177
1178 } else if (r.visible) {
1179 // If this activity is already visible, then there is nothing
1180 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001181 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001182 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001183
1184 } else if (onlyThisProcess == null) {
1185 // This activity is not currently visible, but is running.
1186 // Tell it to become visible.
1187 r.visible = true;
1188 if (r.state != ActivityState.RESUMED && r != starting) {
1189 // If this activity is paused, tell it
1190 // to now show its window.
1191 if (DEBUG_VISBILITY) Slog.v(
1192 TAG, "Making visible and scheduling visibility: " + r);
1193 try {
1194 mService.mWindowManager.setAppVisibility(r.appToken, true);
1195 r.sleeping = false;
1196 r.app.pendingUiClean = true;
1197 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1198 r.stopFreezingScreenLocked(false);
1199 } catch (Exception e) {
1200 // Just skip on any failure; we'll make it
1201 // visible when it next restarts.
1202 Slog.w(TAG, "Exception thrown making visibile: "
1203 + r.intent.getComponent(), e);
1204 }
1205 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001206 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207
Craig Mautnerd44711d2013-02-23 11:24:36 -08001208 // Aggregate current change flags.
1209 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001210
Craig Mautnerd44711d2013-02-23 11:24:36 -08001211 if (r.fullscreen) {
1212 // At this point, nothing else needs to be shown
1213 if (DEBUG_VISBILITY) Slog.v(
1214 TAG, "Stopping: fullscreen at " + r);
1215 behindFullscreen = true;
1216 }
1217 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001218 if (DEBUG_VISBILITY) Slog.v(
1219 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1220 + " state=" + r.state
1221 + " behindFullscreen=" + behindFullscreen);
1222 // Now for any activities that aren't visible to the user, make
1223 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001224 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001225 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001226 r.visible = false;
1227 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001228 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001229 if ((r.state == ActivityState.STOPPING
1230 || r.state == ActivityState.STOPPED)
1231 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001232 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001233 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234 }
1235 } catch (Exception e) {
1236 // Just skip on any failure; we'll make it
1237 // visible when it next restarts.
1238 Slog.w(TAG, "Exception thrown making hidden: "
1239 + r.intent.getComponent(), e);
1240 }
1241 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001242 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001243 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001244 }
1245 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001246 }
1247 }
1248
Craig Mautnere79d42682013-04-01 19:01:53 -07001249 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001250 /**
1251 * Version of ensureActivitiesVisible that can easily be called anywhere.
1252 */
1253 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1254 int configChanges) {
1255 ActivityRecord r = topRunningActivityLocked(null);
1256 if (r != null) {
1257 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1258 }
1259 }
Craig Mautner58547802013-03-05 08:23:53 -08001260
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001261 /**
1262 * Ensure that the top activity in the stack is resumed.
1263 *
1264 * @param prev The previously resumed activity, for when in the process
1265 * of pausing; can be null to call from elsewhere.
1266 *
1267 * @return Returns true if something is being resumed, or false if
1268 * nothing happened.
1269 */
1270 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001271 return resumeTopActivityLocked(prev, null);
1272 }
1273
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07001274 // Checked.
Dianne Hackborn84375872012-06-01 19:03:50 -07001275 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001276 // Find the first activity that is not finishing.
1277 ActivityRecord next = topRunningActivityLocked(null);
1278
1279 // Remember how we'll process this pause/resume situation, and ensure
1280 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001281 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1282 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001283
1284 if (next == null) {
1285 // There are no more activities! Let's just start up the
1286 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001287 ActivityOptions.abort(options);
Craig Mautner69ada552013-04-18 13:51:51 -07001288 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001289 }
1290
1291 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001292
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001293 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001294 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1295 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296 // Make sure we have executed any pending transitions, since there
1297 // should be nothing left to do at this point.
1298 mService.mWindowManager.executeAppTransition();
1299 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001300 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001301 return false;
1302 }
1303
Craig Mautnerde4ef022013-04-07 19:01:33 -07001304 if (prev != null && prev.mLaunchHomeTaskNext && prev.finishing &&
1305 prev.task.getTopActivity() == null) {
Craig Mautner69ada552013-04-18 13:51:51 -07001306 return mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001307 }
1308
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001309 // If we are sleeping, and there is no resumed activity, and the top
1310 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001311 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001312 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001313 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001314 // Make sure we have executed any pending transitions, since there
1315 // should be nothing left to do at this point.
1316 mService.mWindowManager.executeAppTransition();
1317 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001318 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 return false;
1320 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001321
1322 // Make sure that the user who owns this activity is started. If not,
1323 // we will just leave it as is because someone should be bringing
1324 // another user's activities to the top of the stack.
1325 if (mService.mStartedUsers.get(next.userId) == null) {
1326 Slog.w(TAG, "Skipping resume of top activity " + next
1327 + ": user " + next.userId + " is stopped");
1328 return false;
1329 }
1330
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001331 // The activity may be waiting for stop, but that is no longer
1332 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001333 mStackSupervisor.mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001334 mGoingToSleepActivities.remove(next);
1335 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001336 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001337
Dianne Hackborn84375872012-06-01 19:03:50 -07001338 next.updateOptionsLocked(options);
1339
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001340 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1341
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001342 // If we are currently pausing an activity, then don't do anything
1343 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001344 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1345 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001346 return false;
1347 }
1348
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001349 // Okay we are now going to start a switch, to 'next'. We may first
1350 // have to pause the current activity, but this is an important point
1351 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001352 // XXX "App Redirected" dialog is getting too many false positives
1353 // at this point, so turn off for now.
1354 if (false) {
1355 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1356 long now = SystemClock.uptimeMillis();
1357 final boolean inTime = mLastStartedActivity.startTime != 0
1358 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1359 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1360 final int nextUid = next.info.applicationInfo.uid;
1361 if (inTime && lastUid != nextUid
1362 && lastUid != next.launchedFromUid
1363 && mService.checkPermission(
1364 android.Manifest.permission.STOP_APP_SWITCHES,
1365 -1, next.launchedFromUid)
1366 != PackageManager.PERMISSION_GRANTED) {
1367 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1368 } else {
1369 next.startTime = now;
1370 mLastStartedActivity = next;
1371 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001372 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001373 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001374 mLastStartedActivity = next;
1375 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001376 }
Craig Mautner58547802013-03-05 08:23:53 -08001377
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001378 // We need to start pausing the current activity so the top one
1379 // can be resumed...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001380 final ActivityStack lastStack = mStackSupervisor.getLastStack();
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001381 if (lastStack != null && (isHomeStack() ^ lastStack.isHomeStack()) &&
1382 lastStack.mResumedActivity != null) {
Craig Mautner69ada552013-04-18 13:51:51 -07001383 // TODO: Don't pause when launching to the sibling task.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001384 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001385 // At this point we want to put the upcoming activity's process
1386 // at the top of the LRU list, since we know we will be needing it
1387 // very soon and it would be a waste to let it get killed if it
1388 // happens to be sitting towards the end.
1389 if (next.app != null && next.app.thread != null) {
1390 // No reason to do full oom adj update here; we'll let that
1391 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001392 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001393 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001394 lastStack.startPausingLocked(userLeaving, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001395 return true;
1396 }
1397
Christopher Tated3f175c2012-06-14 14:16:54 -07001398 // If the most recent activity was noHistory but was only stopped rather
1399 // than stopped+finished because the device went to sleep, we need to make
1400 // sure to finish it as we're making a new activity topmost.
1401 final ActivityRecord last = mLastPausedActivity;
1402 if (mService.mSleeping && last != null && !last.finishing) {
1403 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1404 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1405 if (DEBUG_STATES) {
1406 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1407 }
1408 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001409 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001410 }
1411 }
1412
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 if (prev != null && prev != next) {
1414 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1415 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001416 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417 if (DEBUG_SWITCH) Slog.v(
1418 TAG, "Resuming top, waiting visible to hide: " + prev);
1419 } else {
1420 // The next activity is already visible, so hide the previous
1421 // activity's windows right now so we can show the new one ASAP.
1422 // We only do this if the previous is finishing, which should mean
1423 // it is on top of the one being resumed so hiding it quickly
1424 // is good. Otherwise, we want to do the normal route of allowing
1425 // the resumed activity to be shown so we can decide if the
1426 // previous should actually be hidden depending on whether the
1427 // new one is found to be full-screen or not.
1428 if (prev.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001429 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1431 + prev + ", waitingVisible="
1432 + (prev != null ? prev.waitingVisible : null)
1433 + ", nowVisible=" + next.nowVisible);
1434 } else {
1435 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1436 + prev + ", waitingVisible="
1437 + (prev != null ? prev.waitingVisible : null)
1438 + ", nowVisible=" + next.nowVisible);
1439 }
1440 }
1441 }
1442
Dianne Hackborne7f97212011-02-24 14:40:20 -08001443 // Launching this app's activity, make sure the app is no longer
1444 // considered stopped.
1445 try {
1446 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001447 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001448 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001449 } catch (IllegalArgumentException e) {
1450 Slog.w(TAG, "Failed trying to unstop package "
1451 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001452 }
1453
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 // We are starting up the next activity, so tell the window manager
1455 // that the previous one will be hidden soon. This way it can know
1456 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001457 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458 if (prev != null) {
1459 if (prev.finishing) {
1460 if (DEBUG_TRANSITION) Slog.v(TAG,
1461 "Prepare close transition: prev=" + prev);
1462 if (mNoAnimActivities.contains(prev)) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001463 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001464 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001465 } else {
1466 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001467 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1468 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001470 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1471 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001472 } else {
1473 if (DEBUG_TRANSITION) Slog.v(TAG,
1474 "Prepare open transition: prev=" + prev);
1475 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001476 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001477 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001478 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 } else {
1480 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001481 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1482 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 }
1484 }
1485 if (false) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001486 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1487 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 }
Craig Mautner967212c2013-04-13 21:10:58 -07001489 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 if (DEBUG_TRANSITION) Slog.v(TAG,
1491 "Prepare open transition: no previous");
1492 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001493 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001494 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001495 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001497 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001498 AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001499 }
1500 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001501 if (!noAnim) {
1502 next.applyOptionsLocked();
1503 } else {
1504 next.clearOptionsLocked();
1505 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506
1507 if (next.app != null && next.app.thread != null) {
1508 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1509
1510 // This activity is now becoming visible.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001511 mService.mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001513 // schedule launch ticks to collect information about slow apps.
1514 next.startLaunchTickingLocked();
1515
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001516 ActivityRecord lastResumedActivity =
1517 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001518 ActivityState lastState = next.state;
1519
1520 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001521
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001522 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523 next.state = ActivityState.RESUMED;
1524 mResumedActivity = next;
1525 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001526 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001527 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 updateLRUListLocked(next);
1529
1530 // Have the window manager re-evaluate the orientation of
1531 // the screen based on the new activity order.
1532 boolean updated = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001533 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001534 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
1535 mService.mConfiguration,
1536 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1537 if (config != null) {
1538 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001539 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001540 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001542
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 if (!updated) {
1544 // The configuration update wasn't able to keep the existing
1545 // instance of the activity, and instead started a new one.
1546 // We should be all done, but let's just make sure our activity
1547 // is still at the top and schedule another run if something
1548 // weird happened.
1549 ActivityRecord nextNext = topRunningActivityLocked(null);
1550 if (DEBUG_SWITCH) Slog.i(TAG,
1551 "Activity config changed during resume: " + next
1552 + ", new next: " + nextNext);
1553 if (nextNext != next) {
1554 // Do over!
1555 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1556 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001557 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558 mService.setFocusedActivityLocked(next);
1559 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001560 if (mStackSupervisor.allResumedActivitiesComplete()) {
1561 ensureActivitiesVisibleLocked(null, 0);
1562 mService.mWindowManager.executeAppTransition();
1563 mNoAnimActivities.clear();
1564 return true;
1565 }
1566 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 }
Craig Mautner58547802013-03-05 08:23:53 -08001568
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 try {
1570 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001571 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572 if (a != null) {
1573 final int N = a.size();
1574 if (!next.finishing && N > 0) {
1575 if (DEBUG_RESULTS) Slog.v(
1576 TAG, "Delivering results to " + next
1577 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001578 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 }
1580 }
1581
1582 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001583 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 }
1585
1586 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001587 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001589
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001590 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001591 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001592 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001593 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001594 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001595
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001596 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597
1598 } catch (Exception e) {
1599 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001600 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1601 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001602 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001603 if (lastStack != null) {
1604 lastStack.mResumedActivity = lastResumedActivity;
1605 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001606 Slog.i(TAG, "Restarting because process died: " + next);
1607 if (!next.hasBeenLaunched) {
1608 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001609 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1610 mStackSupervisor.isFrontStack(lastStack)) {
1611 mService.mWindowManager.setAppStartingWindow(
1612 next.appToken, next.packageName, next.theme,
1613 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
1614 next.nonLocalizedLabel, next.labelRes, next.icon, next.windowFlags,
1615 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001617 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 return true;
1619 }
1620
1621 // From this point on, if something goes wrong there is no way
1622 // to recover the activity.
1623 try {
1624 next.visible = true;
1625 completeResumeLocked(next);
1626 } catch (Exception e) {
1627 // If any exception gets thrown, toss away this
1628 // activity and try the next one.
1629 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001630 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001631 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632 return true;
1633 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 next.stopped = false;
1635
1636 } else {
1637 // Whoops, need to restart this activity!
1638 if (!next.hasBeenLaunched) {
1639 next.hasBeenLaunched = true;
1640 } else {
1641 if (SHOW_APP_STARTING_PREVIEW) {
1642 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001643 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001644 mService.compatibilityInfoForPackageLocked(
1645 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001646 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001647 next.labelRes, next.icon, next.windowFlags,
1648 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649 }
1650 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1651 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001652 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 }
1654
1655 return true;
1656 }
1657
Craig Mautner11bf9a52013-02-19 14:08:51 -08001658
Craig Mautner8849a5e2013-04-02 16:41:03 -07001659 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001660 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001661 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001662 TaskRecord rTask = r.task;
1663 final int taskId = rTask.taskId;
1664 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001665 // Last activity in task had been removed or ActivityManagerService is reusing task.
1666 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001667 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001668 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001669 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001670 mTaskHistory.add(rTask);
1671 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001672 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 if (!newTask) {
1674 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001675 boolean startIt = true;
1676 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1677 task = mTaskHistory.get(taskNdx);
1678 if (task == r.task) {
1679 // Here it is! Now, if this is not yet visible to the
1680 // user, then just add it without starting; it will
1681 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001682 if (!startIt) {
1683 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1684 + task, new RuntimeException("here").fillInStackTrace());
1685 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001686 r.putInHistory();
Craig Mautner70a86932013-02-28 22:37:44 -08001687 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001688 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation,
1689 r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001690 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001692 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001694 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001695 return;
1696 }
1697 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001698 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001699 startIt = false;
1700 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001701 }
1702 }
1703
1704 // Place a new activity at top of stack, so it is next to interact
1705 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001706
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 // If we are not placing the new activity frontmost, we do not want
1708 // to deliver the onUserLeaving callback to the actual frontmost
1709 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001710 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001711 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001712 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1713 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001714 }
Craig Mautner70a86932013-02-28 22:37:44 -08001715
1716 task = r.task;
1717
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001718 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001719 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001720 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001721 task.addActivityToTop(r);
1722
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001723 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001725 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 // We want to show the starting preview window if we are
1727 // switching to a new task, or the next activity's process is
1728 // not currently running.
1729 boolean showStartingIcon = newTask;
1730 ProcessRecord proc = r.app;
1731 if (proc == null) {
1732 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1733 }
1734 if (proc == null || proc.thread == null) {
1735 showStartingIcon = true;
1736 }
1737 if (DEBUG_TRANSITION) Slog.v(TAG,
1738 "Prepare open transition: starting " + r);
1739 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001740 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001741 AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 } else {
1744 mService.mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001745 ? AppTransition.TRANSIT_TASK_OPEN
1746 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 mNoAnimActivities.remove(r);
1748 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001749 r.updateOptionsLocked(options);
Craig Mautner70a86932013-02-28 22:37:44 -08001750 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001751 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001752 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 boolean doShow = true;
1754 if (newTask) {
1755 // Even though this activity is starting fresh, we still need
1756 // to reset it to make sure we apply affinities to move any
1757 // existing activities from other tasks in to it.
1758 // If the caller has requested that the target task be
1759 // reset, then do so.
1760 if ((r.intent.getFlags()
1761 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1762 resetTaskIfNeededLocked(r, r);
1763 doShow = topRunningNonDelayedActivityLocked(null) == r;
1764 }
1765 }
1766 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1767 // Figure out if we are transitioning from another activity that is
1768 // "has the same starting icon" as the next one. This allows the
1769 // window manager to keep the previous window it had previously
1770 // created, if it still had one.
1771 ActivityRecord prev = mResumedActivity;
1772 if (prev != null) {
1773 // We don't want to reuse the previous starting preview if:
1774 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001775 if (prev.task != r.task) {
1776 prev = null;
1777 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001779 else if (prev.nowVisible) {
1780 prev = null;
1781 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 }
1783 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001784 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001785 mService.compatibilityInfoForPackageLocked(
1786 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001787 r.labelRes, r.icon, r.windowFlags,
1788 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 }
1790 } else {
1791 // If this is the first activity, don't do any fancy animations,
1792 // because there is nothing for it to animate on top of.
Craig Mautner70a86932013-02-28 22:37:44 -08001793 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001794 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001795 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001796 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 }
1798 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001799 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001800 }
1801
1802 if (doResume) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001803 mStackSupervisor.resumeTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 }
1805 }
1806
Dianne Hackbornbe707852011-11-11 14:32:10 -08001807 final void validateAppTokensLocked() {
1808 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001809 mValidateAppTokens.ensureCapacity(numActivities());
1810 final int numTasks = mTaskHistory.size();
1811 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1812 TaskRecord task = mTaskHistory.get(taskNdx);
1813 final ArrayList<ActivityRecord> activities = task.mActivities;
1814 if (activities.size() == 0) {
1815 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001816 }
Craig Mautner000f0022013-02-26 15:04:29 -08001817 TaskGroup group = new TaskGroup();
1818 group.taskId = task.taskId;
1819 mValidateAppTokens.add(group);
1820 final int numActivities = activities.size();
1821 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1822 final ActivityRecord r = activities.get(activityNdx);
1823 group.tokens.add(r.appToken);
1824 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001825 }
Craig Mautner00af9fe2013-03-25 09:13:41 -07001826 mService.mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001827 }
1828
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829 /**
1830 * Perform a reset of the given task, if needed as part of launching it.
1831 * Returns the new HistoryRecord at the top of the task.
1832 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001833 /**
1834 * Helper method for #resetTaskIfNeededLocked.
1835 * We are inside of the task being reset... we'll either finish this activity, push it out
1836 * for another task, or leave it as-is.
1837 * @param task The task containing the Activity (taskTop) that might be reset.
1838 * @param forceReset
1839 * @return An ActivityOptions that needs to be processed.
1840 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07001841 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task,
Craig Mautnere3a74d52013-02-22 14:14:58 -08001842 boolean forceReset) {
1843 ActivityOptions topOptions = null;
1844
1845 int replyChainEnd = -1;
1846 boolean canMoveOptions = true;
1847
1848 // We only do this for activities that are not the root of the task (since if we finish
1849 // the root, we may no longer have the task!).
1850 final ArrayList<ActivityRecord> activities = task.mActivities;
1851 final int numActivities = activities.size();
1852 for (int i = numActivities - 1; i > 0; --i ) {
1853 ActivityRecord target = activities.get(i);
1854
1855 final int flags = target.info.flags;
1856 final boolean finishOnTaskLaunch =
1857 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1858 final boolean allowTaskReparenting =
1859 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1860 final boolean clearWhenTaskReset =
1861 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1862
1863 if (!finishOnTaskLaunch
1864 && !clearWhenTaskReset
1865 && target.resultTo != null) {
1866 // If this activity is sending a reply to a previous
1867 // activity, we can't do anything with it now until
1868 // we reach the start of the reply chain.
1869 // XXX note that we are assuming the result is always
1870 // to the previous activity, which is almost always
1871 // the case but we really shouldn't count on.
1872 if (replyChainEnd < 0) {
1873 replyChainEnd = i;
1874 }
1875 } else if (!finishOnTaskLaunch
1876 && !clearWhenTaskReset
1877 && allowTaskReparenting
1878 && target.taskAffinity != null
1879 && !target.taskAffinity.equals(task.affinity)) {
1880 // If this activity has an affinity for another
1881 // task, then we need to move it out of here. We will
1882 // move it as far out of the way as possible, to the
1883 // bottom of the activity stack. This also keeps it
1884 // correctly ordered with any activities we previously
1885 // moved.
1886 TaskRecord bottomTask = mTaskHistory.get(0);
1887 ActivityRecord p = bottomTask.mActivities.get(0);
1888 if (target.taskAffinity != null
1889 && target.taskAffinity.equals(p.task.affinity)) {
1890 // If the activity currently at the bottom has the
1891 // same task affinity as the one we are moving,
1892 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001893 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001894 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1895 + " out to bottom task " + p.task);
1896 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001897 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001898 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001899 target.task.affinityIntent = target.intent;
1900 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1901 + " out to new task " + target.task);
1902 }
1903
1904 final TaskRecord targetTask = target.task;
1905 final int targetTaskId = targetTask.taskId;
1906 mService.mWindowManager.setAppGroupId(target.appToken, targetTaskId);
1907
1908 ThumbnailHolder curThumbHolder = target.thumbHolder;
1909 boolean gotOptions = !canMoveOptions;
1910
1911 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1912 for (int srcPos = start; srcPos >= i; --srcPos) {
1913 p = activities.get(srcPos);
1914 if (p.finishing) {
1915 continue;
1916 }
1917
1918 curThumbHolder = p.thumbHolder;
1919 canMoveOptions = false;
1920 if (!gotOptions && topOptions == null) {
1921 topOptions = p.takeOptionsLocked();
1922 if (topOptions != null) {
1923 gotOptions = true;
1924 }
1925 }
1926 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1927 + task + " adding to task=" + targetTask,
1928 new RuntimeException("here").fillInStackTrace());
1929 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1930 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001931 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001932 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001933
Craig Mautnere3a74d52013-02-22 14:14:58 -08001934 mService.mWindowManager.setAppGroupId(p.appToken, targetTaskId);
1935 }
1936
1937 mService.mWindowManager.moveTaskToBottom(targetTaskId);
1938 if (VALIDATE_TOKENS) {
1939 validateAppTokensLocked();
1940 }
1941
1942 replyChainEnd = -1;
1943 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1944 // If the activity should just be removed -- either
1945 // because it asks for it, or the task should be
1946 // cleared -- then finish it and anything that is
1947 // part of its reply chain.
1948 int end;
1949 if (clearWhenTaskReset) {
1950 // In this case, we want to finish this activity
1951 // and everything above it, so be sneaky and pretend
1952 // like these are all in the reply chain.
1953 end = numActivities - 1;
1954 } else if (replyChainEnd < 0) {
1955 end = i;
1956 } else {
1957 end = replyChainEnd;
1958 }
1959 ActivityRecord p = null;
1960 boolean gotOptions = !canMoveOptions;
1961 for (int srcPos = i; srcPos <= end; srcPos++) {
1962 p = activities.get(srcPos);
1963 if (p.finishing) {
1964 continue;
1965 }
1966 canMoveOptions = false;
1967 if (!gotOptions && topOptions == null) {
1968 topOptions = p.takeOptionsLocked();
1969 if (topOptions != null) {
1970 gotOptions = true;
1971 }
1972 }
Craig Mautner58547802013-03-05 08:23:53 -08001973 if (DEBUG_TASKS) Slog.w(TAG,
1974 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001975 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001976 end--;
1977 srcPos--;
1978 }
1979 }
1980 replyChainEnd = -1;
1981 } else {
1982 // If we were in the middle of a chain, well the
1983 // activity that started it all doesn't want anything
1984 // special, so leave it all as-is.
1985 replyChainEnd = -1;
1986 }
1987 }
1988
1989 return topOptions;
1990 }
1991
1992 /**
1993 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1994 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1995 * @param affinityTask The task we are looking for an affinity to.
1996 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1997 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1998 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1999 */
Craig Mautner77878772013-03-04 19:46:24 -08002000 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
2001 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002002 int replyChainEnd = -1;
2003 final int taskId = task.taskId;
2004 final String taskAffinity = task.affinity;
2005
2006 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2007 final int numActivities = activities.size();
2008 // Do not operate on the root Activity.
2009 for (int i = numActivities - 1; i > 0; --i) {
2010 ActivityRecord target = activities.get(i);
2011
2012 final int flags = target.info.flags;
2013 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2014 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2015
2016 if (target.resultTo != null) {
2017 // If this activity is sending a reply to a previous
2018 // activity, we can't do anything with it now until
2019 // we reach the start of the reply chain.
2020 // XXX note that we are assuming the result is always
2021 // to the previous activity, which is almost always
2022 // the case but we really shouldn't count on.
2023 if (replyChainEnd < 0) {
2024 replyChainEnd = i;
2025 }
2026 } else if (topTaskIsHigher
2027 && allowTaskReparenting
2028 && taskAffinity != null
2029 && taskAffinity.equals(target.taskAffinity)) {
2030 // This activity has an affinity for our task. Either remove it if we are
2031 // clearing or move it over to our task. Note that
2032 // we currently punt on the case where we are resetting a
2033 // task that is not at the top but who has activities above
2034 // with an affinity to it... this is really not a normal
2035 // case, and we will need to later pull that task to the front
2036 // and usually at that point we will do the reset and pick
2037 // up those remaining activities. (This only happens if
2038 // someone starts an activity in a new task from an activity
2039 // in a task that is not currently on top.)
2040 if (forceReset || finishOnTaskLaunch) {
2041 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2042 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2043 for (int srcPos = start; srcPos >= i; --srcPos) {
2044 final ActivityRecord p = activities.get(srcPos);
2045 if (p.finishing) {
2046 continue;
2047 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002048 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002049 }
2050 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002051 if (taskInsertionPoint < 0) {
2052 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002053
Craig Mautner77878772013-03-04 19:46:24 -08002054 }
Craig Mautner77878772013-03-04 19:46:24 -08002055
2056 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2057 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2058 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2059 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002060 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002061 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002062 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002063
Craig Mautnere3a74d52013-02-22 14:14:58 -08002064 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2065 + " to stack at " + task,
2066 new RuntimeException("here").fillInStackTrace());
2067 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2068 + " in to resetting task " + task);
2069 mService.mWindowManager.setAppGroupId(p.appToken, taskId);
2070 }
2071 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002072 if (VALIDATE_TOKENS) {
2073 validateAppTokensLocked();
2074 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002075
2076 // Now we've moved it in to place... but what if this is
2077 // a singleTop activity and we have put it on top of another
2078 // instance of the same activity? Then we drop the instance
2079 // below so it remains singleTop.
2080 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2081 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2082 boolean found = false;
2083 int targetNdx = taskActivities.indexOf(target);
2084 if (targetNdx > 0) {
2085 ActivityRecord p = taskActivities.get(targetNdx - 1);
2086 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002087 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2088 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002089 }
2090 }
2091 }
2092 }
2093
2094 replyChainEnd = -1;
2095 }
2096 }
Craig Mautner77878772013-03-04 19:46:24 -08002097 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002098 }
2099
Craig Mautner8849a5e2013-04-02 16:41:03 -07002100 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002101 ActivityRecord newActivity) {
2102 boolean forceReset =
2103 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2104 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2105 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2106 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2107 forceReset = true;
2108 }
2109 }
2110
2111 final TaskRecord task = taskTop.task;
2112
2113 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2114 * for remaining tasks. Used for later tasks to reparent to task. */
2115 boolean taskFound = false;
2116
2117 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2118 ActivityOptions topOptions = null;
2119
Craig Mautner77878772013-03-04 19:46:24 -08002120 // Preserve the location for reparenting in the new task.
2121 int reparentInsertionPoint = -1;
2122
Craig Mautnere3a74d52013-02-22 14:14:58 -08002123 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2124 final TaskRecord targetTask = mTaskHistory.get(i);
2125
2126 if (targetTask == task) {
2127 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2128 taskFound = true;
2129 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002130 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2131 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002132 }
2133 }
2134
Craig Mautner70a86932013-02-28 22:37:44 -08002135 int taskNdx = mTaskHistory.indexOf(task);
2136 do {
2137 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2138 } while (taskTop == null && taskNdx >= 0);
2139
Craig Mautnere3a74d52013-02-22 14:14:58 -08002140 if (topOptions != null) {
2141 // If we got some ActivityOptions from an activity on top that
2142 // was removed from the task, propagate them to the new real top.
2143 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002144 taskTop.updateOptionsLocked(topOptions);
2145 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002146 topOptions.abort();
2147 }
2148 }
2149
2150 return taskTop;
2151 }
2152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002154 * Find the activity in the history stack within the given task. Returns
2155 * the index within the history at which it's found, or < 0 if not found.
2156 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07002157 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002158 final ComponentName realActivity = r.realActivity;
2159 ArrayList<ActivityRecord> activities = task.mActivities;
2160 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2161 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002162 if (candidate.finishing) {
2163 continue;
2164 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002165 if (candidate.realActivity.equals(realActivity)) {
2166 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 }
2168 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002169 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 }
2171
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
2173 long thisTime, long totalTime) {
2174 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
2175 WaitResult w = mWaitingActivityLaunched.get(i);
2176 w.timeout = timeout;
2177 if (r != null) {
2178 w.who = new ComponentName(r.info.packageName, r.info.name);
2179 }
2180 w.thisTime = thisTime;
2181 w.totalTime = totalTime;
2182 }
2183 mService.notifyAll();
2184 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002185
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002186 void reportActivityVisibleLocked(ActivityRecord r) {
2187 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
2188 WaitResult w = mWaitingActivityVisible.get(i);
2189 w.timeout = false;
2190 if (r != null) {
2191 w.who = new ComponentName(r.info.packageName, r.info.name);
2192 }
2193 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
2194 w.thisTime = w.totalTime;
2195 }
2196 mService.notifyAll();
Craig Mautner27084302013-03-25 08:05:25 -07002197 mStackSupervisor.dismissKeyguard();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002198 }
2199
2200 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2201 String resultWho, int requestCode, int resultCode, Intent data) {
2202
2203 if (callingUid > 0) {
2204 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002205 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002206 }
2207
2208 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2209 + " : who=" + resultWho + " req=" + requestCode
2210 + " res=" + resultCode + " data=" + data);
2211 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2212 try {
2213 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2214 list.add(new ResultInfo(resultWho, requestCode,
2215 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002216 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002217 return;
2218 } catch (Exception e) {
2219 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2220 }
2221 }
2222
2223 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2224 }
2225
2226 private final void stopActivityLocked(ActivityRecord r) {
2227 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2228 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2229 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2230 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002231 if (!mService.mSleeping) {
2232 if (DEBUG_STATES) {
2233 Slog.d(TAG, "no-history finish of " + r);
2234 }
2235 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002236 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002237 } else {
2238 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2239 + " on stop because we're just sleeping");
2240 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002241 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002242 }
2243
2244 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002245 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002246 if (mService.mFocusedActivity == r) {
2247 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2248 }
2249 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002250 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 try {
2252 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002253 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2254 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002255 r.state = ActivityState.STOPPING;
2256 if (DEBUG_VISBILITY) Slog.v(
2257 TAG, "Stopping visible=" + r.visible + " for " + r);
2258 if (!r.visible) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002259 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002261 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002262 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002263 r.setSleeping(true);
2264 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002265 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2266 msg.obj = r;
2267 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 } catch (Exception e) {
2269 // Maybe just ignore exceptions here... if the process
2270 // has crashed, our death notification will clean things
2271 // up.
2272 Slog.w(TAG, "Exception thrown during pause", e);
2273 // Just in case, assume it to be stopped.
2274 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002275 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 r.state = ActivityState.STOPPED;
2277 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002278 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002279 }
2280 }
2281 }
2282 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002283
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002284 final void scheduleIdleLocked() {
2285 Message msg = Message.obtain();
2286 msg.what = IDLE_NOW_MSG;
2287 mHandler.sendMessage(msg);
2288 }
2289
Craig Mautnere79d42682013-04-01 19:01:53 -07002290 // Checked.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002291 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 Configuration config) {
2293 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2294
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002295 ActivityRecord res = null;
2296
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002297 ArrayList<ActivityRecord> stops = null;
2298 ArrayList<ActivityRecord> finishes = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002299 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002300 int NS = 0;
2301 int NF = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002302 IApplicationThread sendThumbnail = null;
2303 boolean booting = false;
2304 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002305 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306
Craig Mautnere79d42682013-04-01 19:01:53 -07002307 ActivityRecord r = ActivityRecord.forToken(token);
2308 if (r != null) {
2309 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2310 r.finishLaunchTickingLocked();
2311 }
2312
2313 // Get the activity record.
2314 if (isInStackLocked(token) != null) {
2315 res = r;
2316
2317 if (fromTimeout) {
2318 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 }
2320
Craig Mautnere79d42682013-04-01 19:01:53 -07002321 // This is a hack to semi-deal with a race condition
2322 // in the client where it can be constructed with a
2323 // newer configuration from when we asked it to launch.
2324 // We'll update with whatever configuration it now says
2325 // it used to launch.
2326 if (config != null) {
2327 r.configuration = config;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002328 }
2329
Craig Mautnere79d42682013-04-01 19:01:53 -07002330 // No longer need to keep the device awake.
2331 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
2332 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2333 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 }
2335
Craig Mautnere79d42682013-04-01 19:01:53 -07002336 // We are now idle. If someone is waiting for a thumbnail from
2337 // us, we can now deliver.
2338 r.idle = true;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002339 if (mStackSupervisor.allResumedActivitiesIdle()) {
2340 mService.scheduleAppGcsLocked();
2341 }
Craig Mautnere79d42682013-04-01 19:01:53 -07002342 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
2343 sendThumbnail = r.app.thread;
2344 r.thumbnailNeeded = false;
2345 }
2346
2347 // If this activity is fullscreen, set up to hide those under it.
2348
2349 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
2350 ensureActivitiesVisibleLocked(null, 0);
2351
2352 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002353 if (!mService.mBooted && mStackSupervisor.isFrontStack(this)) {
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002354 mService.mBooted = true;
2355 enableScreen = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002356 }
Craig Mautnere79d42682013-04-01 19:01:53 -07002357 } else if (fromTimeout) {
2358 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
2359 }
2360
2361 // Atomically retrieve all of the other things to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002362 stops = mStackSupervisor.processStoppingActivitiesLocked(true);
Craig Mautnere79d42682013-04-01 19:01:53 -07002363 NS = stops != null ? stops.size() : 0;
2364 if ((NF=mFinishingActivities.size()) > 0) {
2365 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2366 mFinishingActivities.clear();
2367 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002368
2369 final ArrayList<ActivityRecord> thumbnails;
2370 final int NT = mCancelledThumbnails.size();
2371 if (NT > 0) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002372 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
2373 mCancelledThumbnails.clear();
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002374 } else {
2375 thumbnails = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07002376 }
2377
Craig Mautnerde4ef022013-04-07 19:01:33 -07002378 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002379 booting = mService.mBooting;
2380 mService.mBooting = false;
2381 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002382
Craig Mautnere79d42682013-04-01 19:01:53 -07002383 if (mStartingUsers.size() > 0) {
2384 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2385 mStartingUsers.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 }
2387
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002388 // Perform the following actions from unsynchronized state.
2389 final IApplicationThread thumbnailThread = sendThumbnail;
2390 mHandler.post(new Runnable() {
2391 @Override
2392 public void run() {
2393 if (thumbnailThread != null) {
2394 try {
2395 thumbnailThread.requestThumbnail(token);
2396 } catch (Exception e) {
2397 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
2398 mService.sendPendingThumbnail(null, token, null, null, true);
2399 }
2400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002402 // Report back to any thumbnail receivers.
2403 for (int i = 0; i < NT; i++) {
2404 ActivityRecord r = thumbnails.get(i);
2405 mService.sendPendingThumbnail(r, null, null, null, true);
2406 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002407 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002408 });
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409
2410 // Stop any activities that are scheduled to do so but have been
2411 // waiting for the next one to start.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002412 for (int i = 0; i < NS; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002413 r = stops.get(i);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002414 if (r.finishing) {
2415 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2416 } else {
2417 stopActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002418 }
2419 }
2420
2421 // Finish any activities that are scheduled to do so but have been
2422 // waiting for the next one to start.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002423 for (int i = 0; i < NF; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002424 r = finishes.get(i);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002425 activityRemoved |= destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002426 }
2427
2428 if (booting) {
2429 mService.finishBooting();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002430 } else if (startingUsers != null) {
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002431 for (int i = 0; i < startingUsers.size(); i++) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002432 mService.finishUserSwitch(startingUsers.get(i));
2433 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 }
2435
2436 mService.trimApplications();
2437 //dump();
2438 //mWindowManager.dump();
2439
2440 if (enableScreen) {
2441 mService.enableScreenAfterBoot();
2442 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002443
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002444 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002445 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002446 }
2447
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002448 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 }
2450
2451 /**
2452 * @return Returns true if the activity is being finished, false if for
2453 * some reason it is being left as-is.
2454 */
2455 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002456 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002457 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002458 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002459 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002460 + ", result=" + resultCode + ", data=" + resultData
2461 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002462 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 return false;
2464 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002465
Craig Mautnerd44711d2013-02-23 11:24:36 -08002466 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002467 return true;
2468 }
2469
Craig Mautnerd2328952013-03-05 12:46:26 -08002470 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002471 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2472 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2473 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2474 ActivityRecord r = activities.get(activityNdx);
2475 if (r.resultTo == self && r.requestCode == requestCode) {
2476 if ((r.resultWho == null && resultWho == null) ||
2477 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2478 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2479 false);
2480 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002481 }
2482 }
2483 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002484 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002485 }
2486
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002487 final void finishTopRunningActivityLocked(ProcessRecord app) {
2488 ActivityRecord r = topRunningActivityLocked(null);
2489 if (r != null && r.app == app) {
2490 // If the top running activity is from this crashing
2491 // process, then terminate it to avoid getting in a loop.
2492 Slog.w(TAG, " Force finishing activity "
2493 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002494 int taskNdx = mTaskHistory.indexOf(r.task);
2495 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002496 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002497 // Also terminate any activities below it that aren't yet
2498 // stopped, to avoid a situation where one will get
2499 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002500 --activityNdx;
2501 if (activityNdx < 0) {
2502 do {
2503 --taskNdx;
2504 if (taskNdx < 0) {
2505 break;
2506 }
2507 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2508 } while (activityNdx < 0);
2509 }
2510 if (activityNdx >= 0) {
2511 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002512 if (r.state == ActivityState.RESUMED
2513 || r.state == ActivityState.PAUSING
2514 || r.state == ActivityState.PAUSED) {
2515 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
2516 Slog.w(TAG, " Force finishing activity "
2517 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002518 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002519 }
2520 }
2521 }
2522 }
2523 }
2524
Craig Mautnerd2328952013-03-05 12:46:26 -08002525 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002526 ArrayList<ActivityRecord> activities = r.task.mActivities;
2527 for (int index = activities.indexOf(r); index >= 0; --index) {
2528 ActivityRecord cur = activities.get(index);
2529 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002530 break;
2531 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002532 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002533 }
2534 return true;
2535 }
2536
Dianne Hackborn5c607432012-02-28 14:44:19 -08002537 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2538 // send the result
2539 ActivityRecord resultTo = r.resultTo;
2540 if (resultTo != null) {
2541 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2542 + " who=" + r.resultWho + " req=" + r.requestCode
2543 + " res=" + resultCode + " data=" + resultData);
2544 if (r.info.applicationInfo.uid > 0) {
2545 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2546 resultTo.packageName, resultData,
2547 resultTo.getUriPermissionsLocked());
2548 }
2549 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2550 resultData);
2551 r.resultTo = null;
2552 }
2553 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2554
2555 // Make sure this HistoryRecord is not holding on to other resources,
2556 // because clients have remote IPC references to this object so we
2557 // can't assume that will go away and want to avoid circular IPC refs.
2558 r.results = null;
2559 r.pendingResults = null;
2560 r.newIntents = null;
2561 r.icicle = null;
2562 }
2563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002564 /**
2565 * @return Returns true if this activity has been removed from the history
2566 * list, or false if it is still in the list and will be removed later.
2567 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08002568 final boolean finishActivityLocked(ActivityRecord r, int resultCode,
2569 Intent resultData, String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570 if (r.finishing) {
2571 Slog.w(TAG, "Duplicate finish request for " + r);
2572 return false;
2573 }
2574
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002575 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002576 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002577 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002579 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2580 final int index = activities.indexOf(r);
2581 if (index < (activities.size() - 1)) {
2582 ActivityRecord next = activities.get(index+1);
2583 if (r.frontOfTask) {
2584 // The next activity is now the front of the task.
2585 next.frontOfTask = true;
2586 }
2587 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2588 // If the caller asked that this activity (and all above it)
2589 // be cleared when the task is reset, don't lose that information,
2590 // but propagate it up to the next activity.
2591 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002592 }
2593 }
2594
2595 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002596 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002598 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002599 }
2600 }
2601
Dianne Hackborn5c607432012-02-28 14:44:19 -08002602 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002603
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002604 if (mService.mPendingThumbnails.size() > 0) {
2605 // There are clients waiting to receive thumbnails so, in case
2606 // this is an activity that someone is waiting for, add it
2607 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08002608 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002609 }
2610
Craig Mautnerde4ef022013-04-07 19:01:33 -07002611 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002612 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002613 if (DEBUG_TRANSITION) Slog.v(TAG,
2614 "Prepare close transition: finishing " + r);
2615 mService.mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002616 ? AppTransition.TRANSIT_TASK_CLOSE
2617 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002619 // Tell window manager to prepare for this one to be removed.
Dianne Hackbornbe707852011-11-11 14:32:10 -08002620 mService.mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002621
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002622 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002623 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2624 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2625 startPausingLocked(false, false);
2626 }
2627
2628 } else if (r.state != ActivityState.PAUSING) {
2629 // If the activity is PAUSING, we will complete the finish once
2630 // it is done pausing; else we can just directly finish it here.
2631 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002632 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 } else {
2634 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2635 }
2636
2637 return false;
2638 }
2639
2640 private static final int FINISH_IMMEDIATELY = 0;
2641 private static final int FINISH_AFTER_PAUSE = 1;
2642 private static final int FINISH_AFTER_VISIBLE = 2;
2643
2644 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002645 int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 // First things first: if this activity is currently visible,
2647 // and the resumed activity is not yet visible, then hold off on
2648 // finishing until the resumed one becomes visible.
2649 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002650 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2651 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002652 if (mStackSupervisor.mStoppingActivities.size() > 3
2653 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002654 // If we already have a few activities waiting to stop,
2655 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002656 // them out. Or if r is the last of activity of the last task the stack
2657 // will be empty and must be cleared immediately.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002658 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002659 } else {
2660 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 }
2662 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002663 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2664 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002665 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002666 if (oomAdj) {
2667 mService.updateOomAdjLocked();
2668 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002669 return r;
2670 }
2671
2672 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002673 mStackSupervisor.mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002674 mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002675 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002676 if (mResumedActivity == r) {
2677 mResumedActivity = null;
2678 }
2679 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002680 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002681 r.state = ActivityState.FINISHING;
2682
2683 if (mode == FINISH_IMMEDIATELY
2684 || prevState == ActivityState.STOPPED
2685 || prevState == ActivityState.INITIALIZING) {
2686 // If this activity is already stopped, we can just finish
2687 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002688 boolean activityRemoved = destroyActivityLocked(r, true,
2689 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002690 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002691 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002692 }
2693 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002694 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002695
2696 // Need to go through the full pause cycle to get this
2697 // activity into the stopped state and then finish it.
2698 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
2699 mFinishingActivities.add(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002700 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 return r;
2702 }
2703
Craig Mautnerd2328952013-03-05 12:46:26 -08002704 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002705 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002706 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002707 final TaskRecord task = srec.task;
2708 final ArrayList<ActivityRecord> activities = task.mActivities;
2709 final int start = activities.indexOf(srec);
2710 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002711 return false;
2712 }
2713 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002714 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002715 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002716 final ComponentName dest = destIntent.getComponent();
2717 if (start > 0 && dest != null) {
2718 for (int i = finishTo; i >= 0; i--) {
2719 ActivityRecord r = activities.get(i);
2720 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002721 r.info.name.equals(dest.getClassName())) {
2722 finishTo = i;
2723 parent = r;
2724 foundParentInTask = true;
2725 break;
2726 }
2727 }
2728 }
2729
2730 IActivityController controller = mService.mController;
2731 if (controller != null) {
2732 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2733 if (next != null) {
2734 // ask watcher if this is allowed
2735 boolean resumeOK = true;
2736 try {
2737 resumeOK = controller.activityResuming(next.packageName);
2738 } catch (RemoteException e) {
2739 mService.mController = null;
2740 }
2741
2742 if (!resumeOK) {
2743 return false;
2744 }
2745 }
2746 }
2747 final long origId = Binder.clearCallingIdentity();
2748 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002749 ActivityRecord r = activities.get(i);
2750 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002751 // Only return the supplied result for the first activity finished
2752 resultCode = Activity.RESULT_CANCELED;
2753 resultData = null;
2754 }
2755
2756 if (parent != null && foundParentInTask) {
2757 final int parentLaunchMode = parent.info.launchMode;
2758 final int destIntentFlags = destIntent.getFlags();
2759 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2760 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2761 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2762 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2763 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2764 } else {
2765 try {
2766 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2767 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002768 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002769 null, aInfo, parent.appToken, null,
2770 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2771 0, null, true, null);
2772 foundParentInTask = res == ActivityManager.START_SUCCESS;
2773 } catch (RemoteException e) {
2774 foundParentInTask = false;
2775 }
2776 requestFinishActivityLocked(parent.appToken, resultCode,
2777 resultData, "navigate-up", true);
2778 }
2779 }
2780 Binder.restoreCallingIdentity(origId);
2781 return foundParentInTask;
2782 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002783 /**
2784 * Perform the common clean-up of an activity record. This is called both
2785 * as part of destroyActivityLocked() (when destroying the client-side
2786 * representation) and cleaning things up as a result of its hosting
2787 * processing going away, in which case there is no remaining client-side
2788 * state to destroy so only the cleanup here is needed.
2789 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002790 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2791 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002792 if (mResumedActivity == r) {
2793 mResumedActivity = null;
2794 }
2795 if (mService.mFocusedActivity == r) {
2796 mService.mFocusedActivity = null;
2797 }
2798
2799 r.configDestroy = false;
2800 r.frozenBeforeDestroy = false;
2801
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002802 if (setState) {
2803 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2804 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002805 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002806 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002807 }
2808
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002809 // Make sure this record is no longer in the pending finishes list.
2810 // This could happen, for example, if we are trimming activities
2811 // down to the max limit while they are still waiting to finish.
2812 mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002813 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002814
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002815 // Remove any pending results.
2816 if (r.finishing && r.pendingResults != null) {
2817 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2818 PendingIntentRecord rec = apr.get();
2819 if (rec != null) {
2820 mService.cancelIntentSenderLocked(rec, false);
2821 }
2822 }
2823 r.pendingResults = null;
2824 }
2825
2826 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002827 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828 }
2829
2830 if (mService.mPendingThumbnails.size() > 0) {
2831 // There are clients waiting to receive thumbnails so, in case
2832 // this is an activity that someone is waiting for, add it
2833 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08002834 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 }
2836
2837 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002838 removeTimeoutsForActivityLocked(r);
2839 }
2840
2841 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002843 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002844 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002845 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002846 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 }
2848
Dianne Hackborn5c607432012-02-28 14:44:19 -08002849 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002850 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2851 r.makeFinishing();
2852 if (DEBUG_ADD_REMOVE) {
2853 RuntimeException here = new RuntimeException("here");
2854 here.fillInStackTrace();
2855 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002856 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002857 final TaskRecord task = r.task;
2858 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002859 if (DEBUG_STACK) Slog.i(TAG,
2860 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002861 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002862 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002863 r.takeFromHistory();
2864 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002865 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002866 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002867 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002868 r.app = null;
2869 mService.mWindowManager.removeAppToken(r.appToken);
2870 if (VALIDATE_TOKENS) {
2871 validateAppTokensLocked();
2872 }
2873 cleanUpActivityServicesLocked(r);
2874 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002876
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002877 /**
2878 * Perform clean-up of service connections in an activity record.
2879 */
2880 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2881 // Throw away any services that have been bound by this activity.
2882 if (r.connections != null) {
2883 Iterator<ConnectionRecord> it = r.connections.iterator();
2884 while (it.hasNext()) {
2885 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002886 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887 }
2888 r.connections = null;
2889 }
2890 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002891
2892 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2893 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2894 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2895 mHandler.sendMessage(msg);
2896 }
2897
Dianne Hackborn28695e02011-11-02 21:59:51 -07002898 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002899 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002900 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002901 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2902 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2903 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2904 final ActivityRecord r = activities.get(activityNdx);
2905 if (r.finishing) {
2906 continue;
2907 }
2908 if (r.fullscreen) {
2909 lastIsOpaque = true;
2910 }
2911 if (owner != null && r.app != owner) {
2912 continue;
2913 }
2914 if (!lastIsOpaque) {
2915 continue;
2916 }
2917 // We can destroy this one if we have its icicle saved and
2918 // it is not in the process of pausing/stopping/finishing.
2919 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2920 && r.haveState && !r.visible && r.stopped
2921 && r.state != ActivityState.DESTROYING
2922 && r.state != ActivityState.DESTROYED) {
2923 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2924 + " resumed=" + mResumedActivity
2925 + " pausing=" + mPausingActivity);
2926 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2927 activityRemoved = true;
2928 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002929 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002930 }
2931 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002932 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002933 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002934 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002935 }
2936
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937 /**
2938 * Destroy the current CLIENT SIDE instance of an activity. This may be
2939 * called both when actually finishing an activity, or when performing
2940 * a configuration switch where we destroy the current client-side object
2941 * but then create a new client-side object for this same HistoryRecord.
2942 */
2943 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002944 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002945 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002946 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2948 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002949 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002950 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002951
2952 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002953
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002954 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955
2956 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002957
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002958 if (hadApp) {
2959 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002960 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002961 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2962 mService.mHeavyWeightProcess = null;
2963 mService.mHandler.sendEmptyMessage(
2964 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2965 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002966 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002967 // No longer have activities, so update oom adj.
2968 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 }
2970 }
2971
2972 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002973
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 try {
2975 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002976 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977 r.configChangeFlags);
2978 } catch (Exception e) {
2979 // We can just ignore exceptions here... if the process
2980 // has crashed, our death notification will clean things
2981 // up.
2982 //Slog.w(TAG, "Exception thrown during finish", e);
2983 if (r.finishing) {
2984 removeActivityFromHistoryLocked(r);
2985 removedFromHistory = true;
2986 skipDestroy = true;
2987 }
2988 }
2989
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002990 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002991
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002992 // If the activity is finishing, we need to wait on removing it
2993 // from the list to give it a chance to do its cleanup. During
2994 // that time it may make calls back with its token so we need to
2995 // be able to find it on the list and so we don't want to remove
2996 // it from the list yet. Otherwise, we can just immediately put
2997 // it in the destroyed state since we are not removing it from the
2998 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002999 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003000 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3001 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 r.state = ActivityState.DESTROYING;
3003 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
3004 msg.obj = r;
3005 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3006 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003007 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3008 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003010 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003011 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003012 }
3013 } else {
3014 // remove this record from the history.
3015 if (r.finishing) {
3016 removeActivityFromHistoryLocked(r);
3017 removedFromHistory = true;
3018 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003019 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3020 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003022 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003023 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003024 }
3025 }
3026
3027 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003028
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 if (!mLRUActivities.remove(r) && hadApp) {
3030 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3031 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003032
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 return removedFromHistory;
3034 }
3035
Craig Mautnerd2328952013-03-05 12:46:26 -08003036 final void activityDestroyedLocked(IBinder token) {
3037 final long origId = Binder.clearCallingIdentity();
3038 try {
3039 ActivityRecord r = ActivityRecord.forToken(token);
3040 if (r != null) {
3041 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003043
3044 if (isInStackLocked(token) != null) {
3045 if (r.state == ActivityState.DESTROYING) {
3046 cleanUpActivityLocked(r, true, false);
3047 removeActivityFromHistoryLocked(r);
3048 }
3049 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003050 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Craig Mautnerd2328952013-03-05 12:46:26 -08003051 } finally {
3052 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003053 }
3054 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003055
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003056 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3057 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003058 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003059 if (DEBUG_CLEANUP) Slog.v(
3060 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061 + " with " + i + " entries");
3062 while (i > 0) {
3063 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003064 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003065 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003066 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003067 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003068 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003069 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 }
3071 }
3072 }
3073
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003074 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3075 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003076 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3077 "mStoppingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003078 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003079 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003080 "mWaitingVisibleActivities");
3081 removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities");
3082
3083 boolean hasVisibleActivities = false;
3084
3085 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003086 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003087 if (DEBUG_CLEANUP) Slog.v(
3088 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003089 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3090 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3091 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3092 final ActivityRecord r = activities.get(activityNdx);
3093 --i;
3094 if (DEBUG_CLEANUP) Slog.v(
3095 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3096 if (r.app == app) {
3097 boolean remove;
3098 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3099 // Don't currently have state for the activity, or
3100 // it is finishing -- always remove it.
3101 remove = true;
3102 } else if (r.launchCount > 2 &&
3103 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3104 // We have launched this activity too many times since it was
3105 // able to run, so give up and remove it.
3106 remove = true;
3107 } else {
3108 // The process may be gone, but the activity lives on!
3109 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003110 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003111 if (remove) {
3112 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
3113 RuntimeException here = new RuntimeException("here");
3114 here.fillInStackTrace();
3115 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3116 + ": haveState=" + r.haveState
3117 + " stateNotNeeded=" + r.stateNotNeeded
3118 + " finishing=" + r.finishing
3119 + " state=" + r.state, here);
3120 }
3121 if (!r.finishing) {
3122 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3123 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3124 r.userId, System.identityHashCode(r),
3125 r.task.taskId, r.shortComponentName,
3126 "proc died without state saved");
3127 }
3128 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003129
Craig Mautner0247fc82013-02-28 14:32:06 -08003130 } else {
3131 // We have the current state for this activity, so
3132 // it can be restarted later when needed.
3133 if (localLOGV) Slog.v(
3134 TAG, "Keeping entry, setting app to null");
3135 if (r.visible) {
3136 hasVisibleActivities = true;
3137 }
3138 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3139 + r);
3140 r.app = null;
3141 r.nowVisible = false;
3142 if (!r.haveState) {
3143 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
3144 "App died, clearing saved state of " + r);
3145 r.icicle = null;
3146 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003147 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003148
Craig Mautnerd2328952013-03-05 12:46:26 -08003149 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003150 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003151 }
3152 }
3153
3154 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003156
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003157 final void updateTransitLocked(int transit, Bundle options) {
3158 if (options != null) {
3159 ActivityRecord r = topRunningActivityLocked(null);
3160 if (r != null && r.state != ActivityState.RESUMED) {
3161 r.updateOptionsLocked(options);
3162 } else {
3163 ActivityOptions.abort(options);
3164 }
3165 }
3166 mService.mWindowManager.prepareAppTransition(transit, false);
3167 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003168
Craig Mautnercae015f2013-02-08 14:31:27 -08003169 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003170 final TaskRecord task = taskForIdLocked(taskId);
3171 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003172 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003173 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003174 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003175 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3176 // Caller wants the home activity moved with it. To accomplish this,
3177 // we'll just move the home task to the top first.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003178 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003179 }
3180 moveTaskToFrontLocked(task, null, options);
3181 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003182 }
3183 return false;
3184 }
3185
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003186 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003187 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188
Craig Mautner11bf9a52013-02-19 14:08:51 -08003189 final int numTasks = mTaskHistory.size();
3190 final int index = mTaskHistory.indexOf(tr);
3191 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
3192 // nothing to do!
3193 if (reason != null &&
3194 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3195 ActivityOptions.abort(options);
3196 } else {
3197 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3198 }
3199 return;
3200 }
3201
3202 // Shift all activities with this task up to the top
3203 // of the stack, keeping them in the same internal order.
3204 mTaskHistory.remove(tr);
3205 mTaskHistory.add(tr);
3206
3207 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003208 if (reason != null &&
3209 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003210 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 ActivityRecord r = topRunningActivityLocked(null);
3212 if (r != null) {
3213 mNoAnimActivities.add(r);
3214 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003215 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003216 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003217 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003219
Craig Mautner58547802013-03-05 08:23:53 -08003220 mService.mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003221
Craig Mautnerde4ef022013-04-07 19:01:33 -07003222 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Craig Mautner58547802013-03-05 08:23:53 -08003223 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003224
3225 if (VALIDATE_TOKENS) {
3226 validateAppTokensLocked();
3227 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003228 }
3229
3230 /**
3231 * Worker method for rearranging history stack. Implements the function of moving all
3232 * activities for a specific task (gathering them if disjoint) into a single group at the
3233 * bottom of the stack.
3234 *
3235 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3236 * to premeptively cancel the move.
3237 *
3238 * @param task The taskId to collect and move to the bottom.
3239 * @return Returns true if the move completed, false if not.
3240 */
3241 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3242 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003243
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003244 // If we have a watcher, preflight the move before committing to it. First check
3245 // for *other* available tasks, but if none are available, then try again allowing the
3246 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003247 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 ActivityRecord next = topRunningActivityLocked(null, task);
3249 if (next == null) {
3250 next = topRunningActivityLocked(null, 0);
3251 }
3252 if (next != null) {
3253 // ask watcher if this is allowed
3254 boolean moveOK = true;
3255 try {
3256 moveOK = mService.mController.activityResuming(next.packageName);
3257 } catch (RemoteException e) {
3258 mService.mController = null;
3259 }
3260 if (!moveOK) {
3261 return false;
3262 }
3263 }
3264 }
3265
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003266 if (DEBUG_TRANSITION) Slog.v(TAG,
3267 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268
Craig Mautnerd2328952013-03-05 12:46:26 -08003269 final TaskRecord tr = taskForIdLocked(task);
3270 if (tr == null) {
3271 return false;
3272 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003273 mTaskHistory.remove(tr);
3274 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003275
3276 if (reason != null &&
3277 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003278 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003279 ActivityRecord r = topRunningActivityLocked(null);
3280 if (r != null) {
3281 mNoAnimActivities.add(r);
3282 }
3283 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08003284 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003285 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003287 mService.mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003288
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003289 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003290 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003291 }
3292
Craig Mautnerde4ef022013-04-07 19:01:33 -07003293 if (mResumedActivity != null && mResumedActivity.task == tr &&
3294 mResumedActivity.mLaunchHomeTaskNext) {
3295 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003296 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003297 }
3298
3299 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 return true;
3301 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003302
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003303 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08003304 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003305 if (mResumedActivity != null && mResumedActivity.thumbHolder == tr) {
3306 info.mainThumbnail = screenshotActivities(mResumedActivity);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003307 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07003308 if (info.mainThumbnail == null) {
3309 info.mainThumbnail = tr.lastThumbnail;
3310 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003311 return info;
3312 }
3313
Dianne Hackborn15491c62012-09-19 10:59:14 -07003314 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003315 if (mResumedActivity != null && mResumedActivity.task == tr) {
Dianne Hackborn15491c62012-09-19 10:59:14 -07003316 // This task is the current resumed task, we just need to take
3317 // a screenshot of it and return that.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003318 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003319 }
3320 // Return the information about the task, to figure out the top
3321 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08003322 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003323 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07003324 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003325 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003326 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003327 }
3328
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003329 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
3330 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003331 final TaskRecord task = taskForIdLocked(taskId);
3332 if (task == null) {
3333 return null;
3334 }
Craig Mautner16030772013-02-26 16:05:47 -08003335 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003336 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003337 if (taskRequired) {
3338 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
3339 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003340 return null;
3341 }
3342
3343 if (subTaskIndex < 0) {
3344 // Just remove the entire task.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003345 task.performClearTaskAtIndexLocked(info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003346 return info.root;
3347 }
3348
3349 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003350 if (taskRequired) {
3351 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
3352 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003353 return null;
3354 }
3355
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003356 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003357 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003358 task.performClearTaskAtIndexLocked(subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003359 return subtask.activity;
3360 }
3361
Craig Mautner16030772013-02-26 16:05:47 -08003362 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003363 final TaskAccessInfo thumbs = new TaskAccessInfo();
3364 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08003365 final ArrayList<ActivityRecord> activities = task.mActivities;
3366 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003367 int j = 0;
3368 ThumbnailHolder holder = null;
3369 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003370 ActivityRecord ar = activities.get(j);
3371 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07003372 thumbs.root = ar;
3373 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003374 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003375 if (holder != null) {
3376 thumbs.mainThumbnail = holder.lastThumbnail;
3377 }
3378 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003379 break;
3380 }
3381 j++;
3382 }
3383
3384 if (j >= NA) {
3385 return thumbs;
3386 }
3387
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003388 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
3389 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003390 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003391 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003392 j++;
3393 if (ar.finishing) {
3394 continue;
3395 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003396 if (ar.thumbHolder != holder && holder != null) {
3397 thumbs.numSubThumbbails++;
3398 holder = ar.thumbHolder;
3399 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07003400 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003401 sub.activity = ar;
3402 sub.index = j-1;
3403 subtasks.add(sub);
3404 }
3405 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003406 if (thumbs.numSubThumbbails > 0) {
3407 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003408 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003409 public Bitmap getThumbnail(int index) {
3410 if (index < 0 || index >= thumbs.subtasks.size()) {
3411 return null;
3412 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003413 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003414 if (mResumedActivity != null && mResumedActivity.thumbHolder == sub.holder) {
3415 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003416 }
3417 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003418 }
3419 };
3420 }
3421 return thumbs;
3422 }
3423
Craig Mautner8849a5e2013-04-02 16:41:03 -07003424 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003425 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003426 final Uri data = r.intent.getData();
3427 final String strData = data != null ? data.toSafeString() : null;
3428
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003429 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003430 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003431 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003432 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003433 }
3434
3435 /**
3436 * Make sure the given activity matches the current configuration. Returns
3437 * false if the activity had to be destroyed. Returns true if the
3438 * configuration is the same, or the activity will remain running as-is
3439 * for whatever reason. Ensures the HistoryRecord is updated with the
3440 * correct configuration and all other bookkeeping is handled.
3441 */
3442 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3443 int globalChanges) {
3444 if (mConfigWillChange) {
3445 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3446 "Skipping config check (will change): " + r);
3447 return true;
3448 }
3449
3450 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3451 "Ensuring correct configuration: " + r);
3452
3453 // Short circuit: if the two configurations are the exact same
3454 // object (the common case), then there is nothing to do.
3455 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003456 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3458 "Configuration unchanged in " + r);
3459 return true;
3460 }
3461
3462 // We don't worry about activities that are finishing.
3463 if (r.finishing) {
3464 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3465 "Configuration doesn't matter in finishing " + r);
3466 r.stopFreezingScreenLocked(false);
3467 return true;
3468 }
3469
3470 // Okay we now are going to make this activity have the new config.
3471 // But then we need to figure out how it needs to deal with that.
3472 Configuration oldConfig = r.configuration;
3473 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003474
3475 // Determine what has changed. May be nothing, if this is a config
3476 // that has come back from the app after going idle. In that case
3477 // we just want to leave the official config object now in the
3478 // activity and do nothing else.
3479 final int changes = oldConfig.diff(newConfig);
3480 if (changes == 0 && !r.forceNewConfig) {
3481 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3482 "Configuration no differences in " + r);
3483 return true;
3484 }
3485
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003486 // If the activity isn't currently running, just leave the new
3487 // configuration and it will pick that up next time it starts.
3488 if (r.app == null || r.app.thread == null) {
3489 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3490 "Configuration doesn't matter not running " + r);
3491 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003492 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003493 return true;
3494 }
3495
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003496 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003497 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3498 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3499 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003500 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003501 + ", newConfig=" + newConfig);
3502 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003503 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003504 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3505 r.configChangeFlags |= changes;
3506 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003507 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003508 if (r.app == null || r.app.thread == null) {
3509 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003510 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003511 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003512 } else if (r.state == ActivityState.PAUSING) {
3513 // A little annoying: we are waiting for this activity to
3514 // finish pausing. Let's not do anything now, but just
3515 // flag that it needs to be restarted when done pausing.
3516 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003517 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003518 r.configDestroy = true;
3519 return true;
3520 } else if (r.state == ActivityState.RESUMED) {
3521 // Try to optimize this case: the configuration is changing
3522 // and we need to restart the top, resumed activity.
3523 // Instead of doing the normal handshaking, just say
3524 // "restart!".
3525 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003526 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003527 relaunchActivityLocked(r, r.configChangeFlags, true);
3528 r.configChangeFlags = 0;
3529 } else {
3530 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003531 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003532 relaunchActivityLocked(r, r.configChangeFlags, false);
3533 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003534 }
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003535
3536 // All done... tell the caller we weren't able to keep this
3537 // activity around.
3538 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003539 }
3540
3541 // Default case: the activity can handle this new configuration, so
3542 // hand it over. Note that we don't need to give it the new
3543 // configuration, since we always send configuration changes to all
3544 // process when they happen so it can just use whatever configuration
3545 // it last got.
3546 if (r.app != null && r.app.thread != null) {
3547 try {
3548 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003549 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003550 } catch (RemoteException e) {
3551 // If process died, whatever.
3552 }
3553 }
3554 r.stopFreezingScreenLocked(false);
3555
3556 return true;
3557 }
3558
3559 private final boolean relaunchActivityLocked(ActivityRecord r,
3560 int changes, boolean andResume) {
3561 List<ResultInfo> results = null;
3562 List<Intent> newIntents = null;
3563 if (andResume) {
3564 results = r.results;
3565 newIntents = r.newIntents;
3566 }
3567 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3568 + " with results=" + results + " newIntents=" + newIntents
3569 + " andResume=" + andResume);
3570 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003571 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003572 r.task.taskId, r.shortComponentName);
3573
3574 r.startFreezingScreenLocked(r.app, 0);
3575
3576 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003577 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3578 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3579 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003580 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003581 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003582 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003583 // Note: don't need to call pauseIfSleepingLocked() here, because
3584 // the caller will only pass in 'andResume' if this activity is
3585 // currently resumed, which implies we aren't sleeping.
3586 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003587 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 }
3589
3590 if (andResume) {
3591 r.results = null;
3592 r.newIntents = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003593 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594 mService.reportResumedActivityLocked(r);
3595 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003596 r.state = ActivityState.RESUMED;
3597 } else {
3598 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3599 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003600 }
3601
3602 return true;
3603 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003604
3605 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003606 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3607 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3608 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3609 final ActivityRecord r = activities.get(activityNdx);
3610 if (r.appToken == token) {
3611 return true;
3612 }
3613 if (r.fullscreen && !r.finishing) {
3614 return false;
3615 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003616 }
3617 }
3618 return true;
3619 }
3620
3621 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003622 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3623 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3624 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3625 final ActivityRecord r = activities.get(activityNdx);
3626 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003627 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003628 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003629 }
3630 }
3631 }
3632
3633 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3634 boolean didSomething = false;
3635 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003636 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3637 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3638 int numActivities = activities.size();
3639 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3640 ActivityRecord r = activities.get(activityNdx);
3641 final boolean samePackage = r.packageName.equals(name)
3642 || (name == null && r.userId == userId);
3643 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3644 && (samePackage || r.task == lastTask)
3645 && (r.app == null || evenPersistent || !r.app.persistent)) {
3646 if (!doit) {
3647 if (r.finishing) {
3648 // If this activity is just finishing, then it is not
3649 // interesting as far as something to stop.
3650 continue;
3651 }
3652 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003653 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003654 didSomething = true;
3655 Slog.i(TAG, " Force finishing activity " + r);
3656 if (samePackage) {
3657 if (r.app != null) {
3658 r.app.removed = true;
3659 }
3660 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003661 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003662 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08003663 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08003664 }
3665 }
3666 }
3667 return didSomething;
3668 }
3669
3670 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003671 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003672 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003673 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3674 --maxNum, --taskNdx) {
3675 final TaskRecord task = mTaskHistory.get(taskNdx);
3676 ActivityRecord r = null;
3677 ActivityRecord top = null;
3678 int numActivities = 0;
3679 int numRunning = 0;
3680 final ArrayList<ActivityRecord> activities = task.mActivities;
3681 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3682 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003683
Craig Mautneraab647e2013-02-28 16:31:36 -08003684 // Initialize state for next task if needed.
3685 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3686 top = r;
3687 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003688 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003689
3690 // Add 'r' into the current task.
3691 numActivities++;
3692 if (r.app != null && r.app.thread != null) {
3693 numRunning++;
3694 }
3695
3696 if (localLOGV) Slog.v(
3697 TAG, r.intent.getComponent().flattenToShortString()
3698 + ": task=" + r.task);
3699 }
3700
3701 RunningTaskInfo ci = new RunningTaskInfo();
3702 ci.id = task.taskId;
3703 ci.baseActivity = r.intent.getComponent();
3704 ci.topActivity = top.intent.getComponent();
3705 if (top.thumbHolder != null) {
3706 ci.description = top.thumbHolder.lastDescription;
3707 }
3708 ci.numActivities = numActivities;
3709 ci.numRunning = numRunning;
3710 //System.out.println(
3711 // "#" + maxNum + ": " + " descr=" + ci.description);
3712 if (receiver != null) {
3713 if (localLOGV) Slog.v(
3714 TAG, "State=" + top.state + "Idle=" + top.idle
3715 + " app=" + top.app
3716 + " thr=" + (top.app != null ? top.app.thread : null));
3717 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3718 if (top.idle && top.app != null && top.app.thread != null) {
3719 topRecord = top;
3720 } else {
3721 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003722 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003723 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003724 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003725 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003726 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003727 }
3728 return topRecord;
3729 }
3730
3731 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003732 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003733 if (DEBUG_SWITCH) Slog.d(
3734 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003735 if (top >= 0) {
3736 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3737 int activityTop = activities.size() - 1;
3738 if (activityTop > 0) {
3739 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3740 "unhandled-back", true);
3741 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003742 }
3743 }
3744
Craig Mautnere79d42682013-04-01 19:01:53 -07003745 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003746 if (!containsApp(app)) {
3747 return;
3748 }
3749 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003750 if (mPausingActivity != null && mPausingActivity.app == app) {
3751 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3752 "App died while pausing: " + mPausingActivity);
3753 mPausingActivity = null;
3754 }
3755 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3756 mLastPausedActivity = null;
3757 }
3758
3759 // Remove this application's activities from active lists.
3760 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3761
3762 if (!restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003763 ActivityStack stack = mStackSupervisor.getTopStack();
3764 if (stack == null) {
3765 mStackSupervisor.resumeHomeActivity(null);
3766 } else if (!stack.resumeTopActivityLocked(null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003767 // If there was nothing to resume, and we are not already
3768 // restarting this process, but there is a visible activity that
3769 // is hosted by the process... then make sure all visible
3770 // activities are running, taking care of restarting this
3771 // process.
3772 if (hasVisibleActivities) {
3773 ensureActivitiesVisibleLocked(null, 0);
3774 }
3775 }
3776 }
3777 }
3778
Craig Mautnercae015f2013-02-08 14:31:27 -08003779 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003780 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3781 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3782 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3783 final ActivityRecord r = activities.get(activityNdx);
3784 if (r.app == app) {
3785 Slog.w(TAG, " Force finishing activity "
3786 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003787 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003788 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003789 }
3790 }
3791 }
3792
3793 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3794 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003795 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3796 final TaskRecord task = mTaskHistory.get(taskNdx);
3797 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003798 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003799 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3800 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003801 }
3802
3803 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3804 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3805
3806 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003807 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3808 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003809 }
3810 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003811 final int top = mTaskHistory.size() - 1;
3812 if (top >= 0) {
3813 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3814 int listTop = list.size() - 1;
3815 if (listTop >= 0) {
3816 activities.add(list.get(listTop));
3817 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003818 }
3819 } else {
3820 ItemMatcher matcher = new ItemMatcher();
3821 matcher.build(name);
3822
Craig Mautneraab647e2013-02-28 16:31:36 -08003823 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3824 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3825 if (matcher.match(r1, r1.intent.getComponent())) {
3826 activities.add(r1);
3827 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003828 }
3829 }
3830 }
3831
3832 return activities;
3833 }
3834
3835 ActivityRecord restartPackage(String packageName) {
3836 ActivityRecord starting = topRunningActivityLocked(null);
3837
3838 // All activities that came from the package must be
3839 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003840 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3841 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3842 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3843 final ActivityRecord a = activities.get(activityNdx);
3844 if (a.info.packageName.equals(packageName)) {
3845 a.forceNewConfig = true;
3846 if (starting != null && a == starting && a.visible) {
3847 a.startFreezingScreenLocked(starting.app,
3848 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3849 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003850 }
3851 }
3852 }
3853
3854 return starting;
3855 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003856
Craig Mautnerde4ef022013-04-07 19:01:33 -07003857 boolean removeTask(TaskRecord task) {
3858 mTaskHistory.remove(task);
3859 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003860 }
3861
Craig Mautnerde4ef022013-04-07 19:01:33 -07003862 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003863 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003864 if (toTop) {
3865 mTaskHistory.add(task);
3866 } else {
3867 mTaskHistory.add(0, task);
3868 }
3869 return task;
3870 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003871
3872 ArrayList<TaskRecord> getAllTasks() {
3873 return new ArrayList<TaskRecord>(mTaskHistory);
3874 }
3875
3876 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003877 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003878 if (task == null) {
3879 return;
3880 }
3881 task.stack.mTaskHistory.remove(task);
3882 task.stack = this;
3883 if (toTop) {
3884 mTaskHistory.add(task);
3885 } else {
3886 mTaskHistory.add(0, task);
3887 }
3888 }
3889
3890 public int getStackId() {
3891 return mStackId;
3892 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003893
3894 @Override
3895 public String toString() {
3896 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3897 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003898}