blob: 0ca5db53a0bffdfea9818be66732810aa472711f [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautnerde4ef022013-04-07 19:01:33 -070019import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
20
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070021import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080022import com.android.internal.util.Objects;
Craig Mautnercae015f2013-02-08 14:31:27 -080023import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080024import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080025import com.android.server.wm.TaskGroup;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070026
27import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070028import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070029import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070030import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080031import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070032import android.app.IActivityManager;
Craig Mautnercae015f2013-02-08 14:31:27 -080033import android.app.IThumbnailReceiver;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070034import android.app.IThumbnailRetriever;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070035import android.app.IApplicationThread;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070036import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080037import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070038import android.app.IActivityManager.WaitResult;
39import android.content.ComponentName;
40import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070042import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070043import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070044import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080045import android.content.res.Resources;
46import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080047import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080048import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070050import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.os.Handler;
52import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090053import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.os.Message;
55import android.os.PowerManager;
56import android.os.RemoteException;
57import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070058import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070061import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062
Craig Mautnercae015f2013-02-08 14:31:27 -080063import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080064import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import java.lang.ref.WeakReference;
66import java.util.ArrayList;
67import java.util.Iterator;
68import java.util.List;
69
70/**
71 * State and management of a single stack of activities.
72 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070073final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070075 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
77 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
Craig Mautnerde4ef022013-04-07 19:01:33 -070078 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY || true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
80 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
81 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
82 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
83 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070084 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Craig 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 Mautnerd74f7d72013-02-26 13:41:02 -0800145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700146 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 /**
149 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800150 * running) activities. It contains #TaskRecord objects.
151 */
152 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
153
154 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800155 * Used for validating app tokens with window manager.
156 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800157 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800158
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159 /**
160 * List of running activities, sorted by recent usage.
161 * The first entry in the list is the least recently used.
162 * It contains HistoryRecord objects.
163 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700164 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165
166 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800167 * List of activities that are in the process of going to sleep.
168 */
169 final ArrayList<ActivityRecord> mGoingToSleepActivities
170 = new ArrayList<ActivityRecord>();
171
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 */
176 final ArrayList<ActivityRecord> mNoAnimActivities
177 = new ArrayList<ActivityRecord>();
178
179 /**
180 * List of activities that are ready to be finished, but waiting
181 * for the previous activity to settle down before doing so. It contains
182 * HistoryRecord objects.
183 */
184 final ArrayList<ActivityRecord> mFinishingActivities
185 = new ArrayList<ActivityRecord>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800186
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 /**
188 * List of people waiting to find out about the next launched activity.
189 */
190 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched
191 = new ArrayList<IActivityManager.WaitResult>();
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193 /**
194 * List of people waiting to find out about the next visible activity.
195 */
196 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible
197 = new ArrayList<IActivityManager.WaitResult>();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700198
199 final ArrayList<UserStartedState> mStartingUsers
200 = new ArrayList<UserStartedState>();
201
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202 /**
203 * Set when the system is going to sleep, until we have
204 * successfully paused the current activity and released our wake lock.
205 * At that point the system is allowed to actually sleep.
206 */
207 final PowerManager.WakeLock mGoingToSleep;
208
209 /**
210 * We don't want to allow the device to go to sleep while in the process
211 * of launching an activity. This is primarily to allow alarm intent
212 * receivers to launch an activity and get that to run before the device
213 * goes back to sleep.
214 */
215 final PowerManager.WakeLock mLaunchingActivity;
216
217 /**
218 * When we are in the process of pausing an activity, before starting the
219 * next one, this variable holds the activity that is currently being paused.
220 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800221 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700222
223 /**
224 * This is the last activity that we put into the paused state. This is
225 * used to determine if we need to do an activity transition while sleeping,
226 * when we normally hold the top activity paused.
227 */
228 ActivityRecord mLastPausedActivity = null;
229
230 /**
231 * Current activity that is resumed, or null if there is none.
232 */
233 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700235 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700236 * This is the last activity that has been started. It is only used to
237 * identify when multiple activities are started at once so that the user
238 * can be warned they may not be in the activity they think they are.
239 */
240 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800241
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700242 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700243 * Set when we know we are going to be calling updateConfiguration()
244 * soon, so want to skip intermediate config checks.
245 */
246 boolean mConfigWillChange;
247
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700248 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800249
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800250 /**
251 * Set when we have taken too long waiting to go to sleep.
252 */
253 boolean mSleepTimeout = false;
254
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700255 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800256 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
257 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
258 */
259 private ActivityRecord mLastScreenshotActivity = null;
260 private Bitmap mLastScreenshotBitmap = null;
261
Craig Mautnercae015f2013-02-08 14:31:27 -0800262 /**
263 * List of ActivityRecord objects that have been finished and must
264 * still report back to a pending thumbnail receiver.
265 */
266 private final ArrayList<ActivityRecord> mCancelledThumbnails = new ArrayList<ActivityRecord>();
267
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800268 int mThumbnailWidth = -1;
269 int mThumbnailHeight = -1;
270
Amith Yamasani742a6712011-05-04 14:49:28 -0700271 private int mCurrentUser;
272
Craig Mautnerc00204b2013-03-05 15:02:14 -0800273 final int mStackId;
274
Craig Mautner27084302013-03-25 08:05:25 -0700275 /** Run all ActivityStacks through this */
276 final ActivityStackSupervisor mStackSupervisor;
277
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800278 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
279 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
280 static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
281 static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
282 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
283 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
284 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn29ba7e62012-03-16 15:03:36 -0700285 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700286 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700287 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9;
288
289 static class ScheduleDestroyArgs {
290 final ProcessRecord mOwner;
291 final boolean mOomAdj;
292 final String mReason;
293 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
294 mOwner = owner;
295 mOomAdj = oomAdj;
296 mReason = reason;
297 }
298 }
299
Zoran Marcetaf958b322012-08-09 20:27:12 +0900300 final Handler mHandler;
301
302 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700303 //public Handler() {
304 // if (localLOGV) Slog.v(TAG, "Handler started!");
305 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900306 public ActivityStackHandler(Looper looper) {
307 super(looper);
308 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309
Zoran Marcetaf958b322012-08-09 20:27:12 +0900310 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700311 public void handleMessage(Message msg) {
312 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800313 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700314 synchronized (mService) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700315 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700316 Slog.w(TAG, "Sleep timeout! Sleeping now.");
317 mSleepTimeout = true;
318 checkReadyForSleepLocked();
319 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800320 }
321 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700322 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800323 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700324 // We don't at this point know if the activity is fullscreen,
325 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800326 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700327 synchronized (mService) {
328 if (r.app != null) {
329 mService.logAppTooSlow(r.app, r.pauseTime,
330 "pausing " + r);
331 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700332
Craig Mautnerd2328952013-03-05 12:46:26 -0800333 activityPausedLocked(r != null ? r.appToken : null, true);
334 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 } break;
336 case IDLE_TIMEOUT_MSG: {
337 if (mService.mDidDexOpt) {
338 mService.mDidDexOpt = false;
339 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
340 nmsg.obj = msg.obj;
341 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
342 return;
343 }
344 // We don't at this point know if the activity is fullscreen,
345 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800346 ActivityRecord r = (ActivityRecord)msg.obj;
347 Slog.w(TAG, "Activity idle timeout for " + r);
Craig Mautner6170f732013-04-02 13:05:23 -0700348 synchronized (mService) {
Craig Mautnere79d42682013-04-01 19:01:53 -0700349 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
350 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700351 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700352 case LAUNCH_TICK_MSG: {
353 ActivityRecord r = (ActivityRecord)msg.obj;
354 synchronized (mService) {
355 if (r.continueLaunchTickingLocked()) {
356 mService.logAppTooSlow(r.app, r.launchTickTime,
357 "launching " + r);
358 }
359 }
360 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700361 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800362 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700363 // We don't at this point know if the activity is fullscreen,
364 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800365 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800366 synchronized (mService) {
367 activityDestroyedLocked(r != null ? r.appToken : null);
368 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700369 } break;
370 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800371 ActivityRecord r = (ActivityRecord)msg.obj;
Craig Mautnere79d42682013-04-01 19:01:53 -0700372 synchronized (mService) {
373 activityIdleInternalLocked(r != null ? r.appToken : null, false, null);
374 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700375 } break;
376 case LAUNCH_TIMEOUT_MSG: {
377 if (mService.mDidDexOpt) {
378 mService.mDidDexOpt = false;
379 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
380 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
381 return;
382 }
383 synchronized (mService) {
384 if (mLaunchingActivity.isHeld()) {
385 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
386 mLaunchingActivity.release();
387 }
388 }
389 } break;
390 case RESUME_TOP_ACTIVITY_MSG: {
391 synchronized (mService) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700392 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 }
394 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700395 case STOP_TIMEOUT_MSG: {
396 ActivityRecord r = (ActivityRecord)msg.obj;
397 // We don't at this point know if the activity is fullscreen,
398 // so we need to be conservative and assume it isn't.
399 Slog.w(TAG, "Activity stop timeout for " + r);
400 synchronized (mService) {
401 if (r.isInHistory()) {
402 activityStoppedLocked(r, null, null, null);
403 }
404 }
405 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700406 case DESTROY_ACTIVITIES_MSG: {
407 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
408 synchronized (mService) {
409 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
410 }
411 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700412 }
413 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800414 }
415
Craig Mautner000f0022013-02-26 15:04:29 -0800416 private int numActivities() {
417 int count = 0;
418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 count += mTaskHistory.get(taskNdx).mActivities.size();
420 }
421 return count;
422 }
423
Craig Mautner2219a1b2013-03-25 09:44:30 -0700424 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId,
Craig Mautner2420ead2013-04-01 17:13:20 -0700425 ActivityStackSupervisor supervisor, int userId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900426 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700427 mService = service;
428 mContext = context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700429 PowerManager pm =
430 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
431 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
432 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
433 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800434 mStackId = stackId;
Craig Mautner27084302013-03-25 08:05:25 -0700435 mStackSupervisor = supervisor;
Craig Mautner2420ead2013-04-01 17:13:20 -0700436 mCurrentUser = userId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700437 }
Craig Mautner5962b122012-10-05 14:45:52 -0700438
439 private boolean okToShow(ActivityRecord r) {
440 return r.userId == mCurrentUser
441 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
442 }
443
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700444 final ActivityRecord topRunningActivityLocked(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 != notTop && okToShow(r)) {
451 return r;
452 }
453 }
454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 return null;
456 }
457
458 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800459 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
460 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800461 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800462 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
463 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800464 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
465 return r;
466 }
467 }
468 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700469 return null;
470 }
471
472 /**
473 * This is a simplified version of topRunningActivityLocked that provides a number of
474 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800475 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700476 * @param token If non-null, any history records matching this token will be skipped.
477 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800478 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700479 * @return Returns the HistoryRecord of the next activity on the stack.
480 */
481 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800482 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
483 TaskRecord task = mTaskHistory.get(taskNdx);
484 if (task.taskId == taskId) {
485 continue;
486 }
487 ArrayList<ActivityRecord> activities = task.mActivities;
488 for (int i = activities.size() - 1; i >= 0; --i) {
489 final ActivityRecord r = activities.get(i);
490 // Note: the taskId check depends on real taskId fields being non-zero
491 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
492 return r;
493 }
494 }
495 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700496 return null;
497 }
498
Craig Mautner8849a5e2013-04-02 16:41:03 -0700499 final ActivityRecord topActivity() {
500 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700501 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700502 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
503 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
504 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700505 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700506 }
507 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700508 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700509 }
510
Craig Mautnerd2328952013-03-05 12:46:26 -0800511 TaskRecord taskForIdLocked(int id) {
512 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
513 final TaskRecord task = mTaskHistory.get(taskNdx);
514 if (task.taskId == id) {
515 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800516 }
517 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700518 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700519 }
520
Craig Mautnerd2328952013-03-05 12:46:26 -0800521 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800522 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800523 if (r != null) {
524 final TaskRecord task = r.task;
525 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
526 if (task.stack != this) Slog.w(TAG,
527 "Illegal state! task does not point to stack it is in.");
528 return r;
529 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800530 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800531 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800532 }
533
Craig Mautner2420ead2013-04-01 17:13:20 -0700534 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700535 final boolean hadit = mLRUActivities.remove(r);
536 mLRUActivities.add(r);
537 return hadit;
538 }
539
Craig Mautnerde4ef022013-04-07 19:01:33 -0700540 final boolean isHomeStack() {
541 return mStackId == HOME_STACK_ID;
542 }
543
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700544 /**
545 * Returns the top activity in any existing task matching the given
546 * Intent. Returns null if no such task is found.
547 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700548 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700549 ComponentName cls = intent.getComponent();
550 if (info.targetActivity != null) {
551 cls = new ComponentName(info.packageName, info.targetActivity);
552 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700553 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800554
555 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
556 final TaskRecord task = mTaskHistory.get(taskNdx);
557 final ActivityRecord r = task.getTopActivity();
558 if (r == null || r.finishing || r.userId != userId ||
559 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
560 continue;
561 }
562
563 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
564 // + "/aff=" + r.task.affinity + " to new cls="
565 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
566 if (task.affinity != null) {
567 if (task.affinity.equals(info.taskAffinity)) {
568 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700569 return r;
570 }
Craig Mautner000f0022013-02-26 15:04:29 -0800571 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
572 //Slog.i(TAG, "Found matching class!");
573 //dump();
574 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
575 return r;
576 } else if (task.affinityIntent != null
577 && task.affinityIntent.getComponent().equals(cls)) {
578 //Slog.i(TAG, "Found matching class!");
579 //dump();
580 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
581 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700582 }
583 }
584
585 return null;
586 }
587
588 /**
589 * Returns the first activity (starting from the top of the stack) that
590 * is the same as the given activity. Returns null if no such activity
591 * is found.
592 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700593 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700594 ComponentName cls = intent.getComponent();
595 if (info.targetActivity != null) {
596 cls = new ComponentName(info.packageName, info.targetActivity);
597 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700598 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700599
Craig Mautner000f0022013-02-26 15:04:29 -0800600 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
601 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
602 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
603 ActivityRecord r = activities.get(activityNdx);
604 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 //Slog.i(TAG, "Found matching class!");
606 //dump();
607 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
608 return r;
609 }
610 }
611 }
612
613 return null;
614 }
615
Amith Yamasani742a6712011-05-04 14:49:28 -0700616 /*
617 * Move the activities around in the stack to bring a user to the foreground.
618 * @return whether there are any activities for the specified user.
619 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700620 final boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800621 if (VALIDATE_TOKENS) {
622 validateAppTokensLocked();
623 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700624 mStartingUsers.add(uss);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800625 if (mCurrentUser == userId) {
626 return true;
627 }
628 mCurrentUser = userId;
629
630 // Move userId's tasks to the top.
631 boolean haveActivities = false;
632 TaskRecord task = null;
633 int index = mTaskHistory.size();
634 for (int i = 0; i < index; ++i) {
635 task = mTaskHistory.get(i);
636 if (task.userId == userId) {
637 haveActivities = true;
638 mTaskHistory.remove(i);
639 mTaskHistory.add(task);
640 --index;
641 }
642 }
643
644 // task is now the original topmost TaskRecord. Transition from the old top to the new top.
645 ActivityRecord top = task != null ? task.getTopActivity() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800646 resumeTopActivityLocked(top);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700647 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700648 }
649
Craig Mautner2420ead2013-04-01 17:13:20 -0700650 void minimalResumeActivityLocked(ActivityRecord r) {
651 r.state = ActivityState.RESUMED;
652 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
653 + " (starting new instance)");
654 r.stopped = false;
655 mResumedActivity = r;
656 r.task.touchActiveTime();
657 mService.addRecentTaskLocked(r.task);
658 completeResumeLocked(r);
659 checkReadyForSleepLocked();
660 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700661 }
662
Craig Mautnere79d42682013-04-01 19:01:53 -0700663 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700664 if (r.launchTime == 0) {
665 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700666 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700667 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700668 }
669 } else if (mInitialStartTime == 0) {
670 mInitialStartTime = SystemClock.uptimeMillis();
671 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700672 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800673
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800674 void stopIfSleepingLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700675 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700676 if (!mGoingToSleep.isHeld()) {
677 mGoingToSleep.acquire();
678 if (mLaunchingActivity.isHeld()) {
679 mLaunchingActivity.release();
680 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
681 }
682 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800683 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
684 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
685 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
686 checkReadyForSleepLocked();
687 }
688 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700689
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800690 void awakeFromSleepingLocked() {
691 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
692 mSleepTimeout = false;
693 if (mGoingToSleep.isHeld()) {
694 mGoingToSleep.release();
695 }
696 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800697 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
698 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
699 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
700 activities.get(activityNdx).setSleeping(false);
701 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800702 }
703 mGoingToSleepActivities.clear();
704 }
705
706 void activitySleptLocked(ActivityRecord r) {
707 mGoingToSleepActivities.remove(r);
708 checkReadyForSleepLocked();
709 }
710
Craig Mautnere79d42682013-04-01 19:01:53 -0700711 // Checked.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800712 void checkReadyForSleepLocked() {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700713 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800714 // Do not care.
715 return;
716 }
717
718 if (!mSleepTimeout) {
719 if (mResumedActivity != null) {
720 // Still have something resumed; can't sleep until it is paused.
721 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700722 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
723 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800724 return;
725 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800726 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800727 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800728 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800729 return;
730 }
731
Craig Mautnerde4ef022013-04-07 19:01:33 -0700732 if (mStackSupervisor.mStoppingActivities.size() > 0) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800733 // Still need to tell some activities to stop; can't sleep yet.
734 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
Craig Mautnerde4ef022013-04-07 19:01:33 -0700735 + mStackSupervisor.mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700736 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800737 return;
738 }
739
740 ensureActivitiesVisibleLocked(null, 0);
741
742 // Make sure any stopped but visible activities are now sleeping.
743 // This ensures that the activity's onStop() is called.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800744 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
745 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
746 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
747 final ActivityRecord r = activities.get(activityNdx);
748 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
749 r.setSleeping(true);
750 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800751 }
752 }
753
754 if (mGoingToSleepActivities.size() > 0) {
755 // Still need to tell some activities to sleep; can't sleep yet.
756 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
757 + mGoingToSleepActivities.size() + " activities");
758 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700759 }
760 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800761
762 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
763
764 if (mGoingToSleep.isHeld()) {
765 mGoingToSleep.release();
766 }
767 if (mService.mShuttingDown) {
768 mService.notifyAll();
769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700770 }
Craig Mautner59c00972012-07-30 12:10:24 -0700771
Dianne Hackbornd2835932010-12-13 16:28:46 -0800772 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800773 if (who.noDisplay) {
774 return null;
775 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800776
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800777 Resources res = mService.mContext.getResources();
778 int w = mThumbnailWidth;
779 int h = mThumbnailHeight;
780 if (w < 0) {
781 mThumbnailWidth = w =
782 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
783 mThumbnailHeight = h =
784 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
785 }
786
787 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800788 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
789 || mLastScreenshotBitmap.getWidth() != w
790 || mLastScreenshotBitmap.getHeight() != h) {
791 mLastScreenshotActivity = who;
792 mLastScreenshotBitmap = mService.mWindowManager.screenshotApplications(
793 who.appToken, Display.DEFAULT_DISPLAY, w, h);
794 }
795 if (mLastScreenshotBitmap != null) {
796 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
797 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800798 }
799 return null;
800 }
801
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700802 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800803 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800804 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700805 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800806 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 ActivityRecord prev = mResumedActivity;
808 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700809 Slog.e(TAG, "Trying to pause when nothing is resumed",
810 new RuntimeException("here").fillInStackTrace());
Craig Mautnerde4ef022013-04-07 19:01:33 -0700811 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700812 return;
813 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700814 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
815 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800817 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 mLastPausedActivity = prev;
819 prev.state = ActivityState.PAUSING;
820 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700821 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822
823 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800824
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700825 if (prev.app != null && prev.app.thread != null) {
826 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
827 try {
828 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700829 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700830 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800831 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
832 userLeaving, prev.configChangeFlags);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700833 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834 mService.updateUsageStats(prev, false);
835 }
836 } catch (Exception e) {
837 // Ignore exception, if process died other code will cleanup.
838 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800839 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 mLastPausedActivity = null;
841 }
842 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800843 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 mLastPausedActivity = null;
845 }
846
847 // If we are not going to sleep, we want to ensure the device is
848 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700849 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 mLaunchingActivity.acquire();
851 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
852 // To be safe, don't allow the wake lock to be held for too long.
853 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
854 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
855 }
856 }
857
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800858 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700859 // Have the window manager pause its key dispatching until the new
860 // activity has started. If we're pausing the activity just because
861 // the screen is being turned off and the UI is sleeping, don't interrupt
862 // key dispatch; the same activity will pick it up again on wakeup.
863 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800864 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700865 } else {
866 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
867 }
868
869 // Schedule a pause timeout in case the app doesn't respond.
870 // We don't give it much time because this directly impacts the
871 // responsiveness seen by the user.
872 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
873 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700874 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
876 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
877 } else {
878 // This activity failed to schedule the
879 // pause, so just treat it as being paused now.
880 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700881 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700882 }
883 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700884
Craig Mautnerd2328952013-03-05 12:46:26 -0800885 final void activityResumedLocked(IBinder token) {
886 final ActivityRecord r = ActivityRecord.forToken(token);
887 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
888 r.icicle = null;
889 r.haveState = false;
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700890 }
891
Craig Mautnerd2328952013-03-05 12:46:26 -0800892 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700893 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800894 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700895
Craig Mautnerd2328952013-03-05 12:46:26 -0800896 final ActivityRecord r = isInStackLocked(token);
897 if (r != null) {
898 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
899 if (mPausingActivity == r) {
900 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
901 + (timeout ? " (due to timeout)" : " (pause complete)"));
902 r.state = ActivityState.PAUSED;
903 completePauseLocked();
904 } else {
905 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
906 r.userId, System.identityHashCode(r), r.shortComponentName,
907 mPausingActivity != null
908 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909 }
910 }
911 }
912
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700913 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
914 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700915 if (r.state != ActivityState.STOPPING) {
916 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
917 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
918 return;
919 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700920 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700921 if (icicle != null) {
922 // If icicle is null, this is happening due to a timeout, so we
923 // haven't really saved the state.
924 r.icicle = icicle;
925 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800926 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700927 r.updateThumbnail(thumbnail, description);
928 }
929 if (!r.stopped) {
930 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
931 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
932 r.stopped = true;
933 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700934 if (r.finishing) {
935 r.clearOptionsLocked();
936 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700937 if (r.configDestroy) {
938 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700939 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700940 } else {
941 // Now that this process has stopped, we may want to consider
942 // it to be the previous app to try to keep around in case
943 // the user wants to return to it.
944 ProcessRecord fgApp = null;
945 if (mResumedActivity != null) {
946 fgApp = mResumedActivity.app;
947 } else if (mPausingActivity != null) {
948 fgApp = mPausingActivity.app;
949 }
950 if (r.app != null && fgApp != null && r.app != fgApp
951 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
952 && r.app != mService.mHomeProcess) {
953 mService.mPreviousProcess = r.app;
954 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
955 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800956 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700957 }
958 }
959 }
960
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800961 private final void completePauseLocked() {
962 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700963 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800964
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 if (prev != null) {
966 if (prev.finishing) {
967 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700968 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800969 } else if (prev.app != null) {
970 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
971 if (prev.waitingVisible) {
972 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700973 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800974 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
975 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800976 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800977 if (prev.configDestroy) {
978 // The previous is being paused because the configuration
979 // is changing, which means it is actually stopping...
980 // To juggle the fact that we are also starting a new
981 // instance right now, we need to first completely stop
982 // the current instance before starting the new one.
983 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
984 destroyActivityLocked(prev, true, false, "pause-config");
985 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700986 mStackSupervisor.mStoppingActivities.add(prev);
987 if (mStackSupervisor.mStoppingActivities.size() > 3) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800988 // If we already have a few activities waiting to stop,
989 // then give up on things going idle and start clearing
990 // them out.
991 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
992 scheduleIdleLocked();
993 } else {
994 checkReadyForSleepLocked();
995 }
996 }
997 } else {
998 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
999 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001000 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001003
Craig Mautnerde4ef022013-04-07 19:01:33 -07001004 final ActivityStack topStack = mStackSupervisor.getTopStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -07001005 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001006 topStack.resumeTopActivityLocked(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001008 checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001009 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001010 if (top == null || (prev != null && top != prev)) {
1011 // If there are no more activities available to run,
1012 // do resume anyway to start something. Also if the top
1013 // activity on the stack is not the just paused activity,
1014 // we need to go ahead and resume it to ensure we complete
1015 // an in-flight app switch.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001016 topStack.resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001017 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001019
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001020 if (prev != null) {
1021 prev.resumeKeyDispatchingLocked();
1022 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001023
1024 if (prev.app != null && prev.cpuTimeAtResume > 0
1025 && mService.mBatteryStatsService.isOnBattery()) {
1026 long diff = 0;
1027 synchronized (mService.mProcessStatsThread) {
1028 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1029 - prev.cpuTimeAtResume;
1030 }
1031 if (diff > 0) {
1032 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1033 synchronized (bsi) {
1034 BatteryStatsImpl.Uid.Proc ps =
1035 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1036 prev.info.packageName);
1037 if (ps != null) {
1038 ps.addForegroundTimeLocked(diff);
1039 }
1040 }
1041 }
1042 }
1043 prev.cpuTimeAtResume = 0; // reset it
1044 }
1045
Craig Mautnere79d42682013-04-01 19:01:53 -07001046 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001047 /**
1048 * Once we know that we have asked an application to put an activity in
1049 * the resumed state (either by launching it or explicitly telling it),
1050 * this function updates the rest of our state to match that fact.
1051 */
1052 private final void completeResumeLocked(ActivityRecord next) {
1053 next.idle = false;
1054 next.results = null;
1055 next.newIntents = null;
1056
1057 // schedule an idle timeout in case the app doesn't do it for us.
1058 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1059 msg.obj = next;
1060 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1061
1062 if (false) {
1063 // The activity was never told to pause, so just keep
1064 // things going as-is. To maintain our own state,
1065 // we need to emulate it coming back and saying it is
1066 // idle.
1067 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1068 msg.obj = next;
1069 mHandler.sendMessage(msg);
1070 }
1071
Craig Mautnerde4ef022013-04-07 19:01:33 -07001072 if (mStackSupervisor.isFrontStack(this)) {
1073 // TODO: Should this be done for all stacks, not just mMainStack?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001074 mService.reportResumedActivityLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001075 mService.setFocusedActivityLocked(next);
1076 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001077 if (mStackSupervisor.allResumedActivitiesComplete()) {
1078 next.resumeKeyDispatchingLocked();
1079 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
1080 mService.mWindowManager.executeAppTransition();
1081 mNoAnimActivities.clear();
1082 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001083
1084 // Mark the point when the activity is resuming
1085 // TODO: To be more accurate, the mark should be before the onCreate,
1086 // not after the onResume. But for subsequent starts, onResume is fine.
1087 if (next.app != null) {
1088 synchronized (mService.mProcessStatsThread) {
1089 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1090 }
1091 } else {
1092 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1093 }
1094 }
1095
Craig Mautnere79d42682013-04-01 19:01:53 -07001096 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001097 /**
1098 * Make sure that all activities that need to be visible (that is, they
1099 * currently can be seen by the user) actually are.
1100 */
1101 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1102 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1103 if (DEBUG_VISBILITY) Slog.v(
1104 TAG, "ensureActivitiesVisible behind " + top
1105 + " configChanges=0x" + Integer.toHexString(configChanges));
1106
1107 // If the top activity is not fullscreen, then we need to
1108 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001109 boolean aboveTop = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001110 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this);
1111 int taskNdx;
1112 for (taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001113 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1114 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1115 final ActivityRecord r = activities.get(activityNdx);
1116 if (r.finishing) {
1117 continue;
1118 }
1119 if (aboveTop && r != top) {
1120 continue;
1121 }
1122 aboveTop = false;
1123 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001124 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001125 TAG, "Make visible? " + r + " finishing=" + r.finishing
1126 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001127
Craig Mautnerd44711d2013-02-23 11:24:36 -08001128 final boolean doThisProcess = onlyThisProcess == null
1129 || onlyThisProcess.equals(r.processName);
1130
1131 // First: if this is not the current activity being started, make
1132 // sure it matches the current configuration.
1133 if (r != starting && doThisProcess) {
1134 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001136
1137 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001138 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001139 // This activity needs to be visible, but isn't even
1140 // running... get it started, but don't resume it
1141 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001142 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001143 if (r != starting) {
1144 r.startFreezingScreenLocked(r.app, configChanges);
1145 }
1146 if (!r.visible) {
1147 if (DEBUG_VISBILITY) Slog.v(
1148 TAG, "Starting and making visible: " + r);
1149 mService.mWindowManager.setAppVisibility(r.appToken, true);
1150 }
1151 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001152 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001153 }
1154 }
1155
1156 } else if (r.visible) {
1157 // If this activity is already visible, then there is nothing
1158 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001159 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001161
1162 } else if (onlyThisProcess == null) {
1163 // This activity is not currently visible, but is running.
1164 // Tell it to become visible.
1165 r.visible = true;
1166 if (r.state != ActivityState.RESUMED && r != starting) {
1167 // If this activity is paused, tell it
1168 // to now show its window.
1169 if (DEBUG_VISBILITY) Slog.v(
1170 TAG, "Making visible and scheduling visibility: " + r);
1171 try {
1172 mService.mWindowManager.setAppVisibility(r.appToken, true);
1173 r.sleeping = false;
1174 r.app.pendingUiClean = true;
1175 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1176 r.stopFreezingScreenLocked(false);
1177 } catch (Exception e) {
1178 // Just skip on any failure; we'll make it
1179 // visible when it next restarts.
1180 Slog.w(TAG, "Exception thrown making visibile: "
1181 + r.intent.getComponent(), e);
1182 }
1183 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001185
Craig Mautnerd44711d2013-02-23 11:24:36 -08001186 // Aggregate current change flags.
1187 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001188
Craig Mautnerd44711d2013-02-23 11:24:36 -08001189 if (r.fullscreen) {
1190 // At this point, nothing else needs to be shown
1191 if (DEBUG_VISBILITY) Slog.v(
1192 TAG, "Stopping: fullscreen at " + r);
1193 behindFullscreen = true;
1194 }
1195 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001196 if (DEBUG_VISBILITY) Slog.v(
1197 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1198 + " state=" + r.state
1199 + " behindFullscreen=" + behindFullscreen);
1200 // Now for any activities that aren't visible to the user, make
1201 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001202 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001203 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001204 r.visible = false;
1205 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001206 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207 if ((r.state == ActivityState.STOPPING
1208 || r.state == ActivityState.STOPPED)
1209 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001210 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001211 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 }
1213 } catch (Exception e) {
1214 // Just skip on any failure; we'll make it
1215 // visible when it next restarts.
1216 Slog.w(TAG, "Exception thrown making hidden: "
1217 + r.intent.getComponent(), e);
1218 }
1219 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001220 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001221 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222 }
1223 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001224 }
1225 }
1226
Craig Mautnere79d42682013-04-01 19:01:53 -07001227 // Checked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001228 /**
1229 * Version of ensureActivitiesVisible that can easily be called anywhere.
1230 */
1231 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1232 int configChanges) {
1233 ActivityRecord r = topRunningActivityLocked(null);
1234 if (r != null) {
1235 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1236 }
1237 }
Craig Mautner58547802013-03-05 08:23:53 -08001238
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001239 /**
1240 * Ensure that the top activity in the stack is resumed.
1241 *
1242 * @param prev The previously resumed activity, for when in the process
1243 * of pausing; can be null to call from elsewhere.
1244 *
1245 * @return Returns true if something is being resumed, or false if
1246 * nothing happened.
1247 */
1248 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001249 return resumeTopActivityLocked(prev, null);
1250 }
1251
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07001252 // Checked.
Dianne Hackborn84375872012-06-01 19:03:50 -07001253 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001254 // Find the first activity that is not finishing.
1255 ActivityRecord next = topRunningActivityLocked(null);
1256
1257 // Remember how we'll process this pause/resume situation, and ensure
1258 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001259 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1260 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001261
1262 if (next == null) {
1263 // There are no more activities! Let's just start up the
1264 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001265 ActivityOptions.abort(options);
1266 return mService.startHomeActivityLocked(mCurrentUser);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001267 }
1268
1269 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001270
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001271 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001272 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1273 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001274 // Make sure we have executed any pending transitions, since there
1275 // should be nothing left to do at this point.
1276 mService.mWindowManager.executeAppTransition();
1277 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001278 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001279 return false;
1280 }
1281
Craig Mautnerde4ef022013-04-07 19:01:33 -07001282 if (prev != null && prev.mLaunchHomeTaskNext && prev.finishing &&
1283 prev.task.getTopActivity() == null) {
1284 prev.mLaunchHomeTaskNext = false;
1285 return mService.startHomeActivityLocked(mCurrentUser);
1286 }
1287
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001288 // If we are sleeping, and there is no resumed activity, and the top
1289 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001290 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001291 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001292 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001293 // Make sure we have executed any pending transitions, since there
1294 // should be nothing left to do at this point.
1295 mService.mWindowManager.executeAppTransition();
1296 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001297 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001298 return false;
1299 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001300
1301 // Make sure that the user who owns this activity is started. If not,
1302 // we will just leave it as is because someone should be bringing
1303 // another user's activities to the top of the stack.
1304 if (mService.mStartedUsers.get(next.userId) == null) {
1305 Slog.w(TAG, "Skipping resume of top activity " + next
1306 + ": user " + next.userId + " is stopped");
1307 return false;
1308 }
1309
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001310 // The activity may be waiting for stop, but that is no longer
1311 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001312 mStackSupervisor.mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001313 mGoingToSleepActivities.remove(next);
1314 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001315 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001316
Dianne Hackborn84375872012-06-01 19:03:50 -07001317 next.updateOptionsLocked(options);
1318
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1320
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001321 // If we are currently pausing an activity, then don't do anything
1322 // until that is done.
1323 if (mPausingActivity != null) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001324 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG,
1325 "Skip resume: pausing=" + mPausingActivity);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001326 return false;
1327 }
1328
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001329 // Okay we are now going to start a switch, to 'next'. We may first
1330 // have to pause the current activity, but this is an important point
1331 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001332 // XXX "App Redirected" dialog is getting too many false positives
1333 // at this point, so turn off for now.
1334 if (false) {
1335 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1336 long now = SystemClock.uptimeMillis();
1337 final boolean inTime = mLastStartedActivity.startTime != 0
1338 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1339 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1340 final int nextUid = next.info.applicationInfo.uid;
1341 if (inTime && lastUid != nextUid
1342 && lastUid != next.launchedFromUid
1343 && mService.checkPermission(
1344 android.Manifest.permission.STOP_APP_SWITCHES,
1345 -1, next.launchedFromUid)
1346 != PackageManager.PERMISSION_GRANTED) {
1347 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1348 } else {
1349 next.startTime = now;
1350 mLastStartedActivity = next;
1351 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001352 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001353 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001354 mLastStartedActivity = next;
1355 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001356 }
Craig Mautner58547802013-03-05 08:23:53 -08001357
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001358 // We need to start pausing the current activity so the top one
1359 // can be resumed...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001360 final ActivityStack lastStack = mStackSupervisor.getLastStack();
Craig Mautner967212c2013-04-13 21:10:58 -07001361 if ((isHomeStack() ^ lastStack.isHomeStack()) && lastStack.mResumedActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001362 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001363 // At this point we want to put the upcoming activity's process
1364 // at the top of the LRU list, since we know we will be needing it
1365 // very soon and it would be a waste to let it get killed if it
1366 // happens to be sitting towards the end.
1367 if (next.app != null && next.app.thread != null) {
1368 // No reason to do full oom adj update here; we'll let that
1369 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001370 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001371 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001372 lastStack.startPausingLocked(userLeaving, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001373 return true;
1374 }
1375
Christopher Tated3f175c2012-06-14 14:16:54 -07001376 // If the most recent activity was noHistory but was only stopped rather
1377 // than stopped+finished because the device went to sleep, we need to make
1378 // sure to finish it as we're making a new activity topmost.
1379 final ActivityRecord last = mLastPausedActivity;
1380 if (mService.mSleeping && last != null && !last.finishing) {
1381 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1382 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1383 if (DEBUG_STATES) {
1384 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1385 }
1386 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001387 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001388 }
1389 }
1390
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001391 if (prev != null && prev != next) {
1392 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1393 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001394 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001395 if (DEBUG_SWITCH) Slog.v(
1396 TAG, "Resuming top, waiting visible to hide: " + prev);
1397 } else {
1398 // The next activity is already visible, so hide the previous
1399 // activity's windows right now so we can show the new one ASAP.
1400 // We only do this if the previous is finishing, which should mean
1401 // it is on top of the one being resumed so hiding it quickly
1402 // is good. Otherwise, we want to do the normal route of allowing
1403 // the resumed activity to be shown so we can decide if the
1404 // previous should actually be hidden depending on whether the
1405 // new one is found to be full-screen or not.
1406 if (prev.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001407 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001408 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1409 + prev + ", waitingVisible="
1410 + (prev != null ? prev.waitingVisible : null)
1411 + ", nowVisible=" + next.nowVisible);
1412 } else {
1413 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1414 + prev + ", waitingVisible="
1415 + (prev != null ? prev.waitingVisible : null)
1416 + ", nowVisible=" + next.nowVisible);
1417 }
1418 }
1419 }
1420
Dianne Hackborne7f97212011-02-24 14:40:20 -08001421 // Launching this app's activity, make sure the app is no longer
1422 // considered stopped.
1423 try {
1424 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001425 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001426 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001427 } catch (IllegalArgumentException e) {
1428 Slog.w(TAG, "Failed trying to unstop package "
1429 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001430 }
1431
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001432 // We are starting up the next activity, so tell the window manager
1433 // that the previous one will be hidden soon. This way it can know
1434 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001435 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001436 if (prev != null) {
1437 if (prev.finishing) {
1438 if (DEBUG_TRANSITION) Slog.v(TAG,
1439 "Prepare close transition: prev=" + prev);
1440 if (mNoAnimActivities.contains(prev)) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001441 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001442 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001443 } else {
1444 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001445 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1446 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001447 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001448 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1449 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001450 } else {
1451 if (DEBUG_TRANSITION) Slog.v(TAG,
1452 "Prepare open transition: prev=" + prev);
1453 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001454 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001455 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001456 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001457 } else {
1458 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001459 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1460 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 }
1462 }
1463 if (false) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001464 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1465 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 }
Craig Mautner967212c2013-04-13 21:10:58 -07001467 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468 if (DEBUG_TRANSITION) Slog.v(TAG,
1469 "Prepare open transition: no previous");
1470 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001471 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001472 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001473 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001474 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001475 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001476 AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001477 }
1478 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001479 if (!noAnim) {
1480 next.applyOptionsLocked();
1481 } else {
1482 next.clearOptionsLocked();
1483 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484
1485 if (next.app != null && next.app.thread != null) {
1486 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1487
1488 // This activity is now becoming visible.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001489 mService.mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001491 // schedule launch ticks to collect information about slow apps.
1492 next.startLaunchTickingLocked();
1493
Craig Mautnerde4ef022013-04-07 19:01:33 -07001494 ActivityRecord lastResumedActivity = lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 ActivityState lastState = next.state;
1496
1497 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001498
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001499 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001500 next.state = ActivityState.RESUMED;
1501 mResumedActivity = next;
1502 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001503 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001504 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001505 updateLRUListLocked(next);
1506
1507 // Have the window manager re-evaluate the orientation of
1508 // the screen based on the new activity order.
1509 boolean updated = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001510 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001511 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
1512 mService.mConfiguration,
1513 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1514 if (config != null) {
1515 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001517 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001518 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001519
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 if (!updated) {
1521 // The configuration update wasn't able to keep the existing
1522 // instance of the activity, and instead started a new one.
1523 // We should be all done, but let's just make sure our activity
1524 // is still at the top and schedule another run if something
1525 // weird happened.
1526 ActivityRecord nextNext = topRunningActivityLocked(null);
1527 if (DEBUG_SWITCH) Slog.i(TAG,
1528 "Activity config changed during resume: " + next
1529 + ", new next: " + nextNext);
1530 if (nextNext != next) {
1531 // Do over!
1532 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1533 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001534 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 mService.setFocusedActivityLocked(next);
1536 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001537 if (mStackSupervisor.allResumedActivitiesComplete()) {
1538 ensureActivitiesVisibleLocked(null, 0);
1539 mService.mWindowManager.executeAppTransition();
1540 mNoAnimActivities.clear();
1541 return true;
1542 }
1543 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 }
Craig Mautner58547802013-03-05 08:23:53 -08001545
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 try {
1547 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001548 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 if (a != null) {
1550 final int N = a.size();
1551 if (!next.finishing && N > 0) {
1552 if (DEBUG_RESULTS) Slog.v(
1553 TAG, "Delivering results to " + next
1554 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001555 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 }
1557 }
1558
1559 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001560 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 }
1562
1563 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001564 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001566
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001567 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001568 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001569 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001570 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001572
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001573 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574
1575 } catch (Exception e) {
1576 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001577 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1578 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 next.state = lastState;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001580 lastStack.mResumedActivity = lastResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 Slog.i(TAG, "Restarting because process died: " + next);
1582 if (!next.hasBeenLaunched) {
1583 next.hasBeenLaunched = true;
1584 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001585 if (SHOW_APP_STARTING_PREVIEW && mStackSupervisor.isFrontStack(lastStack)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001586 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001587 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001588 mService.compatibilityInfoForPackageLocked(
1589 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001590 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001591 next.labelRes, next.icon, next.windowFlags,
1592 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 }
1594 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001595 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 return true;
1597 }
1598
1599 // From this point on, if something goes wrong there is no way
1600 // to recover the activity.
1601 try {
1602 next.visible = true;
1603 completeResumeLocked(next);
1604 } catch (Exception e) {
1605 // If any exception gets thrown, toss away this
1606 // activity and try the next one.
1607 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001608 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001609 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001610 return true;
1611 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 next.stopped = false;
1613
1614 } else {
1615 // Whoops, need to restart this activity!
1616 if (!next.hasBeenLaunched) {
1617 next.hasBeenLaunched = true;
1618 } else {
1619 if (SHOW_APP_STARTING_PREVIEW) {
1620 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001621 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001622 mService.compatibilityInfoForPackageLocked(
1623 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001625 next.labelRes, next.icon, next.windowFlags,
1626 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001627 }
1628 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1629 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001630 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001631 }
1632
1633 return true;
1634 }
1635
Craig Mautner11bf9a52013-02-19 14:08:51 -08001636
Craig Mautner8849a5e2013-04-02 16:41:03 -07001637 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001638 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautner70a86932013-02-28 22:37:44 -08001639 TaskRecord task = null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001640 TaskRecord rTask = r.task;
1641 final int taskId = rTask.taskId;
1642 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001643 // Last activity in task had been removed or ActivityManagerService is reusing task.
1644 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001645 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001646 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001647 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001648 mTaskHistory.add(rTask);
1649 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001650 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001651 if (!newTask) {
1652 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001653 boolean startIt = true;
1654 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1655 task = mTaskHistory.get(taskNdx);
1656 if (task == r.task) {
1657 // Here it is! Now, if this is not yet visible to the
1658 // user, then just add it without starting; it will
1659 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001660 if (!startIt) {
1661 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1662 + task, new RuntimeException("here").fillInStackTrace());
1663 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001664 r.putInHistory();
Craig Mautner70a86932013-02-28 22:37:44 -08001665 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001666 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation,
1667 r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001668 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001669 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001670 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001672 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 return;
1674 }
1675 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001676 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677 startIt = false;
1678 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 }
1680 }
1681
1682 // Place a new activity at top of stack, so it is next to interact
1683 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 // If we are not placing the new activity frontmost, we do not want
1686 // to deliver the onUserLeaving callback to the actual frontmost
1687 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001688 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001689 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001690 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1691 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001692 }
Craig Mautner70a86932013-02-28 22:37:44 -08001693
1694 task = r.task;
1695
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001696 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001697 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001698 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001699 task.addActivityToTop(r);
1700
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001701 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001703 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 // We want to show the starting preview window if we are
1705 // switching to a new task, or the next activity's process is
1706 // not currently running.
1707 boolean showStartingIcon = newTask;
1708 ProcessRecord proc = r.app;
1709 if (proc == null) {
1710 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1711 }
1712 if (proc == null || proc.thread == null) {
1713 showStartingIcon = true;
1714 }
1715 if (DEBUG_TRANSITION) Slog.v(TAG,
1716 "Prepare open transition: starting " + r);
1717 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001718 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001719 AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001720 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 } else {
1722 mService.mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001723 ? AppTransition.TRANSIT_TASK_OPEN
1724 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 mNoAnimActivities.remove(r);
1726 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001727 r.updateOptionsLocked(options);
Craig Mautner70a86932013-02-28 22:37:44 -08001728 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001729 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001730 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001731 boolean doShow = true;
1732 if (newTask) {
1733 // Even though this activity is starting fresh, we still need
1734 // to reset it to make sure we apply affinities to move any
1735 // existing activities from other tasks in to it.
1736 // If the caller has requested that the target task be
1737 // reset, then do so.
1738 if ((r.intent.getFlags()
1739 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1740 resetTaskIfNeededLocked(r, r);
1741 doShow = topRunningNonDelayedActivityLocked(null) == r;
1742 }
1743 }
1744 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1745 // Figure out if we are transitioning from another activity that is
1746 // "has the same starting icon" as the next one. This allows the
1747 // window manager to keep the previous window it had previously
1748 // created, if it still had one.
1749 ActivityRecord prev = mResumedActivity;
1750 if (prev != null) {
1751 // We don't want to reuse the previous starting preview if:
1752 // (1) The current activity is in a different task.
1753 if (prev.task != r.task) prev = null;
1754 // (2) The current activity is already displayed.
1755 else if (prev.nowVisible) prev = null;
1756 }
1757 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001758 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001759 mService.compatibilityInfoForPackageLocked(
1760 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001761 r.labelRes, r.icon, r.windowFlags,
1762 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001763 }
1764 } else {
1765 // If this is the first activity, don't do any fancy animations,
1766 // because there is nothing for it to animate on top of.
Craig Mautner70a86932013-02-28 22:37:44 -08001767 mService.mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001768 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001769 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001770 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001771 }
1772 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001773 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 }
1775
1776 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001777 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 }
1779 }
1780
Dianne Hackbornbe707852011-11-11 14:32:10 -08001781 final void validateAppTokensLocked() {
1782 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001783 mValidateAppTokens.ensureCapacity(numActivities());
1784 final int numTasks = mTaskHistory.size();
1785 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1786 TaskRecord task = mTaskHistory.get(taskNdx);
1787 final ArrayList<ActivityRecord> activities = task.mActivities;
1788 if (activities.size() == 0) {
1789 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001790 }
Craig Mautner000f0022013-02-26 15:04:29 -08001791 TaskGroup group = new TaskGroup();
1792 group.taskId = task.taskId;
1793 mValidateAppTokens.add(group);
1794 final int numActivities = activities.size();
1795 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1796 final ActivityRecord r = activities.get(activityNdx);
1797 group.tokens.add(r.appToken);
1798 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001799 }
Craig Mautner00af9fe2013-03-25 09:13:41 -07001800 mService.mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001801 }
1802
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803 /**
1804 * Perform a reset of the given task, if needed as part of launching it.
1805 * Returns the new HistoryRecord at the top of the task.
1806 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001807 /**
1808 * Helper method for #resetTaskIfNeededLocked.
1809 * We are inside of the task being reset... we'll either finish this activity, push it out
1810 * for another task, or leave it as-is.
1811 * @param task The task containing the Activity (taskTop) that might be reset.
1812 * @param forceReset
1813 * @return An ActivityOptions that needs to be processed.
1814 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07001815 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task,
Craig Mautnere3a74d52013-02-22 14:14:58 -08001816 boolean forceReset) {
1817 ActivityOptions topOptions = null;
1818
1819 int replyChainEnd = -1;
1820 boolean canMoveOptions = true;
1821
1822 // We only do this for activities that are not the root of the task (since if we finish
1823 // the root, we may no longer have the task!).
1824 final ArrayList<ActivityRecord> activities = task.mActivities;
1825 final int numActivities = activities.size();
1826 for (int i = numActivities - 1; i > 0; --i ) {
1827 ActivityRecord target = activities.get(i);
1828
1829 final int flags = target.info.flags;
1830 final boolean finishOnTaskLaunch =
1831 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1832 final boolean allowTaskReparenting =
1833 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1834 final boolean clearWhenTaskReset =
1835 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1836
1837 if (!finishOnTaskLaunch
1838 && !clearWhenTaskReset
1839 && target.resultTo != null) {
1840 // If this activity is sending a reply to a previous
1841 // activity, we can't do anything with it now until
1842 // we reach the start of the reply chain.
1843 // XXX note that we are assuming the result is always
1844 // to the previous activity, which is almost always
1845 // the case but we really shouldn't count on.
1846 if (replyChainEnd < 0) {
1847 replyChainEnd = i;
1848 }
1849 } else if (!finishOnTaskLaunch
1850 && !clearWhenTaskReset
1851 && allowTaskReparenting
1852 && target.taskAffinity != null
1853 && !target.taskAffinity.equals(task.affinity)) {
1854 // If this activity has an affinity for another
1855 // task, then we need to move it out of here. We will
1856 // move it as far out of the way as possible, to the
1857 // bottom of the activity stack. This also keeps it
1858 // correctly ordered with any activities we previously
1859 // moved.
1860 TaskRecord bottomTask = mTaskHistory.get(0);
1861 ActivityRecord p = bottomTask.mActivities.get(0);
1862 if (target.taskAffinity != null
1863 && target.taskAffinity.equals(p.task.affinity)) {
1864 // If the activity currently at the bottom has the
1865 // same task affinity as the one we are moving,
1866 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001867 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001868 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1869 + " out to bottom task " + p.task);
1870 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001871 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001872 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001873 target.task.affinityIntent = target.intent;
1874 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1875 + " out to new task " + target.task);
1876 }
1877
1878 final TaskRecord targetTask = target.task;
1879 final int targetTaskId = targetTask.taskId;
1880 mService.mWindowManager.setAppGroupId(target.appToken, targetTaskId);
1881
1882 ThumbnailHolder curThumbHolder = target.thumbHolder;
1883 boolean gotOptions = !canMoveOptions;
1884
1885 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1886 for (int srcPos = start; srcPos >= i; --srcPos) {
1887 p = activities.get(srcPos);
1888 if (p.finishing) {
1889 continue;
1890 }
1891
1892 curThumbHolder = p.thumbHolder;
1893 canMoveOptions = false;
1894 if (!gotOptions && topOptions == null) {
1895 topOptions = p.takeOptionsLocked();
1896 if (topOptions != null) {
1897 gotOptions = true;
1898 }
1899 }
1900 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1901 + task + " adding to task=" + targetTask,
1902 new RuntimeException("here").fillInStackTrace());
1903 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1904 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001905 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001906 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001907
Craig Mautnere3a74d52013-02-22 14:14:58 -08001908 mService.mWindowManager.setAppGroupId(p.appToken, targetTaskId);
1909 }
1910
1911 mService.mWindowManager.moveTaskToBottom(targetTaskId);
1912 if (VALIDATE_TOKENS) {
1913 validateAppTokensLocked();
1914 }
1915
1916 replyChainEnd = -1;
1917 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1918 // If the activity should just be removed -- either
1919 // because it asks for it, or the task should be
1920 // cleared -- then finish it and anything that is
1921 // part of its reply chain.
1922 int end;
1923 if (clearWhenTaskReset) {
1924 // In this case, we want to finish this activity
1925 // and everything above it, so be sneaky and pretend
1926 // like these are all in the reply chain.
1927 end = numActivities - 1;
1928 } else if (replyChainEnd < 0) {
1929 end = i;
1930 } else {
1931 end = replyChainEnd;
1932 }
1933 ActivityRecord p = null;
1934 boolean gotOptions = !canMoveOptions;
1935 for (int srcPos = i; srcPos <= end; srcPos++) {
1936 p = activities.get(srcPos);
1937 if (p.finishing) {
1938 continue;
1939 }
1940 canMoveOptions = false;
1941 if (!gotOptions && topOptions == null) {
1942 topOptions = p.takeOptionsLocked();
1943 if (topOptions != null) {
1944 gotOptions = true;
1945 }
1946 }
Craig Mautner58547802013-03-05 08:23:53 -08001947 if (DEBUG_TASKS) Slog.w(TAG,
1948 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001949 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001950 end--;
1951 srcPos--;
1952 }
1953 }
1954 replyChainEnd = -1;
1955 } else {
1956 // If we were in the middle of a chain, well the
1957 // activity that started it all doesn't want anything
1958 // special, so leave it all as-is.
1959 replyChainEnd = -1;
1960 }
1961 }
1962
1963 return topOptions;
1964 }
1965
1966 /**
1967 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1968 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1969 * @param affinityTask The task we are looking for an affinity to.
1970 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1971 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1972 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1973 */
Craig Mautner77878772013-03-04 19:46:24 -08001974 private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
1975 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001976 int replyChainEnd = -1;
1977 final int taskId = task.taskId;
1978 final String taskAffinity = task.affinity;
1979
1980 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1981 final int numActivities = activities.size();
1982 // Do not operate on the root Activity.
1983 for (int i = numActivities - 1; i > 0; --i) {
1984 ActivityRecord target = activities.get(i);
1985
1986 final int flags = target.info.flags;
1987 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1988 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1989
1990 if (target.resultTo != null) {
1991 // If this activity is sending a reply to a previous
1992 // activity, we can't do anything with it now until
1993 // we reach the start of the reply chain.
1994 // XXX note that we are assuming the result is always
1995 // to the previous activity, which is almost always
1996 // the case but we really shouldn't count on.
1997 if (replyChainEnd < 0) {
1998 replyChainEnd = i;
1999 }
2000 } else if (topTaskIsHigher
2001 && allowTaskReparenting
2002 && taskAffinity != null
2003 && taskAffinity.equals(target.taskAffinity)) {
2004 // This activity has an affinity for our task. Either remove it if we are
2005 // clearing or move it over to our task. Note that
2006 // we currently punt on the case where we are resetting a
2007 // task that is not at the top but who has activities above
2008 // with an affinity to it... this is really not a normal
2009 // case, and we will need to later pull that task to the front
2010 // and usually at that point we will do the reset and pick
2011 // up those remaining activities. (This only happens if
2012 // someone starts an activity in a new task from an activity
2013 // in a task that is not currently on top.)
2014 if (forceReset || finishOnTaskLaunch) {
2015 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2016 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2017 for (int srcPos = start; srcPos >= i; --srcPos) {
2018 final ActivityRecord p = activities.get(srcPos);
2019 if (p.finishing) {
2020 continue;
2021 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002022 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002023 }
2024 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002025 if (taskInsertionPoint < 0) {
2026 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002027
Craig Mautner77878772013-03-04 19:46:24 -08002028 }
Craig Mautner77878772013-03-04 19:46:24 -08002029
2030 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2031 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2032 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2033 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002034 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002035 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002036 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002037
Craig Mautnere3a74d52013-02-22 14:14:58 -08002038 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2039 + " to stack at " + task,
2040 new RuntimeException("here").fillInStackTrace());
2041 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2042 + " in to resetting task " + task);
2043 mService.mWindowManager.setAppGroupId(p.appToken, taskId);
2044 }
2045 mService.mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002046 if (VALIDATE_TOKENS) {
2047 validateAppTokensLocked();
2048 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002049
2050 // Now we've moved it in to place... but what if this is
2051 // a singleTop activity and we have put it on top of another
2052 // instance of the same activity? Then we drop the instance
2053 // below so it remains singleTop.
2054 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2055 ArrayList<ActivityRecord> taskActivities = task.mActivities;
2056 boolean found = false;
2057 int targetNdx = taskActivities.indexOf(target);
2058 if (targetNdx > 0) {
2059 ActivityRecord p = taskActivities.get(targetNdx - 1);
2060 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002061 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2062 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002063 }
2064 }
2065 }
2066 }
2067
2068 replyChainEnd = -1;
2069 }
2070 }
Craig Mautner77878772013-03-04 19:46:24 -08002071 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002072 }
2073
Craig Mautner8849a5e2013-04-02 16:41:03 -07002074 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002075 ActivityRecord newActivity) {
2076 boolean forceReset =
2077 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2078 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2079 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2080 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2081 forceReset = true;
2082 }
2083 }
2084
2085 final TaskRecord task = taskTop.task;
2086
2087 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2088 * for remaining tasks. Used for later tasks to reparent to task. */
2089 boolean taskFound = false;
2090
2091 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2092 ActivityOptions topOptions = null;
2093
Craig Mautner77878772013-03-04 19:46:24 -08002094 // Preserve the location for reparenting in the new task.
2095 int reparentInsertionPoint = -1;
2096
Craig Mautnere3a74d52013-02-22 14:14:58 -08002097 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2098 final TaskRecord targetTask = mTaskHistory.get(i);
2099
2100 if (targetTask == task) {
2101 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2102 taskFound = true;
2103 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002104 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2105 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002106 }
2107 }
2108
Craig Mautner70a86932013-02-28 22:37:44 -08002109 int taskNdx = mTaskHistory.indexOf(task);
2110 do {
2111 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2112 } while (taskTop == null && taskNdx >= 0);
2113
Craig Mautnere3a74d52013-02-22 14:14:58 -08002114 if (topOptions != null) {
2115 // If we got some ActivityOptions from an activity on top that
2116 // was removed from the task, propagate them to the new real top.
2117 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002118 taskTop.updateOptionsLocked(topOptions);
2119 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002120 topOptions.abort();
2121 }
2122 }
2123
2124 return taskTop;
2125 }
2126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 * Find the activity in the history stack within the given task. Returns
2129 * the index within the history at which it's found, or < 0 if not found.
2130 */
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r, TaskRecord task) {
Craig Mautner56f52db2013-02-25 10:03:01 -08002132 final ComponentName realActivity = r.realActivity;
2133 ArrayList<ActivityRecord> activities = task.mActivities;
2134 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2135 ActivityRecord candidate = activities.get(activityNdx);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002136 if (candidate.finishing) {
2137 continue;
2138 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002139 if (candidate.realActivity.equals(realActivity)) {
2140 return candidate;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141 }
2142 }
Craig Mautner56f52db2013-02-25 10:03:01 -08002143 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002144 }
2145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
2147 long thisTime, long totalTime) {
2148 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
2149 WaitResult w = mWaitingActivityLaunched.get(i);
2150 w.timeout = timeout;
2151 if (r != null) {
2152 w.who = new ComponentName(r.info.packageName, r.info.name);
2153 }
2154 w.thisTime = thisTime;
2155 w.totalTime = totalTime;
2156 }
2157 mService.notifyAll();
2158 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 void reportActivityVisibleLocked(ActivityRecord r) {
2161 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
2162 WaitResult w = mWaitingActivityVisible.get(i);
2163 w.timeout = false;
2164 if (r != null) {
2165 w.who = new ComponentName(r.info.packageName, r.info.name);
2166 }
2167 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
2168 w.thisTime = w.totalTime;
2169 }
2170 mService.notifyAll();
Craig Mautner27084302013-03-25 08:05:25 -07002171 mStackSupervisor.dismissKeyguard();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 }
2173
2174 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2175 String resultWho, int requestCode, int resultCode, Intent data) {
2176
2177 if (callingUid > 0) {
2178 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002179 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 }
2181
2182 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2183 + " : who=" + resultWho + " req=" + requestCode
2184 + " res=" + resultCode + " data=" + data);
2185 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2186 try {
2187 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2188 list.add(new ResultInfo(resultWho, requestCode,
2189 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002190 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002191 return;
2192 } catch (Exception e) {
2193 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2194 }
2195 }
2196
2197 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2198 }
2199
2200 private final void stopActivityLocked(ActivityRecord r) {
2201 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2202 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2203 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2204 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002205 if (!mService.mSleeping) {
2206 if (DEBUG_STATES) {
2207 Slog.d(TAG, "no-history finish of " + r);
2208 }
2209 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002210 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002211 } else {
2212 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2213 + " on stop because we're just sleeping");
2214 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002215 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002216 }
2217
2218 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002219 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002220 if (mService.mFocusedActivity == r) {
2221 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2222 }
2223 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002224 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225 try {
2226 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002227 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2228 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 r.state = ActivityState.STOPPING;
2230 if (DEBUG_VISBILITY) Slog.v(
2231 TAG, "Stopping visible=" + r.visible + " for " + r);
2232 if (!r.visible) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002233 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002235 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002236 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002237 r.setSleeping(true);
2238 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002239 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
2240 msg.obj = r;
2241 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002242 } catch (Exception e) {
2243 // Maybe just ignore exceptions here... if the process
2244 // has crashed, our death notification will clean things
2245 // up.
2246 Slog.w(TAG, "Exception thrown during pause", e);
2247 // Just in case, assume it to be stopped.
2248 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002249 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002250 r.state = ActivityState.STOPPED;
2251 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002252 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002253 }
2254 }
2255 }
2256 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002257
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002258 final void scheduleIdleLocked() {
2259 Message msg = Message.obtain();
2260 msg.what = IDLE_NOW_MSG;
2261 mHandler.sendMessage(msg);
2262 }
2263
Craig Mautnere79d42682013-04-01 19:01:53 -07002264 // Checked.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002265 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002266 Configuration config) {
2267 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2268
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002269 ActivityRecord res = null;
2270
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002271 ArrayList<ActivityRecord> stops = null;
2272 ArrayList<ActivityRecord> finishes = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002273 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 int NS = 0;
2275 int NF = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 IApplicationThread sendThumbnail = null;
2277 boolean booting = false;
2278 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002279 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002280
Craig Mautnere79d42682013-04-01 19:01:53 -07002281 ActivityRecord r = ActivityRecord.forToken(token);
2282 if (r != null) {
2283 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2284 r.finishLaunchTickingLocked();
2285 }
2286
2287 // Get the activity record.
2288 if (isInStackLocked(token) != null) {
2289 res = r;
2290
2291 if (fromTimeout) {
2292 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 }
2294
Craig Mautnere79d42682013-04-01 19:01:53 -07002295 // This is a hack to semi-deal with a race condition
2296 // in the client where it can be constructed with a
2297 // newer configuration from when we asked it to launch.
2298 // We'll update with whatever configuration it now says
2299 // it used to launch.
2300 if (config != null) {
2301 r.configuration = config;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002302 }
2303
Craig Mautnere79d42682013-04-01 19:01:53 -07002304 // No longer need to keep the device awake.
2305 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
2306 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2307 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002308 }
2309
Craig Mautnere79d42682013-04-01 19:01:53 -07002310 // We are now idle. If someone is waiting for a thumbnail from
2311 // us, we can now deliver.
2312 r.idle = true;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002313 if (mStackSupervisor.allResumedActivitiesIdle()) {
2314 mService.scheduleAppGcsLocked();
2315 }
Craig Mautnere79d42682013-04-01 19:01:53 -07002316 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
2317 sendThumbnail = r.app.thread;
2318 r.thumbnailNeeded = false;
2319 }
2320
2321 // If this activity is fullscreen, set up to hide those under it.
2322
2323 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
2324 ensureActivitiesVisibleLocked(null, 0);
2325
2326 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002327 if (!mService.mBooted && mStackSupervisor.isFrontStack(this)) {
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002328 mService.mBooted = true;
2329 enableScreen = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 }
Craig Mautnere79d42682013-04-01 19:01:53 -07002331 } else if (fromTimeout) {
2332 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
2333 }
2334
2335 // Atomically retrieve all of the other things to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002336 stops = mStackSupervisor.processStoppingActivitiesLocked(true);
Craig Mautnere79d42682013-04-01 19:01:53 -07002337 NS = stops != null ? stops.size() : 0;
2338 if ((NF=mFinishingActivities.size()) > 0) {
2339 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2340 mFinishingActivities.clear();
2341 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002342
2343 final ArrayList<ActivityRecord> thumbnails;
2344 final int NT = mCancelledThumbnails.size();
2345 if (NT > 0) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002346 thumbnails = new ArrayList<ActivityRecord>(mCancelledThumbnails);
2347 mCancelledThumbnails.clear();
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002348 } else {
2349 thumbnails = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07002350 }
2351
Craig Mautnerde4ef022013-04-07 19:01:33 -07002352 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002353 booting = mService.mBooting;
2354 mService.mBooting = false;
2355 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002356
Craig Mautnere79d42682013-04-01 19:01:53 -07002357 if (mStartingUsers.size() > 0) {
2358 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2359 mStartingUsers.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 }
2361
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002362 // Perform the following actions from unsynchronized state.
2363 final IApplicationThread thumbnailThread = sendThumbnail;
2364 mHandler.post(new Runnable() {
2365 @Override
2366 public void run() {
2367 if (thumbnailThread != null) {
2368 try {
2369 thumbnailThread.requestThumbnail(token);
2370 } catch (Exception e) {
2371 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
2372 mService.sendPendingThumbnail(null, token, null, null, true);
2373 }
2374 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002376 // Report back to any thumbnail receivers.
2377 for (int i = 0; i < NT; i++) {
2378 ActivityRecord r = thumbnails.get(i);
2379 mService.sendPendingThumbnail(r, null, null, null, true);
2380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002382 });
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383
2384 // Stop any activities that are scheduled to do so but have been
2385 // waiting for the next one to start.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002386 for (int i = 0; i < NS; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002387 r = stops.get(i);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002388 if (r.finishing) {
2389 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2390 } else {
2391 stopActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002392 }
2393 }
2394
2395 // Finish any activities that are scheduled to do so but have been
2396 // waiting for the next one to start.
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002397 for (int i = 0; i < NF; i++) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002398 r = finishes.get(i);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002399 activityRemoved |= destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002400 }
2401
2402 if (booting) {
2403 mService.finishBooting();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002404 } else if (startingUsers != null) {
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002405 for (int i = 0; i < startingUsers.size(); i++) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002406 mService.finishUserSwitch(startingUsers.get(i));
2407 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002408 }
2409
2410 mService.trimApplications();
2411 //dump();
2412 //mWindowManager.dump();
2413
2414 if (enableScreen) {
2415 mService.enableScreenAfterBoot();
2416 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002417
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002418 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002419 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002420 }
2421
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002422 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002423 }
2424
2425 /**
2426 * @return Returns true if the activity is being finished, false if for
2427 * some reason it is being left as-is.
2428 */
2429 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002430 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002431 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002432 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002433 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002434 + ", result=" + resultCode + ", data=" + resultData
2435 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002436 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002437 return false;
2438 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439
Craig Mautnerd44711d2013-02-23 11:24:36 -08002440 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 return true;
2442 }
2443
Craig Mautnerd2328952013-03-05 12:46:26 -08002444 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2446 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2447 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2448 ActivityRecord r = activities.get(activityNdx);
2449 if (r.resultTo == self && r.requestCode == requestCode) {
2450 if ((r.resultWho == null && resultWho == null) ||
2451 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2452 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2453 false);
2454 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002455 }
2456 }
2457 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002458 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002459 }
2460
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002461 final void finishTopRunningActivityLocked(ProcessRecord app) {
2462 ActivityRecord r = topRunningActivityLocked(null);
2463 if (r != null && r.app == app) {
2464 // If the top running activity is from this crashing
2465 // process, then terminate it to avoid getting in a loop.
2466 Slog.w(TAG, " Force finishing activity "
2467 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002468 int taskNdx = mTaskHistory.indexOf(r.task);
2469 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002470 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002471 // Also terminate any activities below it that aren't yet
2472 // stopped, to avoid a situation where one will get
2473 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002474 --activityNdx;
2475 if (activityNdx < 0) {
2476 do {
2477 --taskNdx;
2478 if (taskNdx < 0) {
2479 break;
2480 }
2481 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2482 } while (activityNdx < 0);
2483 }
2484 if (activityNdx >= 0) {
2485 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002486 if (r.state == ActivityState.RESUMED
2487 || r.state == ActivityState.PAUSING
2488 || r.state == ActivityState.PAUSED) {
2489 if (!r.isHomeActivity || mService.mHomeProcess != r.app) {
2490 Slog.w(TAG, " Force finishing activity "
2491 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002492 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002493 }
2494 }
2495 }
2496 }
2497 }
2498
Craig Mautnerd2328952013-03-05 12:46:26 -08002499 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002500 ArrayList<ActivityRecord> activities = r.task.mActivities;
2501 for (int index = activities.indexOf(r); index >= 0; --index) {
2502 ActivityRecord cur = activities.get(index);
2503 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002504 break;
2505 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002506 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002507 }
2508 return true;
2509 }
2510
Dianne Hackborn5c607432012-02-28 14:44:19 -08002511 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2512 // send the result
2513 ActivityRecord resultTo = r.resultTo;
2514 if (resultTo != null) {
2515 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2516 + " who=" + r.resultWho + " req=" + r.requestCode
2517 + " res=" + resultCode + " data=" + resultData);
2518 if (r.info.applicationInfo.uid > 0) {
2519 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2520 resultTo.packageName, resultData,
2521 resultTo.getUriPermissionsLocked());
2522 }
2523 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2524 resultData);
2525 r.resultTo = null;
2526 }
2527 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2528
2529 // Make sure this HistoryRecord is not holding on to other resources,
2530 // because clients have remote IPC references to this object so we
2531 // can't assume that will go away and want to avoid circular IPC refs.
2532 r.results = null;
2533 r.pendingResults = null;
2534 r.newIntents = null;
2535 r.icicle = null;
2536 }
2537
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 /**
2539 * @return Returns true if this activity has been removed from the history
2540 * list, or false if it is still in the list and will be removed later.
2541 */
Craig Mautnerd44711d2013-02-23 11:24:36 -08002542 final boolean finishActivityLocked(ActivityRecord r, int resultCode,
2543 Intent resultData, String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 if (r.finishing) {
2545 Slog.w(TAG, "Duplicate finish request for " + r);
2546 return false;
2547 }
2548
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002549 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002551 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002552 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002553 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2554 final int index = activities.indexOf(r);
2555 if (index < (activities.size() - 1)) {
2556 ActivityRecord next = activities.get(index+1);
2557 if (r.frontOfTask) {
2558 // The next activity is now the front of the task.
2559 next.frontOfTask = true;
2560 }
2561 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2562 // If the caller asked that this activity (and all above it)
2563 // be cleared when the task is reset, don't lose that information,
2564 // but propagate it up to the next activity.
2565 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002566 }
2567 }
2568
2569 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002570 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002571 if (mService.mFocusedActivity == r) {
2572 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2573 }
2574 }
2575
Dianne Hackborn5c607432012-02-28 14:44:19 -08002576 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002577
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 if (mService.mPendingThumbnails.size() > 0) {
2579 // There are clients waiting to receive thumbnails so, in case
2580 // this is an activity that someone is waiting for, add it
2581 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08002582 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002583 }
2584
Craig Mautnerde4ef022013-04-07 19:01:33 -07002585 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002586 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002587 if (DEBUG_TRANSITION) Slog.v(TAG,
2588 "Prepare close transition: finishing " + r);
2589 mService.mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002590 ? AppTransition.TRANSIT_TASK_CLOSE
2591 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002592
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 // Tell window manager to prepare for this one to be removed.
Dianne Hackbornbe707852011-11-11 14:32:10 -08002594 mService.mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002595
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002596 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2598 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2599 startPausingLocked(false, false);
2600 }
2601
2602 } else if (r.state != ActivityState.PAUSING) {
2603 // If the activity is PAUSING, we will complete the finish once
2604 // it is done pausing; else we can just directly finish it here.
2605 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002606 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 } else {
2608 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2609 }
2610
2611 return false;
2612 }
2613
2614 private static final int FINISH_IMMEDIATELY = 0;
2615 private static final int FINISH_AFTER_PAUSE = 1;
2616 private static final int FINISH_AFTER_VISIBLE = 2;
2617
2618 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002619 int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002620 // First things first: if this activity is currently visible,
2621 // and the resumed activity is not yet visible, then hold off on
2622 // finishing until the resumed one becomes visible.
2623 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002624 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2625 mStackSupervisor.mStoppingActivities.add(r);
2626 if (mStackSupervisor.mStoppingActivities.size() > 3) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002627 // If we already have a few activities waiting to stop,
2628 // then give up on things going idle and start clearing
2629 // them out.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07002630 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002631 } else {
2632 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 }
2634 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002635 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2636 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002637 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002638 if (oomAdj) {
2639 mService.updateOomAdjLocked();
2640 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 return r;
2642 }
2643
2644 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002645 mStackSupervisor.mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002646 mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002647 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002648 if (mResumedActivity == r) {
2649 mResumedActivity = null;
2650 }
2651 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002652 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 r.state = ActivityState.FINISHING;
2654
2655 if (mode == FINISH_IMMEDIATELY
2656 || prevState == ActivityState.STOPPED
2657 || prevState == ActivityState.INITIALIZING) {
2658 // If this activity is already stopped, we can just finish
2659 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002660 boolean activityRemoved = destroyActivityLocked(r, true,
2661 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002662 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002663 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002664 }
2665 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002667
2668 // Need to go through the full pause cycle to get this
2669 // activity into the stopped state and then finish it.
2670 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
2671 mFinishingActivities.add(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002672 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002673 return r;
2674 }
2675
Craig Mautnerd2328952013-03-05 12:46:26 -08002676 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002677 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002678 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002679 final TaskRecord task = srec.task;
2680 final ArrayList<ActivityRecord> activities = task.mActivities;
2681 final int start = activities.indexOf(srec);
2682 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002683 return false;
2684 }
2685 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002686 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002687 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002688 final ComponentName dest = destIntent.getComponent();
2689 if (start > 0 && dest != null) {
2690 for (int i = finishTo; i >= 0; i--) {
2691 ActivityRecord r = activities.get(i);
2692 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002693 r.info.name.equals(dest.getClassName())) {
2694 finishTo = i;
2695 parent = r;
2696 foundParentInTask = true;
2697 break;
2698 }
2699 }
2700 }
2701
2702 IActivityController controller = mService.mController;
2703 if (controller != null) {
2704 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2705 if (next != null) {
2706 // ask watcher if this is allowed
2707 boolean resumeOK = true;
2708 try {
2709 resumeOK = controller.activityResuming(next.packageName);
2710 } catch (RemoteException e) {
2711 mService.mController = null;
2712 }
2713
2714 if (!resumeOK) {
2715 return false;
2716 }
2717 }
2718 }
2719 final long origId = Binder.clearCallingIdentity();
2720 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002721 ActivityRecord r = activities.get(i);
2722 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002723 // Only return the supplied result for the first activity finished
2724 resultCode = Activity.RESULT_CANCELED;
2725 resultData = null;
2726 }
2727
2728 if (parent != null && foundParentInTask) {
2729 final int parentLaunchMode = parent.info.launchMode;
2730 final int destIntentFlags = destIntent.getFlags();
2731 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2732 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2733 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2734 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2735 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2736 } else {
2737 try {
2738 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2739 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002740 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002741 null, aInfo, parent.appToken, null,
2742 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2743 0, null, true, null);
2744 foundParentInTask = res == ActivityManager.START_SUCCESS;
2745 } catch (RemoteException e) {
2746 foundParentInTask = false;
2747 }
2748 requestFinishActivityLocked(parent.appToken, resultCode,
2749 resultData, "navigate-up", true);
2750 }
2751 }
2752 Binder.restoreCallingIdentity(origId);
2753 return foundParentInTask;
2754 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 /**
2756 * Perform the common clean-up of an activity record. This is called both
2757 * as part of destroyActivityLocked() (when destroying the client-side
2758 * representation) and cleaning things up as a result of its hosting
2759 * processing going away, in which case there is no remaining client-side
2760 * state to destroy so only the cleanup here is needed.
2761 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002762 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2763 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 if (mResumedActivity == r) {
2765 mResumedActivity = null;
2766 }
2767 if (mService.mFocusedActivity == r) {
2768 mService.mFocusedActivity = null;
2769 }
2770
2771 r.configDestroy = false;
2772 r.frozenBeforeDestroy = false;
2773
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002774 if (setState) {
2775 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2776 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002777 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002778 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002779 }
2780
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002781 // Make sure this record is no longer in the pending finishes list.
2782 // This could happen, for example, if we are trimming activities
2783 // down to the max limit while they are still waiting to finish.
2784 mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002785 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002786
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002787 // Remove any pending results.
2788 if (r.finishing && r.pendingResults != null) {
2789 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2790 PendingIntentRecord rec = apr.get();
2791 if (rec != null) {
2792 mService.cancelIntentSenderLocked(rec, false);
2793 }
2794 }
2795 r.pendingResults = null;
2796 }
2797
2798 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002799 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800 }
2801
2802 if (mService.mPendingThumbnails.size() > 0) {
2803 // There are clients waiting to receive thumbnails so, in case
2804 // this is an activity that someone is waiting for, add it
2805 // to the pending list so we can correctly update the clients.
Craig Mautnercae015f2013-02-08 14:31:27 -08002806 mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 }
2808
2809 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002810 removeTimeoutsForActivityLocked(r);
2811 }
2812
2813 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002815 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002816 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002817 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002818 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 }
2820
Dianne Hackborn5c607432012-02-28 14:44:19 -08002821 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002822 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2823 r.makeFinishing();
2824 if (DEBUG_ADD_REMOVE) {
2825 RuntimeException here = new RuntimeException("here");
2826 here.fillInStackTrace();
2827 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002829 final TaskRecord task = r.task;
2830 if (task != null && task.removeActivity(r)) {
2831 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002832 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002833 r.takeFromHistory();
2834 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002835 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002836 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002837 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002838 r.app = null;
2839 mService.mWindowManager.removeAppToken(r.appToken);
2840 if (VALIDATE_TOKENS) {
2841 validateAppTokensLocked();
2842 }
2843 cleanUpActivityServicesLocked(r);
2844 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002846
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 /**
2848 * Perform clean-up of service connections in an activity record.
2849 */
2850 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2851 // Throw away any services that have been bound by this activity.
2852 if (r.connections != null) {
2853 Iterator<ConnectionRecord> it = r.connections.iterator();
2854 while (it.hasNext()) {
2855 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002856 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002857 }
2858 r.connections = null;
2859 }
2860 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002861
2862 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2863 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2864 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2865 mHandler.sendMessage(msg);
2866 }
2867
Dianne Hackborn28695e02011-11-02 21:59:51 -07002868 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002869 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002870 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002871 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2872 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2873 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2874 final ActivityRecord r = activities.get(activityNdx);
2875 if (r.finishing) {
2876 continue;
2877 }
2878 if (r.fullscreen) {
2879 lastIsOpaque = true;
2880 }
2881 if (owner != null && r.app != owner) {
2882 continue;
2883 }
2884 if (!lastIsOpaque) {
2885 continue;
2886 }
2887 // We can destroy this one if we have its icicle saved and
2888 // it is not in the process of pausing/stopping/finishing.
2889 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2890 && r.haveState && !r.visible && r.stopped
2891 && r.state != ActivityState.DESTROYING
2892 && r.state != ActivityState.DESTROYED) {
2893 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2894 + " resumed=" + mResumedActivity
2895 + " pausing=" + mPausingActivity);
2896 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2897 activityRemoved = true;
2898 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002899 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002900 }
2901 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002902 if (activityRemoved) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002903 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002904 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002905 }
2906
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002907 /**
2908 * Destroy the current CLIENT SIDE instance of an activity. This may be
2909 * called both when actually finishing an activity, or when performing
2910 * a configuration switch where we destroy the current client-side object
2911 * but then create a new client-side object for this same HistoryRecord.
2912 */
2913 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002914 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002915 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002916 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2918 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002919 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002920 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002921
2922 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002923
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002924 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925
2926 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002927
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002928 if (hadApp) {
2929 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002930 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2932 mService.mHeavyWeightProcess = null;
2933 mService.mHandler.sendEmptyMessage(
2934 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2935 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002937 // No longer have activities, so update oom adj.
2938 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002939 }
2940 }
2941
2942 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002943
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002944 try {
2945 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002946 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 r.configChangeFlags);
2948 } catch (Exception e) {
2949 // We can just ignore exceptions here... if the process
2950 // has crashed, our death notification will clean things
2951 // up.
2952 //Slog.w(TAG, "Exception thrown during finish", e);
2953 if (r.finishing) {
2954 removeActivityFromHistoryLocked(r);
2955 removedFromHistory = true;
2956 skipDestroy = true;
2957 }
2958 }
2959
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002961
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002962 // If the activity is finishing, we need to wait on removing it
2963 // from the list to give it a chance to do its cleanup. During
2964 // that time it may make calls back with its token so we need to
2965 // be able to find it on the list and so we don't want to remove
2966 // it from the list yet. Otherwise, we can just immediately put
2967 // it in the destroyed state since we are not removing it from the
2968 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002970 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2971 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 r.state = ActivityState.DESTROYING;
2973 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
2974 msg.obj = r;
2975 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2976 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002977 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2978 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002980 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002981 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002982 }
2983 } else {
2984 // remove this record from the history.
2985 if (r.finishing) {
2986 removeActivityFromHistoryLocked(r);
2987 removedFromHistory = true;
2988 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002989 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
2990 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002992 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002993 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 }
2995 }
2996
2997 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002998
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002999 if (!mLRUActivities.remove(r) && hadApp) {
3000 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3001 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003002
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 return removedFromHistory;
3004 }
3005
Craig Mautnerd2328952013-03-05 12:46:26 -08003006 final void activityDestroyedLocked(IBinder token) {
3007 final long origId = Binder.clearCallingIdentity();
3008 try {
3009 ActivityRecord r = ActivityRecord.forToken(token);
3010 if (r != null) {
3011 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003012 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003013
3014 if (isInStackLocked(token) != null) {
3015 if (r.state == ActivityState.DESTROYING) {
3016 cleanUpActivityLocked(r, true, false);
3017 removeActivityFromHistoryLocked(r);
3018 }
3019 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003020 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Craig Mautnerd2328952013-03-05 12:46:26 -08003021 } finally {
3022 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 }
3024 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003025
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003026 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3027 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003028 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003029 if (DEBUG_CLEANUP) Slog.v(
3030 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031 + " with " + i + " entries");
3032 while (i > 0) {
3033 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003034 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003035 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003037 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003039 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040 }
3041 }
3042 }
3043
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003044 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3045 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003046 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3047 "mStoppingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003048 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003049 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003050 "mWaitingVisibleActivities");
3051 removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities");
3052
3053 boolean hasVisibleActivities = false;
3054
3055 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003056 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003057 if (DEBUG_CLEANUP) Slog.v(
3058 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003059 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3060 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3061 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3062 final ActivityRecord r = activities.get(activityNdx);
3063 --i;
3064 if (DEBUG_CLEANUP) Slog.v(
3065 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3066 if (r.app == app) {
3067 boolean remove;
3068 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3069 // Don't currently have state for the activity, or
3070 // it is finishing -- always remove it.
3071 remove = true;
3072 } else if (r.launchCount > 2 &&
3073 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3074 // We have launched this activity too many times since it was
3075 // able to run, so give up and remove it.
3076 remove = true;
3077 } else {
3078 // The process may be gone, but the activity lives on!
3079 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003080 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003081 if (remove) {
3082 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
3083 RuntimeException here = new RuntimeException("here");
3084 here.fillInStackTrace();
3085 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3086 + ": haveState=" + r.haveState
3087 + " stateNotNeeded=" + r.stateNotNeeded
3088 + " finishing=" + r.finishing
3089 + " state=" + r.state, here);
3090 }
3091 if (!r.finishing) {
3092 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3093 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3094 r.userId, System.identityHashCode(r),
3095 r.task.taskId, r.shortComponentName,
3096 "proc died without state saved");
3097 }
3098 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003099
Craig Mautner0247fc82013-02-28 14:32:06 -08003100 } else {
3101 // We have the current state for this activity, so
3102 // it can be restarted later when needed.
3103 if (localLOGV) Slog.v(
3104 TAG, "Keeping entry, setting app to null");
3105 if (r.visible) {
3106 hasVisibleActivities = true;
3107 }
3108 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3109 + r);
3110 r.app = null;
3111 r.nowVisible = false;
3112 if (!r.haveState) {
3113 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
3114 "App died, clearing saved state of " + r);
3115 r.icicle = null;
3116 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003117 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003118
Craig Mautnerd2328952013-03-05 12:46:26 -08003119 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003120 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003121 }
3122 }
3123
3124 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003126
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003127 final void updateTransitLocked(int transit, Bundle options) {
3128 if (options != null) {
3129 ActivityRecord r = topRunningActivityLocked(null);
3130 if (r != null && r.state != ActivityState.RESUMED) {
3131 r.updateOptionsLocked(options);
3132 } else {
3133 ActivityOptions.abort(options);
3134 }
3135 }
3136 mService.mWindowManager.prepareAppTransition(transit, false);
3137 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003138
Craig Mautnercae015f2013-02-08 14:31:27 -08003139 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003140 final TaskRecord task = taskForIdLocked(taskId);
3141 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003142 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003143 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003144 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003145 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3146 // Caller wants the home activity moved with it. To accomplish this,
3147 // we'll just move the home task to the top first.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003148 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003149 }
3150 moveTaskToFrontLocked(task, null, options);
3151 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003152 }
3153 return false;
3154 }
3155
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003156 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003157 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158
Craig Mautner11bf9a52013-02-19 14:08:51 -08003159 final int numTasks = mTaskHistory.size();
3160 final int index = mTaskHistory.indexOf(tr);
3161 if (numTasks == 0 || index < 0 || index == numTasks - 1) {
3162 // nothing to do!
3163 if (reason != null &&
3164 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3165 ActivityOptions.abort(options);
3166 } else {
3167 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3168 }
3169 return;
3170 }
3171
3172 // Shift all activities with this task up to the top
3173 // of the stack, keeping them in the same internal order.
3174 mTaskHistory.remove(tr);
3175 mTaskHistory.add(tr);
3176
3177 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003178 if (reason != null &&
3179 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003180 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 ActivityRecord r = topRunningActivityLocked(null);
3182 if (r != null) {
3183 mNoAnimActivities.add(r);
3184 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003185 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003186 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003187 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003189
Craig Mautner58547802013-03-05 08:23:53 -08003190 mService.mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191
Craig Mautnerde4ef022013-04-07 19:01:33 -07003192 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Craig Mautner58547802013-03-05 08:23:53 -08003193 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003194
3195 if (VALIDATE_TOKENS) {
3196 validateAppTokensLocked();
3197 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003198 }
3199
3200 /**
3201 * Worker method for rearranging history stack. Implements the function of moving all
3202 * activities for a specific task (gathering them if disjoint) into a single group at the
3203 * bottom of the stack.
3204 *
3205 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3206 * to premeptively cancel the move.
3207 *
3208 * @param task The taskId to collect and move to the bottom.
3209 * @return Returns true if the move completed, false if not.
3210 */
3211 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3212 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003213
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003214 // If we have a watcher, preflight the move before committing to it. First check
3215 // for *other* available tasks, but if none are available, then try again allowing the
3216 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003217 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 ActivityRecord next = topRunningActivityLocked(null, task);
3219 if (next == null) {
3220 next = topRunningActivityLocked(null, 0);
3221 }
3222 if (next != null) {
3223 // ask watcher if this is allowed
3224 boolean moveOK = true;
3225 try {
3226 moveOK = mService.mController.activityResuming(next.packageName);
3227 } catch (RemoteException e) {
3228 mService.mController = null;
3229 }
3230 if (!moveOK) {
3231 return false;
3232 }
3233 }
3234 }
3235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236 if (DEBUG_TRANSITION) Slog.v(TAG,
3237 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003238
Craig Mautnerd2328952013-03-05 12:46:26 -08003239 final TaskRecord tr = taskForIdLocked(task);
3240 if (tr == null) {
3241 return false;
3242 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003243 mTaskHistory.remove(tr);
3244 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245
3246 if (reason != null &&
3247 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003248 mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 ActivityRecord r = topRunningActivityLocked(null);
3250 if (r != null) {
3251 mNoAnimActivities.add(r);
3252 }
3253 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08003254 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08003255 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003257 mService.mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003258
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003259 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003260 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261 }
3262
Craig Mautnerde4ef022013-04-07 19:01:33 -07003263 if (mResumedActivity != null && mResumedActivity.task == tr &&
3264 mResumedActivity.mLaunchHomeTaskNext) {
3265 mResumedActivity.mLaunchHomeTaskNext = false;
3266 return mService.startHomeActivityLocked(mCurrentUser);
3267 }
3268
3269 mStackSupervisor.getTopStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270 return true;
3271 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003272
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003273 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
Craig Mautner16030772013-02-26 16:05:47 -08003274 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003275 if (mResumedActivity != null && mResumedActivity.thumbHolder == tr) {
3276 info.mainThumbnail = screenshotActivities(mResumedActivity);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003277 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07003278 if (info.mainThumbnail == null) {
3279 info.mainThumbnail = tr.lastThumbnail;
3280 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003281 return info;
3282 }
3283
Dianne Hackborn15491c62012-09-19 10:59:14 -07003284 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003285 if (mResumedActivity != null && mResumedActivity.task == tr) {
Dianne Hackborn15491c62012-09-19 10:59:14 -07003286 // This task is the current resumed task, we just need to take
3287 // a screenshot of it and return that.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003288 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003289 }
3290 // Return the information about the task, to figure out the top
3291 // thumbnail to return.
Craig Mautner16030772013-02-26 16:05:47 -08003292 TaskAccessInfo info = getTaskAccessInfoLocked(tr, true);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003293 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07003294 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003295 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003296 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003297 }
3298
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003299 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
3300 boolean taskRequired) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003301 final TaskRecord task = taskForIdLocked(taskId);
3302 if (task == null) {
3303 return null;
3304 }
Craig Mautner16030772013-02-26 16:05:47 -08003305 TaskAccessInfo info = getTaskAccessInfoLocked(task, false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003306 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003307 if (taskRequired) {
3308 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
3309 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003310 return null;
3311 }
3312
3313 if (subTaskIndex < 0) {
3314 // Just remove the entire task.
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003315 task.performClearTaskAtIndexLocked(info.rootIndex);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003316 return info.root;
3317 }
3318
3319 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07003320 if (taskRequired) {
3321 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
3322 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003323 return null;
3324 }
3325
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003326 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003327 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07003328 task.performClearTaskAtIndexLocked(subtask.index);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003329 return subtask.activity;
3330 }
3331
Craig Mautner16030772013-02-26 16:05:47 -08003332 public TaskAccessInfo getTaskAccessInfoLocked(TaskRecord task, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003333 final TaskAccessInfo thumbs = new TaskAccessInfo();
3334 // How many different sub-thumbnails?
Craig Mautner16030772013-02-26 16:05:47 -08003335 final ArrayList<ActivityRecord> activities = task.mActivities;
3336 final int NA = activities.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003337 int j = 0;
3338 ThumbnailHolder holder = null;
3339 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003340 ActivityRecord ar = activities.get(j);
3341 if (!ar.finishing) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07003342 thumbs.root = ar;
3343 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003344 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07003345 if (holder != null) {
3346 thumbs.mainThumbnail = holder.lastThumbnail;
3347 }
3348 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003349 break;
3350 }
3351 j++;
3352 }
3353
3354 if (j >= NA) {
3355 return thumbs;
3356 }
3357
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003358 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
3359 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003360 while (j < NA) {
Craig Mautner16030772013-02-26 16:05:47 -08003361 ActivityRecord ar = activities.get(j);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003362 j++;
3363 if (ar.finishing) {
3364 continue;
3365 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003366 if (ar.thumbHolder != holder && holder != null) {
3367 thumbs.numSubThumbbails++;
3368 holder = ar.thumbHolder;
3369 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07003370 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003371 sub.activity = ar;
3372 sub.index = j-1;
3373 subtasks.add(sub);
3374 }
3375 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003376 if (thumbs.numSubThumbbails > 0) {
3377 thumbs.retriever = new IThumbnailRetriever.Stub() {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003378 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003379 public Bitmap getThumbnail(int index) {
3380 if (index < 0 || index >= thumbs.subtasks.size()) {
3381 return null;
3382 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003383 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003384 if (mResumedActivity != null && mResumedActivity.thumbHolder == sub.holder) {
3385 return screenshotActivities(mResumedActivity);
Dianne Hackborn15491c62012-09-19 10:59:14 -07003386 }
3387 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003388 }
3389 };
3390 }
3391 return thumbs;
3392 }
3393
Craig Mautner8849a5e2013-04-02 16:41:03 -07003394 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003395 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003396 final Uri data = r.intent.getData();
3397 final String strData = data != null ? data.toSafeString() : null;
3398
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003399 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003400 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003401 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003402 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003403 }
3404
3405 /**
3406 * Make sure the given activity matches the current configuration. Returns
3407 * false if the activity had to be destroyed. Returns true if the
3408 * configuration is the same, or the activity will remain running as-is
3409 * for whatever reason. Ensures the HistoryRecord is updated with the
3410 * correct configuration and all other bookkeeping is handled.
3411 */
3412 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3413 int globalChanges) {
3414 if (mConfigWillChange) {
3415 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3416 "Skipping config check (will change): " + r);
3417 return true;
3418 }
3419
3420 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3421 "Ensuring correct configuration: " + r);
3422
3423 // Short circuit: if the two configurations are the exact same
3424 // object (the common case), then there is nothing to do.
3425 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003426 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3428 "Configuration unchanged in " + r);
3429 return true;
3430 }
3431
3432 // We don't worry about activities that are finishing.
3433 if (r.finishing) {
3434 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3435 "Configuration doesn't matter in finishing " + r);
3436 r.stopFreezingScreenLocked(false);
3437 return true;
3438 }
3439
3440 // Okay we now are going to make this activity have the new config.
3441 // But then we need to figure out how it needs to deal with that.
3442 Configuration oldConfig = r.configuration;
3443 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003444
3445 // Determine what has changed. May be nothing, if this is a config
3446 // that has come back from the app after going idle. In that case
3447 // we just want to leave the official config object now in the
3448 // activity and do nothing else.
3449 final int changes = oldConfig.diff(newConfig);
3450 if (changes == 0 && !r.forceNewConfig) {
3451 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3452 "Configuration no differences in " + r);
3453 return true;
3454 }
3455
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003456 // If the activity isn't currently running, just leave the new
3457 // configuration and it will pick that up next time it starts.
3458 if (r.app == null || r.app.thread == null) {
3459 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3460 "Configuration doesn't matter not running " + r);
3461 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003462 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003463 return true;
3464 }
3465
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003466 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003467 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3468 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3469 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003470 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003471 + ", newConfig=" + newConfig);
3472 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003473 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003474 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3475 r.configChangeFlags |= changes;
3476 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003477 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003478 if (r.app == null || r.app.thread == null) {
3479 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003480 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003481 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003482 } else if (r.state == ActivityState.PAUSING) {
3483 // A little annoying: we are waiting for this activity to
3484 // finish pausing. Let's not do anything now, but just
3485 // flag that it needs to be restarted when done pausing.
3486 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003487 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003488 r.configDestroy = true;
3489 return true;
3490 } else if (r.state == ActivityState.RESUMED) {
3491 // Try to optimize this case: the configuration is changing
3492 // and we need to restart the top, resumed activity.
3493 // Instead of doing the normal handshaking, just say
3494 // "restart!".
3495 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003496 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003497 relaunchActivityLocked(r, r.configChangeFlags, true);
3498 r.configChangeFlags = 0;
3499 } else {
3500 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003501 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003502 relaunchActivityLocked(r, r.configChangeFlags, false);
3503 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003504 }
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003505
3506 // All done... tell the caller we weren't able to keep this
3507 // activity around.
3508 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003509 }
3510
3511 // Default case: the activity can handle this new configuration, so
3512 // hand it over. Note that we don't need to give it the new
3513 // configuration, since we always send configuration changes to all
3514 // process when they happen so it can just use whatever configuration
3515 // it last got.
3516 if (r.app != null && r.app.thread != null) {
3517 try {
3518 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003519 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003520 } catch (RemoteException e) {
3521 // If process died, whatever.
3522 }
3523 }
3524 r.stopFreezingScreenLocked(false);
3525
3526 return true;
3527 }
3528
3529 private final boolean relaunchActivityLocked(ActivityRecord r,
3530 int changes, boolean andResume) {
3531 List<ResultInfo> results = null;
3532 List<Intent> newIntents = null;
3533 if (andResume) {
3534 results = r.results;
3535 newIntents = r.newIntents;
3536 }
3537 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3538 + " with results=" + results + " newIntents=" + newIntents
3539 + " andResume=" + andResume);
3540 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003541 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003542 r.task.taskId, r.shortComponentName);
3543
3544 r.startFreezingScreenLocked(r.app, 0);
3545
3546 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003547 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3548 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3549 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003550 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003551 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003552 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003553 // Note: don't need to call pauseIfSleepingLocked() here, because
3554 // the caller will only pass in 'andResume' if this activity is
3555 // currently resumed, which implies we aren't sleeping.
3556 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003557 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003558 }
3559
3560 if (andResume) {
3561 r.results = null;
3562 r.newIntents = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003563 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003564 mService.reportResumedActivityLocked(r);
3565 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003566 r.state = ActivityState.RESUMED;
3567 } else {
3568 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3569 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003570 }
3571
3572 return true;
3573 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003574
3575 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003576 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3577 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3578 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3579 final ActivityRecord r = activities.get(activityNdx);
3580 if (r.appToken == token) {
3581 return true;
3582 }
3583 if (r.fullscreen && !r.finishing) {
3584 return false;
3585 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003586 }
3587 }
3588 return true;
3589 }
3590
3591 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003592 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3593 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3594 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3595 final ActivityRecord r = activities.get(activityNdx);
3596 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003597 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003598 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003599 }
3600 }
3601 }
3602
3603 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3604 boolean didSomething = false;
3605 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003606 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3607 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3608 int numActivities = activities.size();
3609 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3610 ActivityRecord r = activities.get(activityNdx);
3611 final boolean samePackage = r.packageName.equals(name)
3612 || (name == null && r.userId == userId);
3613 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3614 && (samePackage || r.task == lastTask)
3615 && (r.app == null || evenPersistent || !r.app.persistent)) {
3616 if (!doit) {
3617 if (r.finishing) {
3618 // If this activity is just finishing, then it is not
3619 // interesting as far as something to stop.
3620 continue;
3621 }
3622 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003623 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003624 didSomething = true;
3625 Slog.i(TAG, " Force finishing activity " + r);
3626 if (samePackage) {
3627 if (r.app != null) {
3628 r.app.removed = true;
3629 }
3630 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003631 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003632 lastTask = r.task;
Craig Mautnerd2328952013-03-05 12:46:26 -08003633 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop", true);
Craig Mautnercae015f2013-02-08 14:31:27 -08003634 }
3635 }
3636 }
3637 return didSomething;
3638 }
3639
3640 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003641 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003642 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003643 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3644 --maxNum, --taskNdx) {
3645 final TaskRecord task = mTaskHistory.get(taskNdx);
3646 ActivityRecord r = null;
3647 ActivityRecord top = null;
3648 int numActivities = 0;
3649 int numRunning = 0;
3650 final ArrayList<ActivityRecord> activities = task.mActivities;
3651 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3652 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003653
Craig Mautneraab647e2013-02-28 16:31:36 -08003654 // Initialize state for next task if needed.
3655 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3656 top = r;
3657 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003658 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003659
3660 // Add 'r' into the current task.
3661 numActivities++;
3662 if (r.app != null && r.app.thread != null) {
3663 numRunning++;
3664 }
3665
3666 if (localLOGV) Slog.v(
3667 TAG, r.intent.getComponent().flattenToShortString()
3668 + ": task=" + r.task);
3669 }
3670
3671 RunningTaskInfo ci = new RunningTaskInfo();
3672 ci.id = task.taskId;
3673 ci.baseActivity = r.intent.getComponent();
3674 ci.topActivity = top.intent.getComponent();
3675 if (top.thumbHolder != null) {
3676 ci.description = top.thumbHolder.lastDescription;
3677 }
3678 ci.numActivities = numActivities;
3679 ci.numRunning = numRunning;
3680 //System.out.println(
3681 // "#" + maxNum + ": " + " descr=" + ci.description);
3682 if (receiver != null) {
3683 if (localLOGV) Slog.v(
3684 TAG, "State=" + top.state + "Idle=" + top.idle
3685 + " app=" + top.app
3686 + " thr=" + (top.app != null ? top.app.thread : null));
3687 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3688 if (top.idle && top.app != null && top.app.thread != null) {
3689 topRecord = top;
3690 } else {
3691 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003692 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003693 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003694 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003695 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003696 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003697 }
3698 return topRecord;
3699 }
3700
3701 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003702 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003703 if (DEBUG_SWITCH) Slog.d(
3704 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003705 if (top >= 0) {
3706 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3707 int activityTop = activities.size() - 1;
3708 if (activityTop > 0) {
3709 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3710 "unhandled-back", true);
3711 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003712 }
3713 }
3714
Craig Mautnere79d42682013-04-01 19:01:53 -07003715 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
3716 if (mPausingActivity != null && mPausingActivity.app == app) {
3717 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3718 "App died while pausing: " + mPausingActivity);
3719 mPausingActivity = null;
3720 }
3721 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3722 mLastPausedActivity = null;
3723 }
3724
3725 // Remove this application's activities from active lists.
3726 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3727
3728 if (!restarting) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003729 if (!mStackSupervisor.getTopStack().resumeTopActivityLocked(null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003730 // If there was nothing to resume, and we are not already
3731 // restarting this process, but there is a visible activity that
3732 // is hosted by the process... then make sure all visible
3733 // activities are running, taking care of restarting this
3734 // process.
3735 if (hasVisibleActivities) {
3736 ensureActivitiesVisibleLocked(null, 0);
3737 }
3738 }
3739 }
3740 }
3741
Craig Mautnercae015f2013-02-08 14:31:27 -08003742 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003743 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3744 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3745 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3746 final ActivityRecord r = activities.get(activityNdx);
3747 if (r.app == app) {
3748 Slog.w(TAG, " Force finishing activity "
3749 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003750 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003751 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003752 }
3753 }
3754 }
3755
3756 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3757 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003758 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3759 final TaskRecord task = mTaskHistory.get(taskNdx);
3760 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003761 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003762 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3763 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003764 }
3765
3766 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3767 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3768
3769 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003770 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3771 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003772 }
3773 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003774 final int top = mTaskHistory.size() - 1;
3775 if (top >= 0) {
3776 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3777 int listTop = list.size() - 1;
3778 if (listTop >= 0) {
3779 activities.add(list.get(listTop));
3780 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003781 }
3782 } else {
3783 ItemMatcher matcher = new ItemMatcher();
3784 matcher.build(name);
3785
Craig Mautneraab647e2013-02-28 16:31:36 -08003786 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3787 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3788 if (matcher.match(r1, r1.intent.getComponent())) {
3789 activities.add(r1);
3790 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003791 }
3792 }
3793 }
3794
3795 return activities;
3796 }
3797
3798 ActivityRecord restartPackage(String packageName) {
3799 ActivityRecord starting = topRunningActivityLocked(null);
3800
3801 // All activities that came from the package must be
3802 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003803 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3804 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3805 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3806 final ActivityRecord a = activities.get(activityNdx);
3807 if (a.info.packageName.equals(packageName)) {
3808 a.forceNewConfig = true;
3809 if (starting != null && a == starting && a.visible) {
3810 a.startFreezingScreenLocked(starting.app,
3811 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3812 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003813 }
3814 }
3815 }
3816
3817 return starting;
3818 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003819
Craig Mautnerde4ef022013-04-07 19:01:33 -07003820 boolean removeTask(TaskRecord task) {
3821 mTaskHistory.remove(task);
3822 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003823 }
3824
Craig Mautnerde4ef022013-04-07 19:01:33 -07003825 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003826 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003827 if (toTop) {
3828 mTaskHistory.add(task);
3829 } else {
3830 mTaskHistory.add(0, task);
3831 }
3832 return task;
3833 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003834
3835 ArrayList<TaskRecord> getAllTasks() {
3836 return new ArrayList<TaskRecord>(mTaskHistory);
3837 }
3838
3839 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003840 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003841 if (task == null) {
3842 return;
3843 }
3844 task.stack.mTaskHistory.remove(task);
3845 task.stack = this;
3846 if (toTop) {
3847 mTaskHistory.add(task);
3848 } else {
3849 mTaskHistory.add(0, task);
3850 }
3851 }
3852
3853 public int getStackId() {
3854 return mStackId;
3855 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003856
3857 @Override
3858 public String toString() {
3859 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3860 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861}