blob: 5b2ac6d928e35defccfdaa25759e0f532d4db876 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautnerde4ef022013-04-07 19:01:33 -070019import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
20
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070021import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080022import com.android.internal.util.Objects;
Craig Mautnercae015f2013-02-08 14:31:27 -080023import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080024import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080025import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070026import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070027
28import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070029import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070030import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070031import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080032import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080033import android.app.IThumbnailReceiver;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070034import android.app.IThumbnailRetriever;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070035import android.app.IApplicationThread;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070036import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080037import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038import android.content.ComponentName;
39import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070043import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080044import android.content.res.Resources;
45import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080046import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080047import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070049import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.os.Handler;
51import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090052import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.os.Message;
54import android.os.PowerManager;
55import android.os.RemoteException;
56import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070057import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070060import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061
Craig Mautnercae015f2013-02-08 14:31:27 -080062import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080063import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import java.lang.ref.WeakReference;
65import java.util.ArrayList;
66import java.util.Iterator;
67import java.util.List;
68
69/**
70 * State and management of a single stack of activities.
71 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070072final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070074 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
76 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
Craig Mautner29219d92013-04-16 20:19:12 -070077 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
79 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
80 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
81 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
82 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070083 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig Mautnera9a3fb12013-04-18 10:01:00 -070084 static final boolean DEBUG_STACK = ActivityManagerService.DEBUG_STACK;
Craig Mautner9658b312013-02-28 10:55:59 -080085
Craig Mautner2420ead2013-04-01 17:13:20 -070086 static final boolean DEBUG_STATES = ActivityStackSupervisor.DEBUG_STATES;
87 static final boolean DEBUG_ADD_REMOVE = ActivityStackSupervisor.DEBUG_ADD_REMOVE;
88 static final boolean DEBUG_SAVED_STATE = ActivityStackSupervisor.DEBUG_SAVED_STATE;
89 static final boolean DEBUG_APP = ActivityStackSupervisor.DEBUG_APP;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070090
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070091 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080092
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070093 // How long we wait until giving up on the last activity telling us it
94 // is idle.
95 static final int IDLE_TIMEOUT = 10*1000;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070096
97 // Ticks during which we check progress while waiting for an app to launch.
98 static final int LAUNCH_TICK = 500;
99
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700100 // How long we wait until giving up on the last activity to pause. This
101 // is short because it directly impacts the responsiveness of starting the
102 // next activity.
103 static final int PAUSE_TIMEOUT = 500;
104
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700105 // How long we wait for the activity to tell us it has stopped before
106 // giving up. This is a good amount of time because we really need this
107 // from the application in order to get its saved state.
108 static final int STOP_TIMEOUT = 10*1000;
109
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800110 // How long we can hold the sleep wake lock before giving up.
111 static final int SLEEP_TIMEOUT = 5*1000;
112
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700113 // How long we can hold the launch wake lock before giving up.
114 static final int LAUNCH_TIMEOUT = 10*1000;
115
116 // How long we wait until giving up on an activity telling us it has
117 // finished destroying itself.
118 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800121 // disabled.
122 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800123
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700124 // How long between activity launches that we consider safe to not warn
125 // the user about an unexpected activity being launched on top.
126 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // Set to false to disable the preview that is shown while a new activity
129 // is being started.
130 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132 enum ActivityState {
133 INITIALIZING,
134 RESUMED,
135 PAUSING,
136 PAUSED,
137 STOPPING,
138 STOPPED,
139 FINISHING,
140 DESTROYING,
141 DESTROYED
142 }
143
144 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700145 final WindowManagerService mWindowManager;
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 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700170 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800171
172 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173 * Animations that for the current transition have requested not to
174 * be considered for the transition animation.
175 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700176 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700177
178 /**
179 * List of activities that are ready to be finished, but waiting
180 * for the previous activity to settle down before doing so. It contains
181 * HistoryRecord objects.
182 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700183 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800184
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700185 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700186
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 /**
188 * Set when the system is going to sleep, until we have
189 * successfully paused the current activity and released our wake lock.
190 * At that point the system is allowed to actually sleep.
191 */
192 final PowerManager.WakeLock mGoingToSleep;
193
194 /**
195 * We don't want to allow the device to go to sleep while in the process
196 * of launching an activity. This is primarily to allow alarm intent
197 * receivers to launch an activity and get that to run before the device
198 * goes back to sleep.
199 */
200 final PowerManager.WakeLock mLaunchingActivity;
201
202 /**
203 * When we are in the process of pausing an activity, before starting the
204 * next one, this variable holds the activity that is currently being paused.
205 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800206 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700207
208 /**
209 * This is the last activity that we put into the paused state. This is
210 * used to determine if we need to do an activity transition while sleeping,
211 * when we normally hold the top activity paused.
212 */
213 ActivityRecord mLastPausedActivity = null;
214
215 /**
216 * Current activity that is resumed, or null if there is none.
217 */
218 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700220 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700221 * This is the last activity that has been started. It is only used to
222 * identify when multiple activities are started at once so that the user
223 * can be warned they may not be in the activity they think they are.
224 */
225 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800226
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700227 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700228 * Set when we know we are going to be calling updateConfiguration()
229 * soon, so want to skip intermediate config checks.
230 */
231 boolean mConfigWillChange;
232
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700233 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800234
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800235 /**
236 * Set when we have taken too long waiting to go to sleep.
237 */
238 boolean mSleepTimeout = false;
239
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700240 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800241 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
242 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
243 */
244 private ActivityRecord mLastScreenshotActivity = null;
245 private Bitmap mLastScreenshotBitmap = null;
246
Craig Mautnercae015f2013-02-08 14:31:27 -0800247 /**
248 * List of ActivityRecord objects that have been finished and must
249 * still report back to a pending thumbnail receiver.
250 */
251 private final ArrayList<ActivityRecord> mCancelledThumbnails = new ArrayList<ActivityRecord>();
252
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800253 int mThumbnailWidth = -1;
254 int mThumbnailHeight = -1;
255
Amith Yamasani742a6712011-05-04 14:49:28 -0700256 private int mCurrentUser;
257
Craig Mautnerc00204b2013-03-05 15:02:14 -0800258 final int mStackId;
259
Craig Mautner27084302013-03-25 08:05:25 -0700260 /** Run all ActivityStacks through this */
261 final ActivityStackSupervisor mStackSupervisor;
262
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800263 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
264 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
265 static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
266 static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
267 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
268 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
269 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn29ba7e62012-03-16 15:03:36 -0700270 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700271 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700272 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9;
273
274 static class ScheduleDestroyArgs {
275 final ProcessRecord mOwner;
276 final boolean mOomAdj;
277 final String mReason;
278 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
279 mOwner = owner;
280 mOomAdj = oomAdj;
281 mReason = reason;
282 }
283 }
284
Zoran Marcetaf958b322012-08-09 20:27:12 +0900285 final Handler mHandler;
286
287 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 //public Handler() {
289 // if (localLOGV) Slog.v(TAG, "Handler started!");
290 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900291 public ActivityStackHandler(Looper looper) {
292 super(looper);
293 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700294
Zoran Marcetaf958b322012-08-09 20:27:12 +0900295 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700296 public void handleMessage(Message msg) {
297 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800298 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700299 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700300 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700301 Slog.w(TAG, "Sleep timeout! Sleeping now.");
302 mSleepTimeout = true;
303 checkReadyForSleepLocked();
304 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800305 }
306 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700307 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800308 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 // We don't at this point know if the activity is fullscreen,
310 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800311 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700312 synchronized (mService) {
313 if (r.app != null) {
314 mService.logAppTooSlow(r.app, r.pauseTime,
315 "pausing " + r);
316 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700317
Craig Mautnerd2328952013-03-05 12:46:26 -0800318 activityPausedLocked(r != null ? r.appToken : null, true);
319 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700320 } break;
321 case IDLE_TIMEOUT_MSG: {
322 if (mService.mDidDexOpt) {
323 mService.mDidDexOpt = false;
324 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
325 nmsg.obj = msg.obj;
326 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
327 return;
328 }
329 // We don't at this point know if the activity is fullscreen,
330 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800331 ActivityRecord r = (ActivityRecord)msg.obj;
332 Slog.w(TAG, "Activity idle timeout for " + r);
Craig Mautner6170f732013-04-02 13:05:23 -0700333 synchronized (mService) {
Craig Mautnere79d42682013-04-01 19:01:53 -0700334 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
335 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700337 case LAUNCH_TICK_MSG: {
338 ActivityRecord r = (ActivityRecord)msg.obj;
339 synchronized (mService) {
340 if (r.continueLaunchTickingLocked()) {
341 mService.logAppTooSlow(r.app, r.launchTickTime,
342 "launching " + r);
343 }
344 }
345 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700346 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800347 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700348 // We don't at this point know if the activity is fullscreen,
349 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800350 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800351 synchronized (mService) {
352 activityDestroyedLocked(r != null ? r.appToken : null);
353 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700354 } break;
355 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800356 ActivityRecord r = (ActivityRecord)msg.obj;
Craig Mautnere79d42682013-04-01 19:01:53 -0700357 synchronized (mService) {
358 activityIdleInternalLocked(r != null ? r.appToken : null, false, null);
359 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700360 } break;
361 case LAUNCH_TIMEOUT_MSG: {
362 if (mService.mDidDexOpt) {
363 mService.mDidDexOpt = false;
364 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
365 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
366 return;
367 }
368 synchronized (mService) {
369 if (mLaunchingActivity.isHeld()) {
370 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
371 mLaunchingActivity.release();
372 }
373 }
374 } break;
375 case RESUME_TOP_ACTIVITY_MSG: {
376 synchronized (mService) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700377 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 }
379 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700380 case STOP_TIMEOUT_MSG: {
381 ActivityRecord r = (ActivityRecord)msg.obj;
382 // We don't at this point know if the activity is fullscreen,
383 // so we need to be conservative and assume it isn't.
384 Slog.w(TAG, "Activity stop timeout for " + r);
385 synchronized (mService) {
386 if (r.isInHistory()) {
387 activityStoppedLocked(r, null, null, null);
388 }
389 }
390 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700391 case DESTROY_ACTIVITIES_MSG: {
392 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
393 synchronized (mService) {
394 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
395 }
396 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700397 }
398 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800399 }
400
Craig Mautner000f0022013-02-26 15:04:29 -0800401 private int numActivities() {
402 int count = 0;
403 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
404 count += mTaskHistory.get(taskNdx).mActivities.size();
405 }
406 return count;
407 }
408
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700409 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900410 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700411 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700412 mWindowManager = service.mWindowManager;
413 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700415 PowerManager pm =
416 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
417 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700418 mLaunchingActivity =
419 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700420 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800421 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700422 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700423 }
Craig Mautner5962b122012-10-05 14:45:52 -0700424
425 private boolean okToShow(ActivityRecord r) {
426 return r.userId == mCurrentUser
427 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
428 }
429
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700430 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800431 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
432 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800433 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800434 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
435 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800436 if (!r.finishing && r != notTop && okToShow(r)) {
437 return r;
438 }
439 }
440 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700441 return null;
442 }
443
444 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800447 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800448 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
449 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800450 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
451 return r;
452 }
453 }
454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 return null;
456 }
457
458 /**
459 * This is a simplified version of topRunningActivityLocked that provides a number of
460 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800461 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 * @param token If non-null, any history records matching this token will be skipped.
463 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800464 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700465 * @return Returns the HistoryRecord of the next activity on the stack.
466 */
467 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
469 TaskRecord task = mTaskHistory.get(taskNdx);
470 if (task.taskId == taskId) {
471 continue;
472 }
473 ArrayList<ActivityRecord> activities = task.mActivities;
474 for (int i = activities.size() - 1; i >= 0; --i) {
475 final ActivityRecord r = activities.get(i);
476 // Note: the taskId check depends on real taskId fields being non-zero
477 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
478 return r;
479 }
480 }
481 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700482 return null;
483 }
484
Craig Mautner8849a5e2013-04-02 16:41:03 -0700485 final ActivityRecord topActivity() {
486 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700487 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700488 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
489 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
490 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700491 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700492 }
493 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700494 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700495 }
496
Craig Mautnerd2328952013-03-05 12:46:26 -0800497 TaskRecord taskForIdLocked(int id) {
498 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
499 final TaskRecord task = mTaskHistory.get(taskNdx);
500 if (task.taskId == id) {
501 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800502 }
503 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700504 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700505 }
506
Craig Mautnerd2328952013-03-05 12:46:26 -0800507 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800508 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800509 if (r != null) {
510 final TaskRecord task = r.task;
511 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
512 if (task.stack != this) Slog.w(TAG,
513 "Illegal state! task does not point to stack it is in.");
514 return r;
515 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800516 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800517 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800518 }
519
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700520 boolean containsApp(ProcessRecord app) {
521 if (app == null) {
522 return false;
523 }
524 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
525 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
526 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
527 final ActivityRecord r = activities.get(activityNdx);
528 if (r.finishing) {
529 continue;
530 }
531 if (r.app == app) {
532 return true;
533 }
534 }
535 }
536 return false;
537 }
538
Craig Mautner2420ead2013-04-01 17:13:20 -0700539 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700540 final boolean hadit = mLRUActivities.remove(r);
541 mLRUActivities.add(r);
542 return hadit;
543 }
544
Craig Mautnerde4ef022013-04-07 19:01:33 -0700545 final boolean isHomeStack() {
546 return mStackId == HOME_STACK_ID;
547 }
548
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700549 /**
550 * Returns the top activity in any existing task matching the given
551 * Intent. Returns null if no such task is found.
552 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700553 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700554 ComponentName cls = intent.getComponent();
555 if (info.targetActivity != null) {
556 cls = new ComponentName(info.packageName, info.targetActivity);
557 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700558 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800559
560 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
561 final TaskRecord task = mTaskHistory.get(taskNdx);
562 final ActivityRecord r = task.getTopActivity();
563 if (r == null || r.finishing || r.userId != userId ||
564 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
565 continue;
566 }
567
568 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
569 // + "/aff=" + r.task.affinity + " to new cls="
570 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
571 if (task.affinity != null) {
572 if (task.affinity.equals(info.taskAffinity)) {
573 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700574 return r;
575 }
Craig Mautner000f0022013-02-26 15:04:29 -0800576 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
577 //Slog.i(TAG, "Found matching class!");
578 //dump();
579 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
580 return r;
581 } else if (task.affinityIntent != null
582 && task.affinityIntent.getComponent().equals(cls)) {
583 //Slog.i(TAG, "Found matching class!");
584 //dump();
585 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
586 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700587 }
588 }
589
590 return null;
591 }
592
593 /**
594 * Returns the first activity (starting from the top of the stack) that
595 * is the same as the given activity. Returns null if no such activity
596 * is found.
597 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700598 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700599 ComponentName cls = intent.getComponent();
600 if (info.targetActivity != null) {
601 cls = new ComponentName(info.packageName, info.targetActivity);
602 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700603 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700604
Craig Mautner000f0022013-02-26 15:04:29 -0800605 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
606 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
607 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
608 ActivityRecord r = activities.get(activityNdx);
609 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700610 //Slog.i(TAG, "Found matching class!");
611 //dump();
612 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
613 return r;
614 }
615 }
616 }
617
618 return null;
619 }
620
Amith Yamasani742a6712011-05-04 14:49:28 -0700621 /*
622 * Move the activities around in the stack to bring a user to the foreground.
623 * @return whether there are any activities for the specified user.
624 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700625 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800626 if (VALIDATE_TOKENS) {
627 validateAppTokensLocked();
628 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700629 mStartingUsers.add(uss);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800630 if (mCurrentUser == userId) {
631 return true;
632 }
633 mCurrentUser = userId;
634
635 // Move userId's tasks to the top.
636 boolean haveActivities = false;
637 TaskRecord task = null;
638 int index = mTaskHistory.size();
639 for (int i = 0; i < index; ++i) {
640 task = mTaskHistory.get(i);
641 if (task.userId == userId) {
642 haveActivities = true;
643 mTaskHistory.remove(i);
644 mTaskHistory.add(task);
645 --index;
646 }
647 }
648
649 // task is now the original topmost TaskRecord. Transition from the old top to the new top.
650 ActivityRecord top = task != null ? task.getTopActivity() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800651 resumeTopActivityLocked(top);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700652 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700653 }
654
Craig Mautner2420ead2013-04-01 17:13:20 -0700655 void minimalResumeActivityLocked(ActivityRecord r) {
656 r.state = ActivityState.RESUMED;
657 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
658 + " (starting new instance)");
659 r.stopped = false;
660 mResumedActivity = r;
661 r.task.touchActiveTime();
662 mService.addRecentTaskLocked(r.task);
663 completeResumeLocked(r);
664 checkReadyForSleepLocked();
665 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700666 }
667
Craig Mautnere79d42682013-04-01 19:01:53 -0700668 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700669 if (r.launchTime == 0) {
670 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700671 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700672 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700673 }
674 } else if (mInitialStartTime == 0) {
675 mInitialStartTime = SystemClock.uptimeMillis();
676 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800678
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800679 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700680 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 if (!mGoingToSleep.isHeld()) {
682 mGoingToSleep.acquire();
683 if (mLaunchingActivity.isHeld()) {
684 mLaunchingActivity.release();
685 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
686 }
687 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800688 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
689 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
690 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
691 checkReadyForSleepLocked();
692 }
693 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700694
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800695 void awakeFromSleepingLocked() {
696 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
697 mSleepTimeout = false;
698 if (mGoingToSleep.isHeld()) {
699 mGoingToSleep.release();
700 }
701 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800702 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
703 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
704 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700705 // TODO: Skip if finishing?
Craig Mautnerd44711d2013-02-23 11:24:36 -0800706 activities.get(activityNdx).setSleeping(false);
707 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800708 }
709 mGoingToSleepActivities.clear();
710 }
711
712 void activitySleptLocked(ActivityRecord r) {
713 mGoingToSleepActivities.remove(r);
714 checkReadyForSleepLocked();
715 }
716
Craig Mautnere79d42682013-04-01 19:01:53 -0700717 // Checked.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800718 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700719 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800720 // Do not care.
721 return;
722 }
723
724 if (!mSleepTimeout) {
725 if (mResumedActivity != null) {
726 // Still have something resumed; can't sleep until it is paused.
727 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700728 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
729 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800730 return;
731 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800732 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800733 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800734 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800735 return;
736 }
737
Craig Mautnerde4ef022013-04-07 19:01:33 -0700738 if (mStackSupervisor.mStoppingActivities.size() > 0) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800739 // Still need to tell some activities to stop; can't sleep yet.
740 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
Craig Mautnerde4ef022013-04-07 19:01:33 -0700741 + mStackSupervisor.mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700742 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800743 return;
744 }
745
746 ensureActivitiesVisibleLocked(null, 0);
747
748 // Make sure any stopped but visible activities are now sleeping.
749 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800750 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
751 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
752 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
753 final ActivityRecord r = activities.get(activityNdx);
754 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
755 r.setSleeping(true);
756 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800757 }
758 }
759
760 if (mGoingToSleepActivities.size() > 0) {
761 // Still need to tell some activities to sleep; can't sleep yet.
762 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
763 + mGoingToSleepActivities.size() + " activities");
764 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 }
766 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800767
768 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
769
770 if (mGoingToSleep.isHeld()) {
771 mGoingToSleep.release();
772 }
773 if (mService.mShuttingDown) {
774 mService.notifyAll();
775 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700776 }
Craig Mautner59c00972012-07-30 12:10:24 -0700777
Dianne Hackbornd2835932010-12-13 16:28:46 -0800778 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800779 if (who.noDisplay) {
780 return null;
781 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800782
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800783 Resources res = mService.mContext.getResources();
784 int w = mThumbnailWidth;
785 int h = mThumbnailHeight;
786 if (w < 0) {
787 mThumbnailWidth = w =
788 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
789 mThumbnailHeight = h =
790 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
791 }
792
793 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800794 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
795 || mLastScreenshotBitmap.getWidth() != w
796 || mLastScreenshotBitmap.getHeight() != h) {
797 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700798 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800799 who.appToken, Display.DEFAULT_DISPLAY, w, h);
800 }
801 if (mLastScreenshotBitmap != null) {
802 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
803 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800804 }
805 return null;
806 }
807
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800809 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800810 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700811 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800812 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700813 ActivityRecord prev = mResumedActivity;
814 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700815 Slog.e(TAG, "Trying to pause when nothing is resumed",
816 new RuntimeException("here").fillInStackTrace());
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700817 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 return;
819 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700820 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
821 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800823 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700824 mLastPausedActivity = prev;
825 prev.state = ActivityState.PAUSING;
826 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700827 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828
829 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800830
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 if (prev.app != null && prev.app.thread != null) {
832 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
833 try {
834 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700835 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800837 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
838 userLeaving, prev.configChangeFlags);
Craig Mautner7d9eaa42013-04-19 13:57:33 -0700839 mService.updateUsageStats(prev, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 } catch (Exception e) {
841 // Ignore exception, if process died other code will cleanup.
842 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800843 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 mLastPausedActivity = null;
845 }
846 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800847 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700848 mLastPausedActivity = null;
849 }
850
851 // If we are not going to sleep, we want to ensure the device is
852 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700853 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 mLaunchingActivity.acquire();
855 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
856 // To be safe, don't allow the wake lock to be held for too long.
857 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
858 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
859 }
860 }
861
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800862 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700863 // Have the window manager pause its key dispatching until the new
864 // activity has started. If we're pausing the activity just because
865 // the screen is being turned off and the UI is sleeping, don't interrupt
866 // key dispatch; the same activity will pick it up again on wakeup.
867 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 } else {
870 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
871 }
872
873 // Schedule a pause timeout in case the app doesn't respond.
874 // We don't give it much time because this directly impacts the
875 // responsiveness seen by the user.
876 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
877 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700878 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
880 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
881 } else {
882 // This activity failed to schedule the
883 // pause, so just treat it as being paused now.
884 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700885 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700886 }
887 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700888
Craig Mautnerd2328952013-03-05 12:46:26 -0800889 final void activityResumedLocked(IBinder token) {
890 final ActivityRecord r = ActivityRecord.forToken(token);
891 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
892 r.icicle = null;
893 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700894 }
895
Craig Mautnerd2328952013-03-05 12:46:26 -0800896 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700897 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800898 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700899
Craig Mautnerd2328952013-03-05 12:46:26 -0800900 final ActivityRecord r = isInStackLocked(token);
901 if (r != null) {
902 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
903 if (mPausingActivity == r) {
904 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
905 + (timeout ? " (due to timeout)" : " (pause complete)"));
906 r.state = ActivityState.PAUSED;
907 completePauseLocked();
908 } else {
909 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
910 r.userId, System.identityHashCode(r), r.shortComponentName,
911 mPausingActivity != null
912 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 }
914 }
915 }
916
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700917 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
918 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700919 if (r.state != ActivityState.STOPPING) {
920 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
921 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
922 return;
923 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700924 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700925 if (icicle != null) {
926 // If icicle is null, this is happening due to a timeout, so we
927 // haven't really saved the state.
928 r.icicle = icicle;
929 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800930 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700931 r.updateThumbnail(thumbnail, description);
932 }
933 if (!r.stopped) {
934 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
935 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
936 r.stopped = true;
937 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700938 if (r.finishing) {
939 r.clearOptionsLocked();
940 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700941 if (r.configDestroy) {
942 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700943 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700944 } else {
945 // Now that this process has stopped, we may want to consider
946 // it to be the previous app to try to keep around in case
947 // the user wants to return to it.
948 ProcessRecord fgApp = null;
949 if (mResumedActivity != null) {
950 fgApp = mResumedActivity.app;
951 } else if (mPausingActivity != null) {
952 fgApp = mPausingActivity.app;
953 }
954 if (r.app != null && fgApp != null && r.app != fgApp
955 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
956 && r.app != mService.mHomeProcess) {
957 mService.mPreviousProcess = r.app;
958 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
959 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800960 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700961 }
962 }
963 }
964
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 private final void completePauseLocked() {
966 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700967 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800968
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800969 if (prev != null) {
970 if (prev.finishing) {
971 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700972 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800973 } else if (prev.app != null) {
974 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
975 if (prev.waitingVisible) {
976 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700977 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
979 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800980 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800981 if (prev.configDestroy) {
982 // The previous is being paused because the configuration
983 // is changing, which means it is actually stopping...
984 // To juggle the fact that we are also starting a new
985 // instance right now, we need to first completely stop
986 // the current instance before starting the new one.
987 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
988 destroyActivityLocked(prev, true, false, "pause-config");
989 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700990 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700991 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
992 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800993 // If we already have a few activities waiting to stop,
994 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700995 // them out. Or if r is the last of activity of the last task the stack
996 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800997 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
998 scheduleIdleLocked();
999 } else {
1000 checkReadyForSleepLocked();
1001 }
1002 }
1003 } else {
1004 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1005 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001006 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001008 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001009
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001010 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -07001011 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 topStack.resumeTopActivityLocked(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001013 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001014 checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001015 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001016 if (top == null || (prev != null && top != prev)) {
1017 // If there are no more activities available to run,
1018 // do resume anyway to start something. Also if the top
1019 // activity on the stack is not the just paused activity,
1020 // we need to go ahead and resume it to ensure we complete
1021 // an in-flight app switch.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001022 topStack.resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001023 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001025
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001026 if (prev != null) {
1027 prev.resumeKeyDispatchingLocked();
1028 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029
1030 if (prev.app != null && prev.cpuTimeAtResume > 0
1031 && mService.mBatteryStatsService.isOnBattery()) {
1032 long diff = 0;
1033 synchronized (mService.mProcessStatsThread) {
1034 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1035 - prev.cpuTimeAtResume;
1036 }
1037 if (diff > 0) {
1038 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1039 synchronized (bsi) {
1040 BatteryStatsImpl.Uid.Proc ps =
1041 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1042 prev.info.packageName);
1043 if (ps != null) {
1044 ps.addForegroundTimeLocked(diff);
1045 }
1046 }
1047 }
1048 }
1049 prev.cpuTimeAtResume = 0; // reset it
1050 }
1051
Craig Mautnere79d42682013-04-01 19:01:53 -07001052 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001053 /**
1054 * Once we know that we have asked an application to put an activity in
1055 * the resumed state (either by launching it or explicitly telling it),
1056 * this function updates the rest of our state to match that fact.
1057 */
1058 private final void completeResumeLocked(ActivityRecord next) {
1059 next.idle = false;
1060 next.results = null;
1061 next.newIntents = null;
1062
1063 // schedule an idle timeout in case the app doesn't do it for us.
1064 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1065 msg.obj = next;
1066 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1067
1068 if (false) {
1069 // The activity was never told to pause, so just keep
1070 // things going as-is. To maintain our own state,
1071 // we need to emulate it coming back and saying it is
1072 // idle.
1073 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1074 msg.obj = next;
1075 mHandler.sendMessage(msg);
1076 }
1077
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001078 mStackSupervisor.reportResumedActivityLocked(next);
1079
1080 next.resumeKeyDispatchingLocked();
1081 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001082
1083 // Mark the point when the activity is resuming
1084 // TODO: To be more accurate, the mark should be before the onCreate,
1085 // not after the onResume. But for subsequent starts, onResume is fine.
1086 if (next.app != null) {
1087 synchronized (mService.mProcessStatsThread) {
1088 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1089 }
1090 } else {
1091 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1092 }
1093 }
1094
Craig Mautnere79d42682013-04-01 19:01:53 -07001095 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096 /**
1097 * Make sure that all activities that need to be visible (that is, they
1098 * currently can be seen by the user) actually are.
1099 */
1100 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1101 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1102 if (DEBUG_VISBILITY) Slog.v(
1103 TAG, "ensureActivitiesVisible behind " + top
1104 + " configChanges=0x" + Integer.toHexString(configChanges));
1105
1106 // If the top activity is not fullscreen, then we need to
1107 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001108 boolean aboveTop = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001109 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this);
1110 int taskNdx;
1111 for (taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001112 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1113 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1114 final ActivityRecord r = activities.get(activityNdx);
1115 if (r.finishing) {
1116 continue;
1117 }
1118 if (aboveTop && r != top) {
1119 continue;
1120 }
1121 aboveTop = false;
1122 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001124 TAG, "Make visible? " + r + " finishing=" + r.finishing
1125 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001126
Craig Mautnerd44711d2013-02-23 11:24:36 -08001127 final boolean doThisProcess = onlyThisProcess == null
1128 || onlyThisProcess.equals(r.processName);
1129
1130 // First: if this is not the current activity being started, make
1131 // sure it matches the current configuration.
1132 if (r != starting && doThisProcess) {
1133 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001135
1136 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001137 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001138 // This activity needs to be visible, but isn't even
1139 // running... get it started, but don't resume it
1140 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001141 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001142 if (r != starting) {
1143 r.startFreezingScreenLocked(r.app, configChanges);
1144 }
1145 if (!r.visible) {
1146 if (DEBUG_VISBILITY) Slog.v(
1147 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001148 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001149 }
1150 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001151 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001152 }
1153 }
1154
1155 } else if (r.visible) {
1156 // If this activity is already visible, then there is nothing
1157 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001158 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001159 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001160
1161 } else if (onlyThisProcess == null) {
1162 // This activity is not currently visible, but is running.
1163 // Tell it to become visible.
1164 r.visible = true;
1165 if (r.state != ActivityState.RESUMED && r != starting) {
1166 // If this activity is paused, tell it
1167 // to now show its window.
1168 if (DEBUG_VISBILITY) Slog.v(
1169 TAG, "Making visible and scheduling visibility: " + r);
1170 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001171 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001172 r.sleeping = false;
1173 r.app.pendingUiClean = true;
1174 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1175 r.stopFreezingScreenLocked(false);
1176 } catch (Exception e) {
1177 // Just skip on any failure; we'll make it
1178 // visible when it next restarts.
1179 Slog.w(TAG, "Exception thrown making visibile: "
1180 + r.intent.getComponent(), e);
1181 }
1182 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184
Craig Mautnerd44711d2013-02-23 11:24:36 -08001185 // Aggregate current change flags.
1186 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187
Craig Mautnerd44711d2013-02-23 11:24:36 -08001188 if (r.fullscreen) {
1189 // At this point, nothing else needs to be shown
1190 if (DEBUG_VISBILITY) Slog.v(
1191 TAG, "Stopping: fullscreen at " + r);
1192 behindFullscreen = true;
1193 }
1194 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001195 if (DEBUG_VISBILITY) Slog.v(
1196 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1197 + " state=" + r.state
1198 + " behindFullscreen=" + behindFullscreen);
1199 // Now for any activities that aren't visible to the user, make
1200 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001201 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001202 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001203 r.visible = false;
1204 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001205 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001206 if ((r.state == ActivityState.STOPPING
1207 || r.state == ActivityState.STOPPED)
1208 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001209 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001210 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211 }
1212 } catch (Exception e) {
1213 // Just skip on any failure; we'll make it
1214 // visible when it next restarts.
1215 Slog.w(TAG, "Exception thrown making hidden: "
1216 + r.intent.getComponent(), e);
1217 }
1218 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001219 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001220 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001221 }
1222 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001223 }
1224 }
1225
Craig Mautnere79d42682013-04-01 19:01:53 -07001226 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001227 /**
1228 * Version of ensureActivitiesVisible that can easily be called anywhere.
1229 */
1230 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1231 int configChanges) {
1232 ActivityRecord r = topRunningActivityLocked(null);
1233 if (r != null) {
1234 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1235 }
1236 }
Craig Mautner58547802013-03-05 08:23:53 -08001237
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001238 /**
1239 * Ensure that the top activity in the stack is resumed.
1240 *
1241 * @param prev The previously resumed activity, for when in the process
1242 * of pausing; can be null to call from elsewhere.
1243 *
1244 * @return Returns true if something is being resumed, or false if
1245 * nothing happened.
1246 */
1247 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001248 return resumeTopActivityLocked(prev, null);
1249 }
1250
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07001251 // Checked.
Dianne Hackborn84375872012-06-01 19:03:50 -07001252 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 // Find the first activity that is not finishing.
1254 ActivityRecord next = topRunningActivityLocked(null);
1255
1256 // Remember how we'll process this pause/resume situation, and ensure
1257 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001258 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1259 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001260
1261 if (next == null) {
1262 // There are no more activities! Let's just start up the
1263 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001264 ActivityOptions.abort(options);
Craig Mautner69ada552013-04-18 13:51:51 -07001265 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001266 }
1267
1268 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001269
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001270 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001271 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1272 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001273 // Make sure we have executed any pending transitions, since there
1274 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001275 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001276 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001277 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001278 return false;
1279 }
1280
Craig Mautnerde4ef022013-04-07 19:01:33 -07001281 if (prev != null && prev.mLaunchHomeTaskNext && prev.finishing &&
1282 prev.task.getTopActivity() == null) {
Craig Mautner69ada552013-04-18 13:51:51 -07001283 return mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001284 }
1285
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 // If we are sleeping, and there is no resumed activity, and the top
1287 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001288 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001289 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001290 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001291 // Make sure we have executed any pending transitions, since there
1292 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001293 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001294 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001295 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296 return false;
1297 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001298
1299 // Make sure that the user who owns this activity is started. If not,
1300 // we will just leave it as is because someone should be bringing
1301 // another user's activities to the top of the stack.
1302 if (mService.mStartedUsers.get(next.userId) == null) {
1303 Slog.w(TAG, "Skipping resume of top activity " + next
1304 + ": user " + next.userId + " is stopped");
1305 return false;
1306 }
1307
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001308 // The activity may be waiting for stop, but that is no longer
1309 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001310 mStackSupervisor.mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001311 mGoingToSleepActivities.remove(next);
1312 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001313 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001314
Dianne Hackborn84375872012-06-01 19:03:50 -07001315 next.updateOptionsLocked(options);
1316
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1318
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001319 // If we are currently pausing an activity, then don't do anything
1320 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001321 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1322 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001323 return false;
1324 }
1325
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001326 // Okay we are now going to start a switch, to 'next'. We may first
1327 // have to pause the current activity, but this is an important point
1328 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001329 // XXX "App Redirected" dialog is getting too many false positives
1330 // at this point, so turn off for now.
1331 if (false) {
1332 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1333 long now = SystemClock.uptimeMillis();
1334 final boolean inTime = mLastStartedActivity.startTime != 0
1335 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1336 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1337 final int nextUid = next.info.applicationInfo.uid;
1338 if (inTime && lastUid != nextUid
1339 && lastUid != next.launchedFromUid
1340 && mService.checkPermission(
1341 android.Manifest.permission.STOP_APP_SWITCHES,
1342 -1, next.launchedFromUid)
1343 != PackageManager.PERMISSION_GRANTED) {
1344 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1345 } else {
1346 next.startTime = now;
1347 mLastStartedActivity = next;
1348 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001349 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001350 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001351 mLastStartedActivity = next;
1352 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001353 }
Craig Mautner58547802013-03-05 08:23:53 -08001354
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001355 // We need to start pausing the current activity so the top one
1356 // can be resumed...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001357 final ActivityStack lastStack = mStackSupervisor.getLastStack();
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001358 if (lastStack != null && (isHomeStack() ^ lastStack.isHomeStack()) &&
1359 lastStack.mResumedActivity != null) {
Craig Mautner69ada552013-04-18 13:51:51 -07001360 // TODO: Don't pause when launching to the sibling task.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001361 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001362 // At this point we want to put the upcoming activity's process
1363 // at the top of the LRU list, since we know we will be needing it
1364 // very soon and it would be a waste to let it get killed if it
1365 // happens to be sitting towards the end.
1366 if (next.app != null && next.app.thread != null) {
1367 // No reason to do full oom adj update here; we'll let that
1368 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001369 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001370 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001371 lastStack.startPausingLocked(userLeaving, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001372 return true;
1373 }
1374
Christopher Tated3f175c2012-06-14 14:16:54 -07001375 // If the most recent activity was noHistory but was only stopped rather
1376 // than stopped+finished because the device went to sleep, we need to make
1377 // sure to finish it as we're making a new activity topmost.
1378 final ActivityRecord last = mLastPausedActivity;
1379 if (mService.mSleeping && last != null && !last.finishing) {
1380 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1381 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1382 if (DEBUG_STATES) {
1383 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1384 }
1385 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001386 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001387 }
1388 }
1389
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 if (prev != null && prev != next) {
1391 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1392 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001393 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001394 if (DEBUG_SWITCH) Slog.v(
1395 TAG, "Resuming top, waiting visible to hide: " + prev);
1396 } else {
1397 // The next activity is already visible, so hide the previous
1398 // activity's windows right now so we can show the new one ASAP.
1399 // We only do this if the previous is finishing, which should mean
1400 // it is on top of the one being resumed so hiding it quickly
1401 // is good. Otherwise, we want to do the normal route of allowing
1402 // the resumed activity to be shown so we can decide if the
1403 // previous should actually be hidden depending on whether the
1404 // new one is found to be full-screen or not.
1405 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001406 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1408 + prev + ", waitingVisible="
1409 + (prev != null ? prev.waitingVisible : null)
1410 + ", nowVisible=" + next.nowVisible);
1411 } else {
1412 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1413 + prev + ", waitingVisible="
1414 + (prev != null ? prev.waitingVisible : null)
1415 + ", nowVisible=" + next.nowVisible);
1416 }
1417 }
1418 }
1419
Dianne Hackborne7f97212011-02-24 14:40:20 -08001420 // Launching this app's activity, make sure the app is no longer
1421 // considered stopped.
1422 try {
1423 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001424 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001425 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001426 } catch (IllegalArgumentException e) {
1427 Slog.w(TAG, "Failed trying to unstop package "
1428 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001429 }
1430
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431 // We are starting up the next activity, so tell the window manager
1432 // that the previous one will be hidden soon. This way it can know
1433 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001434 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 if (prev != null) {
1436 if (prev.finishing) {
1437 if (DEBUG_TRANSITION) Slog.v(TAG,
1438 "Prepare close transition: prev=" + prev);
1439 if (mNoAnimActivities.contains(prev)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001440 noAnim = true;
1441 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001443 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001444 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1445 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001447 mWindowManager.setAppWillBeHidden(prev.appToken);
1448 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001450 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001452 noAnim = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001453 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001455 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001456 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1457 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458 }
1459 }
1460 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001461 mWindowManager.setAppWillBeHidden(prev.appToken);
1462 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 }
Craig Mautner967212c2013-04-13 21:10:58 -07001464 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001465 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001467 noAnim = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001468 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001470 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 }
1472 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001473 if (!noAnim) {
1474 next.applyOptionsLocked();
1475 } else {
1476 next.clearOptionsLocked();
1477 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478
1479 if (next.app != null && next.app.thread != null) {
1480 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1481
1482 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001483 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001485 // schedule launch ticks to collect information about slow apps.
1486 next.startLaunchTickingLocked();
1487
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001488 ActivityRecord lastResumedActivity =
1489 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 ActivityState lastState = next.state;
1491
1492 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001493
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001494 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 next.state = ActivityState.RESUMED;
1496 mResumedActivity = next;
1497 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001498 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001499 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001500 updateLRUListLocked(next);
1501
1502 // Have the window manager re-evaluate the orientation of
1503 // the screen based on the new activity order.
1504 boolean updated = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001505 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001506 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001507 mService.mConfiguration,
1508 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1509 if (config != null) {
1510 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001512 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001514
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001515 if (!updated) {
1516 // The configuration update wasn't able to keep the existing
1517 // instance of the activity, and instead started a new one.
1518 // We should be all done, but let's just make sure our activity
1519 // is still at the top and schedule another run if something
1520 // weird happened.
1521 ActivityRecord nextNext = topRunningActivityLocked(null);
1522 if (DEBUG_SWITCH) Slog.i(TAG,
1523 "Activity config changed during resume: " + next
1524 + ", new next: " + nextNext);
1525 if (nextNext != next) {
1526 // Do over!
1527 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1528 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001529 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001530 mNoAnimActivities.clear();
1531 return true;
1532 }
1533 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534 }
Craig Mautner58547802013-03-05 08:23:53 -08001535
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 try {
1537 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001538 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001539 if (a != null) {
1540 final int N = a.size();
1541 if (!next.finishing && N > 0) {
1542 if (DEBUG_RESULTS) Slog.v(
1543 TAG, "Delivering results to " + next
1544 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001545 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 }
1547 }
1548
1549 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001550 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551 }
1552
1553 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001554 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001555 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001556
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001557 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001558 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001559 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001560 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001562
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001563 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001564
1565 } catch (Exception e) {
1566 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001567 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1568 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001570 if (lastStack != null) {
1571 lastStack.mResumedActivity = lastResumedActivity;
1572 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001573 Slog.i(TAG, "Restarting because process died: " + next);
1574 if (!next.hasBeenLaunched) {
1575 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001576 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1577 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001578 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001579 next.appToken, next.packageName, next.theme,
1580 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
1581 next.nonLocalizedLabel, next.labelRes, next.icon, next.windowFlags,
1582 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001584 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585 return true;
1586 }
1587
1588 // From this point on, if something goes wrong there is no way
1589 // to recover the activity.
1590 try {
1591 next.visible = true;
1592 completeResumeLocked(next);
1593 } catch (Exception e) {
1594 // If any exception gets thrown, toss away this
1595 // activity and try the next one.
1596 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001597 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001598 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599 return true;
1600 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601 next.stopped = false;
1602
1603 } else {
1604 // Whoops, need to restart this activity!
1605 if (!next.hasBeenLaunched) {
1606 next.hasBeenLaunched = true;
1607 } else {
1608 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001609 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001610 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001611 mService.compatibilityInfoForPackageLocked(
1612 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001614 next.labelRes, next.icon, next.windowFlags,
1615 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 }
1617 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1618 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001619 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 }
1621
1622 return true;
1623 }
1624
Craig Mautner11bf9a52013-02-19 14:08:51 -08001625
Craig Mautner8849a5e2013-04-02 16:41:03 -07001626 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001627 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001628 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001629 TaskRecord rTask = r.task;
1630 final int taskId = rTask.taskId;
1631 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001632 // Last activity in task had been removed or ActivityManagerService is reusing task.
1633 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001634 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001635 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001636 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001637 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001638 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001639 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 if (!newTask) {
1641 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001642 boolean startIt = true;
1643 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1644 task = mTaskHistory.get(taskNdx);
1645 if (task == r.task) {
1646 // Here it is! Now, if this is not yet visible to the
1647 // user, then just add it without starting; it will
1648 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001649 if (!startIt) {
1650 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1651 + task, new RuntimeException("here").fillInStackTrace());
1652 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001653 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001654 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1655 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001656 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001657 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001658 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001660 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 return;
1662 }
1663 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001664 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 startIt = false;
1666 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 }
1668 }
1669
1670 // Place a new activity at top of stack, so it is next to interact
1671 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 // If we are not placing the new activity frontmost, we do not want
1674 // to deliver the onUserLeaving callback to the actual frontmost
1675 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001676 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001677 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001678 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1679 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680 }
Craig Mautner70a86932013-02-28 22:37:44 -08001681
1682 task = r.task;
1683
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001685 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001686 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001687 task.addActivityToTop(r);
1688
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001689 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001691 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001692 // We want to show the starting preview window if we are
1693 // switching to a new task, or the next activity's process is
1694 // not currently running.
1695 boolean showStartingIcon = newTask;
1696 ProcessRecord proc = r.app;
1697 if (proc == null) {
1698 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1699 }
1700 if (proc == null || proc.thread == null) {
1701 showStartingIcon = true;
1702 }
1703 if (DEBUG_TRANSITION) Slog.v(TAG,
1704 "Prepare open transition: starting " + r);
1705 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001706 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001708 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001709 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001710 ? AppTransition.TRANSIT_TASK_OPEN
1711 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001712 mNoAnimActivities.remove(r);
1713 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001714 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001715 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001716 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001717 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001718 boolean doShow = true;
1719 if (newTask) {
1720 // Even though this activity is starting fresh, we still need
1721 // to reset it to make sure we apply affinities to move any
1722 // existing activities from other tasks in to it.
1723 // If the caller has requested that the target task be
1724 // reset, then do so.
1725 if ((r.intent.getFlags()
1726 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1727 resetTaskIfNeededLocked(r, r);
1728 doShow = topRunningNonDelayedActivityLocked(null) == r;
1729 }
1730 }
1731 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1732 // Figure out if we are transitioning from another activity that is
1733 // "has the same starting icon" as the next one. This allows the
1734 // window manager to keep the previous window it had previously
1735 // created, if it still had one.
1736 ActivityRecord prev = mResumedActivity;
1737 if (prev != null) {
1738 // We don't want to reuse the previous starting preview if:
1739 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001740 if (prev.task != r.task) {
1741 prev = null;
1742 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001744 else if (prev.nowVisible) {
1745 prev = null;
1746 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001748 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001749 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001750 mService.compatibilityInfoForPackageLocked(
1751 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001752 r.labelRes, r.icon, r.windowFlags,
1753 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 }
1755 } else {
1756 // If this is the first activity, don't do any fancy animations,
1757 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001758 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001759 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001760 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001761 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001762 }
1763 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001764 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001765 }
1766
1767 if (doResume) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001768 mStackSupervisor.resumeTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 }
1770 }
1771
Dianne Hackbornbe707852011-11-11 14:32:10 -08001772 final void validateAppTokensLocked() {
1773 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001774 mValidateAppTokens.ensureCapacity(numActivities());
1775 final int numTasks = mTaskHistory.size();
1776 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1777 TaskRecord task = mTaskHistory.get(taskNdx);
1778 final ArrayList<ActivityRecord> activities = task.mActivities;
1779 if (activities.size() == 0) {
1780 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001781 }
Craig Mautner000f0022013-02-26 15:04:29 -08001782 TaskGroup group = new TaskGroup();
1783 group.taskId = task.taskId;
1784 mValidateAppTokens.add(group);
1785 final int numActivities = activities.size();
1786 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1787 final ActivityRecord r = activities.get(activityNdx);
1788 group.tokens.add(r.appToken);
1789 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001790 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001791 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001792 }
1793
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 /**
1795 * Perform a reset of the given task, if needed as part of launching it.
1796 * Returns the new HistoryRecord at the top of the task.
1797 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001798 /**
1799 * Helper method for #resetTaskIfNeededLocked.
1800 * We are inside of the task being reset... we'll either finish this activity, push it out
1801 * for another task, or leave it as-is.
1802 * @param task The task containing the Activity (taskTop) that might be reset.
1803 * @param forceReset
1804 * @return An ActivityOptions that needs to be processed.
1805 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001806 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001807 ActivityOptions topOptions = null;
1808
1809 int replyChainEnd = -1;
1810 boolean canMoveOptions = true;
1811
1812 // We only do this for activities that are not the root of the task (since if we finish
1813 // the root, we may no longer have the task!).
1814 final ArrayList<ActivityRecord> activities = task.mActivities;
1815 final int numActivities = activities.size();
1816 for (int i = numActivities - 1; i > 0; --i ) {
1817 ActivityRecord target = activities.get(i);
1818
1819 final int flags = target.info.flags;
1820 final boolean finishOnTaskLaunch =
1821 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1822 final boolean allowTaskReparenting =
1823 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1824 final boolean clearWhenTaskReset =
1825 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1826
1827 if (!finishOnTaskLaunch
1828 && !clearWhenTaskReset
1829 && target.resultTo != null) {
1830 // If this activity is sending a reply to a previous
1831 // activity, we can't do anything with it now until
1832 // we reach the start of the reply chain.
1833 // XXX note that we are assuming the result is always
1834 // to the previous activity, which is almost always
1835 // the case but we really shouldn't count on.
1836 if (replyChainEnd < 0) {
1837 replyChainEnd = i;
1838 }
1839 } else if (!finishOnTaskLaunch
1840 && !clearWhenTaskReset
1841 && allowTaskReparenting
1842 && target.taskAffinity != null
1843 && !target.taskAffinity.equals(task.affinity)) {
1844 // If this activity has an affinity for another
1845 // task, then we need to move it out of here. We will
1846 // move it as far out of the way as possible, to the
1847 // bottom of the activity stack. This also keeps it
1848 // correctly ordered with any activities we previously
1849 // moved.
1850 TaskRecord bottomTask = mTaskHistory.get(0);
1851 ActivityRecord p = bottomTask.mActivities.get(0);
1852 if (target.taskAffinity != null
1853 && target.taskAffinity.equals(p.task.affinity)) {
1854 // If the activity currently at the bottom has the
1855 // same task affinity as the one we are moving,
1856 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001857 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001858 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1859 + " out to bottom task " + p.task);
1860 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001861 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001862 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001863 target.task.affinityIntent = target.intent;
1864 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1865 + " out to new task " + target.task);
1866 }
1867
1868 final TaskRecord targetTask = target.task;
1869 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001870 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001871
1872 ThumbnailHolder curThumbHolder = target.thumbHolder;
1873 boolean gotOptions = !canMoveOptions;
1874
1875 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1876 for (int srcPos = start; srcPos >= i; --srcPos) {
1877 p = activities.get(srcPos);
1878 if (p.finishing) {
1879 continue;
1880 }
1881
1882 curThumbHolder = p.thumbHolder;
1883 canMoveOptions = false;
1884 if (!gotOptions && topOptions == null) {
1885 topOptions = p.takeOptionsLocked();
1886 if (topOptions != null) {
1887 gotOptions = true;
1888 }
1889 }
1890 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1891 + task + " adding to task=" + targetTask,
1892 new RuntimeException("here").fillInStackTrace());
1893 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1894 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001895 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001896 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001897
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001898 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001899 }
1900
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001901 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001902 if (VALIDATE_TOKENS) {
1903 validateAppTokensLocked();
1904 }
1905
1906 replyChainEnd = -1;
1907 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1908 // If the activity should just be removed -- either
1909 // because it asks for it, or the task should be
1910 // cleared -- then finish it and anything that is
1911 // part of its reply chain.
1912 int end;
1913 if (clearWhenTaskReset) {
1914 // In this case, we want to finish this activity
1915 // and everything above it, so be sneaky and pretend
1916 // like these are all in the reply chain.
1917 end = numActivities - 1;
1918 } else if (replyChainEnd < 0) {
1919 end = i;
1920 } else {
1921 end = replyChainEnd;
1922 }
1923 ActivityRecord p = null;
1924 boolean gotOptions = !canMoveOptions;
1925 for (int srcPos = i; srcPos <= end; srcPos++) {
1926 p = activities.get(srcPos);
1927 if (p.finishing) {
1928 continue;
1929 }
1930 canMoveOptions = false;
1931 if (!gotOptions && topOptions == null) {
1932 topOptions = p.takeOptionsLocked();
1933 if (topOptions != null) {
1934 gotOptions = true;
1935 }
1936 }
Craig Mautner58547802013-03-05 08:23:53 -08001937 if (DEBUG_TASKS) Slog.w(TAG,
1938 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001939 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001940 end--;
1941 srcPos--;
1942 }
1943 }
1944 replyChainEnd = -1;
1945 } else {
1946 // If we were in the middle of a chain, well the
1947 // activity that started it all doesn't want anything
1948 // special, so leave it all as-is.
1949 replyChainEnd = -1;
1950 }
1951 }
1952
1953 return topOptions;
1954 }
1955
1956 /**
1957 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1958 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1959 * @param affinityTask The task we are looking for an affinity to.
1960 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1961 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1962 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1963 */
Craig Mautner77878772013-03-04 19:46:24 -08001964 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
1965 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001966 int replyChainEnd = -1;
1967 final int taskId = task.taskId;
1968 final String taskAffinity = task.affinity;
1969
1970 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1971 final int numActivities = activities.size();
1972 // Do not operate on the root Activity.
1973 for (int i = numActivities - 1; i > 0; --i) {
1974 ActivityRecord target = activities.get(i);
1975
1976 final int flags = target.info.flags;
1977 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1978 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1979
1980 if (target.resultTo != null) {
1981 // If this activity is sending a reply to a previous
1982 // activity, we can't do anything with it now until
1983 // we reach the start of the reply chain.
1984 // XXX note that we are assuming the result is always
1985 // to the previous activity, which is almost always
1986 // the case but we really shouldn't count on.
1987 if (replyChainEnd < 0) {
1988 replyChainEnd = i;
1989 }
1990 } else if (topTaskIsHigher
1991 && allowTaskReparenting
1992 && taskAffinity != null
1993 && taskAffinity.equals(target.taskAffinity)) {
1994 // This activity has an affinity for our task. Either remove it if we are
1995 // clearing or move it over to our task. Note that
1996 // we currently punt on the case where we are resetting a
1997 // task that is not at the top but who has activities above
1998 // with an affinity to it... this is really not a normal
1999 // case, and we will need to later pull that task to the front
2000 // and usually at that point we will do the reset and pick
2001 // up those remaining activities. (This only happens if
2002 // someone starts an activity in a new task from an activity
2003 // in a task that is not currently on top.)
2004 if (forceReset || finishOnTaskLaunch) {
2005 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2006 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2007 for (int srcPos = start; srcPos >= i; --srcPos) {
2008 final ActivityRecord p = activities.get(srcPos);
2009 if (p.finishing) {
2010 continue;
2011 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002012 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002013 }
2014 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002015 if (taskInsertionPoint < 0) {
2016 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002017
Craig Mautner77878772013-03-04 19:46:24 -08002018 }
Craig Mautner77878772013-03-04 19:46:24 -08002019
2020 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2021 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2022 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2023 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002024 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002025 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002026 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002027
Craig Mautnere3a74d52013-02-22 14:14:58 -08002028 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2029 + " to stack at " + task,
2030 new RuntimeException("here").fillInStackTrace());
2031 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2032 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002033 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002034 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002035 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002036 if (VALIDATE_TOKENS) {
2037 validateAppTokensLocked();
2038 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002039
2040 // Now we've moved it in to place... but what if this is
2041 // a singleTop activity and we have put it on top of another
2042 // instance of the same activity? Then we drop the instance
2043 // below so it remains singleTop.
2044 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2045 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2046 boolean found = false;
2047 int targetNdx = taskActivities.indexOf(target);
2048 if (targetNdx > 0) {
2049 ActivityRecord p = taskActivities.get(targetNdx - 1);
2050 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002051 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2052 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002053 }
2054 }
2055 }
2056 }
2057
2058 replyChainEnd = -1;
2059 }
2060 }
Craig Mautner77878772013-03-04 19:46:24 -08002061 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002062 }
2063
Craig Mautner8849a5e2013-04-02 16:41:03 -07002064 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002065 ActivityRecord newActivity) {
2066 boolean forceReset =
2067 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2068 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2069 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2070 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2071 forceReset = true;
2072 }
2073 }
2074
2075 final TaskRecord task = taskTop.task;
2076
2077 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2078 * for remaining tasks. Used for later tasks to reparent to task. */
2079 boolean taskFound = false;
2080
2081 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2082 ActivityOptions topOptions = null;
2083
Craig Mautner77878772013-03-04 19:46:24 -08002084 // Preserve the location for reparenting in the new task.
2085 int reparentInsertionPoint = -1;
2086
Craig Mautnere3a74d52013-02-22 14:14:58 -08002087 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2088 final TaskRecord targetTask = mTaskHistory.get(i);
2089
2090 if (targetTask == task) {
2091 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2092 taskFound = true;
2093 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002094 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2095 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002096 }
2097 }
2098
Craig Mautner70a86932013-02-28 22:37:44 -08002099 int taskNdx = mTaskHistory.indexOf(task);
2100 do {
2101 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2102 } while (taskTop == null && taskNdx >= 0);
2103
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104 if (topOptions != null) {
2105 // If we got some ActivityOptions from an activity on top that
2106 // was removed from the task, propagate them to the new real top.
2107 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002108 taskTop.updateOptionsLocked(topOptions);
2109 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002110 topOptions.abort();
2111 }
2112 }
2113
2114 return taskTop;
2115 }
2116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 * Find the activity in the history stack within the given task. Returns
2119 * the index within the history at which it's found, or < 0 if not found.
2120 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002122 final ComponentName realActivity = r.realActivity;
2123 ArrayList<ActivityRecord> activities = task.mActivities;
2124 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2125 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002126 if (candidate.finishing) {
2127 continue;
2128 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002129 if (candidate.realActivity.equals(realActivity)) {
2130 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002131 }
2132 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002133 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 }
2135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2137 String resultWho, int requestCode, int resultCode, Intent data) {
2138
2139 if (callingUid > 0) {
2140 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002141 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 }
2143
2144 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2145 + " : who=" + resultWho + " req=" + requestCode
2146 + " res=" + resultCode + " data=" + data);
2147 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2148 try {
2149 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2150 list.add(new ResultInfo(resultWho, requestCode,
2151 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002152 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 return;
2154 } catch (Exception e) {
2155 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2156 }
2157 }
2158
2159 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2160 }
2161
2162 private final void stopActivityLocked(ActivityRecord r) {
2163 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2164 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2165 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2166 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002167 if (!mService.mSleeping) {
2168 if (DEBUG_STATES) {
2169 Slog.d(TAG, "no-history finish of " + r);
2170 }
2171 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002172 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002173 } else {
2174 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2175 + " on stop because we're just sleeping");
2176 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002178 }
2179
2180 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002181 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 if (mService.mFocusedActivity == r) {
2183 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2184 }
2185 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002186 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187 try {
2188 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002189 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2190 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002191 r.state = ActivityState.STOPPING;
2192 if (DEBUG_VISBILITY) Slog.v(
2193 TAG, "Stopping visible=" + r.visible + " for " + r);
2194 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002195 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002196 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002197 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002198 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002199 r.setSleeping(true);
2200 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002201 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2202 msg.obj = r;
2203 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204 } catch (Exception e) {
2205 // Maybe just ignore exceptions here... if the process
2206 // has crashed, our death notification will clean things
2207 // up.
2208 Slog.w(TAG, "Exception thrown during pause", e);
2209 // Just in case, assume it to be stopped.
2210 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002211 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 r.state = ActivityState.STOPPED;
2213 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002214 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002215 }
2216 }
2217 }
2218 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002219
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002220 final void scheduleIdleLocked() {
2221 Message msg = Message.obtain();
2222 msg.what = IDLE_NOW_MSG;
2223 mHandler.sendMessage(msg);
2224 }
2225
Craig Mautnere79d42682013-04-01 19:01:53 -07002226 // Checked.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002227 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002228 Configuration config) {
2229 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2230
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002231 ActivityRecord res = null;
2232
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002233 ArrayList<ActivityRecord> stops = null;
2234 ArrayList<ActivityRecord> finishes = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002235 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002236 int NS = 0;
2237 int NF = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002238 IApplicationThread sendThumbnail = null;
2239 boolean booting = false;
2240 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002241 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002242
Craig Mautnere79d42682013-04-01 19:01:53 -07002243 ActivityRecord r = ActivityRecord.forToken(token);
2244 if (r != null) {
2245 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2246 r.finishLaunchTickingLocked();
2247 }
2248
2249 // Get the activity record.
2250 if (isInStackLocked(token) != null) {
2251 res = r;
2252
2253 if (fromTimeout) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002254 mStackSupervisor.reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002255 }
2256
Craig Mautnere79d42682013-04-01 19:01:53 -07002257 // This is a hack to semi-deal with a race condition
2258 // in the client where it can be constructed with a
2259 // newer configuration from when we asked it to launch.
2260 // We'll update with whatever configuration it now says
2261 // it used to launch.
2262 if (config != null) {
2263 r.configuration = config;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002264 }
2265
Craig Mautnere79d42682013-04-01 19:01:53 -07002266 // No longer need to keep the device awake.
2267 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
2268 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2269 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002270 }
2271
Craig Mautnere79d42682013-04-01 19:01:53 -07002272 // We are now idle. If someone is waiting for a thumbnail from
2273 // us, we can now deliver.
2274 r.idle = true;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002275 if (mStackSupervisor.allResumedActivitiesIdle()) {
2276 mService.scheduleAppGcsLocked();
2277 }
Craig Mautnere79d42682013-04-01 19:01:53 -07002278 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
2279 sendThumbnail = r.app.thread;
2280 r.thumbnailNeeded = false;
2281 }
2282
2283 // If this activity is fullscreen, set up to hide those under it.
2284
2285 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
2286 ensureActivitiesVisibleLocked(null, 0);
2287
2288 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002289 if (!mService.mBooted && mStackSupervisor.isFrontStack(this)) {
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002290 mService.mBooted = true;
2291 enableScreen = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 }
Craig Mautnere79d42682013-04-01 19:01:53 -07002293 } else if (fromTimeout) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002294 mStackSupervisor.reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
Craig Mautnere79d42682013-04-01 19:01:53 -07002295 }
2296
2297 // Atomically retrieve all of the other things to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002298 stops = mStackSupervisor.processStoppingActivitiesLocked(true);
Craig Mautnere79d42682013-04-01 19:01:53 -07002299 NS = stops != null ? stops.size() : 0;
2300 if ((NF=mFinishingActivities.size()) > 0) {
2301 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2302 mFinishingActivities.clear();
2303 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002304
2305 final ArrayList<ActivityRecord> thumbnails;
2306 final int NT = mCancelledThumbnails.size();
2307 if (NT > 0) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002308 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
2309 mCancelledThumbnails.clear();
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002310 } else {
2311 thumbnails = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07002312 }
2313
Craig Mautnerde4ef022013-04-07 19:01:33 -07002314 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002315 booting = mService.mBooting;
2316 mService.mBooting = false;
2317 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002318
Craig Mautnere79d42682013-04-01 19:01:53 -07002319 if (mStartingUsers.size() > 0) {
2320 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2321 mStartingUsers.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 }
2323
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002324 // Perform the following actions from unsynchronized state.
2325 final IApplicationThread thumbnailThread = sendThumbnail;
2326 mHandler.post(new Runnable() {
2327 @Override
2328 public void run() {
2329 if (thumbnailThread != null) {
2330 try {
2331 thumbnailThread.requestThumbnail(token);
2332 } catch (Exception e) {
2333 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
2334 mService.sendPendingThumbnail(null, token, null, null, true);
2335 }
2336 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002338 // Report back to any thumbnail receivers.
2339 for (int i = 0; i < NT; i++) {
2340 ActivityRecord r = thumbnails.get(i);
2341 mService.sendPendingThumbnail(r, null, null, null, true);
2342 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002344 });
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002345
2346 // Stop any activities that are scheduled to do so but have been
2347 // waiting for the next one to start.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002348 for (int i = 0; i < NS; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002349 r = stops.get(i);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002350 if (r.finishing) {
2351 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2352 } else {
Craig Mautner165640b2013-04-20 10:34:33 -07002353 r.task.stack.stopActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002354 }
2355 }
2356
2357 // Finish any activities that are scheduled to do so but have been
2358 // waiting for the next one to start.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002359 for (int i = 0; i < NF; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002360 r = finishes.get(i);
Craig Mautner165640b2013-04-20 10:34:33 -07002361 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002362 }
2363
2364 if (booting) {
2365 mService.finishBooting();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002366 } else if (startingUsers != null) {
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002367 for (int i = 0; i < startingUsers.size(); i++) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002368 mService.finishUserSwitch(startingUsers.get(i));
2369 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002370 }
2371
2372 mService.trimApplications();
2373 //dump();
2374 //mWindowManager.dump();
2375
2376 if (enableScreen) {
2377 mService.enableScreenAfterBoot();
2378 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002379
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002380 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002381 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002382 }
2383
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002384 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385 }
2386
2387 /**
2388 * @return Returns true if the activity is being finished, false if for
2389 * some reason it is being left as-is.
2390 */
2391 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002392 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002393 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002394 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002395 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002396 + ", result=" + resultCode + ", data=" + resultData
2397 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002398 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 return false;
2400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401
Craig Mautnerd44711d2013-02-23 11:24:36 -08002402 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002403 return true;
2404 }
2405
Craig Mautnerd2328952013-03-05 12:46:26 -08002406 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002407 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2408 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2409 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2410 ActivityRecord r = activities.get(activityNdx);
2411 if (r.resultTo == self && r.requestCode == requestCode) {
2412 if ((r.resultWho == null && resultWho == null) ||
2413 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2414 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2415 false);
2416 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002417 }
2418 }
2419 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002420 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002421 }
2422
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002423 final void finishTopRunningActivityLocked(ProcessRecord app) {
2424 ActivityRecord r = topRunningActivityLocked(null);
2425 if (r != null && r.app == app) {
2426 // If the top running activity is from this crashing
2427 // process, then terminate it to avoid getting in a loop.
2428 Slog.w(TAG, " Force finishing activity "
2429 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002430 int taskNdx = mTaskHistory.indexOf(r.task);
2431 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002432 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002433 // Also terminate any activities below it that aren't yet
2434 // stopped, to avoid a situation where one will get
2435 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002436 --activityNdx;
2437 if (activityNdx < 0) {
2438 do {
2439 --taskNdx;
2440 if (taskNdx < 0) {
2441 break;
2442 }
2443 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2444 } while (activityNdx < 0);
2445 }
2446 if (activityNdx >= 0) {
2447 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002448 if (r.state == ActivityState.RESUMED
2449 || r.state == ActivityState.PAUSING
2450 || r.state == ActivityState.PAUSED) {
2451 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
2452 Slog.w(TAG, " Force finishing activity "
2453 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002454 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002455 }
2456 }
2457 }
2458 }
2459 }
2460
Craig Mautnerd2328952013-03-05 12:46:26 -08002461 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002462 ArrayList<ActivityRecord> activities = r.task.mActivities;
2463 for (int index = activities.indexOf(r); index >= 0; --index) {
2464 ActivityRecord cur = activities.get(index);
2465 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002466 break;
2467 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002468 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002469 }
2470 return true;
2471 }
2472
Dianne Hackborn5c607432012-02-28 14:44:19 -08002473 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2474 // send the result
2475 ActivityRecord resultTo = r.resultTo;
2476 if (resultTo != null) {
2477 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2478 + " who=" + r.resultWho + " req=" + r.requestCode
2479 + " res=" + resultCode + " data=" + resultData);
2480 if (r.info.applicationInfo.uid > 0) {
2481 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2482 resultTo.packageName, resultData,
2483 resultTo.getUriPermissionsLocked());
2484 }
2485 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2486 resultData);
2487 r.resultTo = null;
2488 }
2489 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2490
2491 // Make sure this HistoryRecord is not holding on to other resources,
2492 // because clients have remote IPC references to this object so we
2493 // can't assume that will go away and want to avoid circular IPC refs.
2494 r.results = null;
2495 r.pendingResults = null;
2496 r.newIntents = null;
2497 r.icicle = null;
2498 }
2499
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002500 /**
2501 * @return Returns true if this activity has been removed from the history
2502 * list, or false if it is still in the list and will be removed later.
2503 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08002504 final boolean finishActivityLocked(ActivityRecord r, int resultCode,
2505 Intent resultData, String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 if (r.finishing) {
2507 Slog.w(TAG, "Duplicate finish request for " + r);
2508 return false;
2509 }
2510
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002511 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002512 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002513 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002514 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002515 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2516 final int index = activities.indexOf(r);
2517 if (index < (activities.size() - 1)) {
2518 ActivityRecord next = activities.get(index+1);
2519 if (r.frontOfTask) {
2520 // The next activity is now the front of the task.
2521 next.frontOfTask = true;
2522 }
2523 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2524 // If the caller asked that this activity (and all above it)
2525 // be cleared when the task is reset, don't lose that information,
2526 // but propagate it up to the next activity.
2527 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002528 }
2529 }
2530
2531 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002532 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002534 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 }
2536 }
2537
Dianne Hackborn5c607432012-02-28 14:44:19 -08002538 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002539
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002540 if (mService.mPendingThumbnails.size() > 0) {
2541 // There are clients waiting to receive thumbnails so, in case
2542 // this is an activity that someone is waiting for, add it
2543 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08002544 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002545 }
2546
Craig Mautnerde4ef022013-04-07 19:01:33 -07002547 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002548 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002549 if (DEBUG_TRANSITION) Slog.v(TAG,
2550 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002551 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002552 ? AppTransition.TRANSIT_TASK_CLOSE
2553 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002554
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002556 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002557
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002558 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002559 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2560 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2561 startPausingLocked(false, false);
2562 }
2563
2564 } else if (r.state != ActivityState.PAUSING) {
2565 // If the activity is PAUSING, we will complete the finish once
2566 // it is done pausing; else we can just directly finish it here.
2567 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002568 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 } else {
2570 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2571 }
2572
2573 return false;
2574 }
2575
2576 private static final int FINISH_IMMEDIATELY = 0;
2577 private static final int FINISH_AFTER_PAUSE = 1;
2578 private static final int FINISH_AFTER_VISIBLE = 2;
2579
2580 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002581 int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002582 // First things first: if this activity is currently visible,
2583 // and the resumed activity is not yet visible, then hold off on
2584 // finishing until the resumed one becomes visible.
2585 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002586 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2587 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002588 if (mStackSupervisor.mStoppingActivities.size() > 3
2589 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002590 // If we already have a few activities waiting to stop,
2591 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002592 // them out. Or if r is the last of activity of the last task the stack
2593 // will be empty and must be cleared immediately.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002594 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002595 } else {
2596 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 }
2598 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002599 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2600 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002601 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002602 if (oomAdj) {
2603 mService.updateOomAdjLocked();
2604 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 return r;
2606 }
2607
2608 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002609 mStackSupervisor.mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002610 mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002611 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002612 if (mResumedActivity == r) {
2613 mResumedActivity = null;
2614 }
2615 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002616 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002617 r.state = ActivityState.FINISHING;
2618
2619 if (mode == FINISH_IMMEDIATELY
2620 || prevState == ActivityState.STOPPED
2621 || prevState == ActivityState.INITIALIZING) {
2622 // If this activity is already stopped, we can just finish
2623 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002624 boolean activityRemoved = destroyActivityLocked(r, true,
2625 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002626 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002627 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002628 }
2629 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002630 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002631
2632 // Need to go through the full pause cycle to get this
2633 // activity into the stopped state and then finish it.
2634 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
2635 mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002636 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002637 return r;
2638 }
2639
Craig Mautnerd2328952013-03-05 12:46:26 -08002640 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002641 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002642 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002643 final TaskRecord task = srec.task;
2644 final ArrayList<ActivityRecord> activities = task.mActivities;
2645 final int start = activities.indexOf(srec);
2646 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002647 return false;
2648 }
2649 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002650 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002651 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002652 final ComponentName dest = destIntent.getComponent();
2653 if (start > 0 && dest != null) {
2654 for (int i = finishTo; i >= 0; i--) {
2655 ActivityRecord r = activities.get(i);
2656 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002657 r.info.name.equals(dest.getClassName())) {
2658 finishTo = i;
2659 parent = r;
2660 foundParentInTask = true;
2661 break;
2662 }
2663 }
2664 }
2665
2666 IActivityController controller = mService.mController;
2667 if (controller != null) {
2668 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2669 if (next != null) {
2670 // ask watcher if this is allowed
2671 boolean resumeOK = true;
2672 try {
2673 resumeOK = controller.activityResuming(next.packageName);
2674 } catch (RemoteException e) {
2675 mService.mController = null;
2676 }
2677
2678 if (!resumeOK) {
2679 return false;
2680 }
2681 }
2682 }
2683 final long origId = Binder.clearCallingIdentity();
2684 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002685 ActivityRecord r = activities.get(i);
2686 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002687 // Only return the supplied result for the first activity finished
2688 resultCode = Activity.RESULT_CANCELED;
2689 resultData = null;
2690 }
2691
2692 if (parent != null && foundParentInTask) {
2693 final int parentLaunchMode = parent.info.launchMode;
2694 final int destIntentFlags = destIntent.getFlags();
2695 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2696 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2697 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2698 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2699 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2700 } else {
2701 try {
2702 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2703 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002704 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002705 null, aInfo, parent.appToken, null,
2706 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2707 0, null, true, null);
2708 foundParentInTask = res == ActivityManager.START_SUCCESS;
2709 } catch (RemoteException e) {
2710 foundParentInTask = false;
2711 }
2712 requestFinishActivityLocked(parent.appToken, resultCode,
2713 resultData, "navigate-up", true);
2714 }
2715 }
2716 Binder.restoreCallingIdentity(origId);
2717 return foundParentInTask;
2718 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 /**
2720 * Perform the common clean-up of an activity record. This is called both
2721 * as part of destroyActivityLocked() (when destroying the client-side
2722 * representation) and cleaning things up as a result of its hosting
2723 * processing going away, in which case there is no remaining client-side
2724 * state to destroy so only the cleanup here is needed.
2725 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002726 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2727 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728 if (mResumedActivity == r) {
2729 mResumedActivity = null;
2730 }
2731 if (mService.mFocusedActivity == r) {
2732 mService.mFocusedActivity = null;
2733 }
2734
2735 r.configDestroy = false;
2736 r.frozenBeforeDestroy = false;
2737
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002738 if (setState) {
2739 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2740 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002741 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002742 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002743 }
2744
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 // Make sure this record is no longer in the pending finishes list.
2746 // This could happen, for example, if we are trimming activities
2747 // down to the max limit while they are still waiting to finish.
2748 mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002749 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002750
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002751 // Remove any pending results.
2752 if (r.finishing && r.pendingResults != null) {
2753 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2754 PendingIntentRecord rec = apr.get();
2755 if (rec != null) {
2756 mService.cancelIntentSenderLocked(rec, false);
2757 }
2758 }
2759 r.pendingResults = null;
2760 }
2761
2762 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002763 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 }
2765
2766 if (mService.mPendingThumbnails.size() > 0) {
2767 // There are clients waiting to receive thumbnails so, in case
2768 // this is an activity that someone is waiting for, add it
2769 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08002770 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002771 }
2772
2773 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002774 removeTimeoutsForActivityLocked(r);
2775 }
2776
2777 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002778 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002779 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002780 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002781 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002782 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002783 }
2784
Dianne Hackborn5c607432012-02-28 14:44:19 -08002785 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002786 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2787 r.makeFinishing();
2788 if (DEBUG_ADD_REMOVE) {
2789 RuntimeException here = new RuntimeException("here");
2790 here.fillInStackTrace();
2791 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002792 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002793 final TaskRecord task = r.task;
2794 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002795 if (DEBUG_STACK) Slog.i(TAG,
2796 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002797 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002798 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002799 r.takeFromHistory();
2800 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002801 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002802 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002803 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002804 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002805 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002806 if (VALIDATE_TOKENS) {
2807 validateAppTokensLocked();
2808 }
2809 cleanUpActivityServicesLocked(r);
2810 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002811 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002812
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 /**
2814 * Perform clean-up of service connections in an activity record.
2815 */
2816 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2817 // Throw away any services that have been bound by this activity.
2818 if (r.connections != null) {
2819 Iterator<ConnectionRecord> it = r.connections.iterator();
2820 while (it.hasNext()) {
2821 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002822 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 }
2824 r.connections = null;
2825 }
2826 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002827
2828 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2829 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2830 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2831 mHandler.sendMessage(msg);
2832 }
2833
Dianne Hackborn28695e02011-11-02 21:59:51 -07002834 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002835 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002836 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002837 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2838 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2839 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2840 final ActivityRecord r = activities.get(activityNdx);
2841 if (r.finishing) {
2842 continue;
2843 }
2844 if (r.fullscreen) {
2845 lastIsOpaque = true;
2846 }
2847 if (owner != null && r.app != owner) {
2848 continue;
2849 }
2850 if (!lastIsOpaque) {
2851 continue;
2852 }
2853 // We can destroy this one if we have its icicle saved and
2854 // it is not in the process of pausing/stopping/finishing.
2855 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2856 && r.haveState && !r.visible && r.stopped
2857 && r.state != ActivityState.DESTROYING
2858 && r.state != ActivityState.DESTROYED) {
2859 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2860 + " resumed=" + mResumedActivity
2861 + " pausing=" + mPausingActivity);
2862 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2863 activityRemoved = true;
2864 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002865 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002866 }
2867 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002868 if (activityRemoved) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002869 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002870 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002871 }
2872
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 /**
2874 * Destroy the current CLIENT SIDE instance of an activity. This may be
2875 * called both when actually finishing an activity, or when performing
2876 * a configuration switch where we destroy the current client-side object
2877 * but then create a new client-side object for this same HistoryRecord.
2878 */
2879 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002880 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002881 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002882 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2884 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002885 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002886 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887
2888 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002889
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002890 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002891
2892 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002893
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002894 if (hadApp) {
2895 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002896 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2898 mService.mHeavyWeightProcess = null;
2899 mService.mHandler.sendEmptyMessage(
2900 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2901 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002903 // No longer have activities, so update oom adj.
2904 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 }
2906 }
2907
2908 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002909
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002910 try {
2911 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002912 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 r.configChangeFlags);
2914 } catch (Exception e) {
2915 // We can just ignore exceptions here... if the process
2916 // has crashed, our death notification will clean things
2917 // up.
2918 //Slog.w(TAG, "Exception thrown during finish", e);
2919 if (r.finishing) {
2920 removeActivityFromHistoryLocked(r);
2921 removedFromHistory = true;
2922 skipDestroy = true;
2923 }
2924 }
2925
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002926 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002927
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002928 // If the activity is finishing, we need to wait on removing it
2929 // from the list to give it a chance to do its cleanup. During
2930 // that time it may make calls back with its token so we need to
2931 // be able to find it on the list and so we don't want to remove
2932 // it from the list yet. Otherwise, we can just immediately put
2933 // it in the destroyed state since we are not removing it from the
2934 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002936 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2937 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 r.state = ActivityState.DESTROYING;
2939 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
2940 msg.obj = r;
2941 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2942 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002943 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2944 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002945 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002946 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002947 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948 }
2949 } else {
2950 // remove this record from the history.
2951 if (r.finishing) {
2952 removeActivityFromHistoryLocked(r);
2953 removedFromHistory = true;
2954 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002955 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2956 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002958 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002959 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960 }
2961 }
2962
2963 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002964
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965 if (!mLRUActivities.remove(r) && hadApp) {
2966 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2967 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002968
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 return removedFromHistory;
2970 }
2971
Craig Mautnerd2328952013-03-05 12:46:26 -08002972 final void activityDestroyedLocked(IBinder token) {
2973 final long origId = Binder.clearCallingIdentity();
2974 try {
2975 ActivityRecord r = ActivityRecord.forToken(token);
2976 if (r != null) {
2977 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002978 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002979
2980 if (isInStackLocked(token) != null) {
2981 if (r.state == ActivityState.DESTROYING) {
2982 cleanUpActivityLocked(r, true, false);
2983 removeActivityFromHistoryLocked(r);
2984 }
2985 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002986 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Craig Mautnerd2328952013-03-05 12:46:26 -08002987 } finally {
2988 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 }
2990 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002991
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002992 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2993 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002995 if (DEBUG_CLEANUP) Slog.v(
2996 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002997 + " with " + i + " entries");
2998 while (i > 0) {
2999 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003000 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003001 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003003 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003004 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003005 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003006 }
3007 }
3008 }
3009
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003010 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3011 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003012 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3013 "mStoppingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003014 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003015 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003016 "mWaitingVisibleActivities");
3017 removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities");
3018
3019 boolean hasVisibleActivities = false;
3020
3021 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003022 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003023 if (DEBUG_CLEANUP) Slog.v(
3024 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003025 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3026 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3027 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3028 final ActivityRecord r = activities.get(activityNdx);
3029 --i;
3030 if (DEBUG_CLEANUP) Slog.v(
3031 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3032 if (r.app == app) {
3033 boolean remove;
3034 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3035 // Don't currently have state for the activity, or
3036 // it is finishing -- always remove it.
3037 remove = true;
3038 } else if (r.launchCount > 2 &&
3039 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3040 // We have launched this activity too many times since it was
3041 // able to run, so give up and remove it.
3042 remove = true;
3043 } else {
3044 // The process may be gone, but the activity lives on!
3045 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003046 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003047 if (remove) {
3048 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
3049 RuntimeException here = new RuntimeException("here");
3050 here.fillInStackTrace();
3051 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3052 + ": haveState=" + r.haveState
3053 + " stateNotNeeded=" + r.stateNotNeeded
3054 + " finishing=" + r.finishing
3055 + " state=" + r.state, here);
3056 }
3057 if (!r.finishing) {
3058 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3059 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3060 r.userId, System.identityHashCode(r),
3061 r.task.taskId, r.shortComponentName,
3062 "proc died without state saved");
3063 }
3064 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003065
Craig Mautner0247fc82013-02-28 14:32:06 -08003066 } else {
3067 // We have the current state for this activity, so
3068 // it can be restarted later when needed.
3069 if (localLOGV) Slog.v(
3070 TAG, "Keeping entry, setting app to null");
3071 if (r.visible) {
3072 hasVisibleActivities = true;
3073 }
3074 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3075 + r);
3076 r.app = null;
3077 r.nowVisible = false;
3078 if (!r.haveState) {
3079 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
3080 "App died, clearing saved state of " + r);
3081 r.icicle = null;
3082 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003083 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003084
Craig Mautnerd2328952013-03-05 12:46:26 -08003085 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003086 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003087 }
3088 }
3089
3090 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003091 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003092
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003093 final void updateTransitLocked(int transit, Bundle options) {
3094 if (options != null) {
3095 ActivityRecord r = topRunningActivityLocked(null);
3096 if (r != null && r.state != ActivityState.RESUMED) {
3097 r.updateOptionsLocked(options);
3098 } else {
3099 ActivityOptions.abort(options);
3100 }
3101 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003102 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003103 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003104
Craig Mautnercae015f2013-02-08 14:31:27 -08003105 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003106 final TaskRecord task = taskForIdLocked(taskId);
3107 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003108 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003109 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003110 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003111 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3112 // Caller wants the home activity moved with it. To accomplish this,
3113 // we'll just move the home task to the top first.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003114 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003115 }
3116 moveTaskToFrontLocked(task, null, options);
3117 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003118 }
3119 return false;
3120 }
3121
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003122 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003123 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003124
Craig Mautner11bf9a52013-02-19 14:08:51 -08003125 final int numTasks = mTaskHistory.size();
3126 final int index = mTaskHistory.indexOf(tr);
3127 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
3128 // nothing to do!
3129 if (reason != null &&
3130 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3131 ActivityOptions.abort(options);
3132 } else {
3133 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3134 }
3135 return;
3136 }
3137
3138 // Shift all activities with this task up to the top
3139 // of the stack, keeping them in the same internal order.
3140 mTaskHistory.remove(tr);
3141 mTaskHistory.add(tr);
3142
3143 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003144 if (reason != null &&
3145 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003146 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003147 ActivityRecord r = topRunningActivityLocked(null);
3148 if (r != null) {
3149 mNoAnimActivities.add(r);
3150 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003151 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003153 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003155
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003156 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003157
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003158 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Craig Mautner58547802013-03-05 08:23:53 -08003159 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003160
3161 if (VALIDATE_TOKENS) {
3162 validateAppTokensLocked();
3163 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003164 }
3165
3166 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003167 * Worker method for rearranging history stack. Implements the function of moving all
3168 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003170 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003171 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3172 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003173 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 * @param task The taskId to collect and move to the bottom.
3175 * @return Returns true if the move completed, false if not.
3176 */
3177 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3178 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 // If we have a watcher, preflight the move before committing to it. First check
3181 // for *other* available tasks, but if none are available, then try again allowing the
3182 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003183 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 ActivityRecord next = topRunningActivityLocked(null, task);
3185 if (next == null) {
3186 next = topRunningActivityLocked(null, 0);
3187 }
3188 if (next != null) {
3189 // ask watcher if this is allowed
3190 boolean moveOK = true;
3191 try {
3192 moveOK = mService.mController.activityResuming(next.packageName);
3193 } catch (RemoteException e) {
3194 mService.mController = null;
3195 }
3196 if (!moveOK) {
3197 return false;
3198 }
3199 }
3200 }
3201
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003202 if (DEBUG_TRANSITION) Slog.v(TAG,
3203 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204
Craig Mautnerd2328952013-03-05 12:46:26 -08003205 final TaskRecord tr = taskForIdLocked(task);
3206 if (tr == null) {
3207 return false;
3208 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003209 mTaskHistory.remove(tr);
3210 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211
3212 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003213 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3214 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 ActivityRecord r = topRunningActivityLocked(null);
3216 if (r != null) {
3217 mNoAnimActivities.add(r);
3218 }
3219 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003220 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003221 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003223 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003224
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003226 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003227 }
3228
Craig Mautnerde4ef022013-04-07 19:01:33 -07003229 if (mResumedActivity != null && mResumedActivity.task == tr &&
3230 mResumedActivity.mLaunchHomeTaskNext) {
3231 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003232 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003233 }
3234
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003235 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236 return true;
3237 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003238
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003239 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08003240 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003241 if (mResumedActivity != null && mResumedActivity.thumbHolder == tr) {
3242 info.mainThumbnail = screenshotActivities(mResumedActivity);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003243 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07003244 if (info.mainThumbnail == null) {
3245 info.mainThumbnail = tr.lastThumbnail;
3246 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003247 return info;
3248 }
3249
Dianne Hackborn15491c62012-09-19 10:59:14 -07003250 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003251 if (mResumedActivity != null && mResumedActivity.task == tr) {
Dianne Hackborn15491c62012-09-19 10:59:14 -07003252 // This task is the current resumed task, we just need to take
3253 // a screenshot of it and return that.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003254 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003255 }
3256 // Return the information about the task, to figure out the top
3257 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08003258 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003259 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07003260 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003261 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003262 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003263 }
3264
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003265 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
3266 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003267 final TaskRecord task = taskForIdLocked(taskId);
3268 if (task == null) {
3269 return null;
3270 }
Craig Mautner16030772013-02-26 16:05:47 -08003271 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003272 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003273 if (taskRequired) {
3274 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
3275 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003276 return null;
3277 }
3278
3279 if (subTaskIndex < 0) {
3280 // Just remove the entire task.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003281 task.performClearTaskAtIndexLocked(info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003282 return info.root;
3283 }
3284
3285 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003286 if (taskRequired) {
3287 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
3288 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003289 return null;
3290 }
3291
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003292 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003293 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003294 task.performClearTaskAtIndexLocked(subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003295 return subtask.activity;
3296 }
3297
Craig Mautner16030772013-02-26 16:05:47 -08003298 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003299 final TaskAccessInfo thumbs = new TaskAccessInfo();
3300 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08003301 final ArrayList<ActivityRecord> activities = task.mActivities;
3302 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003303 int j = 0;
3304 ThumbnailHolder holder = null;
3305 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003306 ActivityRecord ar = activities.get(j);
3307 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07003308 thumbs.root = ar;
3309 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003310 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003311 if (holder != null) {
3312 thumbs.mainThumbnail = holder.lastThumbnail;
3313 }
3314 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003315 break;
3316 }
3317 j++;
3318 }
3319
3320 if (j >= NA) {
3321 return thumbs;
3322 }
3323
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003324 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
3325 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003326 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003327 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003328 j++;
3329 if (ar.finishing) {
3330 continue;
3331 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003332 if (ar.thumbHolder != holder && holder != null) {
3333 thumbs.numSubThumbbails++;
3334 holder = ar.thumbHolder;
3335 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07003336 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003337 sub.activity = ar;
3338 sub.index = j-1;
3339 subtasks.add(sub);
3340 }
3341 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003342 if (thumbs.numSubThumbbails > 0) {
3343 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003344 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003345 public Bitmap getThumbnail(int index) {
3346 if (index < 0 || index >= thumbs.subtasks.size()) {
3347 return null;
3348 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003349 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003350 if (mResumedActivity != null && mResumedActivity.thumbHolder == sub.holder) {
3351 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003352 }
3353 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003354 }
3355 };
3356 }
3357 return thumbs;
3358 }
3359
Craig Mautner8849a5e2013-04-02 16:41:03 -07003360 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003361 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003362 final Uri data = r.intent.getData();
3363 final String strData = data != null ? data.toSafeString() : null;
3364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003366 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003367 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003368 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 }
3370
3371 /**
3372 * Make sure the given activity matches the current configuration. Returns
3373 * false if the activity had to be destroyed. Returns true if the
3374 * configuration is the same, or the activity will remain running as-is
3375 * for whatever reason. Ensures the HistoryRecord is updated with the
3376 * correct configuration and all other bookkeeping is handled.
3377 */
3378 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3379 int globalChanges) {
3380 if (mConfigWillChange) {
3381 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3382 "Skipping config check (will change): " + r);
3383 return true;
3384 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3387 "Ensuring correct configuration: " + r);
3388
3389 // Short circuit: if the two configurations are the exact same
3390 // object (the common case), then there is nothing to do.
3391 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003392 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003393 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3394 "Configuration unchanged in " + r);
3395 return true;
3396 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003397
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003398 // We don't worry about activities that are finishing.
3399 if (r.finishing) {
3400 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3401 "Configuration doesn't matter in finishing " + r);
3402 r.stopFreezingScreenLocked(false);
3403 return true;
3404 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003405
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003406 // Okay we now are going to make this activity have the new config.
3407 // But then we need to figure out how it needs to deal with that.
3408 Configuration oldConfig = r.configuration;
3409 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003410
3411 // Determine what has changed. May be nothing, if this is a config
3412 // that has come back from the app after going idle. In that case
3413 // we just want to leave the official config object now in the
3414 // activity and do nothing else.
3415 final int changes = oldConfig.diff(newConfig);
3416 if (changes == 0 && !r.forceNewConfig) {
3417 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3418 "Configuration no differences in " + r);
3419 return true;
3420 }
3421
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003422 // If the activity isn't currently running, just leave the new
3423 // configuration and it will pick that up next time it starts.
3424 if (r.app == null || r.app.thread == null) {
3425 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3426 "Configuration doesn't matter not running " + r);
3427 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003428 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003429 return true;
3430 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003431
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003432 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003433 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3434 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3435 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003436 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003437 + ", newConfig=" + newConfig);
3438 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003439 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003440 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3441 r.configChangeFlags |= changes;
3442 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003443 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003444 if (r.app == null || r.app.thread == null) {
3445 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003446 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003447 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003448 } else if (r.state == ActivityState.PAUSING) {
3449 // A little annoying: we are waiting for this activity to
3450 // finish pausing. Let's not do anything now, but just
3451 // flag that it needs to be restarted when done pausing.
3452 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003453 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003454 r.configDestroy = true;
3455 return true;
3456 } else if (r.state == ActivityState.RESUMED) {
3457 // Try to optimize this case: the configuration is changing
3458 // and we need to restart the top, resumed activity.
3459 // Instead of doing the normal handshaking, just say
3460 // "restart!".
3461 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003462 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003463 relaunchActivityLocked(r, r.configChangeFlags, true);
3464 r.configChangeFlags = 0;
3465 } else {
3466 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003467 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003468 relaunchActivityLocked(r, r.configChangeFlags, false);
3469 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003470 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003471
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003472 // All done... tell the caller we weren't able to keep this
3473 // activity around.
3474 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003475 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003476
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003477 // Default case: the activity can handle this new configuration, so
3478 // hand it over. Note that we don't need to give it the new
3479 // configuration, since we always send configuration changes to all
3480 // process when they happen so it can just use whatever configuration
3481 // it last got.
3482 if (r.app != null && r.app.thread != null) {
3483 try {
3484 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003485 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003486 } catch (RemoteException e) {
3487 // If process died, whatever.
3488 }
3489 }
3490 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003491
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003492 return true;
3493 }
3494
3495 private final boolean relaunchActivityLocked(ActivityRecord r,
3496 int changes, boolean andResume) {
3497 List<ResultInfo> results = null;
3498 List<Intent> newIntents = null;
3499 if (andResume) {
3500 results = r.results;
3501 newIntents = r.newIntents;
3502 }
3503 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3504 + " with results=" + results + " newIntents=" + newIntents
3505 + " andResume=" + andResume);
3506 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003507 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 r.task.taskId, r.shortComponentName);
3509
3510 r.startFreezingScreenLocked(r.app, 0);
3511
3512 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003513 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3514 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3515 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003516 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003517 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003518 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003519 // Note: don't need to call pauseIfSleepingLocked() here, because
3520 // the caller will only pass in 'andResume' if this activity is
3521 // currently resumed, which implies we aren't sleeping.
3522 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003523 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003524 }
3525
3526 if (andResume) {
3527 r.results = null;
3528 r.newIntents = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003529 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530 mService.reportResumedActivityLocked(r);
3531 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003532 r.state = ActivityState.RESUMED;
3533 } else {
3534 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3535 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003536 }
3537
3538 return true;
3539 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003540
3541 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003542 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3543 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3544 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3545 final ActivityRecord r = activities.get(activityNdx);
3546 if (r.appToken == token) {
3547 return true;
3548 }
3549 if (r.fullscreen && !r.finishing) {
3550 return false;
3551 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003552 }
3553 }
3554 return true;
3555 }
3556
3557 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003558 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3559 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3560 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3561 final ActivityRecord r = activities.get(activityNdx);
3562 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003563 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003564 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003565 }
3566 }
3567 }
3568
3569 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3570 boolean didSomething = false;
3571 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003572 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3573 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3574 int numActivities = activities.size();
3575 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3576 ActivityRecord r = activities.get(activityNdx);
3577 final boolean samePackage = r.packageName.equals(name)
3578 || (name == null && r.userId == userId);
3579 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3580 && (samePackage || r.task == lastTask)
3581 && (r.app == null || evenPersistent || !r.app.persistent)) {
3582 if (!doit) {
3583 if (r.finishing) {
3584 // If this activity is just finishing, then it is not
3585 // interesting as far as something to stop.
3586 continue;
3587 }
3588 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003589 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003590 didSomething = true;
3591 Slog.i(TAG, " Force finishing activity " + r);
3592 if (samePackage) {
3593 if (r.app != null) {
3594 r.app.removed = true;
3595 }
3596 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003597 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003598 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08003599 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08003600 }
3601 }
3602 }
3603 return didSomething;
3604 }
3605
3606 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003607 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003608 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003609 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3610 --maxNum, --taskNdx) {
3611 final TaskRecord task = mTaskHistory.get(taskNdx);
3612 ActivityRecord r = null;
3613 ActivityRecord top = null;
3614 int numActivities = 0;
3615 int numRunning = 0;
3616 final ArrayList<ActivityRecord> activities = task.mActivities;
3617 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3618 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003619
Craig Mautneraab647e2013-02-28 16:31:36 -08003620 // Initialize state for next task if needed.
3621 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3622 top = r;
3623 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003624 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003625
3626 // Add 'r' into the current task.
3627 numActivities++;
3628 if (r.app != null && r.app.thread != null) {
3629 numRunning++;
3630 }
3631
3632 if (localLOGV) Slog.v(
3633 TAG, r.intent.getComponent().flattenToShortString()
3634 + ": task=" + r.task);
3635 }
3636
3637 RunningTaskInfo ci = new RunningTaskInfo();
3638 ci.id = task.taskId;
3639 ci.baseActivity = r.intent.getComponent();
3640 ci.topActivity = top.intent.getComponent();
3641 if (top.thumbHolder != null) {
3642 ci.description = top.thumbHolder.lastDescription;
3643 }
3644 ci.numActivities = numActivities;
3645 ci.numRunning = numRunning;
3646 //System.out.println(
3647 // "#" + maxNum + ": " + " descr=" + ci.description);
3648 if (receiver != null) {
3649 if (localLOGV) Slog.v(
3650 TAG, "State=" + top.state + "Idle=" + top.idle
3651 + " app=" + top.app
3652 + " thr=" + (top.app != null ? top.app.thread : null));
3653 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3654 if (top.idle && top.app != null && top.app.thread != null) {
3655 topRecord = top;
3656 } else {
3657 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003658 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003659 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003660 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003661 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003662 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003663 }
3664 return topRecord;
3665 }
3666
3667 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003668 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003669 if (DEBUG_SWITCH) Slog.d(
3670 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003671 if (top >= 0) {
3672 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3673 int activityTop = activities.size() - 1;
3674 if (activityTop > 0) {
3675 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3676 "unhandled-back", true);
3677 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003678 }
3679 }
3680
Craig Mautnere79d42682013-04-01 19:01:53 -07003681 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003682 if (!containsApp(app)) {
3683 return;
3684 }
3685 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003686 if (mPausingActivity != null && mPausingActivity.app == app) {
3687 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3688 "App died while pausing: " + mPausingActivity);
3689 mPausingActivity = null;
3690 }
3691 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3692 mLastPausedActivity = null;
3693 }
3694
3695 // Remove this application's activities from active lists.
3696 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3697
3698 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003699 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003700 if (stack == null) {
3701 mStackSupervisor.resumeHomeActivity(null);
3702 } else if (!stack.resumeTopActivityLocked(null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003703 // If there was nothing to resume, and we are not already
3704 // restarting this process, but there is a visible activity that
3705 // is hosted by the process... then make sure all visible
3706 // activities are running, taking care of restarting this
3707 // process.
3708 if (hasVisibleActivities) {
3709 ensureActivitiesVisibleLocked(null, 0);
3710 }
3711 }
3712 }
3713 }
3714
Craig Mautnercae015f2013-02-08 14:31:27 -08003715 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003716 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3717 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3718 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3719 final ActivityRecord r = activities.get(activityNdx);
3720 if (r.app == app) {
3721 Slog.w(TAG, " Force finishing activity "
3722 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003723 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003724 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003725 }
3726 }
3727 }
3728
3729 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3730 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003731 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3732 final TaskRecord task = mTaskHistory.get(taskNdx);
3733 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003734 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003735 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3736 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003737 }
3738
3739 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3740 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3741
3742 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003743 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3744 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003745 }
3746 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003747 final int top = mTaskHistory.size() - 1;
3748 if (top >= 0) {
3749 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3750 int listTop = list.size() - 1;
3751 if (listTop >= 0) {
3752 activities.add(list.get(listTop));
3753 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003754 }
3755 } else {
3756 ItemMatcher matcher = new ItemMatcher();
3757 matcher.build(name);
3758
Craig Mautneraab647e2013-02-28 16:31:36 -08003759 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3760 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3761 if (matcher.match(r1, r1.intent.getComponent())) {
3762 activities.add(r1);
3763 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003764 }
3765 }
3766 }
3767
3768 return activities;
3769 }
3770
3771 ActivityRecord restartPackage(String packageName) {
3772 ActivityRecord starting = topRunningActivityLocked(null);
3773
3774 // All activities that came from the package must be
3775 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003776 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3777 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3778 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3779 final ActivityRecord a = activities.get(activityNdx);
3780 if (a.info.packageName.equals(packageName)) {
3781 a.forceNewConfig = true;
3782 if (starting != null && a == starting && a.visible) {
3783 a.startFreezingScreenLocked(starting.app,
3784 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3785 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003786 }
3787 }
3788 }
3789
3790 return starting;
3791 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003792
Craig Mautnerde4ef022013-04-07 19:01:33 -07003793 boolean removeTask(TaskRecord task) {
3794 mTaskHistory.remove(task);
3795 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003796 }
3797
Craig Mautnerde4ef022013-04-07 19:01:33 -07003798 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003799 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003800 if (toTop) {
3801 mTaskHistory.add(task);
3802 } else {
3803 mTaskHistory.add(0, task);
3804 }
3805 return task;
3806 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003807
3808 ArrayList<TaskRecord> getAllTasks() {
3809 return new ArrayList<TaskRecord>(mTaskHistory);
3810 }
3811
3812 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003813 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003814 if (task == null) {
3815 return;
3816 }
3817 task.stack.mTaskHistory.remove(task);
3818 task.stack = this;
3819 if (toTop) {
3820 mTaskHistory.add(task);
3821 } else {
3822 mTaskHistory.add(0, task);
3823 }
3824 }
3825
3826 public int getStackId() {
3827 return mStackId;
3828 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003829
3830 @Override
3831 public String toString() {
3832 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3833 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003834}